1 //===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This provides Objective-C code generation targeting the Apple runtime.
12 //===----------------------------------------------------------------------===//
14 #include "CGObjCRuntime.h"
16 #include "CGRecordLayout.h"
17 #include "CodeGenModule.h"
18 #include "CodeGenFunction.h"
20 #include "CGCleanup.h"
21 #include "clang/AST/ASTContext.h"
22 #include "clang/AST/Decl.h"
23 #include "clang/AST/DeclObjC.h"
24 #include "clang/AST/RecordLayout.h"
25 #include "clang/AST/StmtObjC.h"
26 #include "clang/Basic/LangOptions.h"
27 #include "clang/Frontend/CodeGenOptions.h"
29 #include "llvm/InlineAsm.h"
30 #include "llvm/IntrinsicInst.h"
31 #include "llvm/LLVMContext.h"
32 #include "llvm/Module.h"
33 #include "llvm/ADT/DenseSet.h"
34 #include "llvm/ADT/SetVector.h"
35 #include "llvm/ADT/SmallString.h"
36 #include "llvm/ADT/SmallPtrSet.h"
37 #include "llvm/Support/CallSite.h"
38 #include "llvm/Support/raw_ostream.h"
39 #include "llvm/Target/TargetData.h"
42 using namespace clang;
43 using namespace CodeGen;
48 typedef std::vector<llvm::Constant*> ConstantVector;
50 // FIXME: We should find a nicer way to make the labels for metadata, string
51 // concatenation is lame.
53 class ObjCCommonTypesHelper {
55 llvm::LLVMContext &VMContext;
58 // The types of these functions don't really matter because we
59 // should always bitcast before calling them.
61 /// id objc_msgSend (id, SEL, ...)
63 /// The default messenger, used for sends whose ABI is unchanged from
64 /// the all-integer/pointer case.
65 llvm::Constant *getMessageSendFn() const {
66 // Add the non-lazy-bind attribute, since objc_msgSend is likely to
68 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
69 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
72 llvm::Attribute::NonLazyBind);
75 /// void objc_msgSend_stret (id, SEL, ...)
77 /// The messenger used when the return value is an aggregate returned
78 /// by indirect reference in the first argument, and therefore the
79 /// self and selector parameters are shifted over by one.
80 llvm::Constant *getMessageSendStretFn() const {
81 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
82 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy,
84 "objc_msgSend_stret");
88 /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
90 /// The messenger used when the return value is returned on the x87
91 /// floating-point stack; without a special entrypoint, the nil case
92 /// would be unbalanced.
93 llvm::Constant *getMessageSendFpretFn() const {
94 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
95 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(
96 llvm::Type::getDoubleTy(VMContext),
98 "objc_msgSend_fpret");
102 /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
104 /// The messenger used for super calls, which have different dispatch
105 /// semantics. The class passed is the superclass of the current
107 llvm::Constant *getMessageSendSuperFn() const {
108 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
109 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
111 "objc_msgSendSuper");
114 /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
116 /// A slightly different messenger used for super calls. The class
117 /// passed is the current class.
118 llvm::Constant *getMessageSendSuperFn2() const {
119 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
120 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
122 "objc_msgSendSuper2");
125 /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super,
128 /// The messenger used for super calls which return an aggregate indirectly.
129 llvm::Constant *getMessageSendSuperStretFn() const {
130 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
131 return CGM.CreateRuntimeFunction(
132 llvm::FunctionType::get(CGM.VoidTy, params, true),
133 "objc_msgSendSuper_stret");
136 /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
139 /// objc_msgSendSuper_stret with the super2 semantics.
140 llvm::Constant *getMessageSendSuperStretFn2() const {
141 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
142 return CGM.CreateRuntimeFunction(
143 llvm::FunctionType::get(CGM.VoidTy, params, true),
144 "objc_msgSendSuper2_stret");
147 llvm::Constant *getMessageSendSuperFpretFn() const {
148 // There is no objc_msgSendSuper_fpret? How can that work?
149 return getMessageSendSuperFn();
152 llvm::Constant *getMessageSendSuperFpretFn2() const {
153 // There is no objc_msgSendSuper_fpret? How can that work?
154 return getMessageSendSuperFn2();
158 CodeGen::CodeGenModule &CGM;
161 llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
162 llvm::Type *Int8PtrTy;
164 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
165 llvm::Type *ObjectPtrTy;
167 /// PtrObjectPtrTy - LLVM type for id *
168 llvm::Type *PtrObjectPtrTy;
170 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
171 llvm::Type *SelectorPtrTy;
172 /// ProtocolPtrTy - LLVM type for external protocol handles
173 /// (typeof(Protocol))
174 llvm::Type *ExternalProtocolPtrTy;
176 // SuperCTy - clang type for struct objc_super.
178 // SuperPtrCTy - clang type for struct objc_super *.
179 QualType SuperPtrCTy;
181 /// SuperTy - LLVM type for struct objc_super.
182 llvm::StructType *SuperTy;
183 /// SuperPtrTy - LLVM type for struct objc_super *.
184 llvm::Type *SuperPtrTy;
186 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
187 /// in GCC parlance).
188 llvm::StructType *PropertyTy;
190 /// PropertyListTy - LLVM type for struct objc_property_list
191 /// (_prop_list_t in GCC parlance).
192 llvm::StructType *PropertyListTy;
193 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
194 llvm::Type *PropertyListPtrTy;
196 // MethodTy - LLVM type for struct objc_method.
197 llvm::StructType *MethodTy;
199 /// CacheTy - LLVM type for struct objc_cache.
201 /// CachePtrTy - LLVM type for struct objc_cache *.
202 llvm::Type *CachePtrTy;
204 llvm::Constant *getGetPropertyFn() {
205 CodeGen::CodeGenTypes &Types = CGM.getTypes();
206 ASTContext &Ctx = CGM.getContext();
207 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
208 llvm::SmallVector<CanQualType,4> Params;
209 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
210 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
211 Params.push_back(IdType);
212 Params.push_back(SelType);
213 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
214 Params.push_back(Ctx.BoolTy);
215 const llvm::FunctionType *FTy =
216 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
217 FunctionType::ExtInfo()),
219 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
222 llvm::Constant *getSetPropertyFn() {
223 CodeGen::CodeGenTypes &Types = CGM.getTypes();
224 ASTContext &Ctx = CGM.getContext();
225 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
226 llvm::SmallVector<CanQualType,6> Params;
227 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
228 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
229 Params.push_back(IdType);
230 Params.push_back(SelType);
231 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
232 Params.push_back(IdType);
233 Params.push_back(Ctx.BoolTy);
234 Params.push_back(Ctx.BoolTy);
235 const llvm::FunctionType *FTy =
236 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
237 FunctionType::ExtInfo()),
239 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
243 llvm::Constant *getCopyStructFn() {
244 CodeGen::CodeGenTypes &Types = CGM.getTypes();
245 ASTContext &Ctx = CGM.getContext();
246 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
247 llvm::SmallVector<CanQualType,5> Params;
248 Params.push_back(Ctx.VoidPtrTy);
249 Params.push_back(Ctx.VoidPtrTy);
250 Params.push_back(Ctx.LongTy);
251 Params.push_back(Ctx.BoolTy);
252 Params.push_back(Ctx.BoolTy);
253 const llvm::FunctionType *FTy =
254 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
255 FunctionType::ExtInfo()),
257 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
260 llvm::Constant *getEnumerationMutationFn() {
261 CodeGen::CodeGenTypes &Types = CGM.getTypes();
262 ASTContext &Ctx = CGM.getContext();
263 // void objc_enumerationMutation (id)
264 llvm::SmallVector<CanQualType,1> Params;
265 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
266 const llvm::FunctionType *FTy =
267 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
268 FunctionType::ExtInfo()),
270 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
273 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
274 llvm::Constant *getGcReadWeakFn() {
275 // id objc_read_weak (id *)
276 llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
277 llvm::FunctionType *FTy =
278 llvm::FunctionType::get(ObjectPtrTy, args, false);
279 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
282 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
283 llvm::Constant *getGcAssignWeakFn() {
284 // id objc_assign_weak (id, id *)
285 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
286 llvm::FunctionType *FTy =
287 llvm::FunctionType::get(ObjectPtrTy, args, false);
288 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
291 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
292 llvm::Constant *getGcAssignGlobalFn() {
293 // id objc_assign_global(id, id *)
294 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
295 llvm::FunctionType *FTy =
296 llvm::FunctionType::get(ObjectPtrTy, args, false);
297 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
300 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
301 llvm::Constant *getGcAssignThreadLocalFn() {
302 // id objc_assign_threadlocal(id src, id * dest)
303 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
304 llvm::FunctionType *FTy =
305 llvm::FunctionType::get(ObjectPtrTy, args, false);
306 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
309 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
310 llvm::Constant *getGcAssignIvarFn() {
311 // id objc_assign_ivar(id, id *, ptrdiff_t)
312 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
314 llvm::FunctionType *FTy =
315 llvm::FunctionType::get(ObjectPtrTy, args, false);
316 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
319 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
320 llvm::Constant *GcMemmoveCollectableFn() {
321 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
322 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
323 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
324 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
327 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
328 llvm::Constant *getGcAssignStrongCastFn() {
329 // id objc_assign_strongCast(id, id *)
330 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
331 llvm::FunctionType *FTy =
332 llvm::FunctionType::get(ObjectPtrTy, args, false);
333 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
336 /// ExceptionThrowFn - LLVM objc_exception_throw function.
337 llvm::Constant *getExceptionThrowFn() {
338 // void objc_exception_throw(id)
339 llvm::Type *args[] = { ObjectPtrTy };
340 llvm::FunctionType *FTy =
341 llvm::FunctionType::get(CGM.VoidTy, args, false);
342 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
345 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
346 llvm::Constant *getExceptionRethrowFn() {
347 // void objc_exception_rethrow(void)
348 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
349 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
352 /// SyncEnterFn - LLVM object_sync_enter function.
353 llvm::Constant *getSyncEnterFn() {
354 // void objc_sync_enter (id)
355 llvm::Type *args[] = { ObjectPtrTy };
356 llvm::FunctionType *FTy =
357 llvm::FunctionType::get(CGM.VoidTy, args, false);
358 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
361 /// SyncExitFn - LLVM object_sync_exit function.
362 llvm::Constant *getSyncExitFn() {
363 // void objc_sync_exit (id)
364 llvm::Type *args[] = { ObjectPtrTy };
365 llvm::FunctionType *FTy =
366 llvm::FunctionType::get(CGM.VoidTy, args, false);
367 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
370 llvm::Constant *getSendFn(bool IsSuper) const {
371 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
374 llvm::Constant *getSendFn2(bool IsSuper) const {
375 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
378 llvm::Constant *getSendStretFn(bool IsSuper) const {
379 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
382 llvm::Constant *getSendStretFn2(bool IsSuper) const {
383 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
386 llvm::Constant *getSendFpretFn(bool IsSuper) const {
387 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
390 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
391 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
394 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
395 ~ObjCCommonTypesHelper(){}
398 /// ObjCTypesHelper - Helper class that encapsulates lazy
399 /// construction of varies types used during ObjC generation.
400 class ObjCTypesHelper : public ObjCCommonTypesHelper {
402 /// SymtabTy - LLVM type for struct objc_symtab.
403 llvm::StructType *SymtabTy;
404 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
405 llvm::Type *SymtabPtrTy;
406 /// ModuleTy - LLVM type for struct objc_module.
407 llvm::StructType *ModuleTy;
409 /// ProtocolTy - LLVM type for struct objc_protocol.
410 llvm::StructType *ProtocolTy;
411 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
412 llvm::Type *ProtocolPtrTy;
413 /// ProtocolExtensionTy - LLVM type for struct
414 /// objc_protocol_extension.
415 llvm::StructType *ProtocolExtensionTy;
416 /// ProtocolExtensionTy - LLVM type for struct
417 /// objc_protocol_extension *.
418 llvm::Type *ProtocolExtensionPtrTy;
419 /// MethodDescriptionTy - LLVM type for struct
420 /// objc_method_description.
421 llvm::StructType *MethodDescriptionTy;
422 /// MethodDescriptionListTy - LLVM type for struct
423 /// objc_method_description_list.
424 llvm::StructType *MethodDescriptionListTy;
425 /// MethodDescriptionListPtrTy - LLVM type for struct
426 /// objc_method_description_list *.
427 llvm::Type *MethodDescriptionListPtrTy;
428 /// ProtocolListTy - LLVM type for struct objc_property_list.
429 llvm::StructType *ProtocolListTy;
430 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
431 llvm::Type *ProtocolListPtrTy;
432 /// CategoryTy - LLVM type for struct objc_category.
433 llvm::StructType *CategoryTy;
434 /// ClassTy - LLVM type for struct objc_class.
435 llvm::StructType *ClassTy;
436 /// ClassPtrTy - LLVM type for struct objc_class *.
437 llvm::Type *ClassPtrTy;
438 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
439 llvm::StructType *ClassExtensionTy;
440 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
441 llvm::Type *ClassExtensionPtrTy;
442 // IvarTy - LLVM type for struct objc_ivar.
443 llvm::StructType *IvarTy;
444 /// IvarListTy - LLVM type for struct objc_ivar_list.
445 llvm::Type *IvarListTy;
446 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
447 llvm::Type *IvarListPtrTy;
448 /// MethodListTy - LLVM type for struct objc_method_list.
449 llvm::Type *MethodListTy;
450 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
451 llvm::Type *MethodListPtrTy;
453 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
454 llvm::Type *ExceptionDataTy;
456 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
457 llvm::Constant *getExceptionTryEnterFn() {
458 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
459 return CGM.CreateRuntimeFunction(
460 llvm::FunctionType::get(CGM.VoidTy, params, false),
461 "objc_exception_try_enter");
464 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
465 llvm::Constant *getExceptionTryExitFn() {
466 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
467 return CGM.CreateRuntimeFunction(
468 llvm::FunctionType::get(CGM.VoidTy, params, false),
469 "objc_exception_try_exit");
472 /// ExceptionExtractFn - LLVM objc_exception_extract function.
473 llvm::Constant *getExceptionExtractFn() {
474 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
475 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
477 "objc_exception_extract");
480 /// ExceptionMatchFn - LLVM objc_exception_match function.
481 llvm::Constant *getExceptionMatchFn() {
482 llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
483 return CGM.CreateRuntimeFunction(
484 llvm::FunctionType::get(CGM.Int32Ty, params, false),
485 "objc_exception_match");
489 /// SetJmpFn - LLVM _setjmp function.
490 llvm::Constant *getSetJmpFn() {
491 // This is specifically the prototype for x86.
492 llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
493 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
499 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
500 ~ObjCTypesHelper() {}
503 /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
505 class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
508 // MethodListnfABITy - LLVM for struct _method_list_t
509 llvm::StructType *MethodListnfABITy;
511 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
512 llvm::Type *MethodListnfABIPtrTy;
514 // ProtocolnfABITy = LLVM for struct _protocol_t
515 llvm::StructType *ProtocolnfABITy;
517 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
518 llvm::Type *ProtocolnfABIPtrTy;
520 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
521 llvm::StructType *ProtocolListnfABITy;
523 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
524 llvm::Type *ProtocolListnfABIPtrTy;
526 // ClassnfABITy - LLVM for struct _class_t
527 llvm::StructType *ClassnfABITy;
529 // ClassnfABIPtrTy - LLVM for struct _class_t*
530 llvm::Type *ClassnfABIPtrTy;
532 // IvarnfABITy - LLVM for struct _ivar_t
533 llvm::StructType *IvarnfABITy;
535 // IvarListnfABITy - LLVM for struct _ivar_list_t
536 llvm::StructType *IvarListnfABITy;
538 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
539 llvm::Type *IvarListnfABIPtrTy;
541 // ClassRonfABITy - LLVM for struct _class_ro_t
542 llvm::StructType *ClassRonfABITy;
544 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
545 llvm::Type *ImpnfABITy;
547 // CategorynfABITy - LLVM for struct _category_t
548 llvm::StructType *CategorynfABITy;
550 // New types for nonfragile abi messaging.
552 // MessageRefTy - LLVM for:
553 // struct _message_ref_t {
557 llvm::StructType *MessageRefTy;
558 // MessageRefCTy - clang type for struct _message_ref_t
559 QualType MessageRefCTy;
561 // MessageRefPtrTy - LLVM for struct _message_ref_t*
562 llvm::Type *MessageRefPtrTy;
563 // MessageRefCPtrTy - clang type for struct _message_ref_t*
564 QualType MessageRefCPtrTy;
566 // MessengerTy - Type of the messenger (shown as IMP above)
567 llvm::FunctionType *MessengerTy;
569 // SuperMessageRefTy - LLVM for:
570 // struct _super_message_ref_t {
571 // SUPER_IMP messenger;
574 llvm::StructType *SuperMessageRefTy;
576 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
577 llvm::Type *SuperMessageRefPtrTy;
579 llvm::Constant *getMessageSendFixupFn() {
580 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
581 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
582 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
584 "objc_msgSend_fixup");
587 llvm::Constant *getMessageSendFpretFixupFn() {
588 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
589 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
590 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
592 "objc_msgSend_fpret_fixup");
595 llvm::Constant *getMessageSendStretFixupFn() {
596 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
597 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
598 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
600 "objc_msgSend_stret_fixup");
603 llvm::Constant *getMessageSendSuper2FixupFn() {
604 // id objc_msgSendSuper2_fixup (struct objc_super *,
605 // struct _super_message_ref_t*, ...)
606 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
607 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
609 "objc_msgSendSuper2_fixup");
612 llvm::Constant *getMessageSendSuper2StretFixupFn() {
613 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
614 // struct _super_message_ref_t*, ...)
615 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
616 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
618 "objc_msgSendSuper2_stret_fixup");
621 llvm::Constant *getObjCEndCatchFn() {
622 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
627 llvm::Constant *getObjCBeginCatchFn() {
628 llvm::Type *params[] = { Int8PtrTy };
629 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
634 llvm::StructType *EHTypeTy;
635 llvm::Type *EHTypePtrTy;
637 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
638 ~ObjCNonFragileABITypesHelper(){}
641 class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
643 // FIXME - accessibility
646 unsigned ivar_bytepos;
648 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
649 : ivar_bytepos(bytepos), ivar_size(size) {}
651 // Allow sorting based on byte pos.
652 bool operator<(const GC_IVAR &b) const {
653 return ivar_bytepos < b.ivar_bytepos;
661 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
662 : skip(_skip), scan(_scan) {}
666 CodeGen::CodeGenModule &CGM;
667 llvm::LLVMContext &VMContext;
668 // FIXME! May not be needing this after all.
671 // gc ivar layout bitmap calculation helper caches.
672 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
673 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
675 /// LazySymbols - Symbols to generate a lazy reference for. See
676 /// DefinedSymbols and FinishModule().
677 llvm::SetVector<IdentifierInfo*> LazySymbols;
679 /// DefinedSymbols - External symbols which are defined by this
680 /// module. The symbols in this list and LazySymbols are used to add
681 /// special linker symbols which ensure that Objective-C modules are
683 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
685 /// ClassNames - uniqued class names.
686 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
688 /// MethodVarNames - uniqued method variable names.
689 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
691 /// DefinedCategoryNames - list of category names in form Class_Category.
692 llvm::SetVector<std::string> DefinedCategoryNames;
694 /// MethodVarTypes - uniqued method type signatures. We have to use
695 /// a StringMap here because have no other unique reference.
696 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
698 /// MethodDefinitions - map of methods which have been defined in
699 /// this translation unit.
700 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
702 /// PropertyNames - uniqued method variable names.
703 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
705 /// ClassReferences - uniqued class references.
706 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
708 /// SelectorReferences - uniqued selector references.
709 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
711 /// Protocols - Protocols for which an objc_protocol structure has
712 /// been emitted. Forward declarations are handled by creating an
713 /// empty structure whose initializer is filled in when/if defined.
714 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
716 /// DefinedProtocols - Protocols which have actually been
717 /// defined. We should not need this, see FIXME in GenerateProtocol.
718 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
720 /// DefinedClasses - List of defined classes.
721 std::vector<llvm::GlobalValue*> DefinedClasses;
723 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
724 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
726 /// DefinedCategories - List of defined categories.
727 std::vector<llvm::GlobalValue*> DefinedCategories;
729 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
730 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
732 /// GetNameForMethod - Return a name for the given method.
733 /// \param[out] NameOut - The return value.
734 void GetNameForMethod(const ObjCMethodDecl *OMD,
735 const ObjCContainerDecl *CD,
736 llvm::SmallVectorImpl<char> &NameOut);
738 /// GetMethodVarName - Return a unique constant for the given
739 /// selector's name. The return value has type char *.
740 llvm::Constant *GetMethodVarName(Selector Sel);
741 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
743 /// GetMethodVarType - Return a unique constant for the given
744 /// selector's name. The return value has type char *.
746 // FIXME: This is a horrible name.
747 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
748 llvm::Constant *GetMethodVarType(const FieldDecl *D);
750 /// GetPropertyName - Return a unique constant for the given
751 /// name. The return value has type char *.
752 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
754 // FIXME: This can be dropped once string functions are unified.
755 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
756 const Decl *Container);
758 /// GetClassName - Return a unique constant for the given selector's
759 /// name. The return value has type char *.
760 llvm::Constant *GetClassName(IdentifierInfo *Ident);
762 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
764 /// BuildIvarLayout - Builds ivar layout bitmap for the class
765 /// implementation for the __strong or __weak case.
767 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
768 bool ForStrongLayout);
770 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
772 void BuildAggrIvarRecordLayout(const RecordType *RT,
773 unsigned int BytePos, bool ForStrongLayout,
775 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
776 const llvm::StructLayout *Layout,
777 const RecordDecl *RD,
778 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
779 unsigned int BytePos, bool ForStrongLayout,
782 /// GetIvarLayoutName - Returns a unique constant for the given
783 /// ivar layout bitmap.
784 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
785 const ObjCCommonTypesHelper &ObjCTypes);
787 /// EmitPropertyList - Emit the given property list. The return
788 /// value has type PropertyListPtrTy.
789 llvm::Constant *EmitPropertyList(llvm::Twine Name,
790 const Decl *Container,
791 const ObjCContainerDecl *OCD,
792 const ObjCCommonTypesHelper &ObjCTypes);
794 /// PushProtocolProperties - Push protocol's property on the input stack.
795 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
796 std::vector<llvm::Constant*> &Properties,
797 const Decl *Container,
798 const ObjCProtocolDecl *PROTO,
799 const ObjCCommonTypesHelper &ObjCTypes);
801 /// GetProtocolRef - Return a reference to the internal protocol
802 /// description, creating an empty one if it has not been
803 /// defined. The return value has type ProtocolPtrTy.
804 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
806 /// CreateMetadataVar - Create a global variable with internal
807 /// linkage for use by the Objective-C runtime.
809 /// This is a convenience wrapper which not only creates the
810 /// variable, but also sets the section and alignment and adds the
811 /// global to the "llvm.used" list.
813 /// \param Name - The variable name.
814 /// \param Init - The variable initializer; this is also used to
815 /// define the type of the variable.
816 /// \param Section - The section the variable should go into, or 0.
817 /// \param Align - The alignment for the variable, or 0.
818 /// \param AddToUsed - Whether the variable should be added to
820 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
821 llvm::Constant *Init,
826 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
827 ReturnValueSlot Return,
833 const CallArgList &CallArgs,
834 const ObjCMethodDecl *OMD,
835 const ObjCCommonTypesHelper &ObjCTypes);
837 /// EmitImageInfo - Emit the image info marker used to encode some module
838 /// level information.
839 void EmitImageInfo();
842 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
843 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
845 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
847 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
848 const ObjCContainerDecl *CD=0);
850 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
852 /// GetOrEmitProtocol - Get the protocol object for the given
853 /// declaration, emitting it if necessary. The return value has type
855 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
857 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
858 /// object for the given declaration, emitting it if needed. These
859 /// forward references will be filled in with empty bodies if no
860 /// definition is seen. The return value has type ProtocolPtrTy.
861 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
862 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
863 const CGBlockInfo &blockInfo);
867 class CGObjCMac : public CGObjCCommonMac {
869 ObjCTypesHelper ObjCTypes;
871 /// EmitModuleInfo - Another marker encoding module level
873 void EmitModuleInfo();
875 /// EmitModuleSymols - Emit module symbols, the list of defined
876 /// classes and categories. The result has type SymtabPtrTy.
877 llvm::Constant *EmitModuleSymbols();
879 /// FinishModule - Write out global data structures at the end of
880 /// processing a translation unit.
883 /// EmitClassExtension - Generate the class extension structure used
884 /// to store the weak ivar layout and properties. The return value
885 /// has type ClassExtensionPtrTy.
886 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
888 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
889 /// for the given class.
890 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
891 const ObjCInterfaceDecl *ID);
893 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
896 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
898 /// EmitSuperClassRef - Emits reference to class's main metadata class.
899 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
901 /// EmitIvarList - Emit the ivar list for the given
902 /// implementation. If ForClass is true the list of class ivars
903 /// (i.e. metaclass ivars) is emitted, otherwise the list of
904 /// interface ivars will be emitted. The return value has type
906 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
909 /// EmitMetaClass - Emit a forward reference to the class structure
910 /// for the metaclass of the given interface. The return value has
912 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
914 /// EmitMetaClass - Emit a class structure for the metaclass of the
915 /// given implementation. The return value has type ClassPtrTy.
916 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
917 llvm::Constant *Protocols,
918 const ConstantVector &Methods);
920 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
922 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
924 /// EmitMethodList - Emit the method list for the given
925 /// implementation. The return value has type MethodListPtrTy.
926 llvm::Constant *EmitMethodList(llvm::Twine Name,
928 const ConstantVector &Methods);
930 /// EmitMethodDescList - Emit a method description list for a list of
931 /// method declarations.
932 /// - TypeName: The name for the type containing the methods.
933 /// - IsProtocol: True iff these methods are for a protocol.
934 /// - ClassMethds: True iff these are class methods.
935 /// - Required: When true, only "required" methods are
936 /// listed. Similarly, when false only "optional" methods are
937 /// listed. For classes this should always be true.
938 /// - begin, end: The method list to output.
940 /// The return value has type MethodDescriptionListPtrTy.
941 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
943 const ConstantVector &Methods);
945 /// GetOrEmitProtocol - Get the protocol object for the given
946 /// declaration, emitting it if necessary. The return value has type
948 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
950 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
951 /// object for the given declaration, emitting it if needed. These
952 /// forward references will be filled in with empty bodies if no
953 /// definition is seen. The return value has type ProtocolPtrTy.
954 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
956 /// EmitProtocolExtension - Generate the protocol extension
957 /// structure used to store optional instance and class methods, and
958 /// protocol properties. The return value has type
959 /// ProtocolExtensionPtrTy.
961 EmitProtocolExtension(const ObjCProtocolDecl *PD,
962 const ConstantVector &OptInstanceMethods,
963 const ConstantVector &OptClassMethods);
965 /// EmitProtocolList - Generate the list of referenced
966 /// protocols. The return value has type ProtocolListPtrTy.
967 llvm::Constant *EmitProtocolList(llvm::Twine Name,
968 ObjCProtocolDecl::protocol_iterator begin,
969 ObjCProtocolDecl::protocol_iterator end);
971 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
972 /// for the given selector.
973 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
977 CGObjCMac(CodeGen::CodeGenModule &cgm);
979 virtual llvm::Function *ModuleInitFunction();
981 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
982 ReturnValueSlot Return,
985 llvm::Value *Receiver,
986 const CallArgList &CallArgs,
987 const ObjCInterfaceDecl *Class,
988 const ObjCMethodDecl *Method);
990 virtual CodeGen::RValue
991 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
992 ReturnValueSlot Return,
995 const ObjCInterfaceDecl *Class,
997 llvm::Value *Receiver,
999 const CallArgList &CallArgs,
1000 const ObjCMethodDecl *Method);
1002 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
1003 const ObjCInterfaceDecl *ID);
1005 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1008 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1010 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1011 const ObjCMethodDecl *Method);
1013 virtual llvm::Constant *GetEHType(QualType T);
1015 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
1017 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
1019 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
1020 const ObjCProtocolDecl *PD);
1022 virtual llvm::Constant *GetPropertyGetFunction();
1023 virtual llvm::Constant *GetPropertySetFunction();
1024 virtual llvm::Constant *GetGetStructFunction();
1025 virtual llvm::Constant *GetSetStructFunction();
1026 virtual llvm::Constant *EnumerationMutationFunction();
1028 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1029 const ObjCAtTryStmt &S);
1030 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1031 const ObjCAtSynchronizedStmt &S);
1032 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
1033 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1034 const ObjCAtThrowStmt &S);
1035 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
1036 llvm::Value *AddrWeakObj);
1037 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1038 llvm::Value *src, llvm::Value *dst);
1039 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1040 llvm::Value *src, llvm::Value *dest,
1041 bool threadlocal = false);
1042 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1043 llvm::Value *src, llvm::Value *dest,
1044 llvm::Value *ivarOffset);
1045 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1046 llvm::Value *src, llvm::Value *dest);
1047 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1048 llvm::Value *dest, llvm::Value *src,
1051 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1053 llvm::Value *BaseValue,
1054 const ObjCIvarDecl *Ivar,
1055 unsigned CVRQualifiers);
1056 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
1057 const ObjCInterfaceDecl *Interface,
1058 const ObjCIvarDecl *Ivar);
1060 /// GetClassGlobal - Return the global variable for the Objective-C
1061 /// class of the given name.
1062 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
1063 assert(false && "CGObjCMac::GetClassGlobal");
1068 class CGObjCNonFragileABIMac : public CGObjCCommonMac {
1070 ObjCNonFragileABITypesHelper ObjCTypes;
1071 llvm::GlobalVariable* ObjCEmptyCacheVar;
1072 llvm::GlobalVariable* ObjCEmptyVtableVar;
1074 /// SuperClassReferences - uniqued super class references.
1075 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
1077 /// MetaClassReferences - uniqued meta class references.
1078 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
1080 /// EHTypeReferences - uniqued class ehtype references.
1081 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
1083 /// VTableDispatchMethods - List of methods for which we generate
1084 /// vtable-based message dispatch.
1085 llvm::DenseSet<Selector> VTableDispatchMethods;
1087 /// DefinedMetaClasses - List of defined meta-classes.
1088 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1090 /// isVTableDispatchedSelector - Returns true if SEL is a
1091 /// vtable-based selector.
1092 bool isVTableDispatchedSelector(Selector Sel);
1094 /// FinishNonFragileABIModule - Write out global data structures at the end of
1095 /// processing a translation unit.
1096 void FinishNonFragileABIModule();
1098 /// AddModuleClassList - Add the given list of class pointers to the
1099 /// module with the provided symbol and section names.
1100 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1101 const char *SymbolName,
1102 const char *SectionName);
1104 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1105 unsigned InstanceStart,
1106 unsigned InstanceSize,
1107 const ObjCImplementationDecl *ID);
1108 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
1109 llvm::Constant *IsAGV,
1110 llvm::Constant *SuperClassGV,
1111 llvm::Constant *ClassRoGV,
1112 bool HiddenVisibility);
1114 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
1116 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
1118 /// EmitMethodList - Emit the method list for the given
1119 /// implementation. The return value has type MethodListnfABITy.
1120 llvm::Constant *EmitMethodList(llvm::Twine Name,
1121 const char *Section,
1122 const ConstantVector &Methods);
1123 /// EmitIvarList - Emit the ivar list for the given
1124 /// implementation. If ForClass is true the list of class ivars
1125 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1126 /// interface ivars will be emitted. The return value has type
1127 /// IvarListnfABIPtrTy.
1128 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
1130 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
1131 const ObjCIvarDecl *Ivar,
1132 unsigned long int offset);
1134 /// GetOrEmitProtocol - Get the protocol object for the given
1135 /// declaration, emitting it if necessary. The return value has type
1137 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
1139 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1140 /// object for the given declaration, emitting it if needed. These
1141 /// forward references will be filled in with empty bodies if no
1142 /// definition is seen. The return value has type ProtocolPtrTy.
1143 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
1145 /// EmitProtocolList - Generate the list of referenced
1146 /// protocols. The return value has type ProtocolListPtrTy.
1147 llvm::Constant *EmitProtocolList(llvm::Twine Name,
1148 ObjCProtocolDecl::protocol_iterator begin,
1149 ObjCProtocolDecl::protocol_iterator end);
1151 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1152 ReturnValueSlot Return,
1153 QualType ResultType,
1155 llvm::Value *Receiver,
1158 const CallArgList &CallArgs,
1159 const ObjCMethodDecl *Method);
1161 /// GetClassGlobal - Return the global variable for the Objective-C
1162 /// class of the given name.
1163 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
1165 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1166 /// for the given class reference.
1167 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
1168 const ObjCInterfaceDecl *ID);
1170 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1171 IdentifierInfo *II);
1173 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
1175 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1176 /// for the given super class reference.
1177 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1178 const ObjCInterfaceDecl *ID);
1180 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1182 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
1183 const ObjCInterfaceDecl *ID);
1185 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1188 llvm::GlobalVariable * ObjCIvarOffsetVariable(
1189 const ObjCInterfaceDecl *ID,
1190 const ObjCIvarDecl *Ivar);
1192 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1193 /// for the given selector.
1194 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1197 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
1198 /// interface. The return value has type EHTypePtrTy.
1199 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
1200 bool ForDefinition);
1202 const char *getMetaclassSymbolPrefix() const {
1203 return "OBJC_METACLASS_$_";
1206 const char *getClassSymbolPrefix() const {
1207 return "OBJC_CLASS_$_";
1210 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
1211 uint32_t &InstanceStart,
1212 uint32_t &InstanceSize);
1214 // Shamelessly stolen from Analysis/CFRefCount.cpp
1215 Selector GetNullarySelector(const char* name) const {
1216 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1217 return CGM.getContext().Selectors.getSelector(0, &II);
1220 Selector GetUnarySelector(const char* name) const {
1221 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1222 return CGM.getContext().Selectors.getSelector(1, &II);
1225 /// ImplementationIsNonLazy - Check whether the given category or
1226 /// class implementation is "non-lazy".
1227 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
1230 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
1231 // FIXME. All stubs for now!
1232 virtual llvm::Function *ModuleInitFunction();
1234 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
1235 ReturnValueSlot Return,
1236 QualType ResultType,
1238 llvm::Value *Receiver,
1239 const CallArgList &CallArgs,
1240 const ObjCInterfaceDecl *Class,
1241 const ObjCMethodDecl *Method);
1243 virtual CodeGen::RValue
1244 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
1245 ReturnValueSlot Return,
1246 QualType ResultType,
1248 const ObjCInterfaceDecl *Class,
1249 bool isCategoryImpl,
1250 llvm::Value *Receiver,
1251 bool IsClassMessage,
1252 const CallArgList &CallArgs,
1253 const ObjCMethodDecl *Method);
1255 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
1256 const ObjCInterfaceDecl *ID);
1258 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1259 bool lvalue = false)
1260 { return EmitSelector(Builder, Sel, lvalue); }
1262 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1264 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1265 const ObjCMethodDecl *Method)
1266 { return EmitSelector(Builder, Method->getSelector()); }
1268 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
1270 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
1271 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
1272 const ObjCProtocolDecl *PD);
1274 virtual llvm::Constant *GetEHType(QualType T);
1276 virtual llvm::Constant *GetPropertyGetFunction() {
1277 return ObjCTypes.getGetPropertyFn();
1279 virtual llvm::Constant *GetPropertySetFunction() {
1280 return ObjCTypes.getSetPropertyFn();
1283 virtual llvm::Constant *GetSetStructFunction() {
1284 return ObjCTypes.getCopyStructFn();
1286 virtual llvm::Constant *GetGetStructFunction() {
1287 return ObjCTypes.getCopyStructFn();
1290 virtual llvm::Constant *EnumerationMutationFunction() {
1291 return ObjCTypes.getEnumerationMutationFn();
1294 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1295 const ObjCAtTryStmt &S);
1296 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1297 const ObjCAtSynchronizedStmt &S);
1298 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1299 const ObjCAtThrowStmt &S);
1300 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
1301 llvm::Value *AddrWeakObj);
1302 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1303 llvm::Value *src, llvm::Value *dst);
1304 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1305 llvm::Value *src, llvm::Value *dest,
1306 bool threadlocal = false);
1307 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1308 llvm::Value *src, llvm::Value *dest,
1309 llvm::Value *ivarOffset);
1310 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1311 llvm::Value *src, llvm::Value *dest);
1312 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1313 llvm::Value *dest, llvm::Value *src,
1315 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1317 llvm::Value *BaseValue,
1318 const ObjCIvarDecl *Ivar,
1319 unsigned CVRQualifiers);
1320 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
1321 const ObjCInterfaceDecl *Interface,
1322 const ObjCIvarDecl *Ivar);
1325 /// A helper class for performing the null-initialization of a return
1327 struct NullReturnState {
1328 llvm::BasicBlock *NullBB;
1330 NullReturnState() : NullBB(0) {}
1332 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1333 // Make blocks for the null-init and call edges.
1334 NullBB = CGF.createBasicBlock("msgSend.nullinit");
1335 llvm::BasicBlock *callBB = CGF.createBasicBlock("msgSend.call");
1337 // Check for a null receiver and, if there is one, jump to the
1339 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1340 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1342 // Otherwise, start performing the call.
1343 CGF.EmitBlock(callBB);
1346 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType) {
1347 if (!NullBB) return result;
1349 // Finish the call path.
1350 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1351 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1353 // Emit the null-init block and perform the null-initialization there.
1354 CGF.EmitBlock(NullBB);
1355 assert(result.isAggregate() && "null init of non-aggregate result?");
1356 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1358 // Jump to the continuation block.
1359 CGF.EmitBlock(contBB);
1365 } // end anonymous namespace
1367 /* *** Helper Functions *** */
1369 /// getConstantGEP() - Help routine to construct simple GEPs.
1370 static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
1374 llvm::Value *Idxs[] = {
1375 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1376 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
1378 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
1381 /// hasObjCExceptionAttribute - Return true if this class or any super
1382 /// class has the __objc_exception__ attribute.
1383 static bool hasObjCExceptionAttribute(ASTContext &Context,
1384 const ObjCInterfaceDecl *OID) {
1385 if (OID->hasAttr<ObjCExceptionAttr>())
1387 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
1388 return hasObjCExceptionAttribute(Context, Super);
1392 /* *** CGObjCMac Public Interface *** */
1394 CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
1400 /// GetClass - Return a reference to the class for the given interface
1402 llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
1403 const ObjCInterfaceDecl *ID) {
1404 return EmitClassRef(Builder, ID);
1407 /// GetSelector - Return the pointer to the unique'd string for this selector.
1408 llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1410 return EmitSelector(Builder, Sel, lval);
1412 llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
1414 return EmitSelector(Builder, Method->getSelector());
1417 llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1418 if (T->isObjCIdType() ||
1419 T->isObjCQualifiedIdType()) {
1420 return CGM.GetAddrOfRTTIDescriptor(
1421 CGM.getContext().ObjCIdRedefinitionType, /*ForEH=*/true);
1423 if (T->isObjCClassType() ||
1424 T->isObjCQualifiedClassType()) {
1425 return CGM.GetAddrOfRTTIDescriptor(
1426 CGM.getContext().ObjCClassRedefinitionType, /*ForEH=*/true);
1428 if (T->isObjCObjectPointerType())
1429 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1431 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1435 /// Generate a constant CFString object.
1437 struct __builtin_CFString {
1438 const int *isa; // point to __CFConstantStringClassReference
1445 /// or Generate a constant NSString object.
1447 struct __builtin_NSString {
1448 const int *isa; // point to __NSConstantStringClassReference
1450 unsigned int length;
1454 llvm::Constant *CGObjCCommonMac::GenerateConstantString(
1455 const StringLiteral *SL) {
1456 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1457 CGM.GetAddrOfConstantCFString(SL) :
1458 CGM.GetAddrOfConstantString(SL));
1461 /// Generates a message send where the super is the receiver. This is
1462 /// a message send to self with special delivery semantics indicating
1463 /// which class's method should be called.
1465 CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
1466 ReturnValueSlot Return,
1467 QualType ResultType,
1469 const ObjCInterfaceDecl *Class,
1470 bool isCategoryImpl,
1471 llvm::Value *Receiver,
1472 bool IsClassMessage,
1473 const CodeGen::CallArgList &CallArgs,
1474 const ObjCMethodDecl *Method) {
1475 // Create and init a super structure; this is a (receiver, class)
1476 // pair we will pass to objc_msgSendSuper.
1477 llvm::Value *ObjCSuper =
1478 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
1479 llvm::Value *ReceiverAsObject =
1480 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
1481 CGF.Builder.CreateStore(ReceiverAsObject,
1482 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
1484 // If this is a class message the metaclass is passed as the target.
1485 llvm::Value *Target;
1486 if (IsClassMessage) {
1487 if (isCategoryImpl) {
1488 // Message sent to 'super' in a class method defined in a category
1489 // implementation requires an odd treatment.
1490 // If we are in a class method, we must retrieve the
1491 // _metaclass_ for the current class, pointed at by
1492 // the class's "isa" pointer. The following assumes that
1493 // isa" is the first ivar in a class (which it must be).
1494 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1495 Target = CGF.Builder.CreateStructGEP(Target, 0);
1496 Target = CGF.Builder.CreateLoad(Target);
1498 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1499 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1500 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1504 else if (isCategoryImpl)
1505 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1507 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1508 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1509 Target = CGF.Builder.CreateLoad(ClassPtr);
1511 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1513 const llvm::Type *ClassTy =
1514 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
1515 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
1516 CGF.Builder.CreateStore(Target,
1517 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
1518 return EmitMessageSend(CGF, Return, ResultType,
1519 EmitSelector(CGF.Builder, Sel),
1520 ObjCSuper, ObjCTypes.SuperPtrCTy,
1521 true, CallArgs, Method, ObjCTypes);
1524 /// Generate code for a message send expression.
1525 CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
1526 ReturnValueSlot Return,
1527 QualType ResultType,
1529 llvm::Value *Receiver,
1530 const CallArgList &CallArgs,
1531 const ObjCInterfaceDecl *Class,
1532 const ObjCMethodDecl *Method) {
1533 return EmitMessageSend(CGF, Return, ResultType,
1534 EmitSelector(CGF.Builder, Sel),
1535 Receiver, CGF.getContext().getObjCIdType(),
1536 false, CallArgs, Method, ObjCTypes);
1540 CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1541 ReturnValueSlot Return,
1542 QualType ResultType,
1547 const CallArgList &CallArgs,
1548 const ObjCMethodDecl *Method,
1549 const ObjCCommonTypesHelper &ObjCTypes) {
1550 CallArgList ActualArgs;
1552 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
1553 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1554 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
1555 ActualArgs.addFrom(CallArgs);
1557 CodeGenTypes &Types = CGM.getTypes();
1558 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
1559 FunctionType::ExtInfo());
1560 const llvm::FunctionType *FTy =
1561 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
1564 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1565 CGM.getContext().getCanonicalType(ResultType) &&
1566 "Result type mismatch!");
1568 NullReturnState nullReturn;
1570 llvm::Constant *Fn = NULL;
1571 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
1572 if (!IsSuper) nullReturn.init(CGF, Arg0);
1573 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
1574 : ObjCTypes.getSendStretFn(IsSuper);
1575 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1576 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1577 : ObjCTypes.getSendFpretFn(IsSuper);
1579 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
1580 : ObjCTypes.getSendFn(IsSuper);
1582 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
1583 RValue rvalue = CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
1584 return nullReturn.complete(CGF, rvalue, ResultType);
1587 static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1588 if (FQT.isObjCGCStrong())
1589 return Qualifiers::Strong;
1591 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
1592 return Qualifiers::Weak;
1594 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1595 return Qualifiers::Strong;
1597 if (const PointerType *PT = FQT->getAs<PointerType>())
1598 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1600 return Qualifiers::GCNone;
1603 llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1604 const CGBlockInfo &blockInfo) {
1605 llvm::Constant *nullPtr =
1606 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
1608 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC &&
1609 !CGM.getLangOptions().ObjCAutoRefCount)
1612 bool hasUnion = false;
1615 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
1616 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
1618 // __isa is the first field in block descriptor and must assume by runtime's
1619 // convention that it is GC'able.
1620 IvarsInfo.push_back(GC_IVAR(0, 1));
1622 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1624 // Calculate the basic layout of the block structure.
1625 const llvm::StructLayout *layout =
1626 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1628 // Ignore the optional 'this' capture: C++ objects are not assumed
1631 // Walk the captured variables.
1632 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1633 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1634 const VarDecl *variable = ci->getVariable();
1635 QualType type = variable->getType();
1637 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1639 // Ignore constant captures.
1640 if (capture.isConstant()) continue;
1642 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1644 // __block variables are passed by their descriptor address.
1645 if (ci->isByRef()) {
1646 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
1650 assert(!type->isArrayType() && "array variable should not be caught");
1651 if (const RecordType *record = type->getAs<RecordType>()) {
1652 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
1656 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1657 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1659 if (GCAttr == Qualifiers::Strong)
1660 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1661 fieldSize / WordSizeInBits));
1662 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
1663 SkipIvars.push_back(GC_IVAR(fieldOffset,
1664 fieldSize / ByteSizeInBits));
1667 if (IvarsInfo.empty())
1670 // Sort on byte position; captures might not be allocated in order,
1671 // and unions can do funny things.
1672 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1673 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
1676 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
1677 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1678 printf("\n block variable layout for block: ");
1679 const unsigned char *s = (unsigned char*)BitMap.c_str();
1680 for (unsigned i = 0; i < BitMap.size(); i++)
1682 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1684 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1691 llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
1692 const ObjCProtocolDecl *PD) {
1693 // FIXME: I don't understand why gcc generates this, or where it is
1694 // resolved. Investigate. Its also wasteful to look this up over and over.
1695 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1697 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
1698 ObjCTypes.ExternalProtocolPtrTy);
1701 void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
1702 // FIXME: We shouldn't need this, the protocol decl should contain enough
1703 // information to tell us whether this was a declaration or a definition.
1704 DefinedProtocols.insert(PD->getIdentifier());
1706 // If we have generated a forward reference to this protocol, emit
1707 // it now. Otherwise do nothing, the protocol objects are lazily
1709 if (Protocols.count(PD->getIdentifier()))
1710 GetOrEmitProtocol(PD);
1713 llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
1714 if (DefinedProtocols.count(PD->getIdentifier()))
1715 return GetOrEmitProtocol(PD);
1717 return GetOrEmitProtocolRef(PD);
1721 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1722 struct _objc_protocol {
1723 struct _objc_protocol_extension *isa;
1724 char *protocol_name;
1725 struct _objc_protocol_list *protocol_list;
1726 struct _objc__method_prototype_list *instance_methods;
1727 struct _objc__method_prototype_list *class_methods
1730 See EmitProtocolExtension().
1732 llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1733 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1735 // Early exit if a defining object has already been generated.
1736 if (Entry && Entry->hasInitializer())
1739 // FIXME: I don't understand why gcc generates this, or where it is
1740 // resolved. Investigate. Its also wasteful to look this up over and over.
1741 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1743 // Construct method lists.
1744 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1745 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
1746 for (ObjCProtocolDecl::instmeth_iterator
1747 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
1748 ObjCMethodDecl *MD = *i;
1749 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1751 return GetOrEmitProtocolRef(PD);
1753 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1754 OptInstanceMethods.push_back(C);
1756 InstanceMethods.push_back(C);
1760 for (ObjCProtocolDecl::classmeth_iterator
1761 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
1762 ObjCMethodDecl *MD = *i;
1763 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1765 return GetOrEmitProtocolRef(PD);
1767 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1768 OptClassMethods.push_back(C);
1770 ClassMethods.push_back(C);
1774 std::vector<llvm::Constant*> Values(5);
1775 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
1776 Values[1] = GetClassName(PD->getIdentifier());
1778 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
1779 PD->protocol_begin(),
1780 PD->protocol_end());
1782 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
1783 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1786 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
1787 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1789 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1793 // Already created, fix the linkage and update the initializer.
1794 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
1795 Entry->setInitializer(Init);
1798 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
1799 llvm::GlobalValue::InternalLinkage,
1801 "\01L_OBJC_PROTOCOL_" + PD->getName());
1802 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1803 // FIXME: Is this necessary? Why only for protocol?
1804 Entry->setAlignment(4);
1806 CGM.AddUsedGlobal(Entry);
1811 llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
1812 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1815 // We use the initializer as a marker of whether this is a forward
1816 // reference or not. At module finalization we add the empty
1817 // contents for protocols which were referenced but never defined.
1819 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
1820 llvm::GlobalValue::ExternalLinkage,
1822 "\01L_OBJC_PROTOCOL_" + PD->getName());
1823 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
1824 // FIXME: Is this necessary? Why only for protocol?
1825 Entry->setAlignment(4);
1832 struct _objc_protocol_extension {
1834 struct objc_method_description_list *optional_instance_methods;
1835 struct objc_method_description_list *optional_class_methods;
1836 struct objc_property_list *instance_properties;
1840 CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1841 const ConstantVector &OptInstanceMethods,
1842 const ConstantVector &OptClassMethods) {
1844 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
1845 std::vector<llvm::Constant*> Values(4);
1846 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
1848 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1850 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1851 OptInstanceMethods);
1853 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
1854 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1856 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
1859 // Return null if no extension bits are used.
1860 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1861 Values[3]->isNullValue())
1862 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1864 llvm::Constant *Init =
1865 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
1867 // No special section, but goes in llvm.used
1868 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
1874 struct objc_protocol_list {
1875 struct objc_protocol_list *next;
1881 CGObjCMac::EmitProtocolList(llvm::Twine Name,
1882 ObjCProtocolDecl::protocol_iterator begin,
1883 ObjCProtocolDecl::protocol_iterator end) {
1884 std::vector<llvm::Constant*> ProtocolRefs;
1886 for (; begin != end; ++begin)
1887 ProtocolRefs.push_back(GetProtocolRef(*begin));
1889 // Just return null for empty protocol lists
1890 if (ProtocolRefs.empty())
1891 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1893 // This list is null terminated.
1894 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1896 llvm::Constant *Values[3];
1897 // This field is only used by the runtime.
1898 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1899 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
1900 ProtocolRefs.size() - 1);
1902 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1903 ProtocolRefs.size()),
1906 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
1907 llvm::GlobalVariable *GV =
1908 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1910 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1913 void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1914 std::vector<llvm::Constant*> &Properties,
1915 const Decl *Container,
1916 const ObjCProtocolDecl *PROTO,
1917 const ObjCCommonTypesHelper &ObjCTypes) {
1918 std::vector<llvm::Constant*> Prop(2);
1919 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1920 E = PROTO->protocol_end(); P != E; ++P)
1921 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1922 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1923 E = PROTO->prop_end(); I != E; ++I) {
1924 const ObjCPropertyDecl *PD = *I;
1925 if (!PropertySet.insert(PD->getIdentifier()))
1927 Prop[0] = GetPropertyName(PD->getIdentifier());
1928 Prop[1] = GetPropertyTypeString(PD, Container);
1929 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1934 struct _objc_property {
1935 const char * const name;
1936 const char * const attributes;
1939 struct _objc_property_list {
1940 uint32_t entsize; // sizeof (struct _objc_property)
1941 uint32_t prop_count;
1942 struct _objc_property[prop_count];
1945 llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
1946 const Decl *Container,
1947 const ObjCContainerDecl *OCD,
1948 const ObjCCommonTypesHelper &ObjCTypes) {
1949 std::vector<llvm::Constant*> Properties, Prop(2);
1950 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
1951 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1952 E = OCD->prop_end(); I != E; ++I) {
1953 const ObjCPropertyDecl *PD = *I;
1954 PropertySet.insert(PD->getIdentifier());
1955 Prop[0] = GetPropertyName(PD->getIdentifier());
1956 Prop[1] = GetPropertyTypeString(PD, Container);
1957 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1960 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
1961 for (ObjCInterfaceDecl::all_protocol_iterator
1962 P = OID->all_referenced_protocol_begin(),
1963 E = OID->all_referenced_protocol_end(); P != E; ++P)
1964 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1967 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
1968 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
1969 E = CD->protocol_end(); P != E; ++P)
1970 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1974 // Return null for empty list.
1975 if (Properties.empty())
1976 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1978 unsigned PropertySize =
1979 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
1980 llvm::Constant *Values[3];
1981 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1982 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1983 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1985 Values[2] = llvm::ConstantArray::get(AT, Properties);
1986 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
1988 llvm::GlobalVariable *GV =
1989 CreateMetadataVar(Name, Init,
1990 (ObjCABI == 2) ? "__DATA, __objc_const" :
1991 "__OBJC,__property,regular,no_dead_strip",
1992 (ObjCABI == 2) ? 8 : 4,
1994 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
1998 struct objc_method_description_list {
2000 struct objc_method_description list[];
2004 CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
2005 std::vector<llvm::Constant*> Desc(2);
2007 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2008 ObjCTypes.SelectorPtrTy);
2009 Desc[1] = GetMethodVarType(MD);
2013 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
2017 llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
2018 const char *Section,
2019 const ConstantVector &Methods) {
2020 // Return null for empty list.
2021 if (Methods.empty())
2022 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
2024 llvm::Constant *Values[2];
2025 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
2026 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
2028 Values[1] = llvm::ConstantArray::get(AT, Methods);
2029 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
2031 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
2032 return llvm::ConstantExpr::getBitCast(GV,
2033 ObjCTypes.MethodDescriptionListPtrTy);
2037 struct _objc_category {
2038 char *category_name;
2040 struct _objc_method_list *instance_methods;
2041 struct _objc_method_list *class_methods;
2042 struct _objc_protocol_list *protocols;
2043 uint32_t size; // <rdar://4585769>
2044 struct _objc_property_list *instance_properties;
2047 void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
2048 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
2050 // FIXME: This is poor design, the OCD should have a pointer to the category
2051 // decl. Additionally, note that Category can be null for the @implementation
2052 // w/o an @interface case. Sema should just create one for us as it does for
2053 // @implementation so everyone else can live life under a clear blue sky.
2054 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
2055 const ObjCCategoryDecl *Category =
2056 Interface->FindCategoryDeclaration(OCD->getIdentifier());
2058 llvm::SmallString<256> ExtName;
2059 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2062 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
2063 for (ObjCCategoryImplDecl::instmeth_iterator
2064 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
2065 // Instance methods should always be defined.
2066 InstanceMethods.push_back(GetMethodConstant(*i));
2068 for (ObjCCategoryImplDecl::classmeth_iterator
2069 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
2070 // Class methods should always be defined.
2071 ClassMethods.push_back(GetMethodConstant(*i));
2074 llvm::Constant *Values[7];
2075 Values[0] = GetClassName(OCD->getIdentifier());
2076 Values[1] = GetClassName(Interface->getIdentifier());
2077 LazySymbols.insert(Interface->getIdentifier());
2079 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
2080 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
2083 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
2084 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
2088 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
2089 Category->protocol_begin(),
2090 Category->protocol_end());
2092 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
2094 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
2096 // If there is no category @interface then there can be no properties.
2098 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
2099 OCD, Category, ObjCTypes);
2101 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
2104 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
2107 llvm::GlobalVariable *GV =
2108 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
2109 "__OBJC,__category,regular,no_dead_strip",
2111 DefinedCategories.push_back(GV);
2112 DefinedCategoryNames.insert(ExtName.str());
2113 // method definition entries must be clear for next implementation.
2114 MethodDefinitions.clear();
2117 // FIXME: Get from somewhere?
2119 eClassFlags_Factory = 0x00001,
2120 eClassFlags_Meta = 0x00002,
2122 eClassFlags_HasCXXStructors = 0x02000,
2123 eClassFlags_Hidden = 0x20000,
2124 eClassFlags_ABI2_Hidden = 0x00010,
2125 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2129 struct _objc_class {
2136 struct _objc_ivar_list *ivars;
2137 struct _objc_method_list *methods;
2138 struct _objc_cache *cache;
2139 struct _objc_protocol_list *protocols;
2140 // Objective-C 1.0 extensions (<rdr://4585769>)
2141 const char *ivar_layout;
2142 struct _objc_class_ext *ext;
2145 See EmitClassExtension();
2147 void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
2148 DefinedSymbols.insert(ID->getIdentifier());
2150 std::string ClassName = ID->getNameAsString();
2152 ObjCInterfaceDecl *Interface =
2153 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
2154 llvm::Constant *Protocols =
2155 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
2156 Interface->all_referenced_protocol_begin(),
2157 Interface->all_referenced_protocol_end());
2158 unsigned Flags = eClassFlags_Factory;
2159 if (ID->hasCXXStructors())
2160 Flags |= eClassFlags_HasCXXStructors;
2162 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
2164 // FIXME: Set CXX-structors flag.
2165 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
2166 Flags |= eClassFlags_Hidden;
2168 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
2169 for (ObjCImplementationDecl::instmeth_iterator
2170 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
2171 // Instance methods should always be defined.
2172 InstanceMethods.push_back(GetMethodConstant(*i));
2174 for (ObjCImplementationDecl::classmeth_iterator
2175 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
2176 // Class methods should always be defined.
2177 ClassMethods.push_back(GetMethodConstant(*i));
2180 for (ObjCImplementationDecl::propimpl_iterator
2181 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
2182 ObjCPropertyImplDecl *PID = *i;
2184 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2185 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2187 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2188 if (llvm::Constant *C = GetMethodConstant(MD))
2189 InstanceMethods.push_back(C);
2190 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2191 if (llvm::Constant *C = GetMethodConstant(MD))
2192 InstanceMethods.push_back(C);
2196 llvm::Constant *Values[12];
2197 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
2198 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
2199 // Record a reference to the super class.
2200 LazySymbols.insert(Super->getIdentifier());
2203 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
2204 ObjCTypes.ClassPtrTy);
2206 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
2208 Values[ 2] = GetClassName(ID->getIdentifier());
2209 // Version is always 0.
2210 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2211 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2212 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
2213 Values[ 6] = EmitIvarList(ID, false);
2215 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
2216 "__OBJC,__inst_meth,regular,no_dead_strip",
2218 // cache is always NULL.
2219 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
2220 Values[ 9] = Protocols;
2221 Values[10] = BuildIvarLayout(ID, true);
2222 Values[11] = EmitClassExtension(ID);
2223 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
2225 std::string Name("\01L_OBJC_CLASS_");
2227 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2228 // Check for a forward reference.
2229 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2231 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2232 "Forward metaclass reference has incorrect type.");
2233 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2234 GV->setInitializer(Init);
2235 GV->setSection(Section);
2236 GV->setAlignment(4);
2237 CGM.AddUsedGlobal(GV);
2240 GV = CreateMetadataVar(Name, Init, Section, 4, true);
2241 DefinedClasses.push_back(GV);
2242 // method definition entries must be clear for next implementation.
2243 MethodDefinitions.clear();
2246 llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2247 llvm::Constant *Protocols,
2248 const ConstantVector &Methods) {
2249 unsigned Flags = eClassFlags_Meta;
2250 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
2252 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
2253 Flags |= eClassFlags_Hidden;
2255 llvm::Constant *Values[12];
2256 // The isa for the metaclass is the root of the hierarchy.
2257 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2258 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2261 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
2262 ObjCTypes.ClassPtrTy);
2263 // The super class for the metaclass is emitted as the name of the
2264 // super class. The runtime fixes this up to point to the
2265 // *metaclass* for the super class.
2266 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
2268 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
2269 ObjCTypes.ClassPtrTy);
2271 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
2273 Values[ 2] = GetClassName(ID->getIdentifier());
2274 // Version is always 0.
2275 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2276 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2277 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
2278 Values[ 6] = EmitIvarList(ID, true);
2280 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
2281 "__OBJC,__cls_meth,regular,no_dead_strip",
2283 // cache is always NULL.
2284 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
2285 Values[ 9] = Protocols;
2286 // ivar_layout for metaclass is always NULL.
2287 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2288 // The class extension is always unused for metaclasses.
2289 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
2290 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
2293 std::string Name("\01L_OBJC_METACLASS_");
2294 Name += ID->getNameAsCString();
2296 // Check for a forward reference.
2297 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2299 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2300 "Forward metaclass reference has incorrect type.");
2301 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2302 GV->setInitializer(Init);
2304 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2305 llvm::GlobalValue::InternalLinkage,
2308 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
2309 GV->setAlignment(4);
2310 CGM.AddUsedGlobal(GV);
2315 llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
2316 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
2318 // FIXME: Should we look these up somewhere other than the module. Its a bit
2319 // silly since we only generate these while processing an implementation, so
2320 // exactly one pointer would work if know when we entered/exitted an
2321 // implementation block.
2323 // Check for an existing forward reference.
2324 // Previously, metaclass with internal linkage may have been defined.
2325 // pass 'true' as 2nd argument so it is returned.
2326 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2328 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2329 "Forward metaclass reference has incorrect type.");
2332 // Generate as an external reference to keep a consistent
2333 // module. This will be patched up when we emit the metaclass.
2334 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2335 llvm::GlobalValue::ExternalLinkage,
2341 llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2342 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2344 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2346 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2347 "Forward class metadata reference has incorrect type.");
2350 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2351 llvm::GlobalValue::ExternalLinkage,
2358 struct objc_class_ext {
2360 const char *weak_ivar_layout;
2361 struct _objc_property_list *properties;
2365 CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
2367 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
2369 llvm::Constant *Values[3];
2370 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
2371 Values[1] = BuildIvarLayout(ID, false);
2372 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
2373 ID, ID->getClassInterface(), ObjCTypes);
2375 // Return null if no extension bits are used.
2376 if (Values[1]->isNullValue() && Values[2]->isNullValue())
2377 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
2379 llvm::Constant *Init =
2380 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
2381 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
2382 Init, "__OBJC,__class_ext,regular,no_dead_strip",
2393 struct objc_ivar_list {
2395 struct objc_ivar list[count];
2398 llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
2400 std::vector<llvm::Constant*> Ivars, Ivar(3);
2402 // When emitting the root class GCC emits ivar entries for the
2403 // actual class structure. It is not clear if we need to follow this
2404 // behavior; for now lets try and get away with not doing it. If so,
2405 // the cleanest solution would be to make up an ObjCInterfaceDecl
2408 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
2410 ObjCInterfaceDecl *OID =
2411 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
2413 for (ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
2414 IVD; IVD = IVD->getNextIvar()) {
2415 // Ignore unnamed bit-fields.
2416 if (!IVD->getDeclName())
2418 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2419 Ivar[1] = GetMethodVarType(IVD);
2420 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
2421 ComputeIvarBaseOffset(CGM, OID, IVD));
2422 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
2425 // Return null for empty list.
2427 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
2429 llvm::Constant *Values[2];
2430 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
2431 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
2433 Values[1] = llvm::ConstantArray::get(AT, Ivars);
2434 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
2436 llvm::GlobalVariable *GV;
2438 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
2439 Init, "__OBJC,__class_vars,regular,no_dead_strip",
2442 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
2443 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
2445 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
2449 struct objc_method {
2455 struct objc_method_list {
2456 struct objc_method_list *obsolete;
2458 struct objc_method methods_list[count];
2462 /// GetMethodConstant - Return a struct objc_method constant for the
2463 /// given method if it has been defined. The result is null if the
2464 /// method has not been defined. The return value has type MethodPtrTy.
2465 llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
2466 llvm::Function *Fn = GetMethodDefinition(MD);
2470 std::vector<llvm::Constant*> Method(3);
2472 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2473 ObjCTypes.SelectorPtrTy);
2474 Method[1] = GetMethodVarType(MD);
2475 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
2476 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
2479 llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
2480 const char *Section,
2481 const ConstantVector &Methods) {
2482 // Return null for empty list.
2483 if (Methods.empty())
2484 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
2486 llvm::Constant *Values[3];
2487 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2488 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
2489 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
2491 Values[2] = llvm::ConstantArray::get(AT, Methods);
2492 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
2494 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
2495 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
2498 llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
2499 const ObjCContainerDecl *CD) {
2500 llvm::SmallString<256> Name;
2501 GetNameForMethod(OMD, CD, Name);
2503 CodeGenTypes &Types = CGM.getTypes();
2504 const llvm::FunctionType *MethodTy =
2505 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
2506 llvm::Function *Method =
2507 llvm::Function::Create(MethodTy,
2508 llvm::GlobalValue::InternalLinkage,
2511 MethodDefinitions.insert(std::make_pair(OMD, Method));
2516 llvm::GlobalVariable *
2517 CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
2518 llvm::Constant *Init,
2519 const char *Section,
2522 const llvm::Type *Ty = Init->getType();
2523 llvm::GlobalVariable *GV =
2524 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
2525 llvm::GlobalValue::InternalLinkage, Init, Name);
2527 GV->setSection(Section);
2529 GV->setAlignment(Align);
2531 CGM.AddUsedGlobal(GV);
2535 llvm::Function *CGObjCMac::ModuleInitFunction() {
2536 // Abuse this interface function as a place to finalize.
2541 llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
2542 return ObjCTypes.getGetPropertyFn();
2545 llvm::Constant *CGObjCMac::GetPropertySetFunction() {
2546 return ObjCTypes.getSetPropertyFn();
2549 llvm::Constant *CGObjCMac::GetGetStructFunction() {
2550 return ObjCTypes.getCopyStructFn();
2552 llvm::Constant *CGObjCMac::GetSetStructFunction() {
2553 return ObjCTypes.getCopyStructFn();
2556 llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
2557 return ObjCTypes.getEnumerationMutationFn();
2560 void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2561 return EmitTryOrSynchronizedStmt(CGF, S);
2564 void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2565 const ObjCAtSynchronizedStmt &S) {
2566 return EmitTryOrSynchronizedStmt(CGF, S);
2570 struct PerformFragileFinally : EHScopeStack::Cleanup {
2572 llvm::Value *SyncArgSlot;
2573 llvm::Value *CallTryExitVar;
2574 llvm::Value *ExceptionData;
2575 ObjCTypesHelper &ObjCTypes;
2576 PerformFragileFinally(const Stmt *S,
2577 llvm::Value *SyncArgSlot,
2578 llvm::Value *CallTryExitVar,
2579 llvm::Value *ExceptionData,
2580 ObjCTypesHelper *ObjCTypes)
2581 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
2582 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2584 void Emit(CodeGenFunction &CGF, Flags flags) {
2585 // Check whether we need to call objc_exception_try_exit.
2586 // In optimized code, this branch will always be folded.
2587 llvm::BasicBlock *FinallyCallExit =
2588 CGF.createBasicBlock("finally.call_exit");
2589 llvm::BasicBlock *FinallyNoCallExit =
2590 CGF.createBasicBlock("finally.no_call_exit");
2591 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2592 FinallyCallExit, FinallyNoCallExit);
2594 CGF.EmitBlock(FinallyCallExit);
2595 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2596 ->setDoesNotThrow();
2598 CGF.EmitBlock(FinallyNoCallExit);
2600 if (isa<ObjCAtTryStmt>(S)) {
2601 if (const ObjCAtFinallyStmt* FinallyStmt =
2602 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2603 // Save the current cleanup destination in case there's
2604 // control flow inside the finally statement.
2605 llvm::Value *CurCleanupDest =
2606 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2608 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2610 if (CGF.HaveInsertPoint()) {
2611 CGF.Builder.CreateStore(CurCleanupDest,
2612 CGF.getNormalCleanupDestSlot());
2614 // Currently, the end of the cleanup must always exist.
2615 CGF.EnsureInsertPoint();
2619 // Emit objc_sync_exit(expr); as finally's sole statement for
2621 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
2622 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2623 ->setDoesNotThrow();
2628 class FragileHazards {
2629 CodeGenFunction &CGF;
2630 llvm::SmallVector<llvm::Value*, 20> Locals;
2631 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2633 llvm::InlineAsm *ReadHazard;
2634 llvm::InlineAsm *WriteHazard;
2636 llvm::FunctionType *GetAsmFnType();
2638 void collectLocals();
2639 void emitReadHazard(CGBuilderTy &Builder);
2642 FragileHazards(CodeGenFunction &CGF);
2644 void emitWriteHazard();
2645 void emitHazardsInNewBlocks();
2649 /// Create the fragile-ABI read and write hazards based on the current
2650 /// state of the function, which is presumed to be immediately prior
2651 /// to a @try block. These hazards are used to maintain correct
2652 /// semantics in the face of optimization and the fragile ABI's
2653 /// cavalier use of setjmp/longjmp.
2654 FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2657 if (Locals.empty()) return;
2659 // Collect all the blocks in the function.
2660 for (llvm::Function::iterator
2661 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2662 BlocksBeforeTry.insert(&*I);
2664 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2666 // Create a read hazard for the allocas. This inhibits dead-store
2667 // optimizations and forces the values to memory. This hazard is
2668 // inserted before any 'throwing' calls in the protected scope to
2669 // reflect the possibility that the variables might be read from the
2670 // catch block if the call throws.
2672 std::string Constraint;
2673 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2674 if (I) Constraint += ',';
2678 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2681 // Create a write hazard for the allocas. This inhibits folding
2682 // loads across the hazard. This hazard is inserted at the
2683 // beginning of the catch path to reflect the possibility that the
2684 // variables might have been written within the protected scope.
2686 std::string Constraint;
2687 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2688 if (I) Constraint += ',';
2689 Constraint += "=*m";
2692 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2696 /// Emit a write hazard at the current location.
2697 void FragileHazards::emitWriteHazard() {
2698 if (Locals.empty()) return;
2700 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
2703 void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2704 assert(!Locals.empty());
2705 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
2708 /// Emit read hazards in all the protected blocks, i.e. all the blocks
2709 /// which have been inserted since the beginning of the try.
2710 void FragileHazards::emitHazardsInNewBlocks() {
2711 if (Locals.empty()) return;
2713 CGBuilderTy Builder(CGF.getLLVMContext());
2715 // Iterate through all blocks, skipping those prior to the try.
2716 for (llvm::Function::iterator
2717 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2718 llvm::BasicBlock &BB = *FI;
2719 if (BlocksBeforeTry.count(&BB)) continue;
2721 // Walk through all the calls in the block.
2722 for (llvm::BasicBlock::iterator
2723 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2724 llvm::Instruction &I = *BI;
2726 // Ignore instructions that aren't non-intrinsic calls.
2727 // These are the only calls that can possibly call longjmp.
2728 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2729 if (isa<llvm::IntrinsicInst>(I))
2732 // Ignore call sites marked nounwind. This may be questionable,
2733 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2734 llvm::CallSite CS(&I);
2735 if (CS.doesNotThrow()) continue;
2737 // Insert a read hazard before the call. This will ensure that
2738 // any writes to the locals are performed before making the
2739 // call. If the call throws, then this is sufficient to
2740 // guarantee correctness as long as it doesn't also write to any
2742 Builder.SetInsertPoint(&BB, BI);
2743 emitReadHazard(Builder);
2748 static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2752 void FragileHazards::collectLocals() {
2753 // Compute a set of allocas to ignore.
2754 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2755 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2756 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2757 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2759 // Collect all the allocas currently in the function. This is
2760 // probably way too aggressive.
2761 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2762 for (llvm::BasicBlock::iterator
2763 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2764 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2765 Locals.push_back(&*I);
2768 llvm::FunctionType *FragileHazards::GetAsmFnType() {
2769 llvm::SmallVector<llvm::Type *, 16> tys(Locals.size());
2770 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
2771 tys[i] = Locals[i]->getType();
2772 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
2777 Objective-C setjmp-longjmp (sjlj) Exception Handling
2780 A catch buffer is a setjmp buffer plus:
2781 - a pointer to the exception that was caught
2782 - a pointer to the previous exception data buffer
2783 - two pointers of reserved storage
2784 Therefore catch buffers form a stack, with a pointer to the top
2785 of the stack kept in thread-local storage.
2787 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2788 objc_exception_try_exit pops the given catch buffer, which is
2789 required to be the top of the EH stack.
2790 objc_exception_throw pops the top of the EH stack, writes the
2791 thrown exception into the appropriate field, and longjmps
2792 to the setjmp buffer. It crashes the process (with a printf
2793 and an abort()) if there are no catch buffers on the stack.
2794 objc_exception_extract just reads the exception pointer out of the
2797 There's no reason an implementation couldn't use a light-weight
2798 setjmp here --- something like __builtin_setjmp, but API-compatible
2799 with the heavyweight setjmp. This will be more important if we ever
2800 want to implement correct ObjC/C++ exception interactions for the
2803 Note that for this use of setjmp/longjmp to be correct, we may need
2804 to mark some local variables volatile: if a non-volatile local
2805 variable is modified between the setjmp and the longjmp, it has
2806 indeterminate value. For the purposes of LLVM IR, it may be
2807 sufficient to make loads and stores within the @try (to variables
2808 declared outside the @try) volatile. This is necessary for
2809 optimized correctness, but is not currently being done; this is
2810 being tracked as rdar://problem/8160285
2812 The basic framework for a @try-catch-finally is as follows:
2814 objc_exception_data d;
2816 bool _call_try_exit = true;
2818 objc_exception_try_enter(&d);
2819 if (!setjmp(d.jmp_buf)) {
2823 id _caught = objc_exception_extract(&d);
2825 // enter new try scope for handlers
2826 if (!setjmp(d.jmp_buf)) {
2827 ... match exception and execute catch blocks ...
2829 // fell off end, rethrow.
2831 ... jump-through-finally to finally_rethrow ...
2833 // exception in catch block
2834 _rethrow = objc_exception_extract(&d);
2835 _call_try_exit = false;
2836 ... jump-through-finally to finally_rethrow ...
2839 ... jump-through-finally to finally_end ...
2843 objc_exception_try_exit(&d);
2845 ... finally block ....
2846 ... dispatch to finally destination ...
2849 objc_exception_throw(_rethrow);
2854 This framework differs slightly from the one gcc uses, in that gcc
2855 uses _rethrow to determine if objc_exception_try_exit should be called
2856 and if the object should be rethrown. This breaks in the face of
2857 throwing nil and introduces unnecessary branches.
2859 We specialize this framework for a few particular circumstances:
2861 - If there are no catch blocks, then we avoid emitting the second
2862 exception handling context.
2864 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2865 e)) we avoid emitting the code to rethrow an uncaught exception.
2867 - FIXME: If there is no @finally block we can do a few more
2870 Rethrows and Jumps-Through-Finally
2873 '@throw;' is supported by pushing the currently-caught exception
2874 onto ObjCEHStack while the @catch blocks are emitted.
2876 Branches through the @finally block are handled with an ordinary
2877 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2878 exceptions are not compatible with C++ exceptions, and this is
2879 hardly the only place where this will go wrong.
2881 @synchronized(expr) { stmt; } is emitted as if it were:
2882 id synch_value = expr;
2883 objc_sync_enter(synch_value);
2884 @try { stmt; } @finally { objc_sync_exit(synch_value); }
2887 void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2889 bool isTry = isa<ObjCAtTryStmt>(S);
2891 // A destination for the fall-through edges of the catch handlers to
2893 CodeGenFunction::JumpDest FinallyEnd =
2894 CGF.getJumpDestInCurrentScope("finally.end");
2896 // A destination for the rethrow edge of the catch handlers to jump
2898 CodeGenFunction::JumpDest FinallyRethrow =
2899 CGF.getJumpDestInCurrentScope("finally.rethrow");
2901 // For @synchronized, call objc_sync_enter(sync.expr). The
2902 // evaluation of the expression must occur before we enter the
2903 // @synchronized. We can't avoid a temp here because we need the
2904 // value to be preserved. If the backend ever does liveness
2905 // correctly after setjmp, this will be unnecessary.
2906 llvm::Value *SyncArgSlot = 0;
2908 llvm::Value *SyncArg =
2909 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2910 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
2911 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2912 ->setDoesNotThrow();
2914 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2915 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
2918 // Allocate memory for the setjmp buffer. This needs to be kept
2919 // live throughout the try and catch blocks.
2920 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2921 "exceptiondata.ptr");
2923 // Create the fragile hazards. Note that this will not capture any
2924 // of the allocas required for exception processing, but will
2925 // capture the current basic block (which extends all the way to the
2926 // setjmp call) as "before the @try".
2927 FragileHazards Hazards(CGF);
2929 // Create a flag indicating whether the cleanup needs to call
2930 // objc_exception_try_exit. This is true except when
2931 // - no catches match and we're branching through the cleanup
2932 // just to rethrow the exception, or
2933 // - a catch matched and we're falling out of the catch handler.
2934 // The setjmp-safety rule here is that we should always store to this
2935 // variable in a place that dominates the branch through the cleanup
2936 // without passing through any setjmps.
2937 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
2940 // A slot containing the exception to rethrow. Only needed when we
2941 // have both a @catch and a @finally.
2942 llvm::Value *PropagatingExnVar = 0;
2944 // Push a normal cleanup to leave the try scope.
2945 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
2951 // Enter a try block:
2952 // - Call objc_exception_try_enter to push ExceptionData on top of
2954 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2955 ->setDoesNotThrow();
2957 // - Call setjmp on the exception data buffer.
2958 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2959 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2960 llvm::Value *SetJmpBuffer =
2961 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
2962 llvm::CallInst *SetJmpResult =
2963 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2964 SetJmpResult->setDoesNotThrow();
2966 // If setjmp returned 0, enter the protected block; otherwise,
2967 // branch to the handler.
2968 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2969 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
2970 llvm::Value *DidCatch =
2971 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
2972 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
2974 // Emit the protected block.
2975 CGF.EmitBlock(TryBlock);
2976 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
2977 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
2978 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
2980 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
2982 // Emit the exception handler block.
2983 CGF.EmitBlock(TryHandler);
2985 // Don't optimize loads of the in-scope locals across this point.
2986 Hazards.emitWriteHazard();
2988 // For a @synchronized (or a @try with no catches), just branch
2989 // through the cleanup to the rethrow block.
2990 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
2991 // Tell the cleanup not to re-pop the exit.
2992 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
2993 CGF.EmitBranchThroughCleanup(FinallyRethrow);
2995 // Otherwise, we have to match against the caught exceptions.
2997 // Retrieve the exception object. We may emit multiple blocks but
2998 // nothing can cross this so the value is already in SSA form.
2999 llvm::CallInst *Caught =
3000 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3001 ExceptionData, "caught");
3002 Caught->setDoesNotThrow();
3004 // Push the exception to rethrow onto the EH value stack for the
3005 // benefit of any @throws in the handlers.
3006 CGF.ObjCEHValueStack.push_back(Caught);
3008 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
3010 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
3012 llvm::BasicBlock *CatchBlock = 0;
3013 llvm::BasicBlock *CatchHandler = 0;
3015 // Save the currently-propagating exception before
3016 // objc_exception_try_enter clears the exception slot.
3017 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3018 "propagating_exception");
3019 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3021 // Enter a new exception try block (in case a @catch block
3022 // throws an exception).
3023 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3024 ->setDoesNotThrow();
3026 llvm::CallInst *SetJmpResult =
3027 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3029 SetJmpResult->setDoesNotThrow();
3031 llvm::Value *Threw =
3032 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3034 CatchBlock = CGF.createBasicBlock("catch");
3035 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3036 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3038 CGF.EmitBlock(CatchBlock);
3041 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
3043 // Handle catch list. As a special case we check if everything is
3044 // matched and avoid generating code for falling off the end if
3046 bool AllMatched = false;
3047 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3048 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
3050 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
3051 const ObjCObjectPointerType *OPT = 0;
3053 // catch(...) always matches.
3057 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
3059 // catch(id e) always matches under this ABI, since only
3060 // ObjC exceptions end up here in the first place.
3061 // FIXME: For the time being we also match id<X>; this should
3062 // be rejected by Sema instead.
3063 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
3067 // If this is a catch-all, we don't need to test anything.
3069 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3072 CGF.EmitAutoVarDecl(*CatchParam);
3073 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
3075 // These types work out because ConvertType(id) == i8*.
3076 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
3079 CGF.EmitStmt(CatchStmt->getCatchBody());
3081 // The scope of the catch variable ends right here.
3082 CatchVarCleanups.ForceCleanup();
3084 CGF.EmitBranchThroughCleanup(FinallyEnd);
3088 assert(OPT && "Unexpected non-object pointer type in @catch");
3089 const ObjCObjectType *ObjTy = OPT->getObjectType();
3091 // FIXME: @catch (Class c) ?
3092 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3093 assert(IDecl && "Catch parameter must have Objective-C type!");
3095 // Check if the @catch block matches the exception object.
3096 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
3098 llvm::CallInst *Match =
3099 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3100 Class, Caught, "match");
3101 Match->setDoesNotThrow();
3103 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3104 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
3106 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
3107 MatchedBlock, NextCatchBlock);
3109 // Emit the @catch block.
3110 CGF.EmitBlock(MatchedBlock);
3112 // Collect any cleanups for the catch variable. The scope lasts until
3113 // the end of the catch body.
3114 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3116 CGF.EmitAutoVarDecl(*CatchParam);
3117 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
3119 // Initialize the catch variable.
3121 CGF.Builder.CreateBitCast(Caught,
3122 CGF.ConvertType(CatchParam->getType()),
3124 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
3126 CGF.EmitStmt(CatchStmt->getCatchBody());
3128 // We're done with the catch variable.
3129 CatchVarCleanups.ForceCleanup();
3131 CGF.EmitBranchThroughCleanup(FinallyEnd);
3133 CGF.EmitBlock(NextCatchBlock);
3136 CGF.ObjCEHValueStack.pop_back();
3138 // If nothing wanted anything to do with the caught exception,
3139 // kill the extract call.
3140 if (Caught->use_empty())
3141 Caught->eraseFromParent();
3144 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3147 // Emit the exception handler for the @catch blocks.
3148 CGF.EmitBlock(CatchHandler);
3150 // In theory we might now need a write hazard, but actually it's
3151 // unnecessary because there's no local-accessing code between
3152 // the try's write hazard and here.
3153 //Hazards.emitWriteHazard();
3155 // Extract the new exception and save it to the
3156 // propagating-exception slot.
3157 assert(PropagatingExnVar);
3158 llvm::CallInst *NewCaught =
3159 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3160 ExceptionData, "caught");
3161 NewCaught->setDoesNotThrow();
3162 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3164 // Don't pop the catch handler; the throw already did.
3165 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
3166 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3170 // Insert read hazards as required in the new blocks.
3171 Hazards.emitHazardsInNewBlocks();
3174 CGF.Builder.restoreIP(TryFallthroughIP);
3175 if (CGF.HaveInsertPoint())
3176 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
3177 CGF.PopCleanupBlock();
3178 CGF.EmitBlock(FinallyEnd.getBlock(), true);
3180 // Emit the rethrow block.
3181 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
3182 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
3183 if (CGF.HaveInsertPoint()) {
3184 // If we have a propagating-exception variable, check it.
3185 llvm::Value *PropagatingExn;
3186 if (PropagatingExnVar) {
3187 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
3189 // Otherwise, just look in the buffer for the exception to throw.
3191 llvm::CallInst *Caught =
3192 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3194 Caught->setDoesNotThrow();
3195 PropagatingExn = Caught;
3198 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
3199 ->setDoesNotThrow();
3200 CGF.Builder.CreateUnreachable();
3203 CGF.Builder.restoreIP(SavedIP);
3206 void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
3207 const ObjCAtThrowStmt &S) {
3208 llvm::Value *ExceptionAsObject;
3210 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3211 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
3213 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3215 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
3216 "Unexpected rethrow outside @catch block.");
3217 ExceptionAsObject = CGF.ObjCEHValueStack.back();
3220 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3221 ->setDoesNotReturn();
3222 CGF.Builder.CreateUnreachable();
3224 // Clear the insertion point to indicate we are in unreachable code.
3225 CGF.Builder.ClearInsertionPoint();
3228 /// EmitObjCWeakRead - Code gen for loading value of a __weak
3229 /// object: objc_read_weak (id *src)
3231 llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
3232 llvm::Value *AddrWeakObj) {
3233 const llvm::Type* DestTy =
3234 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3235 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3236 ObjCTypes.PtrObjectPtrTy);
3237 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
3238 AddrWeakObj, "weakread");
3239 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
3243 /// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3244 /// objc_assign_weak (id src, id *dst)
3246 void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
3247 llvm::Value *src, llvm::Value *dst) {
3248 const llvm::Type * SrcTy = src->getType();
3249 if (!isa<llvm::PointerType>(SrcTy)) {
3250 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
3251 assert(Size <= 8 && "does not support size > 8");
3252 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
3253 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
3254 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3256 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3257 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
3258 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
3259 src, dst, "weakassign");
3263 /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3264 /// objc_assign_global (id src, id *dst)
3266 void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
3267 llvm::Value *src, llvm::Value *dst,
3269 const llvm::Type * SrcTy = src->getType();
3270 if (!isa<llvm::PointerType>(SrcTy)) {
3271 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
3272 assert(Size <= 8 && "does not support size > 8");
3273 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
3274 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
3275 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3277 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3278 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
3280 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3281 src, dst, "globalassign");
3283 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3284 src, dst, "threadlocalassign");
3288 /// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
3289 /// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
3291 void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
3292 llvm::Value *src, llvm::Value *dst,
3293 llvm::Value *ivarOffset) {
3294 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
3295 const llvm::Type * SrcTy = src->getType();
3296 if (!isa<llvm::PointerType>(SrcTy)) {
3297 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
3298 assert(Size <= 8 && "does not support size > 8");
3299 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
3300 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
3301 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3303 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3304 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
3305 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3306 src, dst, ivarOffset);
3310 /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3311 /// objc_assign_strongCast (id src, id *dst)
3313 void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
3314 llvm::Value *src, llvm::Value *dst) {
3315 const llvm::Type * SrcTy = src->getType();
3316 if (!isa<llvm::PointerType>(SrcTy)) {
3317 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
3318 assert(Size <= 8 && "does not support size > 8");
3319 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
3320 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
3321 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3323 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3324 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
3325 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
3326 src, dst, "weakassign");
3330 void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
3331 llvm::Value *DestPtr,
3332 llvm::Value *SrcPtr,
3333 llvm::Value *size) {
3334 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3335 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
3336 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
3337 DestPtr, SrcPtr, size);
3341 /// EmitObjCValueForIvar - Code Gen for ivar reference.
3343 LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3345 llvm::Value *BaseValue,
3346 const ObjCIvarDecl *Ivar,
3347 unsigned CVRQualifiers) {
3348 const ObjCInterfaceDecl *ID =
3349 ObjectTy->getAs<ObjCObjectType>()->getInterface();
3350 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3351 EmitIvarOffset(CGF, ID, Ivar));
3354 llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
3355 const ObjCInterfaceDecl *Interface,
3356 const ObjCIvarDecl *Ivar) {
3357 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
3358 return llvm::ConstantInt::get(
3359 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3363 /* *** Private Interface *** */
3365 /// EmitImageInfo - Emit the image info marker used to encode some module
3366 /// level information.
3368 /// See: <rdr://4810609&4810587&4810587>
3369 /// struct IMAGE_INFO {
3370 /// unsigned version;
3373 enum ImageInfoFlags {
3374 eImageInfo_FixAndContinue = (1 << 0),
3375 eImageInfo_GarbageCollected = (1 << 1),
3376 eImageInfo_GCOnly = (1 << 2),
3377 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3379 // A flag indicating that the module has no instances of a @synthesize of a
3380 // superclass variable. <rdar://problem/6803242>
3381 eImageInfo_CorrectedSynthesize = (1 << 4)
3384 void CGObjCCommonMac::EmitImageInfo() {
3385 unsigned version = 0; // Version is unused?
3388 // FIXME: Fix and continue?
3389 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3390 flags |= eImageInfo_GarbageCollected;
3391 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3392 flags |= eImageInfo_GCOnly;
3394 // We never allow @synthesize of a superclass property.
3395 flags |= eImageInfo_CorrectedSynthesize;
3397 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3399 // Emitted as int[2];
3400 llvm::Constant *values[2] = {
3401 llvm::ConstantInt::get(Int32Ty, version),
3402 llvm::ConstantInt::get(Int32Ty, flags)
3404 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
3406 const char *Section;
3408 Section = "__OBJC, __image_info,regular";
3410 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
3411 llvm::GlobalVariable *GV =
3412 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
3413 llvm::ConstantArray::get(AT, values),
3417 GV->setConstant(true);
3421 // struct objc_module {
3422 // unsigned long version;
3423 // unsigned long size;
3424 // const char *name;
3428 // FIXME: Get from somewhere
3429 static const int ModuleVersion = 7;
3431 void CGObjCMac::EmitModuleInfo() {
3432 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
3434 std::vector<llvm::Constant*> Values(4);
3435 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3436 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
3437 // This used to be the filename, now it is unused. <rdr://4327263>
3438 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
3439 Values[3] = EmitModuleSymbols();
3440 CreateMetadataVar("\01L_OBJC_MODULES",
3441 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
3442 "__OBJC,__module_info,regular,no_dead_strip",
3446 llvm::Constant *CGObjCMac::EmitModuleSymbols() {
3447 unsigned NumClasses = DefinedClasses.size();
3448 unsigned NumCategories = DefinedCategories.size();
3450 // Return null if no symbols were defined.
3451 if (!NumClasses && !NumCategories)
3452 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
3454 llvm::Constant *Values[5];
3455 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
3456 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
3457 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3458 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
3460 // The runtime expects exactly the list of defined classes followed
3461 // by the list of defined categories, in a single array.
3462 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
3463 for (unsigned i=0; i<NumClasses; i++)
3464 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
3465 ObjCTypes.Int8PtrTy);
3466 for (unsigned i=0; i<NumCategories; i++)
3467 Symbols[NumClasses + i] =
3468 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
3469 ObjCTypes.Int8PtrTy);
3472 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
3473 NumClasses + NumCategories),
3476 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
3478 llvm::GlobalVariable *GV =
3479 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3480 "__OBJC,__symbols,regular,no_dead_strip",
3482 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
3485 llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3486 IdentifierInfo *II) {
3487 LazySymbols.insert(II);
3489 llvm::GlobalVariable *&Entry = ClassReferences[II];
3492 llvm::Constant *Casted =
3493 llvm::ConstantExpr::getBitCast(GetClassName(II),
3494 ObjCTypes.ClassPtrTy);
3496 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3497 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3501 return Builder.CreateLoad(Entry, "tmp");
3504 llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3505 const ObjCInterfaceDecl *ID) {
3506 return EmitClassRefFromId(Builder, ID->getIdentifier());
3509 llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3510 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3511 return EmitClassRefFromId(Builder, II);
3514 llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3516 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
3519 llvm::Constant *Casted =
3520 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
3521 ObjCTypes.SelectorPtrTy);
3523 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3524 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
3530 return Builder.CreateLoad(Entry, "tmp");
3533 llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
3534 llvm::GlobalVariable *&Entry = ClassNames[Ident];
3537 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3538 llvm::ConstantArray::get(VMContext,
3539 Ident->getNameStart()),
3541 "__TEXT,__objc_classname,cstring_literals" :
3542 "__TEXT,__cstring,cstring_literals"),
3545 return getConstantGEP(VMContext, Entry, 0, 0);
3548 llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3549 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3550 I = MethodDefinitions.find(MD);
3551 if (I != MethodDefinitions.end())
3554 if (MD->hasBody() && MD->getPCHLevel() > 0) {
3555 // MD isn't emitted yet because it comes from PCH.
3556 CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD));
3557 assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!");
3558 return MethodDefinitions[MD];
3564 /// GetIvarLayoutName - Returns a unique constant for the given
3565 /// ivar layout bitmap.
3566 llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
3567 const ObjCCommonTypesHelper &ObjCTypes) {
3568 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
3571 void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
3572 unsigned int BytePos,
3573 bool ForStrongLayout,
3575 const RecordDecl *RD = RT->getDecl();
3576 // FIXME - Use iterator.
3577 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
3578 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
3579 const llvm::StructLayout *RecLayout =
3580 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
3582 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3583 ForStrongLayout, HasUnion);
3586 void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
3587 const llvm::StructLayout *Layout,
3588 const RecordDecl *RD,
3589 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
3590 unsigned int BytePos, bool ForStrongLayout,
3592 bool IsUnion = (RD && RD->isUnion());
3593 uint64_t MaxUnionIvarSize = 0;
3594 uint64_t MaxSkippedUnionIvarSize = 0;
3595 FieldDecl *MaxField = 0;
3596 FieldDecl *MaxSkippedField = 0;
3597 FieldDecl *LastFieldBitfieldOrUnnamed = 0;
3598 uint64_t MaxFieldOffset = 0;
3599 uint64_t MaxSkippedFieldOffset = 0;
3600 uint64_t LastBitfieldOrUnnamedOffset = 0;
3601 uint64_t FirstFieldDelta = 0;
3603 if (RecFields.empty())
3605 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3606 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3607 if (!RD && CGM.getLangOptions().ObjCAutoRefCount) {
3608 FieldDecl *FirstField = RecFields[0];
3610 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3613 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
3614 FieldDecl *Field = RecFields[i];
3615 uint64_t FieldOffset;
3617 // Note that 'i' here is actually the field index inside RD of Field,
3618 // although this dependency is hidden.
3619 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3620 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
3623 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
3625 // Skip over unnamed or bitfields
3626 if (!Field->getIdentifier() || Field->isBitField()) {
3627 LastFieldBitfieldOrUnnamed = Field;
3628 LastBitfieldOrUnnamedOffset = FieldOffset;
3632 LastFieldBitfieldOrUnnamed = 0;
3633 QualType FQT = Field->getType();
3634 if (FQT->isRecordType() || FQT->isUnionType()) {
3635 if (FQT->isUnionType())
3638 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
3639 BytePos + FieldOffset,
3640 ForStrongLayout, HasUnion);
3644 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3645 const ConstantArrayType *CArray =
3646 dyn_cast_or_null<ConstantArrayType>(Array);
3647 uint64_t ElCount = CArray->getSize().getZExtValue();
3648 assert(CArray && "only array with known element size is supported");
3649 FQT = CArray->getElementType();
3650 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3651 const ConstantArrayType *CArray =
3652 dyn_cast_or_null<ConstantArrayType>(Array);
3653 ElCount *= CArray->getSize().getZExtValue();
3654 FQT = CArray->getElementType();
3657 assert(!FQT->isUnionType() &&
3658 "layout for array of unions not supported");
3659 if (FQT->isRecordType() && ElCount) {
3660 int OldIndex = IvarsInfo.size() - 1;
3661 int OldSkIndex = SkipIvars.size() -1;
3663 const RecordType *RT = FQT->getAs<RecordType>();
3664 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
3665 ForStrongLayout, HasUnion);
3667 // Replicate layout information for each array element. Note that
3668 // one element is already done.
3670 for (int FirstIndex = IvarsInfo.size() - 1,
3671 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
3672 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
3673 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3674 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3675 IvarsInfo[i].ivar_size));
3676 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3677 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3678 SkipIvars[i].ivar_size));
3683 // At this point, we are done with Record/Union and array there of.
3684 // For other arrays we are down to its element type.
3685 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
3687 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
3688 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3689 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
3691 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
3692 if (UnionIvarSize > MaxUnionIvarSize) {
3693 MaxUnionIvarSize = UnionIvarSize;
3695 MaxFieldOffset = FieldOffset;
3698 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
3699 FieldSize / WordSizeInBits));
3701 } else if ((ForStrongLayout &&
3702 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3703 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
3705 // FIXME: Why the asymmetry? We divide by word size in bits on other
3707 uint64_t UnionIvarSize = FieldSize;
3708 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
3709 MaxSkippedUnionIvarSize = UnionIvarSize;
3710 MaxSkippedField = Field;
3711 MaxSkippedFieldOffset = FieldOffset;
3714 // FIXME: Why the asymmetry, we divide by byte size in bits here?
3715 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
3716 FieldSize / ByteSizeInBits));
3721 if (LastFieldBitfieldOrUnnamed) {
3722 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3723 // Last field was a bitfield. Must update skip info.
3724 Expr *BitWidth = LastFieldBitfieldOrUnnamed->getBitWidth();
3725 uint64_t BitFieldSize =
3726 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
3728 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3729 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3730 + ((BitFieldSize % ByteSizeInBits) != 0);
3731 SkipIvars.push_back(skivar);
3733 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3734 // Last field was unnamed. Must update skip info.
3736 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3737 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3738 FieldSize / ByteSizeInBits));
3743 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
3745 if (MaxSkippedField)
3746 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
3747 MaxSkippedUnionIvarSize));
3750 /// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3751 /// the computations and returning the layout bitmap (for ivar or blocks) in
3752 /// the given argument BitMap string container. Routine reads
3753 /// two containers, IvarsInfo and SkipIvars which are assumed to be
3754 /// filled already by the caller.
3755 llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
3756 unsigned int WordsToScan, WordsToSkip;
3757 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3759 // Build the string of skip/scan nibbles
3760 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
3761 unsigned int WordSize =
3762 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
3763 if (IvarsInfo[0].ivar_bytepos == 0) {
3765 WordsToScan = IvarsInfo[0].ivar_size;
3767 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3768 WordsToScan = IvarsInfo[0].ivar_size;
3770 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
3771 unsigned int TailPrevGCObjC =
3772 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
3773 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
3774 // consecutive 'scanned' object pointers.
3775 WordsToScan += IvarsInfo[i].ivar_size;
3777 // Skip over 'gc'able object pointer which lay over each other.
3778 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3780 // Must skip over 1 or more words. We save current skip/scan values
3781 // and start a new pair.
3783 SkScan.skip = WordsToSkip;
3784 SkScan.scan = WordsToScan;
3785 SkipScanIvars.push_back(SkScan);
3788 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3790 SkipScanIvars.push_back(SkScan);
3792 WordsToScan = IvarsInfo[i].ivar_size;
3795 if (WordsToScan > 0) {
3797 SkScan.skip = WordsToSkip;
3798 SkScan.scan = WordsToScan;
3799 SkipScanIvars.push_back(SkScan);
3802 if (!SkipIvars.empty()) {
3803 unsigned int LastIndex = SkipIvars.size()-1;
3804 int LastByteSkipped =
3805 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
3806 LastIndex = IvarsInfo.size()-1;
3807 int LastByteScanned =
3808 IvarsInfo[LastIndex].ivar_bytepos +
3809 IvarsInfo[LastIndex].ivar_size * WordSize;
3810 // Compute number of bytes to skip at the tail end of the last ivar scanned.
3811 if (LastByteSkipped > LastByteScanned) {
3812 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
3814 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3816 SkipScanIvars.push_back(SkScan);
3819 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3821 int SkipScan = SkipScanIvars.size()-1;
3822 for (int i = 0; i <= SkipScan; i++) {
3823 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3824 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3825 // 0xM0 followed by 0x0N detected.
3826 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3827 for (int j = i+1; j < SkipScan; j++)
3828 SkipScanIvars[j] = SkipScanIvars[j+1];
3833 // Generate the string.
3834 for (int i = 0; i <= SkipScan; i++) {
3836 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3837 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3838 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3839 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
3842 for (unsigned int ix = 0; ix < skip_big; ix++)
3843 BitMap += (unsigned char)(0xf0);
3845 // next (skip small, scan)
3847 byte = skip_small << 4;
3851 } else if (scan_small) {
3858 for (unsigned int ix = 0; ix < scan_big; ix++)
3859 BitMap += (unsigned char)(0x0f);
3866 // null terminate string.
3867 unsigned char zero = 0;
3870 llvm::GlobalVariable * Entry =
3871 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3872 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
3874 "__TEXT,__objc_classname,cstring_literals" :
3875 "__TEXT,__cstring,cstring_literals"),
3877 return getConstantGEP(VMContext, Entry, 0, 0);
3880 /// BuildIvarLayout - Builds ivar layout bitmap for the class
3881 /// implementation for the __strong or __weak case.
3882 /// The layout map displays which words in ivar list must be skipped
3883 /// and which must be scanned by GC (see below). String is built of bytes.
3884 /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3885 /// of words to skip and right nibble is count of words to scan. So, each
3886 /// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3887 /// represented by a 0x00 byte which also ends the string.
3888 /// 1. when ForStrongLayout is true, following ivars are scanned:
3891 /// - __strong anything
3893 /// 2. When ForStrongLayout is false, following ivars are scanned:
3894 /// - __weak anything
3896 llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3897 const ObjCImplementationDecl *OMD,
3898 bool ForStrongLayout) {
3899 bool hasUnion = false;
3901 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3902 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC &&
3903 !CGM.getLangOptions().ObjCAutoRefCount)
3904 return llvm::Constant::getNullValue(PtrTy);
3906 ObjCInterfaceDecl *OI =
3907 const_cast<ObjCInterfaceDecl*>(OMD->getClassInterface());
3908 llvm::SmallVector<FieldDecl*, 32> RecFields;
3909 if (CGM.getLangOptions().ObjCAutoRefCount) {
3910 for (ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
3911 IVD; IVD = IVD->getNextIvar())
3912 RecFields.push_back(cast<FieldDecl>(IVD));
3915 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
3916 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
3918 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3919 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3922 if (RecFields.empty())
3923 return llvm::Constant::getNullValue(PtrTy);
3928 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3929 if (IvarsInfo.empty())
3930 return llvm::Constant::getNullValue(PtrTy);
3931 // Sort on byte position in case we encounterred a union nested in
3933 if (hasUnion && !IvarsInfo.empty())
3934 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3935 if (hasUnion && !SkipIvars.empty())
3936 std::sort(SkipIvars.begin(), SkipIvars.end());
3939 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
3941 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
3942 printf("\n%s ivar layout for class '%s': ",
3943 ForStrongLayout ? "strong" : "weak",
3944 OMD->getClassInterface()->getName().data());
3945 const unsigned char *s = (unsigned char*)BitMap.c_str();
3946 for (unsigned i = 0; i < BitMap.size(); i++)
3948 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3950 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3956 llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
3957 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3959 // FIXME: Avoid std::string copying.
3961 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
3962 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
3964 "__TEXT,__objc_methname,cstring_literals" :
3965 "__TEXT,__cstring,cstring_literals"),
3968 return getConstantGEP(VMContext, Entry, 0, 0);
3971 // FIXME: Merge into a single cstring creation function.
3972 llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
3973 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3976 llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
3977 std::string TypeStr;
3978 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3980 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3983 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
3984 llvm::ConstantArray::get(VMContext, TypeStr),
3986 "__TEXT,__objc_methtype,cstring_literals" :
3987 "__TEXT,__cstring,cstring_literals"),
3990 return getConstantGEP(VMContext, Entry, 0, 0);
3993 llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
3994 std::string TypeStr;
3995 if (CGM.getContext().getObjCEncodingForMethodDecl(
3996 const_cast<ObjCMethodDecl*>(D),
4000 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4003 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
4004 llvm::ConstantArray::get(VMContext, TypeStr),
4006 "__TEXT,__objc_methtype,cstring_literals" :
4007 "__TEXT,__cstring,cstring_literals"),
4010 return getConstantGEP(VMContext, Entry, 0, 0);
4013 // FIXME: Merge into a single cstring creation function.
4014 llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
4015 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
4018 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
4019 llvm::ConstantArray::get(VMContext,
4020 Ident->getNameStart()),
4021 "__TEXT,__cstring,cstring_literals",
4024 return getConstantGEP(VMContext, Entry, 0, 0);
4027 // FIXME: Merge into a single cstring creation function.
4028 // FIXME: This Decl should be more precise.
4030 CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4031 const Decl *Container) {
4032 std::string TypeStr;
4033 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
4034 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4037 void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
4038 const ObjCContainerDecl *CD,
4039 llvm::SmallVectorImpl<char> &Name) {
4040 llvm::raw_svector_ostream OS(Name);
4041 assert (CD && "Missing container decl in GetNameForMethod");
4042 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4043 << '[' << CD->getName();
4044 if (const ObjCCategoryImplDecl *CID =
4045 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
4046 OS << '(' << CID << ')';
4047 OS << ' ' << D->getSelector().getAsString() << ']';
4050 void CGObjCMac::FinishModule() {
4053 // Emit the dummy bodies for any protocols which were referenced but
4055 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
4056 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4057 if (I->second->hasInitializer())
4060 std::vector<llvm::Constant*> Values(5);
4061 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
4062 Values[1] = GetClassName(I->first);
4063 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
4064 Values[3] = Values[4] =
4065 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
4066 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
4067 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
4069 CGM.AddUsedGlobal(I->second);
4072 // Add assembler directives to add lazy undefined symbol references
4073 // for classes which are referenced but not defined. This is
4074 // important for correct linker interaction.
4076 // FIXME: It would be nice if we had an LLVM construct for this.
4077 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4078 llvm::SmallString<256> Asm;
4079 Asm += CGM.getModule().getModuleInlineAsm();
4080 if (!Asm.empty() && Asm.back() != '\n')
4083 llvm::raw_svector_ostream OS(Asm);
4084 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4085 e = DefinedSymbols.end(); I != e; ++I)
4086 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4087 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
4088 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
4089 e = LazySymbols.end(); I != e; ++I) {
4090 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
4093 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4094 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4095 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4098 CGM.getModule().setModuleInlineAsm(OS.str());
4102 CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
4103 : CGObjCCommonMac(cgm),
4105 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
4111 ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
4112 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
4113 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4114 ASTContext &Ctx = CGM.getContext();
4116 ShortTy = Types.ConvertType(Ctx.ShortTy);
4117 IntTy = Types.ConvertType(Ctx.IntTy);
4118 LongTy = Types.ConvertType(Ctx.LongTy);
4119 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
4120 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
4122 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
4123 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
4124 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
4126 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4127 // comes out a bit cleaner.
4128 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
4129 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
4131 // I'm not sure I like this. The implicit coordination is a bit
4132 // gross. We should solve this in a reasonable fashion because this
4133 // is a pretty common task (match some runtime data structure with
4134 // an LLVM data structure).
4136 // FIXME: This is leaked.
4137 // FIXME: Merge with rewriter code?
4139 // struct _objc_super {
4143 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
4144 Ctx.getTranslationUnitDecl(),
4145 SourceLocation(), SourceLocation(),
4146 &Ctx.Idents.get("_objc_super"));
4147 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
4148 Ctx.getObjCIdType(), 0, 0, false, false));
4149 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
4150 Ctx.getObjCClassType(), 0, 0, false, false));
4151 RD->completeDefinition();
4153 SuperCTy = Ctx.getTagDeclType(RD);
4154 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
4156 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
4157 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4161 // char *attributes;
4163 PropertyTy = llvm::StructType::createNamed("struct._prop_t",
4164 Int8PtrTy, Int8PtrTy, NULL);
4166 // struct _prop_list_t {
4167 // uint32_t entsize; // sizeof(struct _prop_t)
4168 // uint32_t count_of_properties;
4169 // struct _prop_t prop_list[count_of_properties];
4172 llvm::StructType::createNamed("struct._prop_list_t",
4174 llvm::ArrayType::get(PropertyTy, 0),
4176 // struct _prop_list_t *
4177 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
4179 // struct _objc_method {
4181 // char *method_type;
4184 MethodTy = llvm::StructType::createNamed("struct._objc_method",
4185 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
4188 // struct _objc_cache *
4189 CacheTy = llvm::StructType::createNamed(VMContext, "struct._objc_cache");
4190 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
4194 ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
4195 : ObjCCommonTypesHelper(cgm) {
4196 // struct _objc_method_description {
4200 MethodDescriptionTy =
4201 llvm::StructType::createNamed("struct._objc_method_description",
4202 SelectorPtrTy, Int8PtrTy, NULL);
4204 // struct _objc_method_description_list {
4206 // struct _objc_method_description[1];
4208 MethodDescriptionListTy =
4209 llvm::StructType::createNamed("struct._objc_method_description_list",
4211 llvm::ArrayType::get(MethodDescriptionTy, 0),
4214 // struct _objc_method_description_list *
4215 MethodDescriptionListPtrTy =
4216 llvm::PointerType::getUnqual(MethodDescriptionListTy);
4218 // Protocol description structures
4220 // struct _objc_protocol_extension {
4221 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4222 // struct _objc_method_description_list *optional_instance_methods;
4223 // struct _objc_method_description_list *optional_class_methods;
4224 // struct _objc_property_list *instance_properties;
4226 ProtocolExtensionTy =
4227 llvm::StructType::createNamed("struct._objc_protocol_extension",
4229 MethodDescriptionListPtrTy,
4230 MethodDescriptionListPtrTy,
4234 // struct _objc_protocol_extension *
4235 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
4237 // Handle recursive construction of Protocol and ProtocolList types
4240 llvm::StructType::createNamed(VMContext, "struct._objc_protocol");
4243 llvm::StructType::createNamed(VMContext, "struct._objc_protocol_list");
4244 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
4246 llvm::ArrayType::get(ProtocolTy, 0),
4249 // struct _objc_protocol {
4250 // struct _objc_protocol_extension *isa;
4251 // char *protocol_name;
4252 // struct _objc_protocol **_objc_protocol_list;
4253 // struct _objc_method_description_list *instance_methods;
4254 // struct _objc_method_description_list *class_methods;
4256 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
4257 llvm::PointerType::getUnqual(ProtocolListTy),
4258 MethodDescriptionListPtrTy,
4259 MethodDescriptionListPtrTy,
4262 // struct _objc_protocol_list *
4263 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
4265 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
4267 // Class description structures
4269 // struct _objc_ivar {
4274 IvarTy = llvm::StructType::createNamed("struct._objc_ivar",
4275 Int8PtrTy, Int8PtrTy, IntTy, NULL);
4277 // struct _objc_ivar_list *
4279 llvm::StructType::createNamed(VMContext, "struct._objc_ivar_list");
4280 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
4282 // struct _objc_method_list *
4284 llvm::StructType::createNamed(VMContext, "struct._objc_method_list");
4285 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
4287 // struct _objc_class_extension *
4289 llvm::StructType::createNamed("struct._objc_class_extension",
4290 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
4291 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
4293 ClassTy = llvm::StructType::createNamed(VMContext, "struct._objc_class");
4295 // struct _objc_class {
4297 // Class super_class;
4301 // long instance_size;
4302 // struct _objc_ivar_list *ivars;
4303 // struct _objc_method_list *methods;
4304 // struct _objc_cache *cache;
4305 // struct _objc_protocol_list *protocols;
4306 // char *ivar_layout;
4307 // struct _objc_class_ext *ext;
4309 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
4310 llvm::PointerType::getUnqual(ClassTy),
4320 ClassExtensionPtrTy,
4323 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
4325 // struct _objc_category {
4326 // char *category_name;
4327 // char *class_name;
4328 // struct _objc_method_list *instance_method;
4329 // struct _objc_method_list *class_method;
4330 // uint32_t size; // sizeof(struct _objc_category)
4331 // struct _objc_property_list *instance_properties;// category's @property
4334 llvm::StructType::createNamed("struct._objc_category",
4335 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4336 MethodListPtrTy, ProtocolListPtrTy,
4337 IntTy, PropertyListPtrTy, NULL);
4339 // Global metadata structures
4341 // struct _objc_symtab {
4342 // long sel_ref_cnt;
4344 // short cls_def_cnt;
4345 // short cat_def_cnt;
4346 // char *defs[cls_def_cnt + cat_def_cnt];
4349 llvm::StructType::createNamed("struct._objc_symtab",
4350 LongTy, SelectorPtrTy, ShortTy, ShortTy,
4351 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
4352 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
4354 // struct _objc_module {
4356 // long size; // sizeof(struct _objc_module)
4358 // struct _objc_symtab* symtab;
4361 llvm::StructType::createNamed("struct._objc_module",
4362 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
4365 // FIXME: This is the size of the setjmp buffer and should be target
4366 // specific. 18 is what's used on 32-bit X86.
4367 uint64_t SetJmpBufferSize = 18;
4370 llvm::Type *StackPtrTy = llvm::ArrayType::get(
4371 llvm::Type::getInt8PtrTy(VMContext), 4);
4374 llvm::StructType::createNamed("struct._objc_exception_data",
4375 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4381 ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
4382 : ObjCCommonTypesHelper(cgm) {
4383 // struct _method_list_t {
4384 // uint32_t entsize; // sizeof(struct _objc_method)
4385 // uint32_t method_count;
4386 // struct _objc_method method_list[method_count];
4389 llvm::StructType::createNamed("struct.__method_list_t",
4391 llvm::ArrayType::get(MethodTy, 0),
4393 // struct method_list_t *
4394 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
4396 // struct _protocol_t {
4398 // const char * const protocol_name;
4399 // const struct _protocol_list_t * protocol_list; // super protocols
4400 // const struct method_list_t * const instance_methods;
4401 // const struct method_list_t * const class_methods;
4402 // const struct method_list_t *optionalInstanceMethods;
4403 // const struct method_list_t *optionalClassMethods;
4404 // const struct _prop_list_t * properties;
4405 // const uint32_t size; // sizeof(struct _protocol_t)
4406 // const uint32_t flags; // = 0
4409 // Holder for struct _protocol_list_t *
4410 ProtocolListnfABITy =
4411 llvm::StructType::createNamed(VMContext, "struct._objc_protocol_list");
4414 llvm::StructType::createNamed("struct._protocol_t",
4415 ObjectPtrTy, Int8PtrTy,
4416 llvm::PointerType::getUnqual(ProtocolListnfABITy),
4417 MethodListnfABIPtrTy,
4418 MethodListnfABIPtrTy,
4419 MethodListnfABIPtrTy,
4420 MethodListnfABIPtrTy,
4426 // struct _protocol_t*
4427 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
4429 // struct _protocol_list_t {
4430 // long protocol_count; // Note, this is 32/64 bit
4431 // struct _protocol_t *[protocol_count];
4433 ProtocolListnfABITy->setBody(LongTy,
4434 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
4437 // struct _objc_protocol_list*
4438 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
4441 // unsigned long int *offset; // pointer to ivar offset location
4444 // uint32_t alignment;
4448 llvm::StructType::createNamed("struct._ivar_t",
4449 llvm::PointerType::getUnqual(LongTy),
4456 // struct _ivar_list_t {
4457 // uint32 entsize; // sizeof(struct _ivar_t)
4459 // struct _iver_t list[count];
4462 llvm::StructType::createNamed("struct._ivar_list_t",
4464 llvm::ArrayType::get(IvarnfABITy, 0),
4467 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
4469 // struct _class_ro_t {
4470 // uint32_t const flags;
4471 // uint32_t const instanceStart;
4472 // uint32_t const instanceSize;
4473 // uint32_t const reserved; // only when building for 64bit targets
4474 // const uint8_t * const ivarLayout;
4475 // const char *const name;
4476 // const struct _method_list_t * const baseMethods;
4477 // const struct _objc_protocol_list *const baseProtocols;
4478 // const struct _ivar_list_t *const ivars;
4479 // const uint8_t * const weakIvarLayout;
4480 // const struct _prop_list_t * const properties;
4483 // FIXME. Add 'reserved' field in 64bit abi mode!
4484 ClassRonfABITy = llvm::StructType::createNamed("struct._class_ro_t",
4490 MethodListnfABIPtrTy,
4491 ProtocolListnfABIPtrTy,
4497 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4498 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
4499 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4502 // struct _class_t {
4503 // struct _class_t *isa;
4504 // struct _class_t * const superclass;
4507 // struct class_ro_t *ro;
4510 ClassnfABITy = llvm::StructType::createNamed(VMContext, "struct._class_t");
4511 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
4512 llvm::PointerType::getUnqual(ClassnfABITy),
4514 llvm::PointerType::getUnqual(ImpnfABITy),
4515 llvm::PointerType::getUnqual(ClassRonfABITy),
4518 // LLVM for struct _class_t *
4519 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
4521 // struct _category_t {
4522 // const char * const name;
4523 // struct _class_t *const cls;
4524 // const struct _method_list_t * const instance_methods;
4525 // const struct _method_list_t * const class_methods;
4526 // const struct _protocol_list_t * const protocols;
4527 // const struct _prop_list_t * const properties;
4529 CategorynfABITy = llvm::StructType::createNamed("struct._category_t",
4532 MethodListnfABIPtrTy,
4533 MethodListnfABIPtrTy,
4534 ProtocolListnfABIPtrTy,
4538 // New types for nonfragile abi messaging.
4539 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4540 ASTContext &Ctx = CGM.getContext();
4542 // MessageRefTy - LLVM for:
4543 // struct _message_ref_t {
4548 // First the clang type for struct _message_ref_t
4549 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
4550 Ctx.getTranslationUnitDecl(),
4551 SourceLocation(), SourceLocation(),
4552 &Ctx.Idents.get("_message_ref_t"));
4553 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
4554 Ctx.VoidPtrTy, 0, 0, false, false));
4555 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
4556 Ctx.getObjCSelType(), 0, 0, false, false));
4557 RD->completeDefinition();
4559 MessageRefCTy = Ctx.getTagDeclType(RD);
4560 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4561 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
4563 // MessageRefPtrTy - LLVM for struct _message_ref_t*
4564 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
4566 // SuperMessageRefTy - LLVM for:
4567 // struct _super_message_ref_t {
4568 // SUPER_IMP messenger;
4572 llvm::StructType::createNamed("struct._super_message_ref_t",
4573 ImpnfABITy, SelectorPtrTy, NULL);
4575 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
4576 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4579 // struct objc_typeinfo {
4580 // const void** vtable; // objc_ehtype_vtable + 2
4581 // const char* name; // c++ typeinfo string
4585 llvm::StructType::createNamed("struct._objc_typeinfo",
4586 llvm::PointerType::getUnqual(Int8PtrTy),
4590 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
4593 llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
4594 FinishNonFragileABIModule();
4599 void CGObjCNonFragileABIMac::AddModuleClassList(const
4600 std::vector<llvm::GlobalValue*>
4602 const char *SymbolName,
4603 const char *SectionName) {
4604 unsigned NumClasses = Container.size();
4609 std::vector<llvm::Constant*> Symbols(NumClasses);
4610 for (unsigned i=0; i<NumClasses; i++)
4611 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
4612 ObjCTypes.Int8PtrTy);
4613 llvm::Constant* Init =
4614 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
4618 llvm::GlobalVariable *GV =
4619 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
4620 llvm::GlobalValue::InternalLinkage,
4623 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
4624 GV->setSection(SectionName);
4625 CGM.AddUsedGlobal(GV);
4628 void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4629 // nonfragile abi has no module definition.
4631 // Build list of all implemented class addresses in array
4632 // L_OBJC_LABEL_CLASS_$.
4633 AddModuleClassList(DefinedClasses,
4634 "\01L_OBJC_LABEL_CLASS_$",
4635 "__DATA, __objc_classlist, regular, no_dead_strip");
4637 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4638 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4639 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4641 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4644 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4645 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4646 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4648 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4651 AddModuleClassList(DefinedNonLazyClasses,
4652 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4653 "__DATA, __objc_nlclslist, regular, no_dead_strip");
4655 // Build list of all implemented category addresses in array
4656 // L_OBJC_LABEL_CATEGORY_$.
4657 AddModuleClassList(DefinedCategories,
4658 "\01L_OBJC_LABEL_CATEGORY_$",
4659 "__DATA, __objc_catlist, regular, no_dead_strip");
4660 AddModuleClassList(DefinedNonLazyCategories,
4661 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4662 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
4667 /// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4668 /// VTableDispatchMethods; false otherwise. What this means is that
4669 /// except for the 19 selectors in the list, we generate 32bit-style
4670 /// message dispatch call for all the rest.
4671 bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4672 // At various points we've experimented with using vtable-based
4673 // dispatch for all methods.
4674 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4676 llvm_unreachable("Invalid dispatch method!");
4677 case CodeGenOptions::Legacy:
4679 case CodeGenOptions::NonLegacy:
4681 case CodeGenOptions::Mixed:
4685 // If so, see whether this selector is in the white-list of things which must
4686 // use the new dispatch convention. We lazily build a dense set for this.
4687 if (VTableDispatchMethods.empty()) {
4688 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4689 VTableDispatchMethods.insert(GetNullarySelector("class"));
4690 VTableDispatchMethods.insert(GetNullarySelector("self"));
4691 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4692 VTableDispatchMethods.insert(GetNullarySelector("length"));
4693 VTableDispatchMethods.insert(GetNullarySelector("count"));
4695 // These are vtable-based if GC is disabled.
4696 // Optimistically use vtable dispatch for hybrid compiles.
4697 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly) {
4698 VTableDispatchMethods.insert(GetNullarySelector("retain"));
4699 VTableDispatchMethods.insert(GetNullarySelector("release"));
4700 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4703 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4704 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4705 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4706 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4707 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4708 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4709 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4711 // These are vtable-based if GC is enabled.
4712 // Optimistically use vtable dispatch for hybrid compiles.
4713 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) {
4714 VTableDispatchMethods.insert(GetNullarySelector("hash"));
4715 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4717 // "countByEnumeratingWithState:objects:count"
4718 IdentifierInfo *KeyIdents[] = {
4719 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4720 &CGM.getContext().Idents.get("objects"),
4721 &CGM.getContext().Idents.get("count")
4723 VTableDispatchMethods.insert(
4724 CGM.getContext().Selectors.getSelector(3, KeyIdents));
4728 return VTableDispatchMethods.count(Sel);
4732 enum MetaDataDlags {
4736 OBJC2_CLS_HIDDEN = 0x10,
4737 CLS_EXCEPTION = 0x20,
4739 /// (Obsolete) ARC-specific: this class has a .release_ivars method
4740 CLS_HAS_IVAR_RELEASER = 0x40,
4741 /// class was compiled with -fobjc-arr
4742 CLS_COMPILED_BY_ARC = 0x80 // (1<<7)
4744 /// BuildClassRoTInitializer - generate meta-data for:
4745 /// struct _class_ro_t {
4746 /// uint32_t const flags;
4747 /// uint32_t const instanceStart;
4748 /// uint32_t const instanceSize;
4749 /// uint32_t const reserved; // only when building for 64bit targets
4750 /// const uint8_t * const ivarLayout;
4751 /// const char *const name;
4752 /// const struct _method_list_t * const baseMethods;
4753 /// const struct _protocol_list_t *const baseProtocols;
4754 /// const struct _ivar_list_t *const ivars;
4755 /// const uint8_t * const weakIvarLayout;
4756 /// const struct _prop_list_t * const properties;
4759 llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
4761 unsigned InstanceStart,
4762 unsigned InstanceSize,
4763 const ObjCImplementationDecl *ID) {
4764 std::string ClassName = ID->getNameAsString();
4765 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
4767 if (CGM.getLangOptions().ObjCAutoRefCount)
4768 flags |= CLS_COMPILED_BY_ARC;
4770 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4771 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4772 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
4773 // FIXME. For 64bit targets add 0 here.
4774 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4775 : BuildIvarLayout(ID, true);
4776 Values[ 4] = GetClassName(ID->getIdentifier());
4777 // const struct _method_list_t * const baseMethods;
4778 std::vector<llvm::Constant*> Methods;
4779 std::string MethodListName("\01l_OBJC_$_");
4780 if (flags & CLS_META) {
4781 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
4782 for (ObjCImplementationDecl::classmeth_iterator
4783 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
4784 // Class methods should always be defined.
4785 Methods.push_back(GetMethodConstant(*i));
4788 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
4789 for (ObjCImplementationDecl::instmeth_iterator
4790 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
4791 // Instance methods should always be defined.
4792 Methods.push_back(GetMethodConstant(*i));
4794 for (ObjCImplementationDecl::propimpl_iterator
4795 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
4796 ObjCPropertyImplDecl *PID = *i;
4798 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4799 ObjCPropertyDecl *PD = PID->getPropertyDecl();
4801 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4802 if (llvm::Constant *C = GetMethodConstant(MD))
4803 Methods.push_back(C);
4804 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4805 if (llvm::Constant *C = GetMethodConstant(MD))
4806 Methods.push_back(C);
4810 Values[ 5] = EmitMethodList(MethodListName,
4811 "__DATA, __objc_const", Methods);
4813 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4814 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
4815 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
4817 OID->all_referenced_protocol_begin(),
4818 OID->all_referenced_protocol_end());
4820 if (flags & CLS_META)
4821 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4823 Values[ 7] = EmitIvarList(ID);
4824 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4825 : BuildIvarLayout(ID, false);
4826 if (flags & CLS_META)
4827 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4829 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4830 ID, ID->getClassInterface(), ObjCTypes);
4831 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
4833 llvm::GlobalVariable *CLASS_RO_GV =
4834 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4835 llvm::GlobalValue::InternalLinkage,
4837 (flags & CLS_META) ?
4838 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4839 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
4840 CLASS_RO_GV->setAlignment(
4841 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
4842 CLASS_RO_GV->setSection("__DATA, __objc_const");
4847 /// BuildClassMetaData - This routine defines that to-level meta-data
4848 /// for the given ClassName for:
4849 /// struct _class_t {
4850 /// struct _class_t *isa;
4851 /// struct _class_t * const superclass;
4854 /// struct class_ro_t *ro;
4857 llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
4858 std::string &ClassName,
4859 llvm::Constant *IsAGV,
4860 llvm::Constant *SuperClassGV,
4861 llvm::Constant *ClassRoGV,
4862 bool HiddenVisibility) {
4863 std::vector<llvm::Constant*> Values(5);
4865 Values[1] = SuperClassGV;
4867 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
4868 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4869 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4870 Values[4] = ClassRoGV; // &CLASS_RO_GV
4871 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
4873 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4874 GV->setInitializer(Init);
4875 GV->setSection("__DATA, __objc_data");
4877 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
4878 if (HiddenVisibility)
4879 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4884 CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
4885 return OD->getClassMethod(GetNullarySelector("load")) != 0;
4888 void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
4889 uint32_t &InstanceStart,
4890 uint32_t &InstanceSize) {
4891 const ASTRecordLayout &RL =
4892 CGM.getContext().getASTObjCImplementationLayout(OID);
4894 // InstanceSize is really instance end.
4895 InstanceSize = RL.getDataSize().getQuantity();
4897 // If there are no fields, the start is the same as the end.
4898 if (!RL.getFieldCount())
4899 InstanceStart = InstanceSize;
4901 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
4904 void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4905 std::string ClassName = ID->getNameAsString();
4906 if (!ObjCEmptyCacheVar) {
4907 ObjCEmptyCacheVar = new llvm::GlobalVariable(
4911 llvm::GlobalValue::ExternalLinkage,
4913 "_objc_empty_cache");
4915 ObjCEmptyVtableVar = new llvm::GlobalVariable(
4917 ObjCTypes.ImpnfABITy,
4919 llvm::GlobalValue::ExternalLinkage,
4921 "_objc_empty_vtable");
4923 assert(ID->getClassInterface() &&
4924 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
4925 // FIXME: Is this correct (that meta class size is never computed)?
4926 uint32_t InstanceStart =
4927 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
4928 uint32_t InstanceSize = InstanceStart;
4929 uint32_t flags = CLS_META;
4930 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4931 std::string ObjCClassName(getClassSymbolPrefix());
4933 llvm::GlobalVariable *SuperClassGV, *IsAGV;
4935 bool classIsHidden =
4936 ID->getClassInterface()->getVisibility() == HiddenVisibility;
4938 flags |= OBJC2_CLS_HIDDEN;
4939 if (ID->hasCXXStructors())
4940 flags |= eClassFlags_ABI2_HasCXXStructors;
4941 if (!ID->getClassInterface()->getSuperClass()) {
4944 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
4945 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
4947 // Has a root. Current class is not a root.
4948 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4949 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4951 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
4952 if (Root->isWeakImported())
4953 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
4954 // work on super class metadata symbol.
4955 std::string SuperClassName =
4957 ID->getClassInterface()->getSuperClass()->getNameAsString();
4958 SuperClassGV = GetClassGlobal(SuperClassName);
4959 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
4960 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
4962 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4965 std::string TClassName = ObjCMetaClassName + ClassName;
4966 llvm::GlobalVariable *MetaTClass =
4967 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4969 DefinedMetaClasses.push_back(MetaTClass);
4971 // Metadata for the class
4974 flags |= OBJC2_CLS_HIDDEN;
4975 if (ID->hasCXXStructors())
4976 flags |= eClassFlags_ABI2_HasCXXStructors;
4978 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
4979 flags |= CLS_EXCEPTION;
4981 if (!ID->getClassInterface()->getSuperClass()) {
4985 // Has a root. Current class is not a root.
4986 std::string RootClassName =
4987 ID->getClassInterface()->getSuperClass()->getNameAsString();
4988 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
4989 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
4990 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
4992 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
4993 CLASS_RO_GV = BuildClassRoTInitializer(flags,
4998 TClassName = ObjCClassName + ClassName;
4999 llvm::GlobalVariable *ClassMD =
5000 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5002 DefinedClasses.push_back(ClassMD);
5004 // Determine if this class is also "non-lazy".
5005 if (ImplementationIsNonLazy(ID))
5006 DefinedNonLazyClasses.push_back(ClassMD);
5008 // Force the definition of the EHType if necessary.
5009 if (flags & CLS_EXCEPTION)
5010 GetInterfaceEHType(ID->getClassInterface(), true);
5011 // Make sure method definition entries are all clear for next implementation.
5012 MethodDefinitions.clear();
5015 /// GenerateProtocolRef - This routine is called to generate code for
5016 /// a protocol reference expression; as in:
5018 /// @protocol(Proto1);
5020 /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5021 /// which will hold address of the protocol meta-data.
5023 llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
5024 const ObjCProtocolDecl *PD) {
5026 // This routine is called for @protocol only. So, we must build definition
5027 // of protocol's meta-data (not a reference to it!)
5029 llvm::Constant *Init =
5030 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5031 ObjCTypes.ExternalProtocolPtrTy);
5033 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
5034 ProtocolName += PD->getName();
5036 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5038 return Builder.CreateLoad(PTGV, "tmp");
5039 PTGV = new llvm::GlobalVariable(
5041 Init->getType(), false,
5042 llvm::GlobalValue::WeakAnyLinkage,
5045 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5046 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
5047 CGM.AddUsedGlobal(PTGV);
5048 return Builder.CreateLoad(PTGV, "tmp");
5051 /// GenerateCategory - Build metadata for a category implementation.
5052 /// struct _category_t {
5053 /// const char * const name;
5054 /// struct _class_t *const cls;
5055 /// const struct _method_list_t * const instance_methods;
5056 /// const struct _method_list_t * const class_methods;
5057 /// const struct _protocol_list_t * const protocols;
5058 /// const struct _prop_list_t * const properties;
5061 void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
5062 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
5063 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
5064 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5065 "_$_" + OCD->getNameAsString());
5066 std::string ExtClassName(getClassSymbolPrefix() +
5067 Interface->getNameAsString());
5069 std::vector<llvm::Constant*> Values(6);
5070 Values[0] = GetClassName(OCD->getIdentifier());
5071 // meta-class entry symbol
5072 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
5073 if (Interface->isWeakImported())
5074 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5076 Values[1] = ClassGV;
5077 std::vector<llvm::Constant*> Methods;
5078 std::string MethodListName(Prefix);
5079 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
5080 "_$_" + OCD->getNameAsString();
5082 for (ObjCCategoryImplDecl::instmeth_iterator
5083 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
5084 // Instance methods should always be defined.
5085 Methods.push_back(GetMethodConstant(*i));
5088 Values[2] = EmitMethodList(MethodListName,
5089 "__DATA, __objc_const",
5092 MethodListName = Prefix;
5093 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5094 OCD->getNameAsString();
5096 for (ObjCCategoryImplDecl::classmeth_iterator
5097 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
5098 // Class methods should always be defined.
5099 Methods.push_back(GetMethodConstant(*i));
5102 Values[3] = EmitMethodList(MethodListName,
5103 "__DATA, __objc_const",
5105 const ObjCCategoryDecl *Category =
5106 Interface->FindCategoryDeclaration(OCD->getIdentifier());
5108 llvm::SmallString<256> ExtName;
5109 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5111 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
5112 + Interface->getName() + "_$_"
5113 + Category->getName(),
5114 Category->protocol_begin(),
5115 Category->protocol_end());
5116 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5117 OCD, Category, ObjCTypes);
5119 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5120 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
5123 llvm::Constant *Init =
5124 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
5126 llvm::GlobalVariable *GCATV
5127 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
5129 llvm::GlobalValue::InternalLinkage,
5132 GCATV->setAlignment(
5133 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
5134 GCATV->setSection("__DATA, __objc_const");
5135 CGM.AddUsedGlobal(GCATV);
5136 DefinedCategories.push_back(GCATV);
5138 // Determine if this category is also "non-lazy".
5139 if (ImplementationIsNonLazy(OCD))
5140 DefinedNonLazyCategories.push_back(GCATV);
5141 // method definition entries must be clear for next implementation.
5142 MethodDefinitions.clear();
5145 /// GetMethodConstant - Return a struct objc_method constant for the
5146 /// given method if it has been defined. The result is null if the
5147 /// method has not been defined. The return value has type MethodPtrTy.
5148 llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
5149 const ObjCMethodDecl *MD) {
5150 llvm::Function *Fn = GetMethodDefinition(MD);
5154 std::vector<llvm::Constant*> Method(3);
5156 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5157 ObjCTypes.SelectorPtrTy);
5158 Method[1] = GetMethodVarType(MD);
5159 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
5160 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
5163 /// EmitMethodList - Build meta-data for method declarations
5164 /// struct _method_list_t {
5165 /// uint32_t entsize; // sizeof(struct _objc_method)
5166 /// uint32_t method_count;
5167 /// struct _objc_method method_list[method_count];
5170 llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5171 const char *Section,
5172 const ConstantVector &Methods) {
5173 // Return null for empty list.
5174 if (Methods.empty())
5175 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
5177 llvm::Constant *Values[3];
5178 // sizeof(struct _objc_method)
5179 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
5180 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5182 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
5183 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
5185 Values[2] = llvm::ConstantArray::get(AT, Methods);
5186 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
5188 llvm::GlobalVariable *GV =
5189 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
5190 llvm::GlobalValue::InternalLinkage, Init, Name);
5191 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
5192 GV->setSection(Section);
5193 CGM.AddUsedGlobal(GV);
5194 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
5197 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5199 llvm::GlobalVariable *
5200 CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5201 const ObjCIvarDecl *Ivar) {
5202 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
5203 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
5204 '.' + Ivar->getNameAsString();
5205 llvm::GlobalVariable *IvarOffsetGV =
5206 CGM.getModule().getGlobalVariable(Name);
5209 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
5211 llvm::GlobalValue::ExternalLinkage,
5214 return IvarOffsetGV;
5218 CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5219 const ObjCIvarDecl *Ivar,
5220 unsigned long int Offset) {
5221 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
5222 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
5224 IvarOffsetGV->setAlignment(
5225 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
5227 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5228 // well (i.e., in ObjCIvarOffsetVariable).
5229 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5230 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
5231 ID->getVisibility() == HiddenVisibility)
5232 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
5234 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
5235 IvarOffsetGV->setSection("__DATA, __objc_ivar");
5236 return IvarOffsetGV;
5239 /// EmitIvarList - Emit the ivar list for the given
5240 /// implementation. The return value has type
5241 /// IvarListnfABIPtrTy.
5242 /// struct _ivar_t {
5243 /// unsigned long int *offset; // pointer to ivar offset location
5246 /// uint32_t alignment;
5249 /// struct _ivar_list_t {
5250 /// uint32 entsize; // sizeof(struct _ivar_t)
5252 /// struct _iver_t list[count];
5256 llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
5257 const ObjCImplementationDecl *ID) {
5259 std::vector<llvm::Constant*> Ivars, Ivar(5);
5261 ObjCInterfaceDecl *OID =
5262 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
5263 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
5265 // FIXME. Consolidate this with similar code in GenerateClass.
5267 for (ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
5268 IVD; IVD = IVD->getNextIvar()) {
5269 // Ignore unnamed bit-fields.
5270 if (!IVD->getDeclName())
5272 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
5273 ComputeIvarBaseOffset(CGM, ID, IVD));
5274 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5275 Ivar[2] = GetMethodVarType(IVD);
5276 const llvm::Type *FieldTy =
5277 CGM.getTypes().ConvertTypeForMem(IVD->getType());
5278 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
5279 unsigned Align = CGM.getContext().getPreferredTypeAlign(
5280 IVD->getType().getTypePtr()) >> 3;
5281 Align = llvm::Log2_32(Align);
5282 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
5283 // NOTE. Size of a bitfield does not match gcc's, because of the
5284 // way bitfields are treated special in each. But I am told that
5285 // 'size' for bitfield ivars is ignored by the runtime so it does
5286 // not matter. If it matters, there is enough info to get the
5288 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5289 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
5291 // Return null for empty list.
5293 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
5295 llvm::Constant *Values[3];
5296 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
5297 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5298 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
5299 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
5301 Values[2] = llvm::ConstantArray::get(AT, Ivars);
5302 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
5303 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5304 llvm::GlobalVariable *GV =
5305 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
5306 llvm::GlobalValue::InternalLinkage,
5308 Prefix + OID->getName());
5310 CGM.getTargetData().getABITypeAlignment(Init->getType()));
5311 GV->setSection("__DATA, __objc_const");
5313 CGM.AddUsedGlobal(GV);
5314 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
5317 llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
5318 const ObjCProtocolDecl *PD) {
5319 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
5322 // We use the initializer as a marker of whether this is a forward
5323 // reference or not. At module finalization we add the empty
5324 // contents for protocols which were referenced but never defined.
5326 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5327 llvm::GlobalValue::ExternalLinkage,
5329 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
5330 Entry->setSection("__DATA,__datacoal_nt,coalesced");
5336 /// GetOrEmitProtocol - Generate the protocol meta-data:
5338 /// struct _protocol_t {
5340 /// const char * const protocol_name;
5341 /// const struct _protocol_list_t * protocol_list; // super protocols
5342 /// const struct method_list_t * const instance_methods;
5343 /// const struct method_list_t * const class_methods;
5344 /// const struct method_list_t *optionalInstanceMethods;
5345 /// const struct method_list_t *optionalClassMethods;
5346 /// const struct _prop_list_t * properties;
5347 /// const uint32_t size; // sizeof(struct _protocol_t)
5348 /// const uint32_t flags; // = 0
5353 llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
5354 const ObjCProtocolDecl *PD) {
5355 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
5357 // Early exit if a defining object has already been generated.
5358 if (Entry && Entry->hasInitializer())
5361 // Construct method lists.
5362 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5363 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
5364 for (ObjCProtocolDecl::instmeth_iterator
5365 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
5366 ObjCMethodDecl *MD = *i;
5367 llvm::Constant *C = GetMethodDescriptionConstant(MD);
5369 return GetOrEmitProtocolRef(PD);
5371 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5372 OptInstanceMethods.push_back(C);
5374 InstanceMethods.push_back(C);
5378 for (ObjCProtocolDecl::classmeth_iterator
5379 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
5380 ObjCMethodDecl *MD = *i;
5381 llvm::Constant *C = GetMethodDescriptionConstant(MD);
5383 return GetOrEmitProtocolRef(PD);
5385 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5386 OptClassMethods.push_back(C);
5388 ClassMethods.push_back(C);
5392 std::vector<llvm::Constant*> Values(10);
5394 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
5395 Values[1] = GetClassName(PD->getIdentifier());
5396 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5397 PD->protocol_begin(),
5398 PD->protocol_end());
5400 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
5402 "__DATA, __objc_const",
5404 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
5406 "__DATA, __objc_const",
5408 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
5410 "__DATA, __objc_const",
5411 OptInstanceMethods);
5412 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
5414 "__DATA, __objc_const",
5416 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
5419 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
5420 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5421 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
5422 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
5426 // Already created, fix the linkage and update the initializer.
5427 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
5428 Entry->setInitializer(Init);
5431 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5432 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5433 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
5434 Entry->setAlignment(
5435 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
5436 Entry->setSection("__DATA,__datacoal_nt,coalesced");
5438 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
5439 CGM.AddUsedGlobal(Entry);
5441 // Use this protocol meta-data to build protocol list table in section
5442 // __DATA, __objc_protolist
5443 llvm::GlobalVariable *PTGV =
5444 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5445 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5446 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
5448 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
5449 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
5450 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
5451 CGM.AddUsedGlobal(PTGV);
5455 /// EmitProtocolList - Generate protocol list meta-data:
5457 /// struct _protocol_list_t {
5458 /// long protocol_count; // Note, this is 32/64 bit
5459 /// struct _protocol_t[protocol_count];
5464 CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5465 ObjCProtocolDecl::protocol_iterator begin,
5466 ObjCProtocolDecl::protocol_iterator end) {
5467 std::vector<llvm::Constant*> ProtocolRefs;
5469 // Just return null for empty protocol lists
5471 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5473 // FIXME: We shouldn't need to do this lookup here, should we?
5474 llvm::SmallString<256> TmpName;
5475 Name.toVector(TmpName);
5476 llvm::GlobalVariable *GV =
5477 CGM.getModule().getGlobalVariable(TmpName.str(), true);
5479 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
5481 for (; begin != end; ++begin)
5482 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5484 // This list is null terminated.
5485 ProtocolRefs.push_back(llvm::Constant::getNullValue(
5486 ObjCTypes.ProtocolnfABIPtrTy));
5488 llvm::Constant *Values[2];
5490 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
5492 llvm::ConstantArray::get(
5493 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
5494 ProtocolRefs.size()),
5497 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
5498 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
5499 llvm::GlobalValue::InternalLinkage,
5501 GV->setSection("__DATA, __objc_const");
5503 CGM.getTargetData().getABITypeAlignment(Init->getType()));
5504 CGM.AddUsedGlobal(GV);
5505 return llvm::ConstantExpr::getBitCast(GV,
5506 ObjCTypes.ProtocolListnfABIPtrTy);
5509 /// GetMethodDescriptionConstant - This routine build following meta-data:
5510 /// struct _objc_method {
5512 /// char *method_type;
5517 CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5518 std::vector<llvm::Constant*> Desc(3);
5520 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5521 ObjCTypes.SelectorPtrTy);
5522 Desc[1] = GetMethodVarType(MD);
5526 // Protocol methods have no implementation. So, this entry is always NULL.
5527 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
5528 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
5531 /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5532 /// This code gen. amounts to generating code for:
5534 /// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5537 LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
5538 CodeGen::CodeGenFunction &CGF,
5540 llvm::Value *BaseValue,
5541 const ObjCIvarDecl *Ivar,
5542 unsigned CVRQualifiers) {
5543 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
5544 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5545 EmitIvarOffset(CGF, ID, Ivar));
5548 llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
5549 CodeGen::CodeGenFunction &CGF,
5550 const ObjCInterfaceDecl *Interface,
5551 const ObjCIvarDecl *Ivar) {
5552 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
5555 static void appendSelectorForMessageRefTable(std::string &buffer,
5556 Selector selector) {
5557 if (selector.isUnarySelector()) {
5558 buffer += selector.getNameForSlot(0);
5562 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5563 buffer += selector.getNameForSlot(i);
5568 /// Emit a "v-table" message send. We emit a weak hidden-visibility
5569 /// struct, initially containing the selector pointer and a pointer to
5570 /// a "fixup" variant of the appropriate objc_msgSend. To call, we
5571 /// load and call the function pointer, passing the address of the
5572 /// struct as the second parameter. The runtime determines whether
5573 /// the selector is currently emitted using vtable dispatch; if so, it
5574 /// substitutes a stub function which simply tail-calls through the
5575 /// appropriate vtable slot, and if not, it substitues a stub function
5576 /// which tail-calls objc_msgSend. Both stubs adjust the selector
5577 /// argument to correctly point to the selector.
5579 CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5580 ReturnValueSlot returnSlot,
5581 QualType resultType,
5586 const CallArgList &formalArgs,
5587 const ObjCMethodDecl *method) {
5588 // Compute the actual arguments.
5591 // First argument: the receiver / super-call structure.
5593 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5594 args.add(RValue::get(arg0), arg0Type);
5596 // Second argument: a pointer to the message ref structure. Leave
5597 // the actual argument value blank for now.
5598 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5600 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5602 const CGFunctionInfo &fnInfo =
5603 CGM.getTypes().getFunctionInfo(resultType, args,
5604 FunctionType::ExtInfo());
5606 NullReturnState nullReturn;
5608 // Find the function to call and the mangled name for the message
5609 // ref structure. Using a different mangled name wouldn't actually
5610 // be a problem; it would just be a waste.
5612 // The runtime currently never uses vtable dispatch for anything
5613 // except normal, non-super message-sends.
5614 // FIXME: don't use this for that.
5615 llvm::Constant *fn = 0;
5616 std::string messageRefName("\01l_");
5617 if (CGM.ReturnTypeUsesSRet(fnInfo)) {
5619 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5620 messageRefName += "objc_msgSendSuper2_stret_fixup";
5622 nullReturn.init(CGF, arg0);
5623 fn = ObjCTypes.getMessageSendStretFixupFn();
5624 messageRefName += "objc_msgSend_stret_fixup";
5626 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5627 fn = ObjCTypes.getMessageSendFpretFixupFn();
5628 messageRefName += "objc_msgSend_fpret_fixup";
5631 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5632 messageRefName += "objc_msgSendSuper2_fixup";
5634 fn = ObjCTypes.getMessageSendFixupFn();
5635 messageRefName += "objc_msgSend_fixup";
5638 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5639 messageRefName += '_';
5641 // Append the selector name, except use underscores anywhere we
5642 // would have used colons.
5643 appendSelectorForMessageRefTable(messageRefName, selector);
5645 llvm::GlobalVariable *messageRef
5646 = CGM.getModule().getGlobalVariable(messageRefName);
5648 // Build the message ref structure.
5649 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
5650 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
5651 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5654 llvm::GlobalValue::WeakAnyLinkage,
5657 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5658 messageRef->setAlignment(16);
5659 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5662 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5664 // Update the message ref argument.
5665 args[1].RV = RValue::get(mref);
5667 // Load the function to call from the message ref table.
5668 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5669 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5671 bool variadic = method ? method->isVariadic() : false;
5672 const llvm::FunctionType *fnType =
5673 CGF.getTypes().GetFunctionType(fnInfo, variadic);
5674 callee = CGF.Builder.CreateBitCast(callee,
5675 llvm::PointerType::getUnqual(fnType));
5677 RValue result = CGF.EmitCall(fnInfo, callee, returnSlot, args);
5678 return nullReturn.complete(CGF, result, resultType);
5681 /// Generate code for a message send expression in the nonfragile abi.
5683 CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
5684 ReturnValueSlot Return,
5685 QualType ResultType,
5687 llvm::Value *Receiver,
5688 const CallArgList &CallArgs,
5689 const ObjCInterfaceDecl *Class,
5690 const ObjCMethodDecl *Method) {
5691 return isVTableDispatchedSelector(Sel)
5692 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
5693 Receiver, CGF.getContext().getObjCIdType(),
5694 false, CallArgs, Method)
5695 : EmitMessageSend(CGF, Return, ResultType,
5696 EmitSelector(CGF.Builder, Sel),
5697 Receiver, CGF.getContext().getObjCIdType(),
5698 false, CallArgs, Method, ObjCTypes);
5701 llvm::GlobalVariable *
5702 CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
5703 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5706 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
5707 false, llvm::GlobalValue::ExternalLinkage,
5714 llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
5715 IdentifierInfo *II) {
5716 llvm::GlobalVariable *&Entry = ClassReferences[II];
5719 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
5720 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5722 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5723 false, llvm::GlobalValue::InternalLinkage,
5725 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
5726 Entry->setAlignment(
5727 CGM.getTargetData().getABITypeAlignment(
5728 ObjCTypes.ClassnfABIPtrTy));
5729 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
5730 CGM.AddUsedGlobal(Entry);
5733 return Builder.CreateLoad(Entry, "tmp");
5736 llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5737 const ObjCInterfaceDecl *ID) {
5738 return EmitClassRefFromId(Builder, ID->getIdentifier());
5741 llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
5742 CGBuilderTy &Builder) {
5743 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
5744 return EmitClassRefFromId(Builder, II);
5748 CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
5749 const ObjCInterfaceDecl *ID) {
5750 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
5753 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5754 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5756 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5757 false, llvm::GlobalValue::InternalLinkage,
5759 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
5760 Entry->setAlignment(
5761 CGM.getTargetData().getABITypeAlignment(
5762 ObjCTypes.ClassnfABIPtrTy));
5763 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
5764 CGM.AddUsedGlobal(Entry);
5767 return Builder.CreateLoad(Entry, "tmp");
5770 /// EmitMetaClassRef - Return a Value * of the address of _class_t
5773 llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5774 const ObjCInterfaceDecl *ID) {
5775 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5777 return Builder.CreateLoad(Entry, "tmp");
5779 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
5780 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
5782 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
5783 llvm::GlobalValue::InternalLinkage,
5785 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
5786 Entry->setAlignment(
5787 CGM.getTargetData().getABITypeAlignment(
5788 ObjCTypes.ClassnfABIPtrTy));
5790 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
5791 CGM.AddUsedGlobal(Entry);
5793 return Builder.CreateLoad(Entry, "tmp");
5796 /// GetClass - Return a reference to the class for the given interface
5798 llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5799 const ObjCInterfaceDecl *ID) {
5800 if (ID->isWeakImported()) {
5801 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5802 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5803 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5806 return EmitClassRef(Builder, ID);
5809 /// Generates a message send where the super is the receiver. This is
5810 /// a message send to self with special delivery semantics indicating
5811 /// which class's method should be called.
5813 CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
5814 ReturnValueSlot Return,
5815 QualType ResultType,
5817 const ObjCInterfaceDecl *Class,
5818 bool isCategoryImpl,
5819 llvm::Value *Receiver,
5820 bool IsClassMessage,
5821 const CodeGen::CallArgList &CallArgs,
5822 const ObjCMethodDecl *Method) {
5824 // Create and init a super structure; this is a (receiver, class)
5825 // pair we will pass to objc_msgSendSuper.
5826 llvm::Value *ObjCSuper =
5827 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
5829 llvm::Value *ReceiverAsObject =
5830 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5831 CGF.Builder.CreateStore(ReceiverAsObject,
5832 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
5834 // If this is a class message the metaclass is passed as the target.
5835 llvm::Value *Target;
5836 if (IsClassMessage) {
5837 if (isCategoryImpl) {
5838 // Message sent to "super' in a class method defined in
5839 // a category implementation.
5840 Target = EmitClassRef(CGF.Builder, Class);
5841 Target = CGF.Builder.CreateStructGEP(Target, 0);
5842 Target = CGF.Builder.CreateLoad(Target);
5844 Target = EmitMetaClassRef(CGF.Builder, Class);
5846 Target = EmitSuperClassRef(CGF.Builder, Class);
5848 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5850 const llvm::Type *ClassTy =
5851 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5852 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5853 CGF.Builder.CreateStore(Target,
5854 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
5856 return (isVTableDispatchedSelector(Sel))
5857 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
5858 ObjCSuper, ObjCTypes.SuperPtrCTy,
5859 true, CallArgs, Method)
5860 : EmitMessageSend(CGF, Return, ResultType,
5861 EmitSelector(CGF.Builder, Sel),
5862 ObjCSuper, ObjCTypes.SuperPtrCTy,
5863 true, CallArgs, Method, ObjCTypes);
5866 llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
5867 Selector Sel, bool lval) {
5868 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
5871 llvm::Constant *Casted =
5872 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5873 ObjCTypes.SelectorPtrTy);
5875 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5876 llvm::GlobalValue::InternalLinkage,
5877 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
5878 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
5879 CGM.AddUsedGlobal(Entry);
5884 return Builder.CreateLoad(Entry, "tmp");
5886 /// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
5887 /// objc_assign_ivar (id src, id *dst, ptrdiff_t)
5889 void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
5892 llvm::Value *ivarOffset) {
5893 const llvm::Type * SrcTy = src->getType();
5894 if (!isa<llvm::PointerType>(SrcTy)) {
5895 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
5896 assert(Size <= 8 && "does not support size > 8");
5897 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5898 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
5899 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5901 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5902 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
5903 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5904 src, dst, ivarOffset);
5908 /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5909 /// objc_assign_strongCast (id src, id *dst)
5911 void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
5912 CodeGen::CodeGenFunction &CGF,
5913 llvm::Value *src, llvm::Value *dst) {
5914 const llvm::Type * SrcTy = src->getType();
5915 if (!isa<llvm::PointerType>(SrcTy)) {
5916 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
5917 assert(Size <= 8 && "does not support size > 8");
5918 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5919 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
5920 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5922 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5923 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
5924 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
5925 src, dst, "weakassign");
5929 void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
5930 CodeGen::CodeGenFunction &CGF,
5931 llvm::Value *DestPtr,
5932 llvm::Value *SrcPtr,
5933 llvm::Value *Size) {
5934 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5935 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
5936 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
5937 DestPtr, SrcPtr, Size);
5941 /// EmitObjCWeakRead - Code gen for loading value of a __weak
5942 /// object: objc_read_weak (id *src)
5944 llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
5945 CodeGen::CodeGenFunction &CGF,
5946 llvm::Value *AddrWeakObj) {
5947 const llvm::Type* DestTy =
5948 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5949 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
5950 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
5951 AddrWeakObj, "weakread");
5952 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
5956 /// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5957 /// objc_assign_weak (id src, id *dst)
5959 void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
5960 llvm::Value *src, llvm::Value *dst) {
5961 const llvm::Type * SrcTy = src->getType();
5962 if (!isa<llvm::PointerType>(SrcTy)) {
5963 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
5964 assert(Size <= 8 && "does not support size > 8");
5965 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5966 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
5967 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5969 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5970 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
5971 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
5972 src, dst, "weakassign");
5976 /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5977 /// objc_assign_global (id src, id *dst)
5979 void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
5980 llvm::Value *src, llvm::Value *dst,
5982 const llvm::Type * SrcTy = src->getType();
5983 if (!isa<llvm::PointerType>(SrcTy)) {
5984 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
5985 assert(Size <= 8 && "does not support size > 8");
5986 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5987 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
5988 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5990 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5991 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
5993 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5994 src, dst, "globalassign");
5996 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5997 src, dst, "threadlocalassign");
6002 CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6003 const ObjCAtSynchronizedStmt &S) {
6004 EmitAtSynchronizedStmt(CGF, S,
6005 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6006 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
6010 CGObjCNonFragileABIMac::GetEHType(QualType T) {
6011 // There's a particular fixed type info for 'id'.
6012 if (T->isObjCIdType() ||
6013 T->isObjCQualifiedIdType()) {
6014 llvm::Constant *IDEHType =
6015 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6018 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6020 llvm::GlobalValue::ExternalLinkage,
6021 0, "OBJC_EHTYPE_id");
6025 // All other types should be Objective-C interface pointer types.
6026 const ObjCObjectPointerType *PT =
6027 T->getAs<ObjCObjectPointerType>();
6028 assert(PT && "Invalid @catch type.");
6029 const ObjCInterfaceType *IT = PT->getInterfaceType();
6030 assert(IT && "Invalid @catch type.");
6031 return GetInterfaceEHType(IT->getDecl(), false);
6034 void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6035 const ObjCAtTryStmt &S) {
6036 EmitTryCatchStmt(CGF, S,
6037 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6038 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6039 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
6042 /// EmitThrowStmt - Generate code for a throw statement.
6043 void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6044 const ObjCAtThrowStmt &S) {
6045 if (const Expr *ThrowExpr = S.getThrowExpr()) {
6046 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
6047 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy,
6049 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
6050 .setDoesNotReturn();
6052 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
6053 .setDoesNotReturn();
6056 CGF.Builder.CreateUnreachable();
6057 CGF.Builder.ClearInsertionPoint();
6061 CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
6062 bool ForDefinition) {
6063 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
6065 // If we don't need a definition, return the entry if found or check
6066 // if we use an external reference.
6067 if (!ForDefinition) {
6071 // If this type (or a super class) has the __objc_exception__
6072 // attribute, emit an external reference.
6073 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
6075 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
6076 llvm::GlobalValue::ExternalLinkage,
6079 ID->getIdentifier()->getName()));
6082 // Otherwise we need to either make a new entry or fill in the
6084 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
6085 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6086 std::string VTableName = "objc_ehtype_vtable";
6087 llvm::GlobalVariable *VTableGV =
6088 CGM.getModule().getGlobalVariable(VTableName);
6090 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6092 llvm::GlobalValue::ExternalLinkage,
6095 llvm::Value *VTableIdx =
6096 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
6098 std::vector<llvm::Constant*> Values(3);
6099 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
6100 Values[1] = GetClassName(ID->getIdentifier());
6101 Values[2] = GetClassGlobal(ClassName);
6102 llvm::Constant *Init =
6103 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
6106 Entry->setInitializer(Init);
6108 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
6109 llvm::GlobalValue::WeakAnyLinkage,
6112 ID->getIdentifier()->getName()));
6115 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
6116 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
6117 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6118 ObjCTypes.EHTypeTy));
6120 if (ForDefinition) {
6121 Entry->setSection("__DATA,__objc_const");
6122 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6124 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6132 CodeGen::CGObjCRuntime *
6133 CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
6134 if (CGM.getLangOptions().ObjCNonFragileABI)
6135 return new CGObjCNonFragileABIMac(CGM);
6136 return new CGObjCMac(CGM);