blob: 699fb9aa193092fe22fcfe4b3880de3a8f67056b [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides Objective-C code generation targetting the Apple runtime.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
16#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000017#include "CodeGenFunction.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000020#include "clang/AST/DeclObjC.h"
Daniel Dunbar2bebbf02009-05-03 10:46:44 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000022#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000023#include "clang/Basic/LangOptions.h"
24
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +000025#include "llvm/Intrinsics.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000026#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000027#include "llvm/ADT/DenseSet.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000028#include "llvm/Target/TargetData.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000029#include <sstream>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000030
31using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000032using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000033
Daniel Dunbar97776872009-04-22 07:32:20 +000034// Common CGObjCRuntime functions, these don't belong here, but they
35// don't belong in CGObjCRuntime either so we will live with it for
36// now.
37
Daniel Dunbar532d4da2009-05-03 13:15:50 +000038/// FindIvarInterface - Find the interface containing the ivar.
Daniel Dunbara2435782009-04-22 12:00:04 +000039///
Daniel Dunbar532d4da2009-05-03 13:15:50 +000040/// FIXME: We shouldn't need to do this, the containing context should
41/// be fixed.
42static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context,
43 const ObjCInterfaceDecl *OID,
44 const ObjCIvarDecl *OIVD,
45 unsigned &Index) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +000046 // FIXME: The index here is closely tied to how
47 // ASTContext::getObjCLayout is implemented. This should be fixed to
48 // get the information from the layout directly.
49 Index = 0;
Fariborz Jahanian98200742009-05-12 18:14:29 +000050 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +000051 Context.ShallowCollectObjCIvars(OID, Ivars);
Fariborz Jahanian98200742009-05-12 18:14:29 +000052 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
53 if (OIVD == Ivars[k])
54 return OID;
55 ++Index;
Daniel Dunbara80a0f62009-04-22 17:43:55 +000056 }
Fariborz Jahanian98200742009-05-12 18:14:29 +000057
Daniel Dunbar532d4da2009-05-03 13:15:50 +000058 // Otherwise check in the super class.
Daniel Dunbara81419d2009-05-05 00:36:57 +000059 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Daniel Dunbar532d4da2009-05-03 13:15:50 +000060 return FindIvarInterface(Context, Super, OIVD, Index);
61
62 return 0;
Daniel Dunbara2435782009-04-22 12:00:04 +000063}
64
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000065static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
66 const ObjCInterfaceDecl *OID,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000067 const ObjCImplementationDecl *ID,
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000068 const ObjCIvarDecl *Ivar) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +000069 unsigned Index;
70 const ObjCInterfaceDecl *Container =
71 FindIvarInterface(CGM.getContext(), OID, Ivar, Index);
72 assert(Container && "Unable to find ivar container");
73
74 // If we know have an implementation (and the ivar is in it) then
75 // look up in the implementation layout.
76 const ASTRecordLayout *RL;
77 if (ID && ID->getClassInterface() == Container)
78 RL = &CGM.getContext().getASTObjCImplementationLayout(ID);
79 else
80 RL = &CGM.getContext().getASTObjCInterfaceLayout(Container);
81 return RL->getFieldOffset(Index);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000082}
83
84uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
85 const ObjCInterfaceDecl *OID,
86 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000087 return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8;
88}
89
90uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
91 const ObjCImplementationDecl *OID,
92 const ObjCIvarDecl *Ivar) {
93 return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8;
Daniel Dunbar97776872009-04-22 07:32:20 +000094}
95
96LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
97 const ObjCInterfaceDecl *OID,
98 llvm::Value *BaseValue,
99 const ObjCIvarDecl *Ivar,
100 unsigned CVRQualifiers,
101 llvm::Value *Offset) {
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000102 // Compute (type*) ( (char *) BaseValue + Offset)
Daniel Dunbar97776872009-04-22 07:32:20 +0000103 llvm::Type *I8Ptr = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000104 QualType IvarTy = Ivar->getType();
105 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar97776872009-04-22 07:32:20 +0000106 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar97776872009-04-22 07:32:20 +0000107 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000108 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar97776872009-04-22 07:32:20 +0000109
110 if (Ivar->isBitField()) {
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +0000111 // We need to compute the bit offset for the bit-field, the offset
112 // is to the byte. Note, there is a subtle invariant here: we can
113 // only call this routine on non-sythesized ivars but we may be
114 // called for synthesized ivars. However, a synthesized ivar can
115 // never be a bit-field so this is safe.
116 uint64_t BitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar) % 8;
117
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000118 uint64_t BitFieldSize =
119 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
120 return LValue::MakeBitfield(V, BitOffset, BitFieldSize,
Daniel Dunbare38df862009-05-03 07:52:00 +0000121 IvarTy->isSignedIntegerType(),
122 IvarTy.getCVRQualifiers()|CVRQualifiers);
Daniel Dunbar97776872009-04-22 07:32:20 +0000123 }
124
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000125 LValue LV = LValue::MakeAddr(V, IvarTy.getCVRQualifiers()|CVRQualifiers,
126 CGF.CGM.getContext().getObjCGCAttrKind(IvarTy));
Daniel Dunbar97776872009-04-22 07:32:20 +0000127 LValue::SetObjCIvar(LV, true);
128 return LV;
129}
130
131///
132
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000133namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000134
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000135 typedef std::vector<llvm::Constant*> ConstantVector;
136
Mike Stumpf5408fe2009-05-16 07:57:57 +0000137 // FIXME: We should find a nicer way to make the labels for metadata, string
138 // concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000139
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000140class ObjCCommonTypesHelper {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000141private:
142 llvm::Constant *getMessageSendFn() const {
143 // id objc_msgSend (id, SEL, ...)
144 std::vector<const llvm::Type*> Params;
145 Params.push_back(ObjectPtrTy);
146 Params.push_back(SelectorPtrTy);
147 return
148 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
149 Params, true),
150 "objc_msgSend");
151 }
152
153 llvm::Constant *getMessageSendStretFn() const {
154 // id objc_msgSend_stret (id, SEL, ...)
155 std::vector<const llvm::Type*> Params;
156 Params.push_back(ObjectPtrTy);
157 Params.push_back(SelectorPtrTy);
158 return
159 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
160 Params, true),
161 "objc_msgSend_stret");
162
163 }
164
165 llvm::Constant *getMessageSendFpretFn() const {
166 // FIXME: This should be long double on x86_64?
167 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
168 std::vector<const llvm::Type*> Params;
169 Params.push_back(ObjectPtrTy);
170 Params.push_back(SelectorPtrTy);
171 return
172 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
173 Params,
174 true),
175 "objc_msgSend_fpret");
176
177 }
178
179 llvm::Constant *getMessageSendSuperFn() const {
180 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
181 const char *SuperName = "objc_msgSendSuper";
182 std::vector<const llvm::Type*> Params;
183 Params.push_back(SuperPtrTy);
184 Params.push_back(SelectorPtrTy);
185 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
186 Params, true),
187 SuperName);
188 }
189
190 llvm::Constant *getMessageSendSuperFn2() const {
191 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
192 const char *SuperName = "objc_msgSendSuper2";
193 std::vector<const llvm::Type*> Params;
194 Params.push_back(SuperPtrTy);
195 Params.push_back(SelectorPtrTy);
196 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
197 Params, true),
198 SuperName);
199 }
200
201 llvm::Constant *getMessageSendSuperStretFn() const {
202 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
203 // SEL op, ...)
204 std::vector<const llvm::Type*> Params;
205 Params.push_back(Int8PtrTy);
206 Params.push_back(SuperPtrTy);
207 Params.push_back(SelectorPtrTy);
208 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
209 Params, true),
210 "objc_msgSendSuper_stret");
211 }
212
213 llvm::Constant *getMessageSendSuperStretFn2() const {
214 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
215 // SEL op, ...)
216 std::vector<const llvm::Type*> Params;
217 Params.push_back(Int8PtrTy);
218 Params.push_back(SuperPtrTy);
219 Params.push_back(SelectorPtrTy);
220 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
221 Params, true),
222 "objc_msgSendSuper2_stret");
223 }
224
225 llvm::Constant *getMessageSendSuperFpretFn() const {
226 // There is no objc_msgSendSuper_fpret? How can that work?
227 return getMessageSendSuperFn();
228 }
229
230 llvm::Constant *getMessageSendSuperFpretFn2() const {
231 // There is no objc_msgSendSuper_fpret? How can that work?
232 return getMessageSendSuperFn2();
233 }
234
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000235protected:
236 CodeGen::CodeGenModule &CGM;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000237
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000238public:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +0000239 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000240 const llvm::Type *Int8PtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000241
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000242 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
243 const llvm::Type *ObjectPtrTy;
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000244
245 /// PtrObjectPtrTy - LLVM type for id *
246 const llvm::Type *PtrObjectPtrTy;
247
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000248 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000249 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000250 /// ProtocolPtrTy - LLVM type for external protocol handles
251 /// (typeof(Protocol))
252 const llvm::Type *ExternalProtocolPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000253
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000254 // SuperCTy - clang type for struct objc_super.
255 QualType SuperCTy;
256 // SuperPtrCTy - clang type for struct objc_super *.
257 QualType SuperPtrCTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000258
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000259 /// SuperTy - LLVM type for struct objc_super.
260 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000261 /// SuperPtrTy - LLVM type for struct objc_super *.
262 const llvm::Type *SuperPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000263
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000264 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
265 /// in GCC parlance).
266 const llvm::StructType *PropertyTy;
267
268 /// PropertyListTy - LLVM type for struct objc_property_list
269 /// (_prop_list_t in GCC parlance).
270 const llvm::StructType *PropertyListTy;
271 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
272 const llvm::Type *PropertyListPtrTy;
273
274 // MethodTy - LLVM type for struct objc_method.
275 const llvm::StructType *MethodTy;
276
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000277 /// CacheTy - LLVM type for struct objc_cache.
278 const llvm::Type *CacheTy;
279 /// CachePtrTy - LLVM type for struct objc_cache *.
280 const llvm::Type *CachePtrTy;
281
Chris Lattner72db6c32009-04-22 02:44:54 +0000282 llvm::Constant *getGetPropertyFn() {
283 CodeGen::CodeGenTypes &Types = CGM.getTypes();
284 ASTContext &Ctx = CGM.getContext();
285 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
286 llvm::SmallVector<QualType,16> Params;
287 QualType IdType = Ctx.getObjCIdType();
288 QualType SelType = Ctx.getObjCSelType();
289 Params.push_back(IdType);
290 Params.push_back(SelType);
291 Params.push_back(Ctx.LongTy);
292 Params.push_back(Ctx.BoolTy);
293 const llvm::FunctionType *FTy =
294 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
295 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
296 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000297
Chris Lattner72db6c32009-04-22 02:44:54 +0000298 llvm::Constant *getSetPropertyFn() {
299 CodeGen::CodeGenTypes &Types = CGM.getTypes();
300 ASTContext &Ctx = CGM.getContext();
301 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
302 llvm::SmallVector<QualType,16> Params;
303 QualType IdType = Ctx.getObjCIdType();
304 QualType SelType = Ctx.getObjCSelType();
305 Params.push_back(IdType);
306 Params.push_back(SelType);
307 Params.push_back(Ctx.LongTy);
308 Params.push_back(IdType);
309 Params.push_back(Ctx.BoolTy);
310 Params.push_back(Ctx.BoolTy);
311 const llvm::FunctionType *FTy =
312 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
313 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
314 }
315
316 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000317 CodeGen::CodeGenTypes &Types = CGM.getTypes();
318 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000319 // void objc_enumerationMutation (id)
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000320 llvm::SmallVector<QualType,16> Params;
321 QualType IdType = Ctx.getObjCIdType();
322 Params.push_back(IdType);
323 const llvm::FunctionType *FTy =
324 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000325 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
326 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000327
328 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000329 llvm::Constant *getGcReadWeakFn() {
330 // id objc_read_weak (id *)
331 std::vector<const llvm::Type*> Args;
332 Args.push_back(ObjectPtrTy->getPointerTo());
333 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
334 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
335 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000336
337 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000338 llvm::Constant *getGcAssignWeakFn() {
339 // id objc_assign_weak (id, id *)
340 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
341 Args.push_back(ObjectPtrTy->getPointerTo());
342 llvm::FunctionType *FTy =
343 llvm::FunctionType::get(ObjectPtrTy, Args, false);
344 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
345 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000346
347 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000348 llvm::Constant *getGcAssignGlobalFn() {
349 // id objc_assign_global(id, id *)
350 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
351 Args.push_back(ObjectPtrTy->getPointerTo());
352 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
353 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
354 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000355
356 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000357 llvm::Constant *getGcAssignIvarFn() {
358 // id objc_assign_ivar(id, id *)
359 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
360 Args.push_back(ObjectPtrTy->getPointerTo());
361 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
362 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
363 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000364
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000365 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
366 llvm::Constant *GcMemmoveCollectableFn() {
367 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
368 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
369 Args.push_back(Int8PtrTy);
370 Args.push_back(LongTy);
371 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
372 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
373 }
374
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000375 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000376 llvm::Constant *getGcAssignStrongCastFn() {
377 // id objc_assign_global(id, id *)
378 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
379 Args.push_back(ObjectPtrTy->getPointerTo());
380 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
381 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
382 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000383
384 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000385 llvm::Constant *getExceptionThrowFn() {
386 // void objc_exception_throw(id)
387 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
388 llvm::FunctionType *FTy =
389 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
390 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
391 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000392
Daniel Dunbar1c566672009-02-24 01:43:46 +0000393 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000394 llvm::Constant *getSyncEnterFn() {
395 // void objc_sync_enter (id)
396 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
397 llvm::FunctionType *FTy =
398 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
399 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
400 }
Daniel Dunbar1c566672009-02-24 01:43:46 +0000401
402 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000403 llvm::Constant *getSyncExitFn() {
404 // void objc_sync_exit (id)
405 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
406 llvm::FunctionType *FTy =
407 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
408 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
409 }
Daniel Dunbar1c566672009-02-24 01:43:46 +0000410
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000411 llvm::Constant *getSendFn(bool IsSuper) const {
412 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
413 }
414
415 llvm::Constant *getSendFn2(bool IsSuper) const {
416 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
417 }
418
419 llvm::Constant *getSendStretFn(bool IsSuper) const {
420 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
421 }
422
423 llvm::Constant *getSendStretFn2(bool IsSuper) const {
424 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
425 }
426
427 llvm::Constant *getSendFpretFn(bool IsSuper) const {
428 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
429 }
430
431 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
432 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
433 }
434
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000435 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
436 ~ObjCCommonTypesHelper(){}
437};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000438
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000439/// ObjCTypesHelper - Helper class that encapsulates lazy
440/// construction of varies types used during ObjC generation.
441class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000442public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000443 /// SymtabTy - LLVM type for struct objc_symtab.
444 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000445 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
446 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000447 /// ModuleTy - LLVM type for struct objc_module.
448 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000449
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000450 /// ProtocolTy - LLVM type for struct objc_protocol.
451 const llvm::StructType *ProtocolTy;
452 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
453 const llvm::Type *ProtocolPtrTy;
454 /// ProtocolExtensionTy - LLVM type for struct
455 /// objc_protocol_extension.
456 const llvm::StructType *ProtocolExtensionTy;
457 /// ProtocolExtensionTy - LLVM type for struct
458 /// objc_protocol_extension *.
459 const llvm::Type *ProtocolExtensionPtrTy;
460 /// MethodDescriptionTy - LLVM type for struct
461 /// objc_method_description.
462 const llvm::StructType *MethodDescriptionTy;
463 /// MethodDescriptionListTy - LLVM type for struct
464 /// objc_method_description_list.
465 const llvm::StructType *MethodDescriptionListTy;
466 /// MethodDescriptionListPtrTy - LLVM type for struct
467 /// objc_method_description_list *.
468 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000469 /// ProtocolListTy - LLVM type for struct objc_property_list.
470 const llvm::Type *ProtocolListTy;
471 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
472 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000473 /// CategoryTy - LLVM type for struct objc_category.
474 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000475 /// ClassTy - LLVM type for struct objc_class.
476 const llvm::StructType *ClassTy;
477 /// ClassPtrTy - LLVM type for struct objc_class *.
478 const llvm::Type *ClassPtrTy;
479 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
480 const llvm::StructType *ClassExtensionTy;
481 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
482 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000483 // IvarTy - LLVM type for struct objc_ivar.
484 const llvm::StructType *IvarTy;
485 /// IvarListTy - LLVM type for struct objc_ivar_list.
486 const llvm::Type *IvarListTy;
487 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
488 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000489 /// MethodListTy - LLVM type for struct objc_method_list.
490 const llvm::Type *MethodListTy;
491 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
492 const llvm::Type *MethodListPtrTy;
Anders Carlsson124526b2008-09-09 10:10:21 +0000493
494 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
495 const llvm::Type *ExceptionDataTy;
496
Anders Carlsson124526b2008-09-09 10:10:21 +0000497 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000498 llvm::Constant *getExceptionTryEnterFn() {
499 std::vector<const llvm::Type*> Params;
500 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
501 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
502 Params, false),
503 "objc_exception_try_enter");
504 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000505
506 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000507 llvm::Constant *getExceptionTryExitFn() {
508 std::vector<const llvm::Type*> Params;
509 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
510 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
511 Params, false),
512 "objc_exception_try_exit");
513 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000514
515 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000516 llvm::Constant *getExceptionExtractFn() {
517 std::vector<const llvm::Type*> Params;
518 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
519 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
520 Params, false),
521 "objc_exception_extract");
522
523 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000524
525 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000526 llvm::Constant *getExceptionMatchFn() {
527 std::vector<const llvm::Type*> Params;
528 Params.push_back(ClassPtrTy);
529 Params.push_back(ObjectPtrTy);
530 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
531 Params, false),
532 "objc_exception_match");
533
534 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000535
536 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000537 llvm::Constant *getSetJmpFn() {
538 std::vector<const llvm::Type*> Params;
539 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
540 return
541 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
542 Params, false),
543 "_setjmp");
544
545 }
Chris Lattner10cac6f2008-11-15 21:26:17 +0000546
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000547public:
548 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000549 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000550};
551
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000552/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000553/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000554class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000555public:
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000556
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000557 // MethodListnfABITy - LLVM for struct _method_list_t
558 const llvm::StructType *MethodListnfABITy;
559
560 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
561 const llvm::Type *MethodListnfABIPtrTy;
562
563 // ProtocolnfABITy = LLVM for struct _protocol_t
564 const llvm::StructType *ProtocolnfABITy;
565
Daniel Dunbar948e2582009-02-15 07:36:20 +0000566 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
567 const llvm::Type *ProtocolnfABIPtrTy;
568
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000569 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
570 const llvm::StructType *ProtocolListnfABITy;
571
572 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
573 const llvm::Type *ProtocolListnfABIPtrTy;
574
575 // ClassnfABITy - LLVM for struct _class_t
576 const llvm::StructType *ClassnfABITy;
577
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000578 // ClassnfABIPtrTy - LLVM for struct _class_t*
579 const llvm::Type *ClassnfABIPtrTy;
580
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000581 // IvarnfABITy - LLVM for struct _ivar_t
582 const llvm::StructType *IvarnfABITy;
583
584 // IvarListnfABITy - LLVM for struct _ivar_list_t
585 const llvm::StructType *IvarListnfABITy;
586
587 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
588 const llvm::Type *IvarListnfABIPtrTy;
589
590 // ClassRonfABITy - LLVM for struct _class_ro_t
591 const llvm::StructType *ClassRonfABITy;
592
593 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
594 const llvm::Type *ImpnfABITy;
595
596 // CategorynfABITy - LLVM for struct _category_t
597 const llvm::StructType *CategorynfABITy;
598
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000599 // New types for nonfragile abi messaging.
600
601 // MessageRefTy - LLVM for:
602 // struct _message_ref_t {
603 // IMP messenger;
604 // SEL name;
605 // };
606 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000607 // MessageRefCTy - clang type for struct _message_ref_t
608 QualType MessageRefCTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000609
610 // MessageRefPtrTy - LLVM for struct _message_ref_t*
611 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000612 // MessageRefCPtrTy - clang type for struct _message_ref_t*
613 QualType MessageRefCPtrTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000614
Fariborz Jahanianef163782009-02-05 01:13:09 +0000615 // MessengerTy - Type of the messenger (shown as IMP above)
616 const llvm::FunctionType *MessengerTy;
617
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000618 // SuperMessageRefTy - LLVM for:
619 // struct _super_message_ref_t {
620 // SUPER_IMP messenger;
621 // SEL name;
622 // };
623 const llvm::StructType *SuperMessageRefTy;
624
625 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
626 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000627
Chris Lattner1c02f862009-04-22 02:53:24 +0000628 llvm::Constant *getMessageSendFixupFn() {
629 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
630 std::vector<const llvm::Type*> Params;
631 Params.push_back(ObjectPtrTy);
632 Params.push_back(MessageRefPtrTy);
633 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
634 Params, true),
635 "objc_msgSend_fixup");
636 }
637
638 llvm::Constant *getMessageSendFpretFixupFn() {
639 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
640 std::vector<const llvm::Type*> Params;
641 Params.push_back(ObjectPtrTy);
642 Params.push_back(MessageRefPtrTy);
643 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
644 Params, true),
645 "objc_msgSend_fpret_fixup");
646 }
647
648 llvm::Constant *getMessageSendStretFixupFn() {
649 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
650 std::vector<const llvm::Type*> Params;
651 Params.push_back(ObjectPtrTy);
652 Params.push_back(MessageRefPtrTy);
653 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
654 Params, true),
655 "objc_msgSend_stret_fixup");
656 }
657
658 llvm::Constant *getMessageSendIdFixupFn() {
659 // id objc_msgSendId_fixup(id, struct message_ref_t*, ...)
660 std::vector<const llvm::Type*> Params;
661 Params.push_back(ObjectPtrTy);
662 Params.push_back(MessageRefPtrTy);
663 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
664 Params, true),
665 "objc_msgSendId_fixup");
666 }
667
668 llvm::Constant *getMessageSendIdStretFixupFn() {
669 // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...)
670 std::vector<const llvm::Type*> Params;
671 Params.push_back(ObjectPtrTy);
672 Params.push_back(MessageRefPtrTy);
673 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
674 Params, true),
675 "objc_msgSendId_stret_fixup");
676 }
677 llvm::Constant *getMessageSendSuper2FixupFn() {
678 // id objc_msgSendSuper2_fixup (struct objc_super *,
679 // struct _super_message_ref_t*, ...)
680 std::vector<const llvm::Type*> Params;
681 Params.push_back(SuperPtrTy);
682 Params.push_back(SuperMessageRefPtrTy);
683 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
684 Params, true),
685 "objc_msgSendSuper2_fixup");
686 }
687
688 llvm::Constant *getMessageSendSuper2StretFixupFn() {
689 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
690 // struct _super_message_ref_t*, ...)
691 std::vector<const llvm::Type*> Params;
692 Params.push_back(SuperPtrTy);
693 Params.push_back(SuperMessageRefPtrTy);
694 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
695 Params, true),
696 "objc_msgSendSuper2_stret_fixup");
697 }
698
699
700
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000701 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
702 /// exception personality function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000703 llvm::Value *getEHPersonalityPtr() {
704 llvm::Constant *Personality =
705 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000706 true),
707 "__objc_personality_v0");
708 return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
709 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000710
Chris Lattner8a569112009-04-22 02:15:23 +0000711 llvm::Constant *getUnwindResumeOrRethrowFn() {
712 std::vector<const llvm::Type*> Params;
713 Params.push_back(Int8PtrTy);
714 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
715 Params, false),
716 "_Unwind_Resume_or_Rethrow");
717 }
718
719 llvm::Constant *getObjCEndCatchFn() {
Chris Lattner8a569112009-04-22 02:15:23 +0000720 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
Chris Lattnerb59761b2009-07-01 04:13:52 +0000721 false),
Chris Lattner8a569112009-04-22 02:15:23 +0000722 "objc_end_catch");
723
724 }
725
726 llvm::Constant *getObjCBeginCatchFn() {
727 std::vector<const llvm::Type*> Params;
728 Params.push_back(Int8PtrTy);
729 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
730 Params, false),
731 "objc_begin_catch");
732 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000733
734 const llvm::StructType *EHTypeTy;
735 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000736
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000737 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
738 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000739};
740
741class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000742public:
743 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000744 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000745 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000746 unsigned ivar_bytepos;
747 unsigned ivar_size;
748 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
749 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000750
751 // Allow sorting based on byte pos.
752 bool operator<(const GC_IVAR &b) const {
753 return ivar_bytepos < b.ivar_bytepos;
754 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000755 };
756
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000757 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000758 public:
759 unsigned skip;
760 unsigned scan;
761 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
762 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000763 };
764
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000765protected:
766 CodeGen::CodeGenModule &CGM;
767 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000768 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000769
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000770 // gc ivar layout bitmap calculation helper caches.
771 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
772 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000773
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000774 /// LazySymbols - Symbols to generate a lazy reference for. See
775 /// DefinedSymbols and FinishModule().
776 std::set<IdentifierInfo*> LazySymbols;
777
778 /// DefinedSymbols - External symbols which are defined by this
779 /// module. The symbols in this list and LazySymbols are used to add
780 /// special linker symbols which ensure that Objective-C modules are
781 /// linked properly.
782 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000783
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000784 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000785 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000786
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000787 /// MethodVarNames - uniqued method variable names.
788 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000789
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000790 /// MethodVarTypes - uniqued method type signatures. We have to use
791 /// a StringMap here because have no other unique reference.
792 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000793
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000794 /// MethodDefinitions - map of methods which have been defined in
795 /// this translation unit.
796 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000797
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000798 /// PropertyNames - uniqued method variable names.
799 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000800
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000801 /// ClassReferences - uniqued class references.
802 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000803
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000804 /// SelectorReferences - uniqued selector references.
805 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000806
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000807 /// Protocols - Protocols for which an objc_protocol structure has
808 /// been emitted. Forward declarations are handled by creating an
809 /// empty structure whose initializer is filled in when/if defined.
810 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000811
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000812 /// DefinedProtocols - Protocols which have actually been
813 /// defined. We should not need this, see FIXME in GenerateProtocol.
814 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000815
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000816 /// DefinedClasses - List of defined classes.
817 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000818
819 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
820 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000821
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000822 /// DefinedCategories - List of defined categories.
823 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000824
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000825 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
826 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
827
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000828 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000829 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000830 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000831
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000832 /// GetNameForMethod - Return a name for the given method.
833 /// \param[out] NameOut - The return value.
834 void GetNameForMethod(const ObjCMethodDecl *OMD,
835 const ObjCContainerDecl *CD,
836 std::string &NameOut);
837
838 /// GetMethodVarName - Return a unique constant for the given
839 /// selector's name. The return value has type char *.
840 llvm::Constant *GetMethodVarName(Selector Sel);
841 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
842 llvm::Constant *GetMethodVarName(const std::string &Name);
843
844 /// GetMethodVarType - Return a unique constant for the given
845 /// selector's name. The return value has type char *.
846
847 // FIXME: This is a horrible name.
848 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000849 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000850
851 /// GetPropertyName - Return a unique constant for the given
852 /// name. The return value has type char *.
853 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
854
855 // FIXME: This can be dropped once string functions are unified.
856 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
857 const Decl *Container);
858
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000859 /// GetClassName - Return a unique constant for the given selector's
860 /// name. The return value has type char *.
861 llvm::Constant *GetClassName(IdentifierInfo *Ident);
862
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000863 /// BuildIvarLayout - Builds ivar layout bitmap for the class
864 /// implementation for the __strong or __weak case.
865 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000866 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
867 bool ForStrongLayout);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000868
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000869 void BuildAggrIvarRecordLayout(const RecordType *RT,
870 unsigned int BytePos, bool ForStrongLayout,
871 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000872 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000873 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000874 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000875 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000876 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000877 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000878
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000879 /// GetIvarLayoutName - Returns a unique constant for the given
880 /// ivar layout bitmap.
881 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
882 const ObjCCommonTypesHelper &ObjCTypes);
883
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000884 /// EmitPropertyList - Emit the given property list. The return
885 /// value has type PropertyListPtrTy.
886 llvm::Constant *EmitPropertyList(const std::string &Name,
887 const Decl *Container,
888 const ObjCContainerDecl *OCD,
889 const ObjCCommonTypesHelper &ObjCTypes);
890
Fariborz Jahanianda320092009-01-29 19:24:30 +0000891 /// GetProtocolRef - Return a reference to the internal protocol
892 /// description, creating an empty one if it has not been
893 /// defined. The return value has type ProtocolPtrTy.
894 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000895
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000896 /// CreateMetadataVar - Create a global variable with internal
897 /// linkage for use by the Objective-C runtime.
898 ///
899 /// This is a convenience wrapper which not only creates the
900 /// variable, but also sets the section and alignment and adds the
901 /// global to the UsedGlobals list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000902 ///
903 /// \param Name - The variable name.
904 /// \param Init - The variable initializer; this is also used to
905 /// define the type of the variable.
906 /// \param Section - The section the variable should go into, or 0.
907 /// \param Align - The alignment for the variable, or 0.
908 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000909 /// "llvm.used".
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000910 llvm::GlobalVariable *CreateMetadataVar(const std::string &Name,
911 llvm::Constant *Init,
912 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000913 unsigned Align,
914 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000915
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000916 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
917 QualType ResultType,
918 llvm::Value *Sel,
919 llvm::Value *Arg0,
920 QualType Arg0Ty,
921 bool IsSuper,
922 const CallArgList &CallArgs,
923 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000924
Fariborz Jahanianc38e9af2009-06-23 21:47:46 +0000925 virtual void MergeMetadataGlobals(std::vector<llvm::Constant*> &UsedArray);
926
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000927public:
928 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
929 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000930
Steve Naroff33fdb732009-03-31 16:53:37 +0000931 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000932
933 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
934 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000935
936 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
937
938 /// GetOrEmitProtocol - Get the protocol object for the given
939 /// declaration, emitting it if necessary. The return value has type
940 /// ProtocolPtrTy.
941 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
942
943 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
944 /// object for the given declaration, emitting it if needed. These
945 /// forward references will be filled in with empty bodies if no
946 /// definition is seen. The return value has type ProtocolPtrTy.
947 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000948};
949
950class CGObjCMac : public CGObjCCommonMac {
951private:
952 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000953 /// EmitImageInfo - Emit the image info marker used to encode some module
954 /// level information.
955 void EmitImageInfo();
956
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000957 /// EmitModuleInfo - Another marker encoding module level
958 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000959 void EmitModuleInfo();
960
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000961 /// EmitModuleSymols - Emit module symbols, the list of defined
962 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000963 llvm::Constant *EmitModuleSymbols();
964
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000965 /// FinishModule - Write out global data structures at the end of
966 /// processing a translation unit.
967 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000968
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000969 /// EmitClassExtension - Generate the class extension structure used
970 /// to store the weak ivar layout and properties. The return value
971 /// has type ClassExtensionPtrTy.
972 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
973
974 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
975 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000976 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000977 const ObjCInterfaceDecl *ID);
978
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000979 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000980 QualType ResultType,
981 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000982 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000983 QualType Arg0Ty,
984 bool IsSuper,
985 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000986
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000987 /// EmitIvarList - Emit the ivar list for the given
988 /// implementation. If ForClass is true the list of class ivars
989 /// (i.e. metaclass ivars) is emitted, otherwise the list of
990 /// interface ivars will be emitted. The return value has type
991 /// IvarListPtrTy.
992 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000993 bool ForClass);
994
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000995 /// EmitMetaClass - Emit a forward reference to the class structure
996 /// for the metaclass of the given interface. The return value has
997 /// type ClassPtrTy.
998 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
999
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001000 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001001 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001002 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1003 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001004 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001005
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001006 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001007
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001008 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001009
1010 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001011 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001012 llvm::Constant *EmitMethodList(const std::string &Name,
1013 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001014 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001015
1016 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001017 /// method declarations.
1018 /// - TypeName: The name for the type containing the methods.
1019 /// - IsProtocol: True iff these methods are for a protocol.
1020 /// - ClassMethds: True iff these are class methods.
1021 /// - Required: When true, only "required" methods are
1022 /// listed. Similarly, when false only "optional" methods are
1023 /// listed. For classes this should always be true.
1024 /// - begin, end: The method list to output.
1025 ///
1026 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001027 llvm::Constant *EmitMethodDescList(const std::string &Name,
1028 const char *Section,
1029 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001030
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001031 /// GetOrEmitProtocol - Get the protocol object for the given
1032 /// declaration, emitting it if necessary. The return value has type
1033 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001034 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001035
1036 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1037 /// object for the given declaration, emitting it if needed. These
1038 /// forward references will be filled in with empty bodies if no
1039 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001040 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001041
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001042 /// EmitProtocolExtension - Generate the protocol extension
1043 /// structure used to store optional instance and class methods, and
1044 /// protocol properties. The return value has type
1045 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001046 llvm::Constant *
1047 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1048 const ConstantVector &OptInstanceMethods,
1049 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001050
1051 /// EmitProtocolList - Generate the list of referenced
1052 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc933702008-08-21 21:57:41 +00001053 llvm::Constant *EmitProtocolList(const std::string &Name,
1054 ObjCProtocolDecl::protocol_iterator begin,
1055 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001056
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001057 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1058 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001059 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001060
Fariborz Jahanianda320092009-01-29 19:24:30 +00001061 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001062 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001063
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001064 virtual llvm::Function *ModuleInitFunction();
1065
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001066 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001067 QualType ResultType,
1068 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001069 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001070 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001071 const CallArgList &CallArgs,
1072 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001073
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001074 virtual CodeGen::RValue
1075 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001076 QualType ResultType,
1077 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001078 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001079 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001080 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001081 bool IsClassMessage,
1082 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001083
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001084 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001085 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001086
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001087 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001088
1089 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1090 /// untyped one.
1091 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1092 const ObjCMethodDecl *Method);
1093
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001094 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001095
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001096 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001097
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001098 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001099 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00001100
Chris Lattner74391b42009-03-22 21:03:39 +00001101 virtual llvm::Constant *GetPropertyGetFunction();
1102 virtual llvm::Constant *GetPropertySetFunction();
1103 virtual llvm::Constant *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001104
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001105 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1106 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001107 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1108 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001109 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001110 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001111 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1112 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001113 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1114 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001115 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1116 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001117 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1118 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001119 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1120 llvm::Value *dest, llvm::Value *src,
1121 unsigned long size);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001122
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001123 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1124 QualType ObjectTy,
1125 llvm::Value *BaseValue,
1126 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001127 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001128 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001129 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001130 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001131};
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001132
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001133class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001134private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001135 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001136 llvm::GlobalVariable* ObjCEmptyCacheVar;
1137 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001138
Daniel Dunbar11394522009-04-18 08:51:00 +00001139 /// SuperClassReferences - uniqued super class references.
1140 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
1141
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001142 /// MetaClassReferences - uniqued meta class references.
1143 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001144
1145 /// EHTypeReferences - uniqued class ehtype references.
1146 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001147
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001148 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001149 /// legacy messaging dispatch.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001150 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001151
1152 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001153 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001154 bool LegacyDispatchedSelector(Selector Sel);
1155
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001156 /// FinishNonFragileABIModule - Write out global data structures at the end of
1157 /// processing a translation unit.
1158 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001159
Daniel Dunbar463b8762009-05-15 21:48:48 +00001160 /// AddModuleClassList - Add the given list of class pointers to the
1161 /// module with the provided symbol and section names.
1162 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1163 const char *SymbolName,
1164 const char *SectionName);
1165
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001166 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1167 unsigned InstanceStart,
1168 unsigned InstanceSize,
1169 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001170 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
1171 llvm::Constant *IsAGV,
1172 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001173 llvm::Constant *ClassRoGV,
1174 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001175
1176 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
1177
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001178 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
1179
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001180 /// EmitMethodList - Emit the method list for the given
1181 /// implementation. The return value has type MethodListnfABITy.
1182 llvm::Constant *EmitMethodList(const std::string &Name,
1183 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001184 const ConstantVector &Methods);
1185 /// EmitIvarList - Emit the ivar list for the given
1186 /// implementation. If ForClass is true the list of class ivars
1187 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1188 /// interface ivars will be emitted. The return value has type
1189 /// IvarListnfABIPtrTy.
1190 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001191
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001192 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001193 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001194 unsigned long int offset);
1195
Fariborz Jahanianda320092009-01-29 19:24:30 +00001196 /// GetOrEmitProtocol - Get the protocol object for the given
1197 /// declaration, emitting it if necessary. The return value has type
1198 /// ProtocolPtrTy.
1199 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
1200
1201 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1202 /// object for the given declaration, emitting it if needed. These
1203 /// forward references will be filled in with empty bodies if no
1204 /// definition is seen. The return value has type ProtocolPtrTy.
1205 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
1206
1207 /// EmitProtocolList - Generate the list of referenced
1208 /// protocols. The return value has type ProtocolListPtrTy.
1209 llvm::Constant *EmitProtocolList(const std::string &Name,
1210 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001211 ObjCProtocolDecl::protocol_iterator end);
1212
1213 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1214 QualType ResultType,
1215 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001216 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001217 QualType Arg0Ty,
1218 bool IsSuper,
1219 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001220
1221 /// GetClassGlobal - Return the global variable for the Objective-C
1222 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001223 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
1224
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001225 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001226 /// for the given class reference.
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001227 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001228 const ObjCInterfaceDecl *ID);
1229
1230 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1231 /// for the given super class reference.
1232 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1233 const ObjCInterfaceDecl *ID);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001234
1235 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1236 /// meta-data
1237 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
1238 const ObjCInterfaceDecl *ID);
1239
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001240 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1241 /// the given ivar.
1242 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001243 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001244 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001245 const ObjCIvarDecl *Ivar);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001246
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001247 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1248 /// for the given selector.
1249 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbare588b992009-03-01 04:46:24 +00001250
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001251 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001252 /// interface. The return value has type EHTypePtrTy.
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001253 llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
1254 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001255
1256 const char *getMetaclassSymbolPrefix() const {
1257 return "OBJC_METACLASS_$_";
1258 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001259
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001260 const char *getClassSymbolPrefix() const {
1261 return "OBJC_CLASS_$_";
1262 }
1263
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001264 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001265 uint32_t &InstanceStart,
1266 uint32_t &InstanceSize);
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001267
1268 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001269 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001270 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1271 return CGM.getContext().Selectors.getSelector(0, &II);
1272 }
1273
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001274 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001275 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1276 return CGM.getContext().Selectors.getSelector(1, &II);
1277 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001278
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001279 /// ImplementationIsNonLazy - Check whether the given category or
1280 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001281 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001282
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001283public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001284 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001285 // FIXME. All stubs for now!
1286 virtual llvm::Function *ModuleInitFunction();
1287
1288 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
1289 QualType ResultType,
1290 Selector Sel,
1291 llvm::Value *Receiver,
1292 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001293 const CallArgList &CallArgs,
1294 const ObjCMethodDecl *Method);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001295
1296 virtual CodeGen::RValue
1297 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
1298 QualType ResultType,
1299 Selector Sel,
1300 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001301 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001302 llvm::Value *Receiver,
1303 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001304 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001305
1306 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001307 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001308
1309 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001310 { return EmitSelector(Builder, Sel); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001311
1312 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1313 /// untyped one.
1314 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1315 const ObjCMethodDecl *Method)
1316 { return EmitSelector(Builder, Method->getSelector()); }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001317
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001318 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001319
1320 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001321 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001322 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001323
Chris Lattner74391b42009-03-22 21:03:39 +00001324 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001325 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001326 }
Chris Lattner74391b42009-03-22 21:03:39 +00001327 virtual llvm::Constant *GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001328 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001329 }
Chris Lattner74391b42009-03-22 21:03:39 +00001330 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001331 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001332 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001333
1334 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00001335 const Stmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001336 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001337 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001338 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001339 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001340 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001341 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001342 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001343 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001344 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001345 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001346 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001347 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001348 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1349 llvm::Value *dest, llvm::Value *src,
1350 unsigned long size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001351 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1352 QualType ObjectTy,
1353 llvm::Value *BaseValue,
1354 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001355 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001356 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001357 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001358 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001359};
1360
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001361} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001362
1363/* *** Helper Functions *** */
1364
1365/// getConstantGEP() - Help routine to construct simple GEPs.
1366static llvm::Constant *getConstantGEP(llvm::Constant *C,
1367 unsigned idx0,
1368 unsigned idx1) {
1369 llvm::Value *Idxs[] = {
1370 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
1371 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
1372 };
1373 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
1374}
1375
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001376/// hasObjCExceptionAttribute - Return true if this class or any super
1377/// class has the __objc_exception__ attribute.
Douglas Gregor68584ed2009-06-18 16:11:24 +00001378static bool hasObjCExceptionAttribute(ASTContext &Context,
1379 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001380 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001381 return true;
1382 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001383 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001384 return false;
1385}
1386
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001387/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001388
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001389CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
1390 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001391{
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001392 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001393 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001394}
1395
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001396/// GetClass - Return a reference to the class for the given interface
1397/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001398llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001399 const ObjCInterfaceDecl *ID) {
1400 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001401}
1402
1403/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001404llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001405 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001406}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001407llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
1408 *Method) {
1409 return EmitSelector(Builder, Method->getSelector());
1410}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001411
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001412/// Generate a constant CFString object.
1413/*
1414 struct __builtin_CFString {
1415 const int *isa; // point to __CFConstantStringClassReference
1416 int flags;
1417 const char *str;
1418 long length;
1419 };
1420*/
1421
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001422llvm::Constant *CGObjCCommonMac::GenerateConstantString(
Steve Naroff33fdb732009-03-31 16:53:37 +00001423 const ObjCStringLiteral *SL) {
Steve Naroff8d4141f2009-04-01 13:55:36 +00001424 return CGM.GetAddrOfConstantCFString(SL->getString());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001425}
1426
1427/// Generates a message send where the super is the receiver. This is
1428/// a message send to self with special delivery semantics indicating
1429/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001430CodeGen::RValue
1431CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001432 QualType ResultType,
1433 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001434 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001435 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001436 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001437 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001438 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001439 // Create and init a super structure; this is a (receiver, class)
1440 // pair we will pass to objc_msgSendSuper.
1441 llvm::Value *ObjCSuper =
1442 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
1443 llvm::Value *ReceiverAsObject =
1444 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
1445 CGF.Builder.CreateStore(ReceiverAsObject,
1446 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001447
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001448 // If this is a class message the metaclass is passed as the target.
1449 llvm::Value *Target;
1450 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001451 if (isCategoryImpl) {
1452 // Message sent to 'super' in a class method defined in a category
1453 // implementation requires an odd treatment.
1454 // If we are in a class method, we must retrieve the
1455 // _metaclass_ for the current class, pointed at by
1456 // the class's "isa" pointer. The following assumes that
1457 // isa" is the first ivar in a class (which it must be).
1458 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1459 Target = CGF.Builder.CreateStructGEP(Target, 0);
1460 Target = CGF.Builder.CreateLoad(Target);
1461 }
1462 else {
1463 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1464 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1465 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1466 Target = Super;
1467 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001468 } else {
1469 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1470 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001471 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1472 // ObjCTypes types.
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001473 const llvm::Type *ClassTy =
1474 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001475 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001476 CGF.Builder.CreateStore(Target,
1477 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001478 return EmitLegacyMessageSend(CGF, ResultType,
1479 EmitSelector(CGF.Builder, Sel),
1480 ObjCSuper, ObjCTypes.SuperPtrCTy,
1481 true, CallArgs, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001482}
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001483
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001484/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001485CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001486 QualType ResultType,
1487 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001488 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001489 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001490 const CallArgList &CallArgs,
1491 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001492 return EmitLegacyMessageSend(CGF, ResultType,
1493 EmitSelector(CGF.Builder, Sel),
1494 Receiver, CGF.getContext().getObjCIdType(),
1495 false, CallArgs, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001496}
1497
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001498CodeGen::RValue CGObjCCommonMac::EmitLegacyMessageSend(
1499 CodeGen::CodeGenFunction &CGF,
1500 QualType ResultType,
1501 llvm::Value *Sel,
1502 llvm::Value *Arg0,
1503 QualType Arg0Ty,
1504 bool IsSuper,
1505 const CallArgList &CallArgs,
1506 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001507 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001508 if (!IsSuper)
1509 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001510 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001511 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001512 CGF.getContext().getObjCSelType()));
1513 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001514
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001515 CodeGenTypes &Types = CGM.getTypes();
1516 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001517 // In 64bit ABI, type must be assumed VARARG. In 32bit abi,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001518 // it seems not to matter.
1519 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, (ObjCABI == 2));
1520
1521 llvm::Constant *Fn = NULL;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001522 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001523 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
1524 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001525 } else if (ResultType->isFloatingType()) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001526 if (ObjCABI == 2) {
1527 if (const BuiltinType *BT = ResultType->getAsBuiltinType()) {
1528 BuiltinType::Kind k = BT->getKind();
1529 Fn = (k == BuiltinType::LongDouble) ? ObjCTypes.getSendFpretFn2(IsSuper)
1530 : ObjCTypes.getSendFn2(IsSuper);
Daniel Dunbar42f963d2009-06-10 04:38:50 +00001531 } else {
1532 Fn = ObjCTypes.getSendFn2(IsSuper);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001533 }
1534 }
1535 else
Mike Stumpf5408fe2009-05-16 07:57:57 +00001536 // FIXME. This currently matches gcc's API for x86-32. May need to change
1537 // for others if we have their API.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001538 Fn = ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001539 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001540 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
1541 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001542 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001543 assert(Fn && "EmitLegacyMessageSend - unknown API");
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001544 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +00001545 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001546}
1547
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001548llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001549 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001550 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001551 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001552 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1553
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001554 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
1555 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001556}
1557
Fariborz Jahanianda320092009-01-29 19:24:30 +00001558void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001559 // FIXME: We shouldn't need this, the protocol decl should contain enough
1560 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001561 DefinedProtocols.insert(PD->getIdentifier());
1562
1563 // If we have generated a forward reference to this protocol, emit
1564 // it now. Otherwise do nothing, the protocol objects are lazily
1565 // emitted.
1566 if (Protocols.count(PD->getIdentifier()))
1567 GetOrEmitProtocol(PD);
1568}
1569
Fariborz Jahanianda320092009-01-29 19:24:30 +00001570llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001571 if (DefinedProtocols.count(PD->getIdentifier()))
1572 return GetOrEmitProtocol(PD);
1573 return GetOrEmitProtocolRef(PD);
1574}
1575
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001576/*
1577 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1578 struct _objc_protocol {
1579 struct _objc_protocol_extension *isa;
1580 char *protocol_name;
1581 struct _objc_protocol_list *protocol_list;
1582 struct _objc__method_prototype_list *instance_methods;
1583 struct _objc__method_prototype_list *class_methods
1584 };
1585
1586 See EmitProtocolExtension().
1587*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001588llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1589 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1590
1591 // Early exit if a defining object has already been generated.
1592 if (Entry && Entry->hasInitializer())
1593 return Entry;
1594
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001595 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001596 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001597 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1598
Chris Lattner8ec03f52008-11-24 03:54:41 +00001599 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001600
1601 // Construct method lists.
1602 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1603 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00001604 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001605 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001606 ObjCMethodDecl *MD = *i;
1607 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1608 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1609 OptInstanceMethods.push_back(C);
1610 } else {
1611 InstanceMethods.push_back(C);
1612 }
1613 }
1614
Douglas Gregor6ab35242009-04-09 21:40:53 +00001615 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001616 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001617 ObjCMethodDecl *MD = *i;
1618 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1619 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1620 OptClassMethods.push_back(C);
1621 } else {
1622 ClassMethods.push_back(C);
1623 }
1624 }
1625
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001626 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001627 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001628 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001629 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001630 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001631 PD->protocol_begin(),
1632 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001633 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001634 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1635 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001636 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1637 InstanceMethods);
1638 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001639 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1640 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001641 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1642 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001643 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1644 Values);
1645
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001646 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001647 // Already created, fix the linkage and update the initializer.
1648 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001649 Entry->setInitializer(Init);
1650 } else {
1651 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001652 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001653 llvm::GlobalValue::InternalLinkage,
1654 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00001655 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001656 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001657 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001658 UsedGlobals.push_back(Entry);
1659 // FIXME: Is this necessary? Why only for protocol?
1660 Entry->setAlignment(4);
1661 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001662
1663 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001664}
1665
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001666llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001667 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1668
1669 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001670 // We use the initializer as a marker of whether this is a forward
1671 // reference or not. At module finalization we add the empty
1672 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001673 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001674 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001675 llvm::GlobalValue::ExternalLinkage,
1676 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00001677 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001678 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001679 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001680 UsedGlobals.push_back(Entry);
1681 // FIXME: Is this necessary? Why only for protocol?
1682 Entry->setAlignment(4);
1683 }
1684
1685 return Entry;
1686}
1687
1688/*
1689 struct _objc_protocol_extension {
1690 uint32_t size;
1691 struct objc_method_description_list *optional_instance_methods;
1692 struct objc_method_description_list *optional_class_methods;
1693 struct objc_property_list *instance_properties;
1694 };
1695*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001696llvm::Constant *
1697CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1698 const ConstantVector &OptInstanceMethods,
1699 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001700 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001701 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001702 std::vector<llvm::Constant*> Values(4);
1703 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001704 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001705 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1706 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001707 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1708 OptInstanceMethods);
1709 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001710 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1711 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001712 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1713 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001714 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1715 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001716 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001717
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001718 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001719 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1720 Values[3]->isNullValue())
1721 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1722
1723 llvm::Constant *Init =
1724 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001725
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001726 // No special section, but goes in llvm.used
1727 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
1728 Init,
1729 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001730}
1731
1732/*
1733 struct objc_protocol_list {
1734 struct objc_protocol_list *next;
1735 long count;
1736 Protocol *list[];
1737 };
1738*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001739llvm::Constant *
1740CGObjCMac::EmitProtocolList(const std::string &Name,
1741 ObjCProtocolDecl::protocol_iterator begin,
1742 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001743 std::vector<llvm::Constant*> ProtocolRefs;
1744
Daniel Dunbardbc933702008-08-21 21:57:41 +00001745 for (; begin != end; ++begin)
1746 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001747
1748 // Just return null for empty protocol lists
1749 if (ProtocolRefs.empty())
1750 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1751
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001752 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001753 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1754
1755 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001756 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001757 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1758 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1759 Values[2] =
1760 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1761 ProtocolRefs.size()),
1762 ProtocolRefs);
1763
1764 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1765 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001766 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001767 4, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001768 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1769}
1770
1771/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001772 struct _objc_property {
1773 const char * const name;
1774 const char * const attributes;
1775 };
1776
1777 struct _objc_property_list {
1778 uint32_t entsize; // sizeof (struct _objc_property)
1779 uint32_t prop_count;
1780 struct _objc_property[prop_count];
1781 };
1782*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001783llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1784 const Decl *Container,
1785 const ObjCContainerDecl *OCD,
1786 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001787 std::vector<llvm::Constant*> Properties, Prop(2);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001788 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1789 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001790 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001791 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001792 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001793 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1794 Prop));
1795 }
1796
1797 // Return null for empty list.
1798 if (Properties.empty())
1799 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1800
1801 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00001802 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001803 std::vector<llvm::Constant*> Values(3);
1804 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1805 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1806 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1807 Properties.size());
1808 Values[2] = llvm::ConstantArray::get(AT, Properties);
1809 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1810
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001811 llvm::GlobalVariable *GV =
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001812 CreateMetadataVar(Name, Init,
1813 (ObjCABI == 2) ? "__DATA, __objc_const" :
1814 "__OBJC,__property,regular,no_dead_strip",
1815 (ObjCABI == 2) ? 8 : 4,
1816 true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001817 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001818}
1819
1820/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001821 struct objc_method_description_list {
1822 int count;
1823 struct objc_method_description list[];
1824 };
1825*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001826llvm::Constant *
1827CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1828 std::vector<llvm::Constant*> Desc(2);
1829 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1830 ObjCTypes.SelectorPtrTy);
1831 Desc[1] = GetMethodVarType(MD);
1832 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1833 Desc);
1834}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001835
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001836llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1837 const char *Section,
1838 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001839 // Return null for empty list.
1840 if (Methods.empty())
1841 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1842
1843 std::vector<llvm::Constant*> Values(2);
1844 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1845 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1846 Methods.size());
1847 Values[1] = llvm::ConstantArray::get(AT, Methods);
1848 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1849
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001850 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001851 return llvm::ConstantExpr::getBitCast(GV,
1852 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001853}
1854
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001855/*
1856 struct _objc_category {
1857 char *category_name;
1858 char *class_name;
1859 struct _objc_method_list *instance_methods;
1860 struct _objc_method_list *class_methods;
1861 struct _objc_protocol_list *protocols;
1862 uint32_t size; // <rdar://4585769>
1863 struct _objc_property_list *instance_properties;
1864 };
1865 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001866void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00001867 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001868
Mike Stumpf5408fe2009-05-16 07:57:57 +00001869 // FIXME: This is poor design, the OCD should have a pointer to the category
1870 // decl. Additionally, note that Category can be null for the @implementation
1871 // w/o an @interface case. Sema should just create one for us as it does for
1872 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001873 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001874 const ObjCCategoryDecl *Category =
1875 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001876 std::string ExtName(Interface->getNameAsString() + "_" +
1877 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001878
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001879 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001880 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001881 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001882 // Instance methods should always be defined.
1883 InstanceMethods.push_back(GetMethodConstant(*i));
1884 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001885 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001886 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001887 // Class methods should always be defined.
1888 ClassMethods.push_back(GetMethodConstant(*i));
1889 }
1890
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001891 std::vector<llvm::Constant*> Values(7);
1892 Values[0] = GetClassName(OCD->getIdentifier());
1893 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00001894 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001895 Values[2] =
1896 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1897 ExtName,
1898 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001899 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001900 Values[3] =
1901 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001902 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001903 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001904 if (Category) {
1905 Values[4] =
1906 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1907 Category->protocol_begin(),
1908 Category->protocol_end());
1909 } else {
1910 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1911 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001912 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001913
1914 // If there is no category @interface then there can be no properties.
1915 if (Category) {
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001916 Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001917 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001918 } else {
1919 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1920 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001921
1922 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1923 Values);
1924
1925 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001926 CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init,
1927 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001928 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001929 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001930}
1931
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001932// FIXME: Get from somewhere?
1933enum ClassFlags {
1934 eClassFlags_Factory = 0x00001,
1935 eClassFlags_Meta = 0x00002,
1936 // <rdr://5142207>
1937 eClassFlags_HasCXXStructors = 0x02000,
1938 eClassFlags_Hidden = 0x20000,
1939 eClassFlags_ABI2_Hidden = 0x00010,
1940 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1941};
1942
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001943/*
1944 struct _objc_class {
1945 Class isa;
1946 Class super_class;
1947 const char *name;
1948 long version;
1949 long info;
1950 long instance_size;
1951 struct _objc_ivar_list *ivars;
1952 struct _objc_method_list *methods;
1953 struct _objc_cache *cache;
1954 struct _objc_protocol_list *protocols;
1955 // Objective-C 1.0 extensions (<rdr://4585769>)
1956 const char *ivar_layout;
1957 struct _objc_class_ext *ext;
1958 };
1959
1960 See EmitClassExtension();
1961 */
1962void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001963 DefinedSymbols.insert(ID->getIdentifier());
1964
Chris Lattner8ec03f52008-11-24 03:54:41 +00001965 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001966 // FIXME: Gross
1967 ObjCInterfaceDecl *Interface =
1968 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc933702008-08-21 21:57:41 +00001969 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001970 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001971 Interface->protocol_begin(),
1972 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001973 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00001974 unsigned Size =
1975 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001976
1977 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00001978 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001979 Flags |= eClassFlags_Hidden;
1980
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001981 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001982 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001983 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001984 // Instance methods should always be defined.
1985 InstanceMethods.push_back(GetMethodConstant(*i));
1986 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001987 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001988 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001989 // Class methods should always be defined.
1990 ClassMethods.push_back(GetMethodConstant(*i));
1991 }
1992
Douglas Gregor653f1b12009-04-23 01:02:12 +00001993 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001994 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001995 ObjCPropertyImplDecl *PID = *i;
1996
1997 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1998 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1999
2000 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2001 if (llvm::Constant *C = GetMethodConstant(MD))
2002 InstanceMethods.push_back(C);
2003 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2004 if (llvm::Constant *C = GetMethodConstant(MD))
2005 InstanceMethods.push_back(C);
2006 }
2007 }
2008
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002009 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002010 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002011 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002012 // Record a reference to the super class.
2013 LazySymbols.insert(Super->getIdentifier());
2014
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002015 Values[ 1] =
2016 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
2017 ObjCTypes.ClassPtrTy);
2018 } else {
2019 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
2020 }
2021 Values[ 2] = GetClassName(ID->getIdentifier());
2022 // Version is always 0.
2023 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2024 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2025 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002026 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002027 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002028 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002029 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002030 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002031 // cache is always NULL.
2032 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
2033 Values[ 9] = Protocols;
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002034 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002035 Values[11] = EmitClassExtension(ID);
2036 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
2037 Values);
2038
2039 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002040 CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init,
2041 "__OBJC,__class,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002042 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002043 DefinedClasses.push_back(GV);
2044}
2045
2046llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2047 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002048 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002049 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002050 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002051
Daniel Dunbar04d40782009-04-14 06:00:08 +00002052 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002053 Flags |= eClassFlags_Hidden;
2054
2055 std::vector<llvm::Constant*> Values(12);
2056 // The isa for the metaclass is the root of the hierarchy.
2057 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2058 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2059 Root = Super;
2060 Values[ 0] =
2061 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
2062 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002063 // The super class for the metaclass is emitted as the name of the
2064 // super class. The runtime fixes this up to point to the
2065 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002066 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
2067 Values[ 1] =
2068 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
2069 ObjCTypes.ClassPtrTy);
2070 } else {
2071 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
2072 }
2073 Values[ 2] = GetClassName(ID->getIdentifier());
2074 // Version is always 0.
2075 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2076 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2077 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002078 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002079 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002080 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002081 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002082 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002083 // cache is always NULL.
2084 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
2085 Values[ 9] = Protocols;
2086 // ivar_layout for metaclass is always NULL.
2087 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2088 // The class extension is always unused for metaclasses.
2089 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
2090 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
2091 Values);
2092
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002093 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002094 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002095
2096 // Check for a forward reference.
2097 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2098 if (GV) {
2099 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2100 "Forward metaclass reference has incorrect type.");
2101 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2102 GV->setInitializer(Init);
2103 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002104 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002105 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002106 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002107 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002108 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002109 GV->setAlignment(4);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002110 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002111
2112 return GV;
2113}
2114
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002115llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002116 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002117
Mike Stumpf5408fe2009-05-16 07:57:57 +00002118 // FIXME: Should we look these up somewhere other than the module. Its a bit
2119 // silly since we only generate these while processing an implementation, so
2120 // exactly one pointer would work if know when we entered/exitted an
2121 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002122
2123 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002124 // Previously, metaclass with internal linkage may have been defined.
2125 // pass 'true' as 2nd argument so it is returned.
2126 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002127 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2128 "Forward metaclass reference has incorrect type.");
2129 return GV;
2130 } else {
2131 // Generate as an external reference to keep a consistent
2132 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002133 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002134 llvm::GlobalValue::ExternalLinkage,
2135 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002136 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002137 }
2138}
2139
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002140/*
2141 struct objc_class_ext {
2142 uint32_t size;
2143 const char *weak_ivar_layout;
2144 struct _objc_property_list *properties;
2145 };
2146*/
2147llvm::Constant *
2148CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
2149 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002150 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002151
2152 std::vector<llvm::Constant*> Values(3);
2153 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002154 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002155 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002156 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002157
2158 // Return null if no extension bits are used.
2159 if (Values[1]->isNullValue() && Values[2]->isNullValue())
2160 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
2161
2162 llvm::Constant *Init =
2163 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002164 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002165 Init, "__OBJC,__class_ext,regular,no_dead_strip",
2166 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002167}
2168
2169/*
2170 struct objc_ivar {
2171 char *ivar_name;
2172 char *ivar_type;
2173 int ivar_offset;
2174 };
2175
2176 struct objc_ivar_list {
2177 int ivar_count;
2178 struct objc_ivar list[count];
2179 };
2180 */
2181llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002182 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002183 std::vector<llvm::Constant*> Ivars, Ivar(3);
2184
2185 // When emitting the root class GCC emits ivar entries for the
2186 // actual class structure. It is not clear if we need to follow this
2187 // behavior; for now lets try and get away with not doing it. If so,
2188 // the cleanest solution would be to make up an ObjCInterfaceDecl
2189 // for the class.
2190 if (ForClass)
2191 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002192
2193 ObjCInterfaceDecl *OID =
2194 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002195
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002196 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002197 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002198
2199 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2200 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002201 // Ignore unnamed bit-fields.
2202 if (!IVD->getDeclName())
2203 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002204 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2205 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00002206 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002207 ComputeIvarBaseOffset(CGM, OID, IVD));
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002208 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002209 }
2210
2211 // Return null for empty list.
2212 if (Ivars.empty())
2213 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
2214
2215 std::vector<llvm::Constant*> Values(2);
2216 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
2217 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
2218 Ivars.size());
2219 Values[1] = llvm::ConstantArray::get(AT, Ivars);
2220 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2221
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002222 llvm::GlobalVariable *GV;
2223 if (ForClass)
2224 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(),
Daniel Dunbar58a29122009-03-09 22:18:41 +00002225 Init, "__OBJC,__class_vars,regular,no_dead_strip",
2226 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002227 else
2228 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_"
2229 + ID->getNameAsString(),
2230 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002231 4, true);
2232 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002233}
2234
2235/*
2236 struct objc_method {
2237 SEL method_name;
2238 char *method_types;
2239 void *method;
2240 };
2241
2242 struct objc_method_list {
2243 struct objc_method_list *obsolete;
2244 int count;
2245 struct objc_method methods_list[count];
2246 };
2247*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002248
2249/// GetMethodConstant - Return a struct objc_method constant for the
2250/// given method if it has been defined. The result is null if the
2251/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002252llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002253 // FIXME: Use DenseMap::lookup
2254 llvm::Function *Fn = MethodDefinitions[MD];
2255 if (!Fn)
2256 return 0;
2257
2258 std::vector<llvm::Constant*> Method(3);
2259 Method[0] =
2260 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2261 ObjCTypes.SelectorPtrTy);
2262 Method[1] = GetMethodVarType(MD);
2263 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
2264 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
2265}
2266
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002267llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
2268 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002269 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002270 // Return null for empty list.
2271 if (Methods.empty())
2272 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
2273
2274 std::vector<llvm::Constant*> Values(3);
2275 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2276 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
2277 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
2278 Methods.size());
2279 Values[2] = llvm::ConstantArray::get(AT, Methods);
2280 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2281
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002282 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002283 return llvm::ConstantExpr::getBitCast(GV,
2284 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002285}
2286
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002287llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00002288 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002289 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002290 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002291
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002292 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002293 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002294 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002295 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002296 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002297 llvm::GlobalValue::InternalLinkage,
2298 Name,
2299 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002300 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002301
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002302 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002303}
2304
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002305llvm::GlobalVariable *
2306CGObjCCommonMac::CreateMetadataVar(const std::string &Name,
2307 llvm::Constant *Init,
2308 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002309 unsigned Align,
2310 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002311 const llvm::Type *Ty = Init->getType();
2312 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002313 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002314 llvm::GlobalValue::InternalLinkage,
2315 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00002316 Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002317 if (Section)
2318 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002319 if (Align)
2320 GV->setAlignment(Align);
2321 if (AddToUsed)
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002322 UsedGlobals.push_back(GV);
2323 return GV;
2324}
2325
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002326llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002327 // Abuse this interface function as a place to finalize.
2328 FinishModule();
2329
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002330 return NULL;
2331}
2332
Chris Lattner74391b42009-03-22 21:03:39 +00002333llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002334 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002335}
2336
Chris Lattner74391b42009-03-22 21:03:39 +00002337llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002338 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002339}
2340
Chris Lattner74391b42009-03-22 21:03:39 +00002341llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002342 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002343}
2344
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002345/*
2346
2347Objective-C setjmp-longjmp (sjlj) Exception Handling
2348--
2349
2350The basic framework for a @try-catch-finally is as follows:
2351{
2352 objc_exception_data d;
2353 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002354 bool _call_try_exit = true;
2355
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002356 objc_exception_try_enter(&d);
2357 if (!setjmp(d.jmp_buf)) {
2358 ... try body ...
2359 } else {
2360 // exception path
2361 id _caught = objc_exception_extract(&d);
2362
2363 // enter new try scope for handlers
2364 if (!setjmp(d.jmp_buf)) {
2365 ... match exception and execute catch blocks ...
2366
2367 // fell off end, rethrow.
2368 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00002369 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002370 } else {
2371 // exception in catch block
2372 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002373 _call_try_exit = false;
2374 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002375 }
2376 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002377 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002378
2379finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002380 if (_call_try_exit)
2381 objc_exception_try_exit(&d);
2382
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002383 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002384 ... dispatch to finally destination ...
2385
2386finally_rethrow:
2387 objc_exception_throw(_rethrow);
2388
2389finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002390}
2391
2392This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00002393uses _rethrow to determine if objc_exception_try_exit should be called
2394and if the object should be rethrown. This breaks in the face of
2395throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002396
2397We specialize this framework for a few particular circumstances:
2398
2399 - If there are no catch blocks, then we avoid emitting the second
2400 exception handling context.
2401
2402 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2403 e)) we avoid emitting the code to rethrow an uncaught exception.
2404
2405 - FIXME: If there is no @finally block we can do a few more
2406 simplifications.
2407
2408Rethrows and Jumps-Through-Finally
2409--
2410
2411Support for implicit rethrows and jumping through the finally block is
2412handled by storing the current exception-handling context in
2413ObjCEHStack.
2414
Daniel Dunbar898d5082008-09-30 01:06:03 +00002415In order to implement proper @finally semantics, we support one basic
2416mechanism for jumping through the finally block to an arbitrary
2417destination. Constructs which generate exits from a @try or @catch
2418block use this mechanism to implement the proper semantics by chaining
2419jumps, as necessary.
2420
2421This mechanism works like the one used for indirect goto: we
2422arbitrarily assign an ID to each destination and store the ID for the
2423destination in a variable prior to entering the finally block. At the
2424end of the finally block we simply create a switch to the proper
2425destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002426
2427Code gen for @synchronized(expr) stmt;
2428Effectively generating code for:
2429objc_sync_enter(expr);
2430@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002431*/
2432
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002433void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2434 const Stmt &S) {
2435 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002436 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002437 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002438 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00002439 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
2440 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
2441 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar1c566672009-02-24 01:43:46 +00002442
2443 // For @synchronized, call objc_sync_enter(sync.expr). The
2444 // evaluation of the expression must occur before we enter the
2445 // @synchronized. We can safely avoid a temp here because jumps into
2446 // @synchronized are illegal & this will dominate uses.
2447 llvm::Value *SyncArg = 0;
2448 if (!isTry) {
2449 SyncArg =
2450 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2451 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00002452 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002453 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002454
2455 // Push an EH context entry, used for handling rethrows and jumps
2456 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002457 CGF.PushCleanupBlock(FinallyBlock);
2458
Anders Carlsson273558f2009-02-07 21:37:21 +00002459 CGF.ObjCEHValueStack.push_back(0);
2460
Daniel Dunbar898d5082008-09-30 01:06:03 +00002461 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00002462 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2463 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002464 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
2465 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002466 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
2467 "_call_try_exit");
2468 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
2469
Anders Carlsson80f25672008-09-09 17:59:25 +00002470 // Enter a new try block and call setjmp.
Chris Lattner34b02a12009-04-22 02:26:14 +00002471 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Anders Carlsson80f25672008-09-09 17:59:25 +00002472 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
2473 "jmpbufarray");
2474 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
Chris Lattner34b02a12009-04-22 02:26:14 +00002475 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002476 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002477
Daniel Dunbar55e87422008-11-11 02:29:29 +00002478 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2479 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002480 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002481 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002482
2483 // Emit the @try block.
2484 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002485 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
2486 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002487 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002488
2489 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002490 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002491
2492 // Retrieve the exception object. We may emit multiple blocks but
2493 // nothing can cross this so the value is already in SSA form.
Chris Lattner34b02a12009-04-22 02:26:14 +00002494 llvm::Value *Caught =
2495 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2496 ExceptionData, "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00002497 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002498 if (!isTry)
2499 {
2500 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002501 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002502 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002503 }
2504 else if (const ObjCAtCatchStmt* CatchStmt =
2505 cast<ObjCAtTryStmt>(S).getCatchStmts())
2506 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00002507 // Enter a new exception try block (in case a @catch block throws
2508 // an exception).
Chris Lattner34b02a12009-04-22 02:26:14 +00002509 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002510
Chris Lattner34b02a12009-04-22 02:26:14 +00002511 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002512 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002513 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00002514
Daniel Dunbar55e87422008-11-11 02:29:29 +00002515 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
2516 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002517 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002518
2519 CGF.EmitBlock(CatchBlock);
2520
Daniel Dunbar55e40722008-09-27 07:03:52 +00002521 // Handle catch list. As a special case we check if everything is
2522 // matched and avoid generating code for falling off the end if
2523 // so.
2524 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00002525 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00002526 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00002527
Steve Naroff7ba138a2009-03-03 19:52:17 +00002528 const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00002529 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00002530
Anders Carlsson80f25672008-09-09 17:59:25 +00002531 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00002532 if (!CatchParam) {
2533 AllMatched = true;
2534 } else {
Steve Naroff14108da2009-07-10 23:34:53 +00002535 OPT = CatchParam->getType()->getAsObjCObjectPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002536
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002537 // catch(id e) always matches.
2538 // FIXME: For the time being we also match id<X>; this should
2539 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00002540 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00002541 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00002542 }
2543
Daniel Dunbar55e40722008-09-27 07:03:52 +00002544 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00002545 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002546 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002547 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002548 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002549 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002550
Anders Carlssondde0a942008-09-11 09:15:33 +00002551 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002552 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002553 break;
2554 }
2555
Steve Naroff14108da2009-07-10 23:34:53 +00002556 assert(OPT && "Unexpected non-object pointer type in @catch");
2557 QualType T = OPT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002558 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002559 assert(ObjCType && "Catch parameter must have Objective-C type!");
2560
2561 // Check if the @catch block matches the exception object.
2562 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2563
Chris Lattner34b02a12009-04-22 02:26:14 +00002564 llvm::Value *Match =
2565 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
2566 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002567
Daniel Dunbar55e87422008-11-11 02:29:29 +00002568 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002569
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002570 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002571 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002572
2573 // Emit the @catch block.
2574 CGF.EmitBlock(MatchedBlock);
Steve Naroff7ba138a2009-03-03 19:52:17 +00002575 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002576 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002577
2578 llvm::Value *Tmp =
Steve Naroff7ba138a2009-03-03 19:52:17 +00002579 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002580 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002581 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002582
2583 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002584 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002585
2586 CGF.EmitBlock(NextCatchBlock);
2587 }
2588
Daniel Dunbar55e40722008-09-27 07:03:52 +00002589 if (!AllMatched) {
2590 // None of the handlers caught the exception, so store it to be
2591 // rethrown at the end of the @finally block.
2592 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002593 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002594 }
2595
2596 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002597 CGF.EmitBlock(CatchHandler);
Chris Lattner34b02a12009-04-22 02:26:14 +00002598 CGF.Builder.CreateStore(
2599 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2600 ExceptionData),
Daniel Dunbar55e40722008-09-27 07:03:52 +00002601 RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002602 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002603 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002604 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002605 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002606 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002607 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002608 }
2609
Daniel Dunbar898d5082008-09-30 01:06:03 +00002610 // Pop the exception-handling stack entry. It is important to do
2611 // this now, because the code in the @finally block is not in this
2612 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002613 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2614
Anders Carlsson273558f2009-02-07 21:37:21 +00002615 CGF.ObjCEHValueStack.pop_back();
2616
Anders Carlsson80f25672008-09-09 17:59:25 +00002617 // Emit the @finally block.
2618 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002619 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2620
2621 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2622
2623 CGF.EmitBlock(FinallyExit);
Chris Lattner34b02a12009-04-22 02:26:14 +00002624 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002625
2626 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002627 if (isTry) {
2628 if (const ObjCAtFinallyStmt* FinallyStmt =
2629 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2630 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar1c566672009-02-24 01:43:46 +00002631 } else {
2632 // Emit objc_sync_exit(expr); as finally's sole statement for
2633 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00002634 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002635 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002636
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002637 // Emit the switch block
2638 if (Info.SwitchBlock)
2639 CGF.EmitBlock(Info.SwitchBlock);
2640 if (Info.EndBlock)
2641 CGF.EmitBlock(Info.EndBlock);
2642
Daniel Dunbar898d5082008-09-30 01:06:03 +00002643 CGF.EmitBlock(FinallyRethrow);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002644 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar898d5082008-09-30 01:06:03 +00002645 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002646 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002647
2648 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002649}
2650
2651void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002652 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002653 llvm::Value *ExceptionAsObject;
2654
2655 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2656 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2657 ExceptionAsObject =
2658 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2659 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002660 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002661 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002662 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002663 }
2664
Chris Lattnerbbccd612009-04-22 02:38:11 +00002665 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002666 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002667
2668 // Clear the insertion point to indicate we are in unreachable code.
2669 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002670}
2671
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002672/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002673/// object: objc_read_weak (id *src)
2674///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002675llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002676 llvm::Value *AddrWeakObj)
2677{
Eli Friedman8339b352009-03-07 03:57:15 +00002678 const llvm::Type* DestTy =
2679 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002680 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00002681 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002682 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00002683 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002684 return read_weak;
2685}
2686
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002687/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2688/// objc_assign_weak (id src, id *dst)
2689///
2690void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2691 llvm::Value *src, llvm::Value *dst)
2692{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002693 const llvm::Type * SrcTy = src->getType();
2694 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002695 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002696 assert(Size <= 8 && "does not support size > 8");
2697 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2698 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002699 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2700 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002701 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2702 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00002703 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002704 src, dst, "weakassign");
2705 return;
2706}
2707
Fariborz Jahanian58626502008-11-19 00:59:10 +00002708/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2709/// objc_assign_global (id src, id *dst)
2710///
2711void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2712 llvm::Value *src, llvm::Value *dst)
2713{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002714 const llvm::Type * SrcTy = src->getType();
2715 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002716 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002717 assert(Size <= 8 && "does not support size > 8");
2718 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2719 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002720 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2721 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002722 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2723 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002724 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002725 src, dst, "globalassign");
2726 return;
2727}
2728
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002729/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2730/// objc_assign_ivar (id src, id *dst)
2731///
2732void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2733 llvm::Value *src, llvm::Value *dst)
2734{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002735 const llvm::Type * SrcTy = src->getType();
2736 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002737 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002738 assert(Size <= 8 && "does not support size > 8");
2739 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2740 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002741 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2742 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002743 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2744 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002745 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002746 src, dst, "assignivar");
2747 return;
2748}
2749
Fariborz Jahanian58626502008-11-19 00:59:10 +00002750/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2751/// objc_assign_strongCast (id src, id *dst)
2752///
2753void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2754 llvm::Value *src, llvm::Value *dst)
2755{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002756 const llvm::Type * SrcTy = src->getType();
2757 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002758 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002759 assert(Size <= 8 && "does not support size > 8");
2760 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2761 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002762 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2763 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002764 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2765 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002766 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002767 src, dst, "weakassign");
2768 return;
2769}
2770
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002771void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
2772 llvm::Value *DestPtr,
2773 llvm::Value *SrcPtr,
2774 unsigned long size) {
2775 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
2776 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
2777 llvm::Value *N = llvm::ConstantInt::get(ObjCTypes.LongTy, size);
2778 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
2779 DestPtr, SrcPtr, N);
2780 return;
2781}
2782
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002783/// EmitObjCValueForIvar - Code Gen for ivar reference.
2784///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002785LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2786 QualType ObjectTy,
2787 llvm::Value *BaseValue,
2788 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002789 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00002790 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00002791 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
2792 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002793}
2794
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002795llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00002796 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002797 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00002798 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002799 return llvm::ConstantInt::get(
2800 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2801 Offset);
2802}
2803
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002804/* *** Private Interface *** */
2805
2806/// EmitImageInfo - Emit the image info marker used to encode some module
2807/// level information.
2808///
2809/// See: <rdr://4810609&4810587&4810587>
2810/// struct IMAGE_INFO {
2811/// unsigned version;
2812/// unsigned flags;
2813/// };
2814enum ImageInfoFlags {
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002815 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what
2816 // this implies.
2817 eImageInfo_GarbageCollected = (1 << 1),
2818 eImageInfo_GCOnly = (1 << 2),
2819 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
2820
2821 // A flag indicating that the module has no instances of an
2822 // @synthesize of a superclass variable. <rdar://problem/6803242>
2823 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002824};
2825
2826void CGObjCMac::EmitImageInfo() {
2827 unsigned version = 0; // Version is unused?
2828 unsigned flags = 0;
2829
2830 // FIXME: Fix and continue?
2831 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2832 flags |= eImageInfo_GarbageCollected;
2833 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2834 flags |= eImageInfo_GCOnly;
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002835
2836 // We never allow @synthesize of a superclass property.
2837 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002838
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002839 // Emitted as int[2];
2840 llvm::Constant *values[2] = {
2841 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2842 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2843 };
2844 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002845
2846 const char *Section;
2847 if (ObjCABI == 1)
2848 Section = "__OBJC, __image_info,regular";
2849 else
2850 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002851 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002852 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
2853 llvm::ConstantArray::get(AT, values, 2),
2854 Section,
2855 0,
2856 true);
2857 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002858}
2859
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002860
2861// struct objc_module {
2862// unsigned long version;
2863// unsigned long size;
2864// const char *name;
2865// Symtab symtab;
2866// };
2867
2868// FIXME: Get from somewhere
2869static const int ModuleVersion = 7;
2870
2871void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00002872 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002873
2874 std::vector<llvm::Constant*> Values(4);
2875 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2876 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002877 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002878 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002879 Values[3] = EmitModuleSymbols();
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002880 CreateMetadataVar("\01L_OBJC_MODULES",
2881 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
2882 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002883 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002884}
2885
2886llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002887 unsigned NumClasses = DefinedClasses.size();
2888 unsigned NumCategories = DefinedCategories.size();
2889
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002890 // Return null if no symbols were defined.
2891 if (!NumClasses && !NumCategories)
2892 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2893
2894 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002895 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2896 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2897 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2898 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2899
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002900 // The runtime expects exactly the list of defined classes followed
2901 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002902 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002903 for (unsigned i=0; i<NumClasses; i++)
2904 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2905 ObjCTypes.Int8PtrTy);
2906 for (unsigned i=0; i<NumCategories; i++)
2907 Symbols[NumClasses + i] =
2908 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2909 ObjCTypes.Int8PtrTy);
2910
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002911 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002912 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002913 NumClasses + NumCategories),
2914 Symbols);
2915
2916 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2917
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002918 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002919 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
2920 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002921 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002922 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2923}
2924
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002925llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002926 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002927 LazySymbols.insert(ID->getIdentifier());
2928
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002929 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2930
2931 if (!Entry) {
2932 llvm::Constant *Casted =
2933 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2934 ObjCTypes.ClassPtrTy);
2935 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002936 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
2937 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002938 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002939 }
2940
2941 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002942}
2943
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002944llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002945 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2946
2947 if (!Entry) {
2948 llvm::Constant *Casted =
2949 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2950 ObjCTypes.SelectorPtrTy);
2951 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002952 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
2953 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002954 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002955 }
2956
2957 return Builder.CreateLoad(Entry, false, "tmp");
2958}
2959
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002960llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002961 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002962
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002963 if (!Entry)
2964 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2965 llvm::ConstantArray::get(Ident->getName()),
2966 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00002967 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002968
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002969 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002970}
2971
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002972/// GetIvarLayoutName - Returns a unique constant for the given
2973/// ivar layout bitmap.
2974llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
2975 const ObjCCommonTypesHelper &ObjCTypes) {
2976 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2977}
2978
Daniel Dunbard58edcb2009-05-03 14:10:34 +00002979static QualType::GCAttrTypes GetGCAttrTypeForType(ASTContext &Ctx,
2980 QualType FQT) {
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002981 if (FQT.isObjCGCStrong())
2982 return QualType::Strong;
2983
2984 if (FQT.isObjCGCWeak())
2985 return QualType::Weak;
2986
Daniel Dunbard58edcb2009-05-03 14:10:34 +00002987 if (Ctx.isObjCObjectPointerType(FQT))
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002988 return QualType::Strong;
2989
2990 if (const PointerType *PT = FQT->getAsPointerType())
Daniel Dunbard58edcb2009-05-03 14:10:34 +00002991 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002992
2993 return QualType::GCNone;
2994}
2995
Daniel Dunbard58edcb2009-05-03 14:10:34 +00002996void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
2997 unsigned int BytePos,
2998 bool ForStrongLayout,
2999 bool &HasUnion) {
3000 const RecordDecl *RD = RT->getDecl();
3001 // FIXME - Use iterator.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003002 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003003 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
3004 const llvm::StructLayout *RecLayout =
3005 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
3006
3007 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3008 ForStrongLayout, HasUnion);
3009}
3010
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003011void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003012 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003013 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003014 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003015 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003016 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003017 bool IsUnion = (RD && RD->isUnion());
3018 uint64_t MaxUnionIvarSize = 0;
3019 uint64_t MaxSkippedUnionIvarSize = 0;
3020 FieldDecl *MaxField = 0;
3021 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003022 FieldDecl *LastFieldBitfield = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003023 uint64_t MaxFieldOffset = 0;
3024 uint64_t MaxSkippedFieldOffset = 0;
3025 uint64_t LastBitfieldOffset = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003026
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003027 if (RecFields.empty())
3028 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003029 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3030 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3031
Chris Lattnerf1690852009-03-31 08:48:01 +00003032 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003033 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003034 uint64_t FieldOffset;
3035 if (RD)
3036 FieldOffset =
3037 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
3038 else
3039 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003040
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003041 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003042 if (!Field->getIdentifier() || Field->isBitField()) {
3043 LastFieldBitfield = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003044 LastBitfieldOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003045 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003046 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003047
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003048 LastFieldBitfield = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003049 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003050 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003051 if (FQT->isUnionType())
3052 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003053
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003054 BuildAggrIvarRecordLayout(FQT->getAsRecordType(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003055 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003056 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003057 continue;
3058 }
Chris Lattnerf1690852009-03-31 08:48:01 +00003059
3060 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003061 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003062 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003063 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003064 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003065 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003066 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3067 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003068 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003069 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003070 FQT = CArray->getElementType();
3071 }
3072
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003073 assert(!FQT->isUnionType() &&
3074 "layout for array of unions not supported");
3075 if (FQT->isRecordType()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003076 int OldIndex = IvarsInfo.size() - 1;
3077 int OldSkIndex = SkipIvars.size() -1;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003078
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003079 const RecordType *RT = FQT->getAsRecordType();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003080 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003081 ForStrongLayout, HasUnion);
3082
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003083 // Replicate layout information for each array element. Note that
3084 // one element is already done.
3085 uint64_t ElIx = 1;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003086 for (int FirstIndex = IvarsInfo.size() - 1,
3087 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003088 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003089 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3090 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3091 IvarsInfo[i].ivar_size));
3092 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3093 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3094 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003095 }
3096 continue;
3097 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003098 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003099 // At this point, we are done with Record/Union and array there of.
3100 // For other arrays we are down to its element type.
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003101 QualType::GCAttrTypes GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003102
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003103 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003104 if ((ForStrongLayout && GCAttr == QualType::Strong)
3105 || (!ForStrongLayout && GCAttr == QualType::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003106 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003107 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003108 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003109 MaxUnionIvarSize = UnionIvarSize;
3110 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003111 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003112 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003113 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003114 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003115 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003116 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003117 } else if ((ForStrongLayout &&
3118 (GCAttr == QualType::GCNone || GCAttr == QualType::Weak))
3119 || (!ForStrongLayout && GCAttr != QualType::Weak)) {
3120 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003121 // FIXME: Why the asymmetry? We divide by word size in bits on other
3122 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003123 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003124 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003125 MaxSkippedUnionIvarSize = UnionIvarSize;
3126 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003127 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003128 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003129 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003130 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003131 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003132 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003133 }
3134 }
3135 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003136
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003137 if (LastFieldBitfield) {
3138 // Last field was a bitfield. Must update skip info.
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003139 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3140 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00003141 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar487993b2009-05-03 13:32:01 +00003142 GC_IVAR skivar;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003143 skivar.ivar_bytepos = BytePos + LastBitfieldOffset;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003144 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3145 + ((BitFieldSize % ByteSizeInBits) != 0);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003146 SkipIvars.push_back(skivar);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003147 }
3148
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003149 if (MaxField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003150 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003151 MaxUnionIvarSize));
3152 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003153 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003154 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003155}
3156
3157/// BuildIvarLayout - Builds ivar layout bitmap for the class
3158/// implementation for the __strong or __weak case.
3159/// The layout map displays which words in ivar list must be skipped
3160/// and which must be scanned by GC (see below). String is built of bytes.
3161/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3162/// of words to skip and right nibble is count of words to scan. So, each
3163/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3164/// represented by a 0x00 byte which also ends the string.
3165/// 1. when ForStrongLayout is true, following ivars are scanned:
3166/// - id, Class
3167/// - object *
3168/// - __strong anything
3169///
3170/// 2. When ForStrongLayout is false, following ivars are scanned:
3171/// - __weak anything
3172///
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003173llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003174 const ObjCImplementationDecl *OMD,
3175 bool ForStrongLayout) {
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003176 bool hasUnion = false;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003177
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003178 unsigned int WordsToScan, WordsToSkip;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003179 const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3180 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3181 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003182
Chris Lattnerf1690852009-03-31 08:48:01 +00003183 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003184 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003185 CGM.getContext().CollectObjCIvars(OI, RecFields);
Fariborz Jahanian98200742009-05-12 18:14:29 +00003186
Daniel Dunbar37153282009-05-04 04:10:48 +00003187 // Add this implementations synthesized ivars.
Fariborz Jahanian98200742009-05-12 18:14:29 +00003188 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
3189 CGM.getContext().CollectSynthesizedIvars(OI, Ivars);
3190 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3191 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3192
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003193 if (RecFields.empty())
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003194 return llvm::Constant::getNullValue(PtrTy);
Chris Lattnerf1690852009-03-31 08:48:01 +00003195
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003196 SkipIvars.clear();
3197 IvarsInfo.clear();
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00003198
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003199 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003200 if (IvarsInfo.empty())
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003201 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003202
3203 // Sort on byte position in case we encounterred a union nested in
3204 // the ivar list.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003205 if (hasUnion && !IvarsInfo.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003206 std::sort(IvarsInfo.begin(), IvarsInfo.end());
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003207 if (hasUnion && !SkipIvars.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003208 std::sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003209
3210 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003211 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003212 unsigned int WordSize =
Duncan Sands9408c452009-05-09 07:08:47 +00003213 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003214 if (IvarsInfo[0].ivar_bytepos == 0) {
3215 WordsToSkip = 0;
3216 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003217 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003218 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3219 WordsToScan = IvarsInfo[0].ivar_size;
3220 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003221 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003222 unsigned int TailPrevGCObjC =
3223 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003224 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003225 // consecutive 'scanned' object pointers.
3226 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003227 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003228 // Skip over 'gc'able object pointer which lay over each other.
3229 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3230 continue;
3231 // Must skip over 1 or more words. We save current skip/scan values
3232 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003233 SKIP_SCAN SkScan;
3234 SkScan.skip = WordsToSkip;
3235 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003236 SkipScanIvars.push_back(SkScan);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003237
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003238 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003239 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3240 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003241 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003242 WordsToSkip = 0;
3243 WordsToScan = IvarsInfo[i].ivar_size;
3244 }
3245 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003246 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003247 SKIP_SCAN SkScan;
3248 SkScan.skip = WordsToSkip;
3249 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003250 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003251 }
3252
3253 bool BytesSkipped = false;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003254 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003255 unsigned int LastIndex = SkipIvars.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003256 int LastByteSkipped =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003257 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
3258 LastIndex = IvarsInfo.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003259 int LastByteScanned =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003260 IvarsInfo[LastIndex].ivar_bytepos +
3261 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003262 BytesSkipped = (LastByteSkipped > LastByteScanned);
3263 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003264 if (BytesSkipped) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003265 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003266 SKIP_SCAN SkScan;
3267 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3268 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003269 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003270 }
3271 }
3272 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3273 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003274 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003275 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003276 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3277 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3278 // 0xM0 followed by 0x0N detected.
3279 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3280 for (int j = i+1; j < SkipScan; j++)
3281 SkipScanIvars[j] = SkipScanIvars[j+1];
3282 --SkipScan;
3283 }
3284 }
3285
3286 // Generate the string.
3287 std::string BitMap;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003288 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003289 unsigned char byte;
3290 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3291 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3292 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3293 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
3294
3295 if (skip_small > 0 || skip_big > 0)
3296 BytesSkipped = true;
3297 // first skip big.
3298 for (unsigned int ix = 0; ix < skip_big; ix++)
3299 BitMap += (unsigned char)(0xf0);
3300
3301 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003302 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003303 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003304 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003305 byte |= 0xf;
3306 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003307 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003308 byte |= scan_small;
3309 scan_small = 0;
3310 }
3311 BitMap += byte;
3312 }
3313 // next scan big
3314 for (unsigned int ix = 0; ix < scan_big; ix++)
3315 BitMap += (unsigned char)(0x0f);
3316 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003317 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003318 byte = scan_small;
3319 BitMap += byte;
3320 }
3321 }
3322 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003323 unsigned char zero = 0;
3324 BitMap += zero;
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003325
3326 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
3327 printf("\n%s ivar layout for class '%s': ",
3328 ForStrongLayout ? "strong" : "weak",
3329 OMD->getClassInterface()->getNameAsCString());
3330 const unsigned char *s = (unsigned char*)BitMap.c_str();
3331 for (unsigned i = 0; i < BitMap.size(); i++)
3332 if (!(s[i] & 0xf0))
3333 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3334 else
3335 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3336 printf("\n");
3337 }
3338
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003339 // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as
3340 // final layout.
3341 if (ForStrongLayout && !BytesSkipped)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003342 return llvm::Constant::getNullValue(PtrTy);
3343 llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3344 llvm::ConstantArray::get(BitMap.c_str()),
3345 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003346 1, true);
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003347 return getConstantGEP(Entry, 0, 0);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003348}
3349
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003350llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003351 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3352
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003353 // FIXME: Avoid std::string copying.
3354 if (!Entry)
3355 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
3356 llvm::ConstantArray::get(Sel.getAsString()),
3357 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003358 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003359
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003360 return getConstantGEP(Entry, 0, 0);
3361}
3362
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003363// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003364llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003365 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3366}
3367
3368// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003369llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003370 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3371}
3372
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003373llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003374 std::string TypeStr;
3375 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3376
3377 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003378
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003379 if (!Entry)
3380 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
3381 llvm::ConstantArray::get(TypeStr),
3382 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003383 1, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003384
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003385 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003386}
3387
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003388llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003389 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003390 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3391 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003392
3393 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3394
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003395 if (!Entry)
3396 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
3397 llvm::ConstantArray::get(TypeStr),
3398 "__TEXT,__cstring,cstring_literals",
3399 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003400
3401 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003402}
3403
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003404// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003405llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003406 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
3407
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003408 if (!Entry)
3409 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
3410 llvm::ConstantArray::get(Ident->getName()),
3411 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003412 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003413
3414 return getConstantGEP(Entry, 0, 0);
3415}
3416
3417// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003418// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003419llvm::Constant *
3420 CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3421 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003422 std::string TypeStr;
3423 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003424 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3425}
3426
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003427void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
3428 const ObjCContainerDecl *CD,
3429 std::string &NameOut) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00003430 NameOut = '\01';
3431 NameOut += (D->isInstanceMethod() ? '-' : '+');
Chris Lattner077bf5e2008-11-24 03:33:13 +00003432 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003433 assert (CD && "Missing container decl in GetNameForMethod");
3434 NameOut += CD->getNameAsString();
Fariborz Jahanian1e9aef32009-04-16 18:34:20 +00003435 if (const ObjCCategoryImplDecl *CID =
3436 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) {
3437 NameOut += '(';
3438 NameOut += CID->getNameAsString();
3439 NameOut+= ')';
3440 }
Chris Lattner077bf5e2008-11-24 03:33:13 +00003441 NameOut += ' ';
3442 NameOut += D->getSelector().getAsString();
3443 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003444}
3445
Fariborz Jahanianc38e9af2009-06-23 21:47:46 +00003446void CGObjCCommonMac::MergeMetadataGlobals(
3447 std::vector<llvm::Constant*> &UsedArray) {
3448 llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3449 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3450 e = UsedGlobals.end(); i != e; ++i) {
3451 UsedArray.push_back(llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(*i),
3452 i8PTy));
3453 }
3454}
3455
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003456void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003457 EmitModuleInfo();
3458
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003459 // Emit the dummy bodies for any protocols which were referenced but
3460 // never defined.
3461 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
3462 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
3463 if (i->second->hasInitializer())
3464 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003465
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003466 std::vector<llvm::Constant*> Values(5);
3467 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
3468 Values[1] = GetClassName(i->first);
3469 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
3470 Values[3] = Values[4] =
3471 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
3472 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
3473 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
3474 Values));
3475 }
3476
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003477 // Add assembler directives to add lazy undefined symbol references
3478 // for classes which are referenced but not defined. This is
3479 // important for correct linker interaction.
3480
3481 // FIXME: Uh, this isn't particularly portable.
3482 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00003483
3484 if (!CGM.getModule().getModuleInlineAsm().empty())
3485 s << "\n";
3486
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003487 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
3488 e = LazySymbols.end(); i != e; ++i) {
3489 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
3490 }
3491 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
3492 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003493 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003494 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
3495 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00003496
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003497 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003498}
3499
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003500CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003501 : CGObjCCommonMac(cgm),
3502 ObjCTypes(cgm)
3503{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003504 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003505 ObjCABI = 2;
3506}
3507
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003508/* *** */
3509
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003510ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
3511: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003512{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003513 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3514 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003515
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003516 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003517 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003518 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003519 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003520 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3521
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003522 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003523 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003524 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003525
Mike Stumpf5408fe2009-05-16 07:57:57 +00003526 // FIXME: It would be nice to unify this with the opaque type, so that the IR
3527 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003528 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
3529 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003530
3531 // I'm not sure I like this. The implicit coordination is a bit
3532 // gross. We should solve this in a reasonable fashion because this
3533 // is a pretty common task (match some runtime data structure with
3534 // an LLVM data structure).
3535
3536 // FIXME: This is leaked.
3537 // FIXME: Merge with rewriter code?
3538
3539 // struct _objc_super {
3540 // id self;
3541 // Class cls;
3542 // }
3543 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3544 SourceLocation(),
3545 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003546 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00003547 Ctx.getObjCIdType(), 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003548 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00003549 Ctx.getObjCClassType(), 0, false));
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003550 RD->completeDefinition(Ctx);
3551
3552 SuperCTy = Ctx.getTagDeclType(RD);
3553 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
3554
3555 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003556 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
3557
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003558 // struct _prop_t {
3559 // char *name;
3560 // char *attributes;
3561 // }
Chris Lattner1c02f862009-04-22 02:53:24 +00003562 PropertyTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003563 CGM.getModule().addTypeName("struct._prop_t",
3564 PropertyTy);
3565
3566 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003567 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003568 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003569 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003570 // }
3571 PropertyListTy = llvm::StructType::get(IntTy,
3572 IntTy,
3573 llvm::ArrayType::get(PropertyTy, 0),
3574 NULL);
3575 CGM.getModule().addTypeName("struct._prop_list_t",
3576 PropertyListTy);
3577 // struct _prop_list_t *
3578 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
3579
3580 // struct _objc_method {
3581 // SEL _cmd;
3582 // char *method_type;
3583 // char *_imp;
3584 // }
3585 MethodTy = llvm::StructType::get(SelectorPtrTy,
3586 Int8PtrTy,
3587 Int8PtrTy,
3588 NULL);
3589 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003590
3591 // struct _objc_cache *
3592 CacheTy = llvm::OpaqueType::get();
3593 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
3594 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003595}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003596
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003597ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
3598 : ObjCCommonTypesHelper(cgm)
3599{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003600 // struct _objc_method_description {
3601 // SEL name;
3602 // char *types;
3603 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003604 MethodDescriptionTy =
3605 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003606 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003607 NULL);
3608 CGM.getModule().addTypeName("struct._objc_method_description",
3609 MethodDescriptionTy);
3610
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003611 // struct _objc_method_description_list {
3612 // int count;
3613 // struct _objc_method_description[1];
3614 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003615 MethodDescriptionListTy =
3616 llvm::StructType::get(IntTy,
3617 llvm::ArrayType::get(MethodDescriptionTy, 0),
3618 NULL);
3619 CGM.getModule().addTypeName("struct._objc_method_description_list",
3620 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003621
3622 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003623 MethodDescriptionListPtrTy =
3624 llvm::PointerType::getUnqual(MethodDescriptionListTy);
3625
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003626 // Protocol description structures
3627
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003628 // struct _objc_protocol_extension {
3629 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3630 // struct _objc_method_description_list *optional_instance_methods;
3631 // struct _objc_method_description_list *optional_class_methods;
3632 // struct _objc_property_list *instance_properties;
3633 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003634 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003635 llvm::StructType::get(IntTy,
3636 MethodDescriptionListPtrTy,
3637 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003638 PropertyListPtrTy,
3639 NULL);
3640 CGM.getModule().addTypeName("struct._objc_protocol_extension",
3641 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003642
3643 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003644 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
3645
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003646 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003647
3648 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
3649 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3650
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003651 const llvm::Type *T =
3652 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
3653 LongTy,
3654 llvm::ArrayType::get(ProtocolTyHolder, 0),
3655 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003656 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3657
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003658 // struct _objc_protocol {
3659 // struct _objc_protocol_extension *isa;
3660 // char *protocol_name;
3661 // struct _objc_protocol **_objc_protocol_list;
3662 // struct _objc_method_description_list *instance_methods;
3663 // struct _objc_method_description_list *class_methods;
3664 // }
3665 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003666 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003667 llvm::PointerType::getUnqual(ProtocolListTyHolder),
3668 MethodDescriptionListPtrTy,
3669 MethodDescriptionListPtrTy,
3670 NULL);
3671 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
3672
3673 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
3674 CGM.getModule().addTypeName("struct._objc_protocol_list",
3675 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003676 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003677 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
3678
3679 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003680 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003681 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003682
3683 // Class description structures
3684
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003685 // struct _objc_ivar {
3686 // char *ivar_name;
3687 // char *ivar_type;
3688 // int ivar_offset;
3689 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003690 IvarTy = llvm::StructType::get(Int8PtrTy,
3691 Int8PtrTy,
3692 IntTy,
3693 NULL);
3694 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
3695
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003696 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003697 IvarListTy = llvm::OpaqueType::get();
3698 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
3699 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
3700
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003701 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003702 MethodListTy = llvm::OpaqueType::get();
3703 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
3704 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
3705
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003706 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003707 ClassExtensionTy =
3708 llvm::StructType::get(IntTy,
3709 Int8PtrTy,
3710 PropertyListPtrTy,
3711 NULL);
3712 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
3713 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
3714
3715 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3716
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003717 // struct _objc_class {
3718 // Class isa;
3719 // Class super_class;
3720 // char *name;
3721 // long version;
3722 // long info;
3723 // long instance_size;
3724 // struct _objc_ivar_list *ivars;
3725 // struct _objc_method_list *methods;
3726 // struct _objc_cache *cache;
3727 // struct _objc_protocol_list *protocols;
3728 // char *ivar_layout;
3729 // struct _objc_class_ext *ext;
3730 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003731 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3732 llvm::PointerType::getUnqual(ClassTyHolder),
3733 Int8PtrTy,
3734 LongTy,
3735 LongTy,
3736 LongTy,
3737 IvarListPtrTy,
3738 MethodListPtrTy,
3739 CachePtrTy,
3740 ProtocolListPtrTy,
3741 Int8PtrTy,
3742 ClassExtensionPtrTy,
3743 NULL);
3744 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
3745
3746 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
3747 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
3748 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
3749
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003750 // struct _objc_category {
3751 // char *category_name;
3752 // char *class_name;
3753 // struct _objc_method_list *instance_method;
3754 // struct _objc_method_list *class_method;
3755 // uint32_t size; // sizeof(struct _objc_category)
3756 // struct _objc_property_list *instance_properties;// category's @property
3757 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003758 CategoryTy = llvm::StructType::get(Int8PtrTy,
3759 Int8PtrTy,
3760 MethodListPtrTy,
3761 MethodListPtrTy,
3762 ProtocolListPtrTy,
3763 IntTy,
3764 PropertyListPtrTy,
3765 NULL);
3766 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
3767
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003768 // Global metadata structures
3769
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003770 // struct _objc_symtab {
3771 // long sel_ref_cnt;
3772 // SEL *refs;
3773 // short cls_def_cnt;
3774 // short cat_def_cnt;
3775 // char *defs[cls_def_cnt + cat_def_cnt];
3776 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003777 SymtabTy = llvm::StructType::get(LongTy,
3778 SelectorPtrTy,
3779 ShortTy,
3780 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003781 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003782 NULL);
3783 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
3784 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
3785
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003786 // struct _objc_module {
3787 // long version;
3788 // long size; // sizeof(struct _objc_module)
3789 // char *name;
3790 // struct _objc_symtab* symtab;
3791 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003792 ModuleTy =
3793 llvm::StructType::get(LongTy,
3794 LongTy,
3795 Int8PtrTy,
3796 SymtabPtrTy,
3797 NULL);
3798 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003799
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003800
Mike Stumpf5408fe2009-05-16 07:57:57 +00003801 // FIXME: This is the size of the setjmp buffer and should be target
3802 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00003803 uint64_t SetJmpBufferSize = 18;
3804
3805 // Exceptions
3806 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00003807 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003808
3809 ExceptionDataTy =
3810 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
3811 SetJmpBufferSize),
3812 StackPtrTy, NULL);
3813 CGM.getModule().addTypeName("struct._objc_exception_data",
3814 ExceptionDataTy);
3815
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003816}
3817
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003818ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003819: ObjCCommonTypesHelper(cgm)
3820{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003821 // struct _method_list_t {
3822 // uint32_t entsize; // sizeof(struct _objc_method)
3823 // uint32_t method_count;
3824 // struct _objc_method method_list[method_count];
3825 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003826 MethodListnfABITy = llvm::StructType::get(IntTy,
3827 IntTy,
3828 llvm::ArrayType::get(MethodTy, 0),
3829 NULL);
3830 CGM.getModule().addTypeName("struct.__method_list_t",
3831 MethodListnfABITy);
3832 // struct method_list_t *
3833 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003834
3835 // struct _protocol_t {
3836 // id isa; // NULL
3837 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003838 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003839 // const struct method_list_t * const instance_methods;
3840 // const struct method_list_t * const class_methods;
3841 // const struct method_list_t *optionalInstanceMethods;
3842 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003843 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003844 // const uint32_t size; // sizeof(struct _protocol_t)
3845 // const uint32_t flags; // = 0
3846 // }
3847
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003848 // Holder for struct _protocol_list_t *
3849 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3850
3851 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3852 Int8PtrTy,
3853 llvm::PointerType::getUnqual(
3854 ProtocolListTyHolder),
3855 MethodListnfABIPtrTy,
3856 MethodListnfABIPtrTy,
3857 MethodListnfABIPtrTy,
3858 MethodListnfABIPtrTy,
3859 PropertyListPtrTy,
3860 IntTy,
3861 IntTy,
3862 NULL);
3863 CGM.getModule().addTypeName("struct._protocol_t",
3864 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003865
3866 // struct _protocol_t*
3867 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003868
Fariborz Jahanianda320092009-01-29 19:24:30 +00003869 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003870 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00003871 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003872 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003873 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3874 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003875 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003876 NULL);
3877 CGM.getModule().addTypeName("struct._objc_protocol_list",
3878 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003879 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3880 ProtocolListnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003881
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003882 // struct _objc_protocol_list*
3883 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003884
3885 // struct _ivar_t {
3886 // unsigned long int *offset; // pointer to ivar offset location
3887 // char *name;
3888 // char *type;
3889 // uint32_t alignment;
3890 // uint32_t size;
3891 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003892 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3893 Int8PtrTy,
3894 Int8PtrTy,
3895 IntTy,
3896 IntTy,
3897 NULL);
3898 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3899
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003900 // struct _ivar_list_t {
3901 // uint32 entsize; // sizeof(struct _ivar_t)
3902 // uint32 count;
3903 // struct _iver_t list[count];
3904 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003905 IvarListnfABITy = llvm::StructType::get(IntTy,
3906 IntTy,
3907 llvm::ArrayType::get(
3908 IvarnfABITy, 0),
3909 NULL);
3910 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3911
3912 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003913
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003914 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003915 // uint32_t const flags;
3916 // uint32_t const instanceStart;
3917 // uint32_t const instanceSize;
3918 // uint32_t const reserved; // only when building for 64bit targets
3919 // const uint8_t * const ivarLayout;
3920 // const char *const name;
3921 // const struct _method_list_t * const baseMethods;
3922 // const struct _objc_protocol_list *const baseProtocols;
3923 // const struct _ivar_list_t *const ivars;
3924 // const uint8_t * const weakIvarLayout;
3925 // const struct _prop_list_t * const properties;
3926 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003927
3928 // FIXME. Add 'reserved' field in 64bit abi mode!
3929 ClassRonfABITy = llvm::StructType::get(IntTy,
3930 IntTy,
3931 IntTy,
3932 Int8PtrTy,
3933 Int8PtrTy,
3934 MethodListnfABIPtrTy,
3935 ProtocolListnfABIPtrTy,
3936 IvarListnfABIPtrTy,
3937 Int8PtrTy,
3938 PropertyListPtrTy,
3939 NULL);
3940 CGM.getModule().addTypeName("struct._class_ro_t",
3941 ClassRonfABITy);
3942
3943 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3944 std::vector<const llvm::Type*> Params;
3945 Params.push_back(ObjectPtrTy);
3946 Params.push_back(SelectorPtrTy);
3947 ImpnfABITy = llvm::PointerType::getUnqual(
3948 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3949
3950 // struct _class_t {
3951 // struct _class_t *isa;
3952 // struct _class_t * const superclass;
3953 // void *cache;
3954 // IMP *vtable;
3955 // struct class_ro_t *ro;
3956 // }
3957
3958 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3959 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3960 llvm::PointerType::getUnqual(ClassTyHolder),
3961 CachePtrTy,
3962 llvm::PointerType::getUnqual(ImpnfABITy),
3963 llvm::PointerType::getUnqual(
3964 ClassRonfABITy),
3965 NULL);
3966 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3967
3968 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3969 ClassnfABITy);
3970
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003971 // LLVM for struct _class_t *
3972 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3973
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003974 // struct _category_t {
3975 // const char * const name;
3976 // struct _class_t *const cls;
3977 // const struct _method_list_t * const instance_methods;
3978 // const struct _method_list_t * const class_methods;
3979 // const struct _protocol_list_t * const protocols;
3980 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003981 // }
3982 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003983 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003984 MethodListnfABIPtrTy,
3985 MethodListnfABIPtrTy,
3986 ProtocolListnfABIPtrTy,
3987 PropertyListPtrTy,
3988 NULL);
3989 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003990
3991 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003992 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3993 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003994
3995 // MessageRefTy - LLVM for:
3996 // struct _message_ref_t {
3997 // IMP messenger;
3998 // SEL name;
3999 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004000
4001 // First the clang type for struct _message_ref_t
4002 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
4003 SourceLocation(),
4004 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004005 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00004006 Ctx.VoidPtrTy, 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004007 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Douglas Gregor6ab35242009-04-09 21:40:53 +00004008 Ctx.getObjCSelType(), 0, false));
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004009 RD->completeDefinition(Ctx);
4010
4011 MessageRefCTy = Ctx.getTagDeclType(RD);
4012 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4013 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004014
4015 // MessageRefPtrTy - LLVM for struct _message_ref_t*
4016 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
4017
4018 // SuperMessageRefTy - LLVM for:
4019 // struct _super_message_ref_t {
4020 // SUPER_IMP messenger;
4021 // SEL name;
4022 // };
4023 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
4024 SelectorPtrTy,
4025 NULL);
4026 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
4027
4028 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
4029 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4030
Daniel Dunbare588b992009-03-01 04:46:24 +00004031
4032 // struct objc_typeinfo {
4033 // const void** vtable; // objc_ehtype_vtable + 2
4034 // const char* name; // c++ typeinfo string
4035 // Class cls;
4036 // };
4037 EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy),
4038 Int8PtrTy,
4039 ClassnfABIPtrTy,
4040 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004041 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Daniel Dunbare588b992009-03-01 04:46:24 +00004042 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004043}
4044
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004045llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
4046 FinishNonFragileABIModule();
4047
4048 return NULL;
4049}
4050
Daniel Dunbar463b8762009-05-15 21:48:48 +00004051void CGObjCNonFragileABIMac::AddModuleClassList(const
4052 std::vector<llvm::GlobalValue*>
4053 &Container,
4054 const char *SymbolName,
4055 const char *SectionName) {
4056 unsigned NumClasses = Container.size();
4057
4058 if (!NumClasses)
4059 return;
4060
4061 std::vector<llvm::Constant*> Symbols(NumClasses);
4062 for (unsigned i=0; i<NumClasses; i++)
4063 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
4064 ObjCTypes.Int8PtrTy);
4065 llvm::Constant* Init =
4066 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
4067 NumClasses),
4068 Symbols);
4069
4070 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004071 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004072 llvm::GlobalValue::InternalLinkage,
4073 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004074 SymbolName);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004075 GV->setAlignment(8);
4076 GV->setSection(SectionName);
4077 UsedGlobals.push_back(GV);
4078}
4079
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004080void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4081 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004082
Daniel Dunbar463b8762009-05-15 21:48:48 +00004083 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004084 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar463b8762009-05-15 21:48:48 +00004085 AddModuleClassList(DefinedClasses,
4086 "\01L_OBJC_LABEL_CLASS_$",
4087 "__DATA, __objc_classlist, regular, no_dead_strip");
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004088 AddModuleClassList(DefinedNonLazyClasses,
4089 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4090 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004091
4092 // Build list of all implemented category addresses in array
4093 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar463b8762009-05-15 21:48:48 +00004094 AddModuleClassList(DefinedCategories,
4095 "\01L_OBJC_LABEL_CATEGORY_$",
4096 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004097 AddModuleClassList(DefinedNonLazyCategories,
4098 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4099 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004100
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004101 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
4102 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
4103 std::vector<llvm::Constant*> Values(2);
4104 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004105 unsigned int flags = 0;
Fariborz Jahanian66a5c2c2009-02-24 23:34:44 +00004106 // FIXME: Fix and continue?
4107 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
4108 flags |= eImageInfo_GarbageCollected;
4109 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
4110 flags |= eImageInfo_GCOnly;
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004111 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004112 llvm::Constant* Init = llvm::ConstantArray::get(
4113 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
4114 Values);
4115 llvm::GlobalVariable *IMGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004116 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004117 llvm::GlobalValue::InternalLinkage,
4118 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004119 "\01L_OBJC_IMAGE_INFO");
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004120 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
Daniel Dunbar325f7582009-04-23 08:03:21 +00004121 IMGV->setConstant(true);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004122 UsedGlobals.push_back(IMGV);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004123}
4124
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004125/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004126/// NonLegacyDispatchMethods; false otherwise. What this means is that
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004127/// except for the 19 selectors in the list, we generate 32bit-style
4128/// message dispatch call for all the rest.
4129///
4130bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004131 if (NonLegacyDispatchMethods.empty()) {
4132 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4133 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4134 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4135 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4136 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4137 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4138 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4139 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4140 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4141 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
4142
4143 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4144 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4145 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4146 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4147 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4148 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4149 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4150 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004151 // "countByEnumeratingWithState:objects:count"
4152 IdentifierInfo *KeyIdents[] = {
4153 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4154 &CGM.getContext().Idents.get("objects"),
4155 &CGM.getContext().Idents.get("count")
4156 };
4157 NonLegacyDispatchMethods.insert(
4158 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004159 }
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004160 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004161}
4162
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004163// Metadata flags
4164enum MetaDataDlags {
4165 CLS = 0x0,
4166 CLS_META = 0x1,
4167 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004168 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004169 CLS_EXCEPTION = 0x20
4170};
4171/// BuildClassRoTInitializer - generate meta-data for:
4172/// struct _class_ro_t {
4173/// uint32_t const flags;
4174/// uint32_t const instanceStart;
4175/// uint32_t const instanceSize;
4176/// uint32_t const reserved; // only when building for 64bit targets
4177/// const uint8_t * const ivarLayout;
4178/// const char *const name;
4179/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004180/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004181/// const struct _ivar_list_t *const ivars;
4182/// const uint8_t * const weakIvarLayout;
4183/// const struct _prop_list_t * const properties;
4184/// }
4185///
4186llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
4187 unsigned flags,
4188 unsigned InstanceStart,
4189 unsigned InstanceSize,
4190 const ObjCImplementationDecl *ID) {
4191 std::string ClassName = ID->getNameAsString();
4192 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
4193 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4194 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4195 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
4196 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004197 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4198 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004199 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004200 // const struct _method_list_t * const baseMethods;
4201 std::vector<llvm::Constant*> Methods;
4202 std::string MethodListName("\01l_OBJC_$_");
4203 if (flags & CLS_META) {
4204 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004205 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004206 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004207 // Class methods should always be defined.
4208 Methods.push_back(GetMethodConstant(*i));
4209 }
4210 } else {
4211 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004212 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004213 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004214 // Instance methods should always be defined.
4215 Methods.push_back(GetMethodConstant(*i));
4216 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00004217 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004218 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004219 ObjCPropertyImplDecl *PID = *i;
4220
4221 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4222 ObjCPropertyDecl *PD = PID->getPropertyDecl();
4223
4224 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4225 if (llvm::Constant *C = GetMethodConstant(MD))
4226 Methods.push_back(C);
4227 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4228 if (llvm::Constant *C = GetMethodConstant(MD))
4229 Methods.push_back(C);
4230 }
4231 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004232 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004233 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004234 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004235
4236 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4237 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
4238 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
4239 + OID->getNameAsString(),
4240 OID->protocol_begin(),
4241 OID->protocol_end());
4242
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004243 if (flags & CLS_META)
4244 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4245 else
4246 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004247 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4248 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004249 if (flags & CLS_META)
4250 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4251 else
4252 Values[ 9] =
4253 EmitPropertyList(
4254 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
4255 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004256 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
4257 Values);
4258 llvm::GlobalVariable *CLASS_RO_GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004259 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004260 llvm::GlobalValue::InternalLinkage,
4261 Init,
4262 (flags & CLS_META) ?
4263 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
Owen Anderson1c431b32009-07-08 19:05:04 +00004264 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004265 CLASS_RO_GV->setAlignment(
4266 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004267 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004268 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004269
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004270}
4271
4272/// BuildClassMetaData - This routine defines that to-level meta-data
4273/// for the given ClassName for:
4274/// struct _class_t {
4275/// struct _class_t *isa;
4276/// struct _class_t * const superclass;
4277/// void *cache;
4278/// IMP *vtable;
4279/// struct class_ro_t *ro;
4280/// }
4281///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004282llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
4283 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004284 llvm::Constant *IsAGV,
4285 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004286 llvm::Constant *ClassRoGV,
4287 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004288 std::vector<llvm::Constant*> Values(5);
4289 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004290 Values[1] = SuperClassGV
4291 ? SuperClassGV
4292 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004293 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4294 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4295 Values[4] = ClassRoGV; // &CLASS_RO_GV
4296 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
4297 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004298 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4299 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004300 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004301 GV->setAlignment(
4302 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004303 if (HiddenVisibility)
4304 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004305 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004306}
4307
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004308bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004309CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004310 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004311}
4312
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004313void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004314 uint32_t &InstanceStart,
4315 uint32_t &InstanceSize) {
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004316 const ASTRecordLayout &RL =
4317 CGM.getContext().getASTObjCImplementationLayout(OID);
4318
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004319 // InstanceSize is really instance end.
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004320 InstanceSize = llvm::RoundUpToAlignment(RL.getNextOffset(), 8) / 8;
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004321
4322 // If there are no fields, the start is the same as the end.
4323 if (!RL.getFieldCount())
4324 InstanceStart = InstanceSize;
4325 else
4326 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004327}
4328
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004329void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4330 std::string ClassName = ID->getNameAsString();
4331 if (!ObjCEmptyCacheVar) {
4332 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004333 CGM.getModule(),
Daniel Dunbar948e2582009-02-15 07:36:20 +00004334 ObjCTypes.CacheTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004335 false,
4336 llvm::GlobalValue::ExternalLinkage,
4337 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004338 "_objc_empty_cache");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004339
4340 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004341 CGM.getModule(),
Daniel Dunbar948e2582009-02-15 07:36:20 +00004342 ObjCTypes.ImpnfABITy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004343 false,
4344 llvm::GlobalValue::ExternalLinkage,
4345 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004346 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004347 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004348 assert(ID->getClassInterface() &&
4349 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004350 // FIXME: Is this correct (that meta class size is never computed)?
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004351 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004352 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004353 uint32_t InstanceSize = InstanceStart;
4354 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004355 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4356 std::string ObjCClassName(getClassSymbolPrefix());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004357
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004358 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004359
Daniel Dunbar04d40782009-04-14 06:00:08 +00004360 bool classIsHidden =
4361 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004362 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004363 flags |= OBJC2_CLS_HIDDEN;
4364 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004365 // class is root
4366 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004367 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004368 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004369 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004370 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004371 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4372 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4373 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004374 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004375 // work on super class metadata symbol.
4376 std::string SuperClassName =
4377 ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004378 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004379 }
4380 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4381 InstanceStart,
4382 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004383 std::string TClassName = ObjCMetaClassName + ClassName;
4384 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004385 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4386 classIsHidden);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004387
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004388 // Metadata for the class
4389 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004390 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004391 flags |= OBJC2_CLS_HIDDEN;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004392
Douglas Gregor68584ed2009-06-18 16:11:24 +00004393 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004394 flags |= CLS_EXCEPTION;
4395
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004396 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004397 flags |= CLS_ROOT;
4398 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004399 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004400 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004401 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004402 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004403 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004404 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004405 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004406 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004407 InstanceStart,
4408 InstanceSize,
4409 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004410
4411 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004412 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004413 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4414 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004415 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004416
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004417 // Determine if this class is also "non-lazy".
4418 if (ImplementationIsNonLazy(ID))
4419 DefinedNonLazyClasses.push_back(ClassMD);
4420
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004421 // Force the definition of the EHType if necessary.
4422 if (flags & CLS_EXCEPTION)
4423 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004424}
4425
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004426/// GenerateProtocolRef - This routine is called to generate code for
4427/// a protocol reference expression; as in:
4428/// @code
4429/// @protocol(Proto1);
4430/// @endcode
4431/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4432/// which will hold address of the protocol meta-data.
4433///
4434llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
4435 const ObjCProtocolDecl *PD) {
4436
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004437 // This routine is called for @protocol only. So, we must build definition
4438 // of protocol's meta-data (not a reference to it!)
4439 //
4440 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004441 ObjCTypes.ExternalProtocolPtrTy);
4442
4443 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4444 ProtocolName += PD->getNameAsCString();
4445
4446 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4447 if (PTGV)
4448 return Builder.CreateLoad(PTGV, false, "tmp");
4449 PTGV = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004450 CGM.getModule(),
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004451 Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00004452 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004453 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004454 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004455 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4456 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4457 UsedGlobals.push_back(PTGV);
4458 return Builder.CreateLoad(PTGV, false, "tmp");
4459}
4460
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004461/// GenerateCategory - Build metadata for a category implementation.
4462/// struct _category_t {
4463/// const char * const name;
4464/// struct _class_t *const cls;
4465/// const struct _method_list_t * const instance_methods;
4466/// const struct _method_list_t * const class_methods;
4467/// const struct _protocol_list_t * const protocols;
4468/// const struct _prop_list_t * const properties;
4469/// }
4470///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004471void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004472 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004473 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
4474 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004475 "_$_" + OCD->getNameAsString());
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004476 std::string ExtClassName(getClassSymbolPrefix() +
4477 Interface->getNameAsString());
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004478
4479 std::vector<llvm::Constant*> Values(6);
4480 Values[0] = GetClassName(OCD->getIdentifier());
4481 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004482 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004483 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004484 std::vector<llvm::Constant*> Methods;
4485 std::string MethodListName(Prefix);
4486 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
4487 "_$_" + OCD->getNameAsString();
4488
Douglas Gregor653f1b12009-04-23 01:02:12 +00004489 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004490 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004491 // Instance methods should always be defined.
4492 Methods.push_back(GetMethodConstant(*i));
4493 }
4494
4495 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004496 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004497 Methods);
4498
4499 MethodListName = Prefix;
4500 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4501 OCD->getNameAsString();
4502 Methods.clear();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004503 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004504 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004505 // Class methods should always be defined.
4506 Methods.push_back(GetMethodConstant(*i));
4507 }
4508
4509 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004510 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004511 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004512 const ObjCCategoryDecl *Category =
4513 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004514 if (Category) {
4515 std::string ExtName(Interface->getNameAsString() + "_$_" +
4516 OCD->getNameAsString());
4517 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
4518 + Interface->getNameAsString() + "_$_"
4519 + Category->getNameAsString(),
4520 Category->protocol_begin(),
4521 Category->protocol_end());
4522 Values[5] =
4523 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
4524 OCD, Category, ObjCTypes);
4525 }
4526 else {
4527 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4528 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4529 }
4530
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004531 llvm::Constant *Init =
4532 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
4533 Values);
4534 llvm::GlobalVariable *GCATV
Owen Anderson1c431b32009-07-08 19:05:04 +00004535 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004536 false,
4537 llvm::GlobalValue::InternalLinkage,
4538 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004539 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004540 GCATV->setAlignment(
4541 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004542 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004543 UsedGlobals.push_back(GCATV);
4544 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004545
4546 // Determine if this category is also "non-lazy".
4547 if (ImplementationIsNonLazy(OCD))
4548 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004549}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004550
4551/// GetMethodConstant - Return a struct objc_method constant for the
4552/// given method if it has been defined. The result is null if the
4553/// method has not been defined. The return value has type MethodPtrTy.
4554llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
4555 const ObjCMethodDecl *MD) {
4556 // FIXME: Use DenseMap::lookup
4557 llvm::Function *Fn = MethodDefinitions[MD];
4558 if (!Fn)
4559 return 0;
4560
4561 std::vector<llvm::Constant*> Method(3);
4562 Method[0] =
4563 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4564 ObjCTypes.SelectorPtrTy);
4565 Method[1] = GetMethodVarType(MD);
4566 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
4567 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
4568}
4569
4570/// EmitMethodList - Build meta-data for method declarations
4571/// struct _method_list_t {
4572/// uint32_t entsize; // sizeof(struct _objc_method)
4573/// uint32_t method_count;
4574/// struct _objc_method method_list[method_count];
4575/// }
4576///
4577llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
4578 const std::string &Name,
4579 const char *Section,
4580 const ConstantVector &Methods) {
4581 // Return null for empty list.
4582 if (Methods.empty())
4583 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
4584
4585 std::vector<llvm::Constant*> Values(3);
4586 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00004587 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004588 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4589 // method_count
4590 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
4591 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
4592 Methods.size());
4593 Values[2] = llvm::ConstantArray::get(AT, Methods);
4594 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4595
4596 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004597 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004598 llvm::GlobalValue::InternalLinkage,
4599 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004600 Name);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004601 GV->setAlignment(
4602 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004603 GV->setSection(Section);
4604 UsedGlobals.push_back(GV);
4605 return llvm::ConstantExpr::getBitCast(GV,
4606 ObjCTypes.MethodListnfABIPtrTy);
4607}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004608
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004609/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4610/// the given ivar.
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004611llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004612 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004613 const ObjCIvarDecl *Ivar) {
Daniel Dunbara81419d2009-05-05 00:36:57 +00004614 // FIXME: We shouldn't need to do this lookup.
4615 unsigned Index;
4616 const ObjCInterfaceDecl *Container =
4617 FindIvarInterface(CGM.getContext(), ID, Ivar, Index);
4618 assert(Container && "Unable to find ivar container!");
4619 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00004620 '.' + Ivar->getNameAsString();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004621 llvm::GlobalVariable *IvarOffsetGV =
4622 CGM.getModule().getGlobalVariable(Name);
4623 if (!IvarOffsetGV)
4624 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004625 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004626 false,
4627 llvm::GlobalValue::ExternalLinkage,
4628 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004629 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004630 return IvarOffsetGV;
4631}
4632
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004633llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004634 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004635 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004636 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00004637 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
4638 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
4639 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004640 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004641 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00004642
Mike Stumpf5408fe2009-05-16 07:57:57 +00004643 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
4644 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00004645 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
4646 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
4647 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004648 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00004649 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00004650 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004651 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004652 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004653}
4654
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004655/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00004656/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004657/// IvarListnfABIPtrTy.
4658/// struct _ivar_t {
4659/// unsigned long int *offset; // pointer to ivar offset location
4660/// char *name;
4661/// char *type;
4662/// uint32_t alignment;
4663/// uint32_t size;
4664/// }
4665/// struct _ivar_list_t {
4666/// uint32 entsize; // sizeof(struct _ivar_t)
4667/// uint32 count;
4668/// struct _iver_t list[count];
4669/// }
4670///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004671
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004672llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4673 const ObjCImplementationDecl *ID) {
4674
4675 std::vector<llvm::Constant*> Ivars, Ivar(5);
4676
4677 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4678 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4679
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004680 // FIXME. Consolidate this with similar code in GenerateClass.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004681
Daniel Dunbar91636d62009-04-20 00:33:43 +00004682 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00004683 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00004684 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Fariborz Jahanian99eee362009-04-01 19:37:34 +00004685
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004686 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
4687 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00004688 // Ignore unnamed bit-fields.
4689 if (!IVD->getDeclName())
4690 continue;
Daniel Dunbar3eec8aa2009-04-20 05:53:40 +00004691 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004692 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004693 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
4694 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004695 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004696 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00004697 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004698 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004699 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004700 Align = llvm::Log2_32(Align);
4701 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00004702 // NOTE. Size of a bitfield does not match gcc's, because of the
4703 // way bitfields are treated special in each. But I am told that
4704 // 'size' for bitfield ivars is ignored by the runtime so it does
4705 // not matter. If it matters, there is enough info to get the
4706 // bitfield right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004707 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4708 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4709 }
4710 // Return null for empty list.
4711 if (Ivars.empty())
4712 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4713 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00004714 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004715 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4716 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4717 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4718 Ivars.size());
4719 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4720 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4721 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4722 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004723 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004724 llvm::GlobalValue::InternalLinkage,
4725 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004726 Prefix + OID->getNameAsString());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004727 GV->setAlignment(
4728 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004729 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004730
4731 UsedGlobals.push_back(GV);
4732 return llvm::ConstantExpr::getBitCast(GV,
4733 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004734}
4735
4736llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4737 const ObjCProtocolDecl *PD) {
4738 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4739
4740 if (!Entry) {
4741 // We use the initializer as a marker of whether this is a forward
4742 // reference or not. At module finalization we add the empty
4743 // contents for protocols which were referenced but never defined.
4744 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00004745 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004746 llvm::GlobalValue::ExternalLinkage,
4747 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004748 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString());
Fariborz Jahanianda320092009-01-29 19:24:30 +00004749 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4750 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004751 }
4752
4753 return Entry;
4754}
4755
4756/// GetOrEmitProtocol - Generate the protocol meta-data:
4757/// @code
4758/// struct _protocol_t {
4759/// id isa; // NULL
4760/// const char * const protocol_name;
4761/// const struct _protocol_list_t * protocol_list; // super protocols
4762/// const struct method_list_t * const instance_methods;
4763/// const struct method_list_t * const class_methods;
4764/// const struct method_list_t *optionalInstanceMethods;
4765/// const struct method_list_t *optionalClassMethods;
4766/// const struct _prop_list_t * properties;
4767/// const uint32_t size; // sizeof(struct _protocol_t)
4768/// const uint32_t flags; // = 0
4769/// }
4770/// @endcode
4771///
4772
4773llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4774 const ObjCProtocolDecl *PD) {
4775 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4776
4777 // Early exit if a defining object has already been generated.
4778 if (Entry && Entry->hasInitializer())
4779 return Entry;
4780
4781 const char *ProtocolName = PD->getNameAsCString();
4782
4783 // Construct method lists.
4784 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4785 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00004786 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004787 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004788 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004789 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004790 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4791 OptInstanceMethods.push_back(C);
4792 } else {
4793 InstanceMethods.push_back(C);
4794 }
4795 }
4796
Douglas Gregor6ab35242009-04-09 21:40:53 +00004797 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004798 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004799 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004800 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004801 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4802 OptClassMethods.push_back(C);
4803 } else {
4804 ClassMethods.push_back(C);
4805 }
4806 }
4807
4808 std::vector<llvm::Constant*> Values(10);
4809 // isa is NULL
4810 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4811 Values[1] = GetClassName(PD->getIdentifier());
4812 Values[2] = EmitProtocolList(
4813 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4814 PD->protocol_begin(),
4815 PD->protocol_end());
4816
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004817 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004818 + PD->getNameAsString(),
4819 "__DATA, __objc_const",
4820 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004821 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004822 + PD->getNameAsString(),
4823 "__DATA, __objc_const",
4824 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004825 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004826 + PD->getNameAsString(),
4827 "__DATA, __objc_const",
4828 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004829 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004830 + PD->getNameAsString(),
4831 "__DATA, __objc_const",
4832 OptClassMethods);
4833 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4834 0, PD, ObjCTypes);
4835 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00004836 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004837 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4838 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4839 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4840 Values);
4841
4842 if (Entry) {
4843 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00004844 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004845 Entry->setInitializer(Init);
4846 } else {
4847 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00004848 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004849 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004850 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004851 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004852 Entry->setAlignment(
4853 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004854 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004855 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004856 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4857
4858 // Use this protocol meta-data to build protocol list table in section
4859 // __DATA, __objc_protolist
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004860 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Owen Anderson1c431b32009-07-08 19:05:04 +00004861 CGM.getModule(),
Daniel Dunbar948e2582009-02-15 07:36:20 +00004862 ObjCTypes.ProtocolnfABIPtrTy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004863 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004864 Entry,
4865 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
Owen Anderson1c431b32009-07-08 19:05:04 +00004866 +ProtocolName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004867 PTGV->setAlignment(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004868 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004869 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004870 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4871 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004872 return Entry;
4873}
4874
4875/// EmitProtocolList - Generate protocol list meta-data:
4876/// @code
4877/// struct _protocol_list_t {
4878/// long protocol_count; // Note, this is 32/64 bit
4879/// struct _protocol_t[protocol_count];
4880/// }
4881/// @endcode
4882///
4883llvm::Constant *
4884CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4885 ObjCProtocolDecl::protocol_iterator begin,
4886 ObjCProtocolDecl::protocol_iterator end) {
4887 std::vector<llvm::Constant*> ProtocolRefs;
4888
Fariborz Jahanianda320092009-01-29 19:24:30 +00004889 // Just return null for empty protocol lists
Daniel Dunbar948e2582009-02-15 07:36:20 +00004890 if (begin == end)
Fariborz Jahanianda320092009-01-29 19:24:30 +00004891 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4892
Daniel Dunbar948e2582009-02-15 07:36:20 +00004893 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanianda320092009-01-29 19:24:30 +00004894 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4895 if (GV)
Daniel Dunbar948e2582009-02-15 07:36:20 +00004896 return llvm::ConstantExpr::getBitCast(GV,
4897 ObjCTypes.ProtocolListnfABIPtrTy);
4898
4899 for (; begin != end; ++begin)
4900 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4901
Fariborz Jahanianda320092009-01-29 19:24:30 +00004902 // This list is null terminated.
4903 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004904 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004905
4906 std::vector<llvm::Constant*> Values(2);
4907 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4908 Values[1] =
Daniel Dunbar948e2582009-02-15 07:36:20 +00004909 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004910 ProtocolRefs.size()),
4911 ProtocolRefs);
4912
4913 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00004914 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004915 llvm::GlobalValue::InternalLinkage,
4916 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004917 Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004918 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004919 GV->setAlignment(
4920 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004921 UsedGlobals.push_back(GV);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004922 return llvm::ConstantExpr::getBitCast(GV,
4923 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004924}
4925
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004926/// GetMethodDescriptionConstant - This routine build following meta-data:
4927/// struct _objc_method {
4928/// SEL _cmd;
4929/// char *method_type;
4930/// char *_imp;
4931/// }
4932
4933llvm::Constant *
4934CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4935 std::vector<llvm::Constant*> Desc(3);
4936 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4937 ObjCTypes.SelectorPtrTy);
4938 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004939 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004940 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4941 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4942}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004943
4944/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4945/// This code gen. amounts to generating code for:
4946/// @code
4947/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4948/// @encode
4949///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004950LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004951 CodeGen::CodeGenFunction &CGF,
4952 QualType ObjectTy,
4953 llvm::Value *BaseValue,
4954 const ObjCIvarDecl *Ivar,
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004955 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00004956 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00004957 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4958 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004959}
4960
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004961llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4962 CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00004963 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004964 const ObjCIvarDecl *Ivar) {
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00004965 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),
4966 false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004967}
4968
Fariborz Jahanian46551122009-02-04 00:22:57 +00004969CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4970 CodeGen::CodeGenFunction &CGF,
4971 QualType ResultType,
4972 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004973 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004974 QualType Arg0Ty,
4975 bool IsSuper,
4976 const CallArgList &CallArgs) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00004977 // FIXME. Even though IsSuper is passes. This function doese not handle calls
4978 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004979 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004980 llvm::Value *Arg0 = Receiver;
4981 if (!IsSuper)
4982 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004983
4984 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00004985 // FIXME. This is too much work to get the ABI-specific result type needed to
4986 // find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004987 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4988 llvm::SmallVector<QualType, 16>());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00004989 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004990 std::string Name("\01l_");
4991 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004992#if 0
4993 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004994 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00004995 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004996 // FIXME. Is there a better way of getting these names.
4997 // They are available in RuntimeFunctions vector pair.
4998 Name += "objc_msgSendId_stret_fixup";
4999 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005000 else
5001#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005002 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005003 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005004 Name += "objc_msgSendSuper2_stret_fixup";
5005 }
5006 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005007 {
Chris Lattner1c02f862009-04-22 02:53:24 +00005008 Fn = ObjCTypes.getMessageSendStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005009 Name += "objc_msgSend_stret_fixup";
5010 }
5011 }
Fariborz Jahanian5b2bad02009-04-30 16:31:11 +00005012 else if (!IsSuper && ResultType->isFloatingType()) {
Daniel Dunbarc0183e82009-06-26 18:32:06 +00005013 if (ResultType->isSpecificBuiltinType(BuiltinType::LongDouble)) {
5014 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5015 Name += "objc_msgSend_fpret_fixup";
5016 }
5017 else {
5018 Fn = ObjCTypes.getMessageSendFixupFn();
5019 Name += "objc_msgSend_fixup";
Fariborz Jahanian5b2bad02009-04-30 16:31:11 +00005020 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005021 }
5022 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005023#if 0
5024// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005025 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005026 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005027 Name += "objc_msgSendId_fixup";
5028 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005029 else
5030#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005031 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005032 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005033 Name += "objc_msgSendSuper2_fixup";
5034 }
5035 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005036 {
Chris Lattner1c02f862009-04-22 02:53:24 +00005037 Fn = ObjCTypes.getMessageSendFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005038 Name += "objc_msgSend_fixup";
5039 }
5040 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005041 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005042 Name += '_';
5043 std::string SelName(Sel.getAsString());
5044 // Replace all ':' in selector name with '_' ouch!
5045 for(unsigned i = 0; i < SelName.size(); i++)
5046 if (SelName[i] == ':')
5047 SelName[i] = '_';
5048 Name += SelName;
5049 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5050 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005051 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005052 std::vector<llvm::Constant*> Values(2);
5053 Values[0] = Fn;
5054 Values[1] = GetMethodVarName(Sel);
5055 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00005056 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005057 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005058 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005059 Name);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005060 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005061 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005062 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005063 }
5064 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005065
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005066 CallArgList ActualArgs;
5067 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
5068 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
5069 ObjCTypes.MessageRefCPtrTy));
5070 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005071 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
5072 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5073 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005074 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005075 Callee = CGF.Builder.CreateBitCast(Callee,
5076 llvm::PointerType::getUnqual(FTy));
5077 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005078}
5079
5080/// Generate code for a message send expression in the nonfragile abi.
5081CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
5082 CodeGen::CodeGenFunction &CGF,
5083 QualType ResultType,
5084 Selector Sel,
5085 llvm::Value *Receiver,
5086 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00005087 const CallArgList &CallArgs,
5088 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005089 return LegacyDispatchedSelector(Sel)
5090 ? EmitLegacyMessageSend(CGF, ResultType, EmitSelector(CGF.Builder, Sel),
5091 Receiver, CGF.getContext().getObjCIdType(),
5092 false, CallArgs, ObjCTypes)
5093 : EmitMessageSend(CGF, ResultType, Sel,
5094 Receiver, CGF.getContext().getObjCIdType(),
5095 false, CallArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005096}
5097
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005098llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005099CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005100 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5101
Daniel Dunbardfff2302009-03-02 05:18:14 +00005102 if (!GV) {
Owen Anderson1c431b32009-07-08 19:05:04 +00005103 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
5104 false, llvm::GlobalValue::ExternalLinkage,
5105 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005106 }
5107
5108 return GV;
5109}
5110
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005111llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005112 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005113 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
5114
5115 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005116 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005117 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005118 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005119 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5120 false, llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005121 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005122 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005123 Entry->setAlignment(
5124 CGM.getTargetData().getPrefTypeAlignment(
5125 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005126 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
5127 UsedGlobals.push_back(Entry);
5128 }
5129
5130 return Builder.CreateLoad(Entry, false, "tmp");
5131}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005132
Daniel Dunbar11394522009-04-18 08:51:00 +00005133llvm::Value *
5134CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
5135 const ObjCInterfaceDecl *ID) {
5136 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
5137
5138 if (!Entry) {
5139 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5140 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5141 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005142 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5143 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar11394522009-04-18 08:51:00 +00005144 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005145 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005146 Entry->setAlignment(
5147 CGM.getTargetData().getPrefTypeAlignment(
5148 ObjCTypes.ClassnfABIPtrTy));
5149 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005150 UsedGlobals.push_back(Entry);
5151 }
5152
5153 return Builder.CreateLoad(Entry, false, "tmp");
5154}
5155
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005156/// EmitMetaClassRef - Return a Value * of the address of _class_t
5157/// meta-data
5158///
5159llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5160 const ObjCInterfaceDecl *ID) {
5161 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5162 if (Entry)
5163 return Builder.CreateLoad(Entry, false, "tmp");
5164
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005165 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005166 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005167 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005168 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005169 llvm::GlobalValue::InternalLinkage,
5170 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005171 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005172 Entry->setAlignment(
5173 CGM.getTargetData().getPrefTypeAlignment(
5174 ObjCTypes.ClassnfABIPtrTy));
5175
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005176 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005177 UsedGlobals.push_back(Entry);
5178
5179 return Builder.CreateLoad(Entry, false, "tmp");
5180}
5181
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005182/// GetClass - Return a reference to the class for the given interface
5183/// decl.
5184llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5185 const ObjCInterfaceDecl *ID) {
5186 return EmitClassRef(Builder, ID);
5187}
5188
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005189/// Generates a message send where the super is the receiver. This is
5190/// a message send to self with special delivery semantics indicating
5191/// which class's method should be called.
5192CodeGen::RValue
5193CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
5194 QualType ResultType,
5195 Selector Sel,
5196 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005197 bool isCategoryImpl,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005198 llvm::Value *Receiver,
5199 bool IsClassMessage,
5200 const CodeGen::CallArgList &CallArgs) {
5201 // ...
5202 // Create and init a super structure; this is a (receiver, class)
5203 // pair we will pass to objc_msgSendSuper.
5204 llvm::Value *ObjCSuper =
5205 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
5206
5207 llvm::Value *ReceiverAsObject =
5208 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5209 CGF.Builder.CreateStore(ReceiverAsObject,
5210 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
5211
5212 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005213 llvm::Value *Target;
5214 if (IsClassMessage) {
5215 if (isCategoryImpl) {
5216 // Message sent to "super' in a class method defined in
5217 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005218 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005219 Target = CGF.Builder.CreateStructGEP(Target, 0);
5220 Target = CGF.Builder.CreateLoad(Target);
5221 }
5222 else
5223 Target = EmitMetaClassRef(CGF.Builder, Class);
5224 }
5225 else
Daniel Dunbar11394522009-04-18 08:51:00 +00005226 Target = EmitSuperClassRef(CGF.Builder, Class);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005227
Mike Stumpf5408fe2009-05-16 07:57:57 +00005228 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5229 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005230 const llvm::Type *ClassTy =
5231 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5232 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5233 CGF.Builder.CreateStore(Target,
5234 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
5235
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005236 return (LegacyDispatchedSelector(Sel))
5237 ? EmitLegacyMessageSend(CGF, ResultType,EmitSelector(CGF.Builder, Sel),
5238 ObjCSuper, ObjCTypes.SuperPtrCTy,
5239 true, CallArgs,
5240 ObjCTypes)
5241 : EmitMessageSend(CGF, ResultType, Sel,
5242 ObjCSuper, ObjCTypes.SuperPtrCTy,
5243 true, CallArgs);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005244}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005245
5246llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
5247 Selector Sel) {
5248 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
5249
5250 if (!Entry) {
5251 llvm::Constant *Casted =
5252 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5253 ObjCTypes.SelectorPtrTy);
5254 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005255 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005256 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005257 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005258 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005259 UsedGlobals.push_back(Entry);
5260 }
5261
5262 return Builder.CreateLoad(Entry, false, "tmp");
5263}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005264/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
5265/// objc_assign_ivar (id src, id *dst)
5266///
5267void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
5268 llvm::Value *src, llvm::Value *dst)
5269{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005270 const llvm::Type * SrcTy = src->getType();
5271 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005272 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005273 assert(Size <= 8 && "does not support size > 8");
5274 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5275 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005276 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5277 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005278 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5279 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005280 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005281 src, dst, "assignivar");
5282 return;
5283}
5284
5285/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5286/// objc_assign_strongCast (id src, id *dst)
5287///
5288void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
5289 CodeGen::CodeGenFunction &CGF,
5290 llvm::Value *src, llvm::Value *dst)
5291{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005292 const llvm::Type * SrcTy = src->getType();
5293 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005294 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005295 assert(Size <= 8 && "does not support size > 8");
5296 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5297 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005298 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5299 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005300 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5301 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005302 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005303 src, dst, "weakassign");
5304 return;
5305}
5306
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005307void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
5308 CodeGen::CodeGenFunction &CGF,
5309 llvm::Value *DestPtr,
5310 llvm::Value *SrcPtr,
5311 unsigned long size) {
5312 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5313 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
5314 llvm::Value *N = llvm::ConstantInt::get(ObjCTypes.LongTy, size);
5315 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
5316 DestPtr, SrcPtr, N);
5317 return;
5318}
5319
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005320/// EmitObjCWeakRead - Code gen for loading value of a __weak
5321/// object: objc_read_weak (id *src)
5322///
5323llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
5324 CodeGen::CodeGenFunction &CGF,
5325 llvm::Value *AddrWeakObj)
5326{
Eli Friedman8339b352009-03-07 03:57:15 +00005327 const llvm::Type* DestTy =
5328 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005329 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005330 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005331 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005332 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005333 return read_weak;
5334}
5335
5336/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5337/// objc_assign_weak (id src, id *dst)
5338///
5339void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
5340 llvm::Value *src, llvm::Value *dst)
5341{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005342 const llvm::Type * SrcTy = src->getType();
5343 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005344 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005345 assert(Size <= 8 && "does not support size > 8");
5346 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5347 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005348 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5349 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005350 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5351 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005352 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005353 src, dst, "weakassign");
5354 return;
5355}
5356
5357/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5358/// objc_assign_global (id src, id *dst)
5359///
5360void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
5361 llvm::Value *src, llvm::Value *dst)
5362{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005363 const llvm::Type * SrcTy = src->getType();
5364 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005365 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005366 assert(Size <= 8 && "does not support size > 8");
5367 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5368 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005369 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5370 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005371 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5372 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005373 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005374 src, dst, "globalassign");
5375 return;
5376}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005377
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005378void
5379CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5380 const Stmt &S) {
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005381 bool isTry = isa<ObjCAtTryStmt>(S);
5382 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
5383 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005384 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005385 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005386 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005387 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
5388
5389 // For @synchronized, call objc_sync_enter(sync.expr). The
5390 // evaluation of the expression must occur before we enter the
5391 // @synchronized. We can safely avoid a temp here because jumps into
5392 // @synchronized are illegal & this will dominate uses.
5393 llvm::Value *SyncArg = 0;
5394 if (!isTry) {
5395 SyncArg =
5396 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
5397 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005398 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005399 }
5400
5401 // Push an EH context entry, used for handling rethrows and jumps
5402 // through finally.
5403 CGF.PushCleanupBlock(FinallyBlock);
5404
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005405 CGF.setInvokeDest(TryHandler);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005406
5407 CGF.EmitBlock(TryBlock);
5408 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
5409 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
5410 CGF.EmitBranchThroughCleanup(FinallyEnd);
5411
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005412 // Emit the exception handler.
5413
5414 CGF.EmitBlock(TryHandler);
5415
5416 llvm::Value *llvm_eh_exception =
5417 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
5418 llvm::Value *llvm_eh_selector_i64 =
5419 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
5420 llvm::Value *llvm_eh_typeid_for_i64 =
5421 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
5422 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5423 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
5424
5425 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
5426 SelectorArgs.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005427 SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005428
5429 // Construct the lists of (type, catch body) to handle.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005430 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005431 bool HasCatchAll = false;
5432 if (isTry) {
5433 if (const ObjCAtCatchStmt* CatchStmt =
5434 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
5435 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005436 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Steve Naroff7ba138a2009-03-03 19:52:17 +00005437 Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005438
5439 // catch(...) always matches.
Steve Naroff7ba138a2009-03-03 19:52:17 +00005440 if (!CatchDecl) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005441 // Use i8* null here to signal this is a catch all, not a cleanup.
5442 llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
5443 SelectorArgs.push_back(Null);
5444 HasCatchAll = true;
5445 break;
5446 }
5447
Steve Naroff14108da2009-07-10 23:34:53 +00005448 if (CatchDecl->getType()->isObjCIdType() ||
Daniel Dunbarede8de92009-03-06 00:01:21 +00005449 CatchDecl->getType()->isObjCQualifiedIdType()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005450 llvm::Value *IDEHType =
5451 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5452 if (!IDEHType)
5453 IDEHType =
Owen Anderson1c431b32009-07-08 19:05:04 +00005454 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5455 false,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005456 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005457 0, "OBJC_EHTYPE_id");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005458 SelectorArgs.push_back(IDEHType);
5459 HasCatchAll = true;
5460 break;
5461 }
5462
5463 // All other types should be Objective-C interface pointer types.
Steve Naroff14108da2009-07-10 23:34:53 +00005464 const ObjCObjectPointerType *PT =
5465 CatchDecl->getType()->getAsObjCObjectPointerType();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005466 assert(PT && "Invalid @catch type.");
Steve Naroff14108da2009-07-10 23:34:53 +00005467 const ObjCInterfaceType *IT = PT->getInterfaceType();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005468 assert(IT && "Invalid @catch type.");
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005469 llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005470 SelectorArgs.push_back(EHType);
5471 }
5472 }
5473 }
5474
5475 // We use a cleanup unless there was already a catch all.
5476 if (!HasCatchAll) {
5477 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Daniel Dunbarede8de92009-03-06 00:01:21 +00005478 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005479 }
5480
5481 llvm::Value *Selector =
5482 CGF.Builder.CreateCall(llvm_eh_selector_i64,
5483 SelectorArgs.begin(), SelectorArgs.end(),
5484 "selector");
5485 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005486 const ParmVarDecl *CatchParam = Handlers[i].first;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005487 const Stmt *CatchBody = Handlers[i].second;
5488
5489 llvm::BasicBlock *Next = 0;
5490
5491 // The last handler always matches.
5492 if (i + 1 != e) {
5493 assert(CatchParam && "Only last handler can be a catch all.");
5494
5495 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
5496 Next = CGF.createBasicBlock("catch.next");
5497 llvm::Value *Id =
5498 CGF.Builder.CreateCall(llvm_eh_typeid_for_i64,
5499 CGF.Builder.CreateBitCast(SelectorArgs[i+2],
5500 ObjCTypes.Int8PtrTy));
5501 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id),
5502 Match, Next);
5503
5504 CGF.EmitBlock(Match);
5505 }
5506
5507 if (CatchBody) {
5508 llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end");
5509 llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler");
5510
5511 // Cleanups must call objc_end_catch.
5512 //
Mike Stumpf5408fe2009-05-16 07:57:57 +00005513 // FIXME: It seems incorrect for objc_begin_catch to be inside this
5514 // context, but this matches gcc.
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005515 CGF.PushCleanupBlock(MatchEnd);
5516 CGF.setInvokeDest(MatchHandler);
5517
5518 llvm::Value *ExcObject =
Chris Lattner8a569112009-04-22 02:15:23 +00005519 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005520
5521 // Bind the catch parameter if it exists.
5522 if (CatchParam) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005523 ExcObject =
5524 CGF.Builder.CreateBitCast(ExcObject,
5525 CGF.ConvertType(CatchParam->getType()));
5526 // CatchParam is a ParmVarDecl because of the grammar
5527 // construction used to handle this, but for codegen purposes
5528 // we treat this as a local decl.
5529 CGF.EmitLocalBlockVarDecl(*CatchParam);
5530 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005531 }
5532
5533 CGF.ObjCEHValueStack.push_back(ExcObject);
5534 CGF.EmitStmt(CatchBody);
5535 CGF.ObjCEHValueStack.pop_back();
5536
5537 CGF.EmitBranchThroughCleanup(FinallyEnd);
5538
5539 CGF.EmitBlock(MatchHandler);
5540
5541 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5542 // We are required to emit this call to satisfy LLVM, even
5543 // though we don't use the result.
5544 llvm::SmallVector<llvm::Value*, 8> Args;
5545 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005546 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005547 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5548 0));
5549 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5550 CGF.Builder.CreateStore(Exc, RethrowPtr);
5551 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5552
5553 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5554
5555 CGF.EmitBlock(MatchEnd);
5556
5557 // Unfortunately, we also have to generate another EH frame here
5558 // in case this throws.
5559 llvm::BasicBlock *MatchEndHandler =
5560 CGF.createBasicBlock("match.end.handler");
5561 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattner8a569112009-04-22 02:15:23 +00005562 CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005563 Cont, MatchEndHandler,
5564 Args.begin(), Args.begin());
5565
5566 CGF.EmitBlock(Cont);
5567 if (Info.SwitchBlock)
5568 CGF.EmitBlock(Info.SwitchBlock);
5569 if (Info.EndBlock)
5570 CGF.EmitBlock(Info.EndBlock);
5571
5572 CGF.EmitBlock(MatchEndHandler);
5573 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5574 // We are required to emit this call to satisfy LLVM, even
5575 // though we don't use the result.
5576 Args.clear();
5577 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005578 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005579 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5580 0));
5581 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5582 CGF.Builder.CreateStore(Exc, RethrowPtr);
5583 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5584
5585 if (Next)
5586 CGF.EmitBlock(Next);
5587 } else {
5588 assert(!Next && "catchup should be last handler.");
5589
5590 CGF.Builder.CreateStore(Exc, RethrowPtr);
5591 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5592 }
5593 }
5594
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005595 // Pop the cleanup entry, the @finally is outside this cleanup
5596 // scope.
5597 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5598 CGF.setInvokeDest(PrevLandingPad);
5599
5600 CGF.EmitBlock(FinallyBlock);
5601
5602 if (isTry) {
5603 if (const ObjCAtFinallyStmt* FinallyStmt =
5604 cast<ObjCAtTryStmt>(S).getFinallyStmt())
5605 CGF.EmitStmt(FinallyStmt->getFinallyBody());
5606 } else {
5607 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
5608 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00005609 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005610 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005611
5612 if (Info.SwitchBlock)
5613 CGF.EmitBlock(Info.SwitchBlock);
5614 if (Info.EndBlock)
5615 CGF.EmitBlock(Info.EndBlock);
5616
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005617 // Branch around the rethrow code.
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005618 CGF.EmitBranch(FinallyEnd);
5619
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005620 CGF.EmitBlock(FinallyRethrow);
Chris Lattner8a569112009-04-22 02:15:23 +00005621 CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005622 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005623 CGF.Builder.CreateUnreachable();
5624
5625 CGF.EmitBlock(FinallyEnd);
5626}
5627
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005628/// EmitThrowStmt - Generate code for a throw statement.
5629void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5630 const ObjCAtThrowStmt &S) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005631 llvm::Value *Exception;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005632 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005633 Exception = CGF.EmitScalarExpr(ThrowExpr);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005634 } else {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005635 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
5636 "Unexpected rethrow outside @catch block.");
5637 Exception = CGF.ObjCEHValueStack.back();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005638 }
5639
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005640 llvm::Value *ExceptionAsObject =
5641 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5642 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5643 if (InvokeDest) {
5644 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattnerbbccd612009-04-22 02:38:11 +00005645 CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005646 Cont, InvokeDest,
5647 &ExceptionAsObject, &ExceptionAsObject + 1);
5648 CGF.EmitBlock(Cont);
5649 } else
Chris Lattnerbbccd612009-04-22 02:38:11 +00005650 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005651 CGF.Builder.CreateUnreachable();
5652
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005653 // Clear the insertion point to indicate we are in unreachable code.
5654 CGF.Builder.ClearInsertionPoint();
5655}
Daniel Dunbare588b992009-03-01 04:46:24 +00005656
5657llvm::Value *
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005658CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
5659 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00005660 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00005661
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005662 // If we don't need a definition, return the entry if found or check
5663 // if we use an external reference.
5664 if (!ForDefinition) {
5665 if (Entry)
5666 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00005667
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005668 // If this type (or a super class) has the __objc_exception__
5669 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00005670 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005671 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005672 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005673 llvm::GlobalValue::ExternalLinkage,
5674 0,
5675 (std::string("OBJC_EHTYPE_$_") +
Owen Anderson1c431b32009-07-08 19:05:04 +00005676 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005677 }
5678
5679 // Otherwise we need to either make a new entry or fill in the
5680 // initializer.
5681 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005682 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00005683 std::string VTableName = "objc_ehtype_vtable";
5684 llvm::GlobalVariable *VTableGV =
5685 CGM.getModule().getGlobalVariable(VTableName);
5686 if (!VTableGV)
Owen Anderson1c431b32009-07-08 19:05:04 +00005687 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
5688 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00005689 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005690 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00005691
5692 llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2);
5693
5694 std::vector<llvm::Constant*> Values(3);
5695 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
5696 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005697 Values[2] = GetClassGlobal(ClassName);
Daniel Dunbare588b992009-03-01 04:46:24 +00005698 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
5699
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005700 if (Entry) {
5701 Entry->setInitializer(Init);
5702 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00005703 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005704 llvm::GlobalValue::WeakAnyLinkage,
5705 Init,
5706 (std::string("OBJC_EHTYPE_$_") +
Owen Anderson1c431b32009-07-08 19:05:04 +00005707 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005708 }
5709
Daniel Dunbar04d40782009-04-14 06:00:08 +00005710 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005711 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005712 Entry->setAlignment(8);
5713
5714 if (ForDefinition) {
5715 Entry->setSection("__DATA,__objc_const");
5716 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
5717 } else {
5718 Entry->setSection("__DATA,__datacoal_nt,coalesced");
5719 }
Daniel Dunbare588b992009-03-01 04:46:24 +00005720
5721 return Entry;
5722}
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005723
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005724/* *** */
5725
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005726CodeGen::CGObjCRuntime *
5727CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00005728 return new CGObjCMac(CGM);
5729}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005730
5731CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005732CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005733 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005734}