CGObjCMac.cpp revision a223cca7751637f8ec1a860010c4148757fb4752
1//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This provides Objective-C code generation targetting the Apple runtime. 11// 12//===----------------------------------------------------------------------===// 13 14#include "CGObjCRuntime.h" 15 16#include "CodeGenModule.h" 17#include "CodeGenFunction.h" 18#include "clang/AST/ASTContext.h" 19#include "clang/AST/Decl.h" 20#include "clang/AST/DeclObjC.h" 21#include "clang/Basic/LangOptions.h" 22 23#include "llvm/Module.h" 24#include "llvm/ADT/DenseSet.h" 25#include "llvm/Target/TargetData.h" 26#include <sstream> 27 28using namespace clang; 29using namespace CodeGen; 30 31namespace { 32 33 typedef std::vector<llvm::Constant*> ConstantVector; 34 35 // FIXME: We should find a nicer way to make the labels for 36 // metadata, string concatenation is lame. 37 38class ObjCCommonTypesHelper { 39protected: 40 CodeGen::CodeGenModule &CGM; 41 42public: 43 const llvm::Type *ShortTy, *IntTy, *LongTy; 44 const llvm::Type *Int8PtrTy; 45 46 /// ObjectPtrTy - LLVM type for object handles (typeof(id)) 47 const llvm::Type *ObjectPtrTy; 48 49 /// PtrObjectPtrTy - LLVM type for id * 50 const llvm::Type *PtrObjectPtrTy; 51 52 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) 53 const llvm::Type *SelectorPtrTy; 54 /// ProtocolPtrTy - LLVM type for external protocol handles 55 /// (typeof(Protocol)) 56 const llvm::Type *ExternalProtocolPtrTy; 57 58 // SuperCTy - clang type for struct objc_super. 59 QualType SuperCTy; 60 // SuperPtrCTy - clang type for struct objc_super *. 61 QualType SuperPtrCTy; 62 63 /// SuperTy - LLVM type for struct objc_super. 64 const llvm::StructType *SuperTy; 65 /// SuperPtrTy - LLVM type for struct objc_super *. 66 const llvm::Type *SuperPtrTy; 67 68 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t 69 /// in GCC parlance). 70 const llvm::StructType *PropertyTy; 71 72 /// PropertyListTy - LLVM type for struct objc_property_list 73 /// (_prop_list_t in GCC parlance). 74 const llvm::StructType *PropertyListTy; 75 /// PropertyListPtrTy - LLVM type for struct objc_property_list*. 76 const llvm::Type *PropertyListPtrTy; 77 78 // MethodTy - LLVM type for struct objc_method. 79 const llvm::StructType *MethodTy; 80 81 /// CacheTy - LLVM type for struct objc_cache. 82 const llvm::Type *CacheTy; 83 /// CachePtrTy - LLVM type for struct objc_cache *. 84 const llvm::Type *CachePtrTy; 85 86 llvm::Function *GetPropertyFn, *SetPropertyFn; 87 88 llvm::Function *EnumerationMutationFn; 89 90 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. 91 llvm::Function *GcReadWeakFn; 92 93 /// GcAssignWeakFn -- LLVM objc_assign_weak function. 94 llvm::Function *GcAssignWeakFn; 95 96 /// GcAssignGlobalFn -- LLVM objc_assign_global function. 97 llvm::Function *GcAssignGlobalFn; 98 99 /// GcAssignIvarFn -- LLVM objc_assign_ivar function. 100 llvm::Function *GcAssignIvarFn; 101 102 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. 103 llvm::Function *GcAssignStrongCastFn; 104 105 /// ExceptionThrowFn - LLVM objc_exception_throw function. 106 llvm::Function *ExceptionThrowFn; 107 108 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); 109 ~ObjCCommonTypesHelper(){} 110}; 111 112/// ObjCTypesHelper - Helper class that encapsulates lazy 113/// construction of varies types used during ObjC generation. 114class ObjCTypesHelper : public ObjCCommonTypesHelper { 115private: 116 117 llvm::Function *MessageSendFn, *MessageSendStretFn, *MessageSendFpretFn; 118 llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn, 119 *MessageSendSuperFpretFn; 120 121public: 122 /// SymtabTy - LLVM type for struct objc_symtab. 123 const llvm::StructType *SymtabTy; 124 /// SymtabPtrTy - LLVM type for struct objc_symtab *. 125 const llvm::Type *SymtabPtrTy; 126 /// ModuleTy - LLVM type for struct objc_module. 127 const llvm::StructType *ModuleTy; 128 129 /// ProtocolTy - LLVM type for struct objc_protocol. 130 const llvm::StructType *ProtocolTy; 131 /// ProtocolPtrTy - LLVM type for struct objc_protocol *. 132 const llvm::Type *ProtocolPtrTy; 133 /// ProtocolExtensionTy - LLVM type for struct 134 /// objc_protocol_extension. 135 const llvm::StructType *ProtocolExtensionTy; 136 /// ProtocolExtensionTy - LLVM type for struct 137 /// objc_protocol_extension *. 138 const llvm::Type *ProtocolExtensionPtrTy; 139 /// MethodDescriptionTy - LLVM type for struct 140 /// objc_method_description. 141 const llvm::StructType *MethodDescriptionTy; 142 /// MethodDescriptionListTy - LLVM type for struct 143 /// objc_method_description_list. 144 const llvm::StructType *MethodDescriptionListTy; 145 /// MethodDescriptionListPtrTy - LLVM type for struct 146 /// objc_method_description_list *. 147 const llvm::Type *MethodDescriptionListPtrTy; 148 /// ProtocolListTy - LLVM type for struct objc_property_list. 149 const llvm::Type *ProtocolListTy; 150 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. 151 const llvm::Type *ProtocolListPtrTy; 152 /// CategoryTy - LLVM type for struct objc_category. 153 const llvm::StructType *CategoryTy; 154 /// ClassTy - LLVM type for struct objc_class. 155 const llvm::StructType *ClassTy; 156 /// ClassPtrTy - LLVM type for struct objc_class *. 157 const llvm::Type *ClassPtrTy; 158 /// ClassExtensionTy - LLVM type for struct objc_class_ext. 159 const llvm::StructType *ClassExtensionTy; 160 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. 161 const llvm::Type *ClassExtensionPtrTy; 162 // IvarTy - LLVM type for struct objc_ivar. 163 const llvm::StructType *IvarTy; 164 /// IvarListTy - LLVM type for struct objc_ivar_list. 165 const llvm::Type *IvarListTy; 166 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. 167 const llvm::Type *IvarListPtrTy; 168 /// MethodListTy - LLVM type for struct objc_method_list. 169 const llvm::Type *MethodListTy; 170 /// MethodListPtrTy - LLVM type for struct objc_method_list *. 171 const llvm::Type *MethodListPtrTy; 172 173 /// ExceptionDataTy - LLVM type for struct _objc_exception_data. 174 const llvm::Type *ExceptionDataTy; 175 176 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. 177 llvm::Function *ExceptionTryEnterFn; 178 179 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. 180 llvm::Function *ExceptionTryExitFn; 181 182 /// ExceptionExtractFn - LLVM objc_exception_extract function. 183 llvm::Function *ExceptionExtractFn; 184 185 /// ExceptionMatchFn - LLVM objc_exception_match function. 186 llvm::Function *ExceptionMatchFn; 187 188 /// SetJmpFn - LLVM _setjmp function. 189 llvm::Function *SetJmpFn; 190 191 /// SyncEnterFn - LLVM object_sync_enter function. 192 llvm::Function *SyncEnterFn; 193 194 /// SyncExitFn - LLVM object_sync_exit function. 195 llvm::Function *SyncExitFn; 196 197public: 198 ObjCTypesHelper(CodeGen::CodeGenModule &cgm); 199 ~ObjCTypesHelper() {} 200 201 202 llvm::Function *getSendFn(bool IsSuper) { 203 return IsSuper ? MessageSendSuperFn : MessageSendFn; 204 } 205 206 llvm::Function *getSendStretFn(bool IsSuper) { 207 return IsSuper ? MessageSendSuperStretFn : MessageSendStretFn; 208 } 209 210 llvm::Function *getSendFpretFn(bool IsSuper) { 211 return IsSuper ? MessageSendSuperFpretFn : MessageSendFpretFn; 212 } 213}; 214 215/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's 216/// modern abi 217class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { 218public: 219 llvm::Function *MessageSendFixupFn, *MessageSendFpretFixupFn, 220 *MessageSendStretFixupFn, *MessageSendIdFixupFn, 221 *MessageSendIdStretFixupFn, *MessageSendSuper2FixupFn, 222 *MessageSendSuper2StretFixupFn; 223 224 // MethodListnfABITy - LLVM for struct _method_list_t 225 const llvm::StructType *MethodListnfABITy; 226 227 // MethodListnfABIPtrTy - LLVM for struct _method_list_t* 228 const llvm::Type *MethodListnfABIPtrTy; 229 230 // ProtocolnfABITy = LLVM for struct _protocol_t 231 const llvm::StructType *ProtocolnfABITy; 232 233 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* 234 const llvm::Type *ProtocolnfABIPtrTy; 235 236 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list 237 const llvm::StructType *ProtocolListnfABITy; 238 239 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* 240 const llvm::Type *ProtocolListnfABIPtrTy; 241 242 // ClassnfABITy - LLVM for struct _class_t 243 const llvm::StructType *ClassnfABITy; 244 245 // ClassnfABIPtrTy - LLVM for struct _class_t* 246 const llvm::Type *ClassnfABIPtrTy; 247 248 // IvarnfABITy - LLVM for struct _ivar_t 249 const llvm::StructType *IvarnfABITy; 250 251 // IvarListnfABITy - LLVM for struct _ivar_list_t 252 const llvm::StructType *IvarListnfABITy; 253 254 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* 255 const llvm::Type *IvarListnfABIPtrTy; 256 257 // ClassRonfABITy - LLVM for struct _class_ro_t 258 const llvm::StructType *ClassRonfABITy; 259 260 // ImpnfABITy - LLVM for id (*)(id, SEL, ...) 261 const llvm::Type *ImpnfABITy; 262 263 // CategorynfABITy - LLVM for struct _category_t 264 const llvm::StructType *CategorynfABITy; 265 266 // New types for nonfragile abi messaging. 267 268 // MessageRefTy - LLVM for: 269 // struct _message_ref_t { 270 // IMP messenger; 271 // SEL name; 272 // }; 273 const llvm::StructType *MessageRefTy; 274 // MessageRefCTy - clang type for struct _message_ref_t 275 QualType MessageRefCTy; 276 277 // MessageRefPtrTy - LLVM for struct _message_ref_t* 278 const llvm::Type *MessageRefPtrTy; 279 // MessageRefCPtrTy - clang type for struct _message_ref_t* 280 QualType MessageRefCPtrTy; 281 282 // MessengerTy - Type of the messenger (shown as IMP above) 283 const llvm::FunctionType *MessengerTy; 284 285 // SuperMessageRefTy - LLVM for: 286 // struct _super_message_ref_t { 287 // SUPER_IMP messenger; 288 // SEL name; 289 // }; 290 const llvm::StructType *SuperMessageRefTy; 291 292 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* 293 const llvm::Type *SuperMessageRefPtrTy; 294 295 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); 296 ~ObjCNonFragileABITypesHelper(){} 297}; 298 299class CGObjCCommonMac : public CodeGen::CGObjCRuntime { 300protected: 301 CodeGen::CodeGenModule &CGM; 302 // FIXME! May not be needing this after all. 303 unsigned ObjCABI; 304 305 /// LazySymbols - Symbols to generate a lazy reference for. See 306 /// DefinedSymbols and FinishModule(). 307 std::set<IdentifierInfo*> LazySymbols; 308 309 /// DefinedSymbols - External symbols which are defined by this 310 /// module. The symbols in this list and LazySymbols are used to add 311 /// special linker symbols which ensure that Objective-C modules are 312 /// linked properly. 313 std::set<IdentifierInfo*> DefinedSymbols; 314 315 /// ClassNames - uniqued class names. 316 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; 317 318 /// MethodVarNames - uniqued method variable names. 319 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; 320 321 /// MethodVarTypes - uniqued method type signatures. We have to use 322 /// a StringMap here because have no other unique reference. 323 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; 324 325 /// MethodDefinitions - map of methods which have been defined in 326 /// this translation unit. 327 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; 328 329 /// PropertyNames - uniqued method variable names. 330 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; 331 332 /// ClassReferences - uniqued class references. 333 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; 334 335 /// SelectorReferences - uniqued selector references. 336 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; 337 338 /// Protocols - Protocols for which an objc_protocol structure has 339 /// been emitted. Forward declarations are handled by creating an 340 /// empty structure whose initializer is filled in when/if defined. 341 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; 342 343 /// DefinedProtocols - Protocols which have actually been 344 /// defined. We should not need this, see FIXME in GenerateProtocol. 345 llvm::DenseSet<IdentifierInfo*> DefinedProtocols; 346 347 /// DefinedClasses - List of defined classes. 348 std::vector<llvm::GlobalValue*> DefinedClasses; 349 350 /// DefinedCategories - List of defined categories. 351 std::vector<llvm::GlobalValue*> DefinedCategories; 352 353 /// UsedGlobals - List of globals to pack into the llvm.used metadata 354 /// to prevent them from being clobbered. 355 std::vector<llvm::GlobalVariable*> UsedGlobals; 356 357 /// GetNameForMethod - Return a name for the given method. 358 /// \param[out] NameOut - The return value. 359 void GetNameForMethod(const ObjCMethodDecl *OMD, 360 const ObjCContainerDecl *CD, 361 std::string &NameOut); 362 363 /// GetMethodVarName - Return a unique constant for the given 364 /// selector's name. The return value has type char *. 365 llvm::Constant *GetMethodVarName(Selector Sel); 366 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); 367 llvm::Constant *GetMethodVarName(const std::string &Name); 368 369 /// GetMethodVarType - Return a unique constant for the given 370 /// selector's name. The return value has type char *. 371 372 // FIXME: This is a horrible name. 373 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); 374 llvm::Constant *GetMethodVarType(const std::string &Name); 375 376 /// GetPropertyName - Return a unique constant for the given 377 /// name. The return value has type char *. 378 llvm::Constant *GetPropertyName(IdentifierInfo *Ident); 379 380 // FIXME: This can be dropped once string functions are unified. 381 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, 382 const Decl *Container); 383 384 /// GetClassName - Return a unique constant for the given selector's 385 /// name. The return value has type char *. 386 llvm::Constant *GetClassName(IdentifierInfo *Ident); 387 388 const RecordDecl *GetFirstIvarInRecord(const ObjCInterfaceDecl *OID, 389 RecordDecl::field_iterator &FIV, 390 RecordDecl::field_iterator &PIV); 391 /// EmitPropertyList - Emit the given property list. The return 392 /// value has type PropertyListPtrTy. 393 llvm::Constant *EmitPropertyList(const std::string &Name, 394 const Decl *Container, 395 const ObjCContainerDecl *OCD, 396 const ObjCCommonTypesHelper &ObjCTypes); 397 398 /// GetProtocolRef - Return a reference to the internal protocol 399 /// description, creating an empty one if it has not been 400 /// defined. The return value has type ProtocolPtrTy. 401 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); 402 403public: 404 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm) 405 { } 406 407 virtual llvm::Constant *GenerateConstantString(const std::string &String); 408 409 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, 410 const ObjCContainerDecl *CD=0); 411 412 virtual void GenerateProtocol(const ObjCProtocolDecl *PD); 413 414 /// GetOrEmitProtocol - Get the protocol object for the given 415 /// declaration, emitting it if necessary. The return value has type 416 /// ProtocolPtrTy. 417 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; 418 419 /// GetOrEmitProtocolRef - Get a forward reference to the protocol 420 /// object for the given declaration, emitting it if needed. These 421 /// forward references will be filled in with empty bodies if no 422 /// definition is seen. The return value has type ProtocolPtrTy. 423 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; 424}; 425 426class CGObjCMac : public CGObjCCommonMac { 427private: 428 ObjCTypesHelper ObjCTypes; 429 /// EmitImageInfo - Emit the image info marker used to encode some module 430 /// level information. 431 void EmitImageInfo(); 432 433 /// EmitModuleInfo - Another marker encoding module level 434 /// information. 435 void EmitModuleInfo(); 436 437 /// EmitModuleSymols - Emit module symbols, the list of defined 438 /// classes and categories. The result has type SymtabPtrTy. 439 llvm::Constant *EmitModuleSymbols(); 440 441 /// FinishModule - Write out global data structures at the end of 442 /// processing a translation unit. 443 void FinishModule(); 444 445 /// EmitClassExtension - Generate the class extension structure used 446 /// to store the weak ivar layout and properties. The return value 447 /// has type ClassExtensionPtrTy. 448 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); 449 450 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, 451 /// for the given class. 452 llvm::Value *EmitClassRef(CGBuilderTy &Builder, 453 const ObjCInterfaceDecl *ID); 454 455 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, 456 QualType ResultType, 457 Selector Sel, 458 llvm::Value *Arg0, 459 QualType Arg0Ty, 460 bool IsSuper, 461 const CallArgList &CallArgs); 462 463 /// EmitIvarList - Emit the ivar list for the given 464 /// implementation. If ForClass is true the list of class ivars 465 /// (i.e. metaclass ivars) is emitted, otherwise the list of 466 /// interface ivars will be emitted. The return value has type 467 /// IvarListPtrTy. 468 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, 469 bool ForClass); 470 471 /// EmitMetaClass - Emit a forward reference to the class structure 472 /// for the metaclass of the given interface. The return value has 473 /// type ClassPtrTy. 474 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); 475 476 /// EmitMetaClass - Emit a class structure for the metaclass of the 477 /// given implementation. The return value has type ClassPtrTy. 478 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, 479 llvm::Constant *Protocols, 480 const llvm::Type *InterfaceTy, 481 const ConstantVector &Methods); 482 483 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); 484 485 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); 486 487 /// EmitMethodList - Emit the method list for the given 488 /// implementation. The return value has type MethodListPtrTy. 489 llvm::Constant *EmitMethodList(const std::string &Name, 490 const char *Section, 491 const ConstantVector &Methods); 492 493 /// EmitMethodDescList - Emit a method description list for a list of 494 /// method declarations. 495 /// - TypeName: The name for the type containing the methods. 496 /// - IsProtocol: True iff these methods are for a protocol. 497 /// - ClassMethds: True iff these are class methods. 498 /// - Required: When true, only "required" methods are 499 /// listed. Similarly, when false only "optional" methods are 500 /// listed. For classes this should always be true. 501 /// - begin, end: The method list to output. 502 /// 503 /// The return value has type MethodDescriptionListPtrTy. 504 llvm::Constant *EmitMethodDescList(const std::string &Name, 505 const char *Section, 506 const ConstantVector &Methods); 507 508 /// GetOrEmitProtocol - Get the protocol object for the given 509 /// declaration, emitting it if necessary. The return value has type 510 /// ProtocolPtrTy. 511 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); 512 513 /// GetOrEmitProtocolRef - Get a forward reference to the protocol 514 /// object for the given declaration, emitting it if needed. These 515 /// forward references will be filled in with empty bodies if no 516 /// definition is seen. The return value has type ProtocolPtrTy. 517 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); 518 519 /// EmitProtocolExtension - Generate the protocol extension 520 /// structure used to store optional instance and class methods, and 521 /// protocol properties. The return value has type 522 /// ProtocolExtensionPtrTy. 523 llvm::Constant * 524 EmitProtocolExtension(const ObjCProtocolDecl *PD, 525 const ConstantVector &OptInstanceMethods, 526 const ConstantVector &OptClassMethods); 527 528 /// EmitProtocolList - Generate the list of referenced 529 /// protocols. The return value has type ProtocolListPtrTy. 530 llvm::Constant *EmitProtocolList(const std::string &Name, 531 ObjCProtocolDecl::protocol_iterator begin, 532 ObjCProtocolDecl::protocol_iterator end); 533 534 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, 535 /// for the given selector. 536 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); 537 538 public: 539 CGObjCMac(CodeGen::CodeGenModule &cgm); 540 541 virtual llvm::Function *ModuleInitFunction(); 542 543 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, 544 QualType ResultType, 545 Selector Sel, 546 llvm::Value *Receiver, 547 bool IsClassMessage, 548 const CallArgList &CallArgs); 549 550 virtual CodeGen::RValue 551 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, 552 QualType ResultType, 553 Selector Sel, 554 const ObjCInterfaceDecl *Class, 555 llvm::Value *Receiver, 556 bool IsClassMessage, 557 const CallArgList &CallArgs); 558 559 virtual llvm::Value *GetClass(CGBuilderTy &Builder, 560 const ObjCInterfaceDecl *ID); 561 562 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); 563 564 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); 565 566 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); 567 568 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, 569 const ObjCProtocolDecl *PD); 570 571 virtual llvm::Function *GetPropertyGetFunction(); 572 virtual llvm::Function *GetPropertySetFunction(); 573 virtual llvm::Function *EnumerationMutationFunction(); 574 575 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, 576 const Stmt &S); 577 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, 578 const ObjCAtThrowStmt &S); 579 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, 580 llvm::Value *AddrWeakObj); 581 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, 582 llvm::Value *src, llvm::Value *dst); 583 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, 584 llvm::Value *src, llvm::Value *dest); 585 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, 586 llvm::Value *src, llvm::Value *dest); 587 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, 588 llvm::Value *src, llvm::Value *dest); 589 590 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, 591 QualType ObjectTy, 592 llvm::Value *BaseValue, 593 const ObjCIvarDecl *Ivar, 594 const FieldDecl *Field, 595 unsigned CVRQualifiers); 596 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, 597 ObjCInterfaceDecl *Interface, 598 const ObjCIvarDecl *Ivar); 599}; 600 601class CGObjCNonFragileABIMac : public CGObjCCommonMac { 602private: 603 ObjCNonFragileABITypesHelper ObjCTypes; 604 llvm::GlobalVariable* ObjCEmptyCacheVar; 605 llvm::GlobalVariable* ObjCEmptyVtableVar; 606 /// MetaClassReferences - uniqued meta class references. 607 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; 608 609 /// FinishNonFragileABIModule - Write out global data structures at the end of 610 /// processing a translation unit. 611 void FinishNonFragileABIModule(); 612 613 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, 614 unsigned InstanceStart, 615 unsigned InstanceSize, 616 const ObjCImplementationDecl *ID); 617 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, 618 llvm::Constant *IsAGV, 619 llvm::Constant *SuperClassGV, 620 llvm::Constant *ClassRoGV, 621 bool HiddenVisibility); 622 623 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); 624 625 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); 626 627 /// EmitMethodList - Emit the method list for the given 628 /// implementation. The return value has type MethodListnfABITy. 629 llvm::Constant *EmitMethodList(const std::string &Name, 630 const char *Section, 631 const ConstantVector &Methods); 632 /// EmitIvarList - Emit the ivar list for the given 633 /// implementation. If ForClass is true the list of class ivars 634 /// (i.e. metaclass ivars) is emitted, otherwise the list of 635 /// interface ivars will be emitted. The return value has type 636 /// IvarListnfABIPtrTy. 637 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); 638 639 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, 640 const ObjCIvarDecl *Ivar, 641 unsigned long int offset); 642 643 /// GetOrEmitProtocol - Get the protocol object for the given 644 /// declaration, emitting it if necessary. The return value has type 645 /// ProtocolPtrTy. 646 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); 647 648 /// GetOrEmitProtocolRef - Get a forward reference to the protocol 649 /// object for the given declaration, emitting it if needed. These 650 /// forward references will be filled in with empty bodies if no 651 /// definition is seen. The return value has type ProtocolPtrTy. 652 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); 653 654 /// EmitProtocolList - Generate the list of referenced 655 /// protocols. The return value has type ProtocolListPtrTy. 656 llvm::Constant *EmitProtocolList(const std::string &Name, 657 ObjCProtocolDecl::protocol_iterator begin, 658 ObjCProtocolDecl::protocol_iterator end); 659 660 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, 661 QualType ResultType, 662 Selector Sel, 663 llvm::Value *Receiver, 664 QualType Arg0Ty, 665 bool IsSuper, 666 const CallArgList &CallArgs); 667 668 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, 669 /// for the given class. 670 llvm::Value *EmitClassRef(CGBuilderTy &Builder, 671 const ObjCInterfaceDecl *ID, 672 bool IsSuper = false); 673 674 /// EmitMetaClassRef - Return a Value * of the address of _class_t 675 /// meta-data 676 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, 677 const ObjCInterfaceDecl *ID); 678 679 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for 680 /// the given ivar. 681 /// 682 llvm::GlobalVariable * ObjCIvarOffsetVariable(std::string &Name, 683 const ObjCInterfaceDecl *ID, 684 const ObjCIvarDecl *Ivar); 685 686 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, 687 /// for the given selector. 688 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); 689 690public: 691 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); 692 // FIXME. All stubs for now! 693 virtual llvm::Function *ModuleInitFunction(); 694 695 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, 696 QualType ResultType, 697 Selector Sel, 698 llvm::Value *Receiver, 699 bool IsClassMessage, 700 const CallArgList &CallArgs); 701 702 virtual CodeGen::RValue 703 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, 704 QualType ResultType, 705 Selector Sel, 706 const ObjCInterfaceDecl *Class, 707 llvm::Value *Receiver, 708 bool IsClassMessage, 709 const CallArgList &CallArgs); 710 711 virtual llvm::Value *GetClass(CGBuilderTy &Builder, 712 const ObjCInterfaceDecl *ID); 713 714 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel) 715 { return EmitSelector(Builder, Sel); } 716 717 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); 718 719 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); 720 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, 721 const ObjCProtocolDecl *PD); 722 723 virtual llvm::Function *GetPropertyGetFunction(){ 724 return ObjCTypes.GetPropertyFn; 725 } 726 virtual llvm::Function *GetPropertySetFunction(){ 727 return ObjCTypes.SetPropertyFn; 728 } 729 virtual llvm::Function *EnumerationMutationFunction() { 730 return ObjCTypes.EnumerationMutationFn; 731 } 732 733 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, 734 const Stmt &S) { 735 CGF.ErrorUnsupported(&S, "try or synchronized statement"); 736 } 737 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, 738 const ObjCAtThrowStmt &S); 739 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, 740 llvm::Value *AddrWeakObj); 741 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, 742 llvm::Value *src, llvm::Value *dst); 743 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, 744 llvm::Value *src, llvm::Value *dest); 745 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, 746 llvm::Value *src, llvm::Value *dest); 747 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, 748 llvm::Value *src, llvm::Value *dest); 749 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, 750 QualType ObjectTy, 751 llvm::Value *BaseValue, 752 const ObjCIvarDecl *Ivar, 753 const FieldDecl *Field, 754 unsigned CVRQualifiers); 755 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, 756 ObjCInterfaceDecl *Interface, 757 const ObjCIvarDecl *Ivar); 758}; 759 760} // end anonymous namespace 761 762/* *** Helper Functions *** */ 763 764/// getConstantGEP() - Help routine to construct simple GEPs. 765static llvm::Constant *getConstantGEP(llvm::Constant *C, 766 unsigned idx0, 767 unsigned idx1) { 768 llvm::Value *Idxs[] = { 769 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0), 770 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1) 771 }; 772 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2); 773} 774 775/* *** CGObjCMac Public Interface *** */ 776 777CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), 778 ObjCTypes(cgm) 779{ 780 ObjCABI = 1; 781 EmitImageInfo(); 782} 783 784/// GetClass - Return a reference to the class for the given interface 785/// decl. 786llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, 787 const ObjCInterfaceDecl *ID) { 788 return EmitClassRef(Builder, ID); 789} 790 791/// GetSelector - Return the pointer to the unique'd string for this selector. 792llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) { 793 return EmitSelector(Builder, Sel); 794} 795 796/// Generate a constant CFString object. 797/* 798 struct __builtin_CFString { 799 const int *isa; // point to __CFConstantStringClassReference 800 int flags; 801 const char *str; 802 long length; 803 }; 804*/ 805 806llvm::Constant *CGObjCCommonMac::GenerateConstantString( 807 const std::string &String) { 808 return CGM.GetAddrOfConstantCFString(String); 809} 810 811/// Generates a message send where the super is the receiver. This is 812/// a message send to self with special delivery semantics indicating 813/// which class's method should be called. 814CodeGen::RValue 815CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, 816 QualType ResultType, 817 Selector Sel, 818 const ObjCInterfaceDecl *Class, 819 llvm::Value *Receiver, 820 bool IsClassMessage, 821 const CodeGen::CallArgList &CallArgs) { 822 // Create and init a super structure; this is a (receiver, class) 823 // pair we will pass to objc_msgSendSuper. 824 llvm::Value *ObjCSuper = 825 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); 826 llvm::Value *ReceiverAsObject = 827 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); 828 CGF.Builder.CreateStore(ReceiverAsObject, 829 CGF.Builder.CreateStructGEP(ObjCSuper, 0)); 830 831 // If this is a class message the metaclass is passed as the target. 832 llvm::Value *Target; 833 if (IsClassMessage) { 834 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); 835 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); 836 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); 837 Target = Super; 838 } else { 839 Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); 840 } 841 // FIXME: We shouldn't need to do this cast, rectify the ASTContext 842 // and ObjCTypes types. 843 const llvm::Type *ClassTy = 844 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); 845 Target = CGF.Builder.CreateBitCast(Target, ClassTy); 846 CGF.Builder.CreateStore(Target, 847 CGF.Builder.CreateStructGEP(ObjCSuper, 1)); 848 849 return EmitMessageSend(CGF, ResultType, Sel, 850 ObjCSuper, ObjCTypes.SuperPtrCTy, 851 true, CallArgs); 852} 853 854/// Generate code for a message send expression. 855CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, 856 QualType ResultType, 857 Selector Sel, 858 llvm::Value *Receiver, 859 bool IsClassMessage, 860 const CallArgList &CallArgs) { 861 llvm::Value *Arg0 = 862 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp"); 863 return EmitMessageSend(CGF, ResultType, Sel, 864 Arg0, CGF.getContext().getObjCIdType(), 865 false, CallArgs); 866} 867 868CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, 869 QualType ResultType, 870 Selector Sel, 871 llvm::Value *Arg0, 872 QualType Arg0Ty, 873 bool IsSuper, 874 const CallArgList &CallArgs) { 875 CallArgList ActualArgs; 876 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); 877 ActualArgs.push_back(std::make_pair(RValue::get(EmitSelector(CGF.Builder, 878 Sel)), 879 CGF.getContext().getObjCSelType())); 880 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); 881 882 CodeGenTypes &Types = CGM.getTypes(); 883 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); 884 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, false); 885 886 llvm::Constant *Fn; 887 if (CGM.ReturnTypeUsesSret(FnInfo)) { 888 Fn = ObjCTypes.getSendStretFn(IsSuper); 889 } else if (ResultType->isFloatingType()) { 890 // FIXME: Sadly, this is wrong. This actually depends on the 891 // architecture. This happens to be right for x86-32 though. 892 Fn = ObjCTypes.getSendFpretFn(IsSuper); 893 } else { 894 Fn = ObjCTypes.getSendFn(IsSuper); 895 } 896 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy)); 897 return CGF.EmitCall(FnInfo, Fn, ActualArgs); 898} 899 900llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, 901 const ObjCProtocolDecl *PD) { 902 // FIXME: I don't understand why gcc generates this, or where it is 903 // resolved. Investigate. Its also wasteful to look this up over and 904 // over. 905 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); 906 907 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), 908 ObjCTypes.ExternalProtocolPtrTy); 909} 910 911void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { 912 // FIXME: We shouldn't need this, the protocol decl should contain 913 // enough information to tell us whether this was a declaration or a 914 // definition. 915 DefinedProtocols.insert(PD->getIdentifier()); 916 917 // If we have generated a forward reference to this protocol, emit 918 // it now. Otherwise do nothing, the protocol objects are lazily 919 // emitted. 920 if (Protocols.count(PD->getIdentifier())) 921 GetOrEmitProtocol(PD); 922} 923 924llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { 925 if (DefinedProtocols.count(PD->getIdentifier())) 926 return GetOrEmitProtocol(PD); 927 return GetOrEmitProtocolRef(PD); 928} 929 930/* 931 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions 932 struct _objc_protocol { 933 struct _objc_protocol_extension *isa; 934 char *protocol_name; 935 struct _objc_protocol_list *protocol_list; 936 struct _objc__method_prototype_list *instance_methods; 937 struct _objc__method_prototype_list *class_methods 938 }; 939 940 See EmitProtocolExtension(). 941*/ 942llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { 943 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; 944 945 // Early exit if a defining object has already been generated. 946 if (Entry && Entry->hasInitializer()) 947 return Entry; 948 949 // FIXME: I don't understand why gcc generates this, or where it is 950 // resolved. Investigate. Its also wasteful to look this up over and 951 // over. 952 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); 953 954 const char *ProtocolName = PD->getNameAsCString(); 955 956 // Construct method lists. 957 std::vector<llvm::Constant*> InstanceMethods, ClassMethods; 958 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; 959 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(), 960 e = PD->instmeth_end(); i != e; ++i) { 961 ObjCMethodDecl *MD = *i; 962 llvm::Constant *C = GetMethodDescriptionConstant(MD); 963 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { 964 OptInstanceMethods.push_back(C); 965 } else { 966 InstanceMethods.push_back(C); 967 } 968 } 969 970 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(), 971 e = PD->classmeth_end(); i != e; ++i) { 972 ObjCMethodDecl *MD = *i; 973 llvm::Constant *C = GetMethodDescriptionConstant(MD); 974 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { 975 OptClassMethods.push_back(C); 976 } else { 977 ClassMethods.push_back(C); 978 } 979 } 980 981 std::vector<llvm::Constant*> Values(5); 982 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); 983 Values[1] = GetClassName(PD->getIdentifier()); 984 Values[2] = 985 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(), 986 PD->protocol_begin(), 987 PD->protocol_end()); 988 Values[3] = 989 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" 990 + PD->getNameAsString(), 991 "__OBJC,__cat_inst_meth,regular,no_dead_strip", 992 InstanceMethods); 993 Values[4] = 994 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" 995 + PD->getNameAsString(), 996 "__OBJC,__cat_cls_meth,regular,no_dead_strip", 997 ClassMethods); 998 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, 999 Values); 1000 1001 if (Entry) { 1002 // Already created, fix the linkage and update the initializer. 1003 Entry->setLinkage(llvm::GlobalValue::InternalLinkage); 1004 Entry->setInitializer(Init); 1005 } else { 1006 Entry = 1007 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, 1008 llvm::GlobalValue::InternalLinkage, 1009 Init, 1010 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName, 1011 &CGM.getModule()); 1012 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); 1013 UsedGlobals.push_back(Entry); 1014 // FIXME: Is this necessary? Why only for protocol? 1015 Entry->setAlignment(4); 1016 } 1017 1018 return Entry; 1019} 1020 1021llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { 1022 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; 1023 1024 if (!Entry) { 1025 // We use the initializer as a marker of whether this is a forward 1026 // reference or not. At module finalization we add the empty 1027 // contents for protocols which were referenced but never defined. 1028 Entry = 1029 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, 1030 llvm::GlobalValue::ExternalLinkage, 1031 0, 1032 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(), 1033 &CGM.getModule()); 1034 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); 1035 UsedGlobals.push_back(Entry); 1036 // FIXME: Is this necessary? Why only for protocol? 1037 Entry->setAlignment(4); 1038 } 1039 1040 return Entry; 1041} 1042 1043/* 1044 struct _objc_protocol_extension { 1045 uint32_t size; 1046 struct objc_method_description_list *optional_instance_methods; 1047 struct objc_method_description_list *optional_class_methods; 1048 struct objc_property_list *instance_properties; 1049 }; 1050*/ 1051llvm::Constant * 1052CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, 1053 const ConstantVector &OptInstanceMethods, 1054 const ConstantVector &OptClassMethods) { 1055 uint64_t Size = 1056 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolExtensionTy); 1057 std::vector<llvm::Constant*> Values(4); 1058 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); 1059 Values[1] = 1060 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" 1061 + PD->getNameAsString(), 1062 "__OBJC,__cat_inst_meth,regular,no_dead_strip", 1063 OptInstanceMethods); 1064 Values[2] = 1065 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" 1066 + PD->getNameAsString(), 1067 "__OBJC,__cat_cls_meth,regular,no_dead_strip", 1068 OptClassMethods); 1069 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + 1070 PD->getNameAsString(), 1071 0, PD, ObjCTypes); 1072 1073 // Return null if no extension bits are used. 1074 if (Values[1]->isNullValue() && Values[2]->isNullValue() && 1075 Values[3]->isNullValue()) 1076 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); 1077 1078 llvm::Constant *Init = 1079 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); 1080 llvm::GlobalVariable *GV = 1081 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false, 1082 llvm::GlobalValue::InternalLinkage, 1083 Init, 1084 "\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(), 1085 &CGM.getModule()); 1086 // No special section, but goes in llvm.used 1087 UsedGlobals.push_back(GV); 1088 1089 return GV; 1090} 1091 1092/* 1093 struct objc_protocol_list { 1094 struct objc_protocol_list *next; 1095 long count; 1096 Protocol *list[]; 1097 }; 1098*/ 1099llvm::Constant * 1100CGObjCMac::EmitProtocolList(const std::string &Name, 1101 ObjCProtocolDecl::protocol_iterator begin, 1102 ObjCProtocolDecl::protocol_iterator end) { 1103 std::vector<llvm::Constant*> ProtocolRefs; 1104 1105 for (; begin != end; ++begin) 1106 ProtocolRefs.push_back(GetProtocolRef(*begin)); 1107 1108 // Just return null for empty protocol lists 1109 if (ProtocolRefs.empty()) 1110 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); 1111 1112 // This list is null terminated. 1113 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); 1114 1115 std::vector<llvm::Constant*> Values(3); 1116 // This field is only used by the runtime. 1117 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); 1118 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); 1119 Values[2] = 1120 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, 1121 ProtocolRefs.size()), 1122 ProtocolRefs); 1123 1124 llvm::Constant *Init = llvm::ConstantStruct::get(Values); 1125 llvm::GlobalVariable *GV = 1126 new llvm::GlobalVariable(Init->getType(), false, 1127 llvm::GlobalValue::InternalLinkage, 1128 Init, 1129 Name, 1130 &CGM.getModule()); 1131 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip"); 1132 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); 1133} 1134 1135/* 1136 struct _objc_property { 1137 const char * const name; 1138 const char * const attributes; 1139 }; 1140 1141 struct _objc_property_list { 1142 uint32_t entsize; // sizeof (struct _objc_property) 1143 uint32_t prop_count; 1144 struct _objc_property[prop_count]; 1145 }; 1146*/ 1147llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name, 1148 const Decl *Container, 1149 const ObjCContainerDecl *OCD, 1150 const ObjCCommonTypesHelper &ObjCTypes) { 1151 std::vector<llvm::Constant*> Properties, Prop(2); 1152 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(), 1153 E = OCD->prop_end(); I != E; ++I) { 1154 const ObjCPropertyDecl *PD = *I; 1155 Prop[0] = GetPropertyName(PD->getIdentifier()); 1156 Prop[1] = GetPropertyTypeString(PD, Container); 1157 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, 1158 Prop)); 1159 } 1160 1161 // Return null for empty list. 1162 if (Properties.empty()) 1163 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); 1164 1165 unsigned PropertySize = 1166 CGM.getTargetData().getTypePaddedSize(ObjCTypes.PropertyTy); 1167 std::vector<llvm::Constant*> Values(3); 1168 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); 1169 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); 1170 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, 1171 Properties.size()); 1172 Values[2] = llvm::ConstantArray::get(AT, Properties); 1173 llvm::Constant *Init = llvm::ConstantStruct::get(Values); 1174 1175 llvm::GlobalVariable *GV = 1176 new llvm::GlobalVariable(Init->getType(), false, 1177 llvm::GlobalValue::InternalLinkage, 1178 Init, 1179 Name, 1180 &CGM.getModule()); 1181 if (ObjCABI == 2) 1182 GV->setSection("__DATA, __objc_const"); 1183 // No special section on property lists? 1184 UsedGlobals.push_back(GV); 1185 return llvm::ConstantExpr::getBitCast(GV, 1186 ObjCTypes.PropertyListPtrTy); 1187 1188} 1189 1190/* 1191 struct objc_method_description_list { 1192 int count; 1193 struct objc_method_description list[]; 1194 }; 1195*/ 1196llvm::Constant * 1197CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { 1198 std::vector<llvm::Constant*> Desc(2); 1199 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), 1200 ObjCTypes.SelectorPtrTy); 1201 Desc[1] = GetMethodVarType(MD); 1202 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, 1203 Desc); 1204} 1205 1206llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name, 1207 const char *Section, 1208 const ConstantVector &Methods) { 1209 // Return null for empty list. 1210 if (Methods.empty()) 1211 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); 1212 1213 std::vector<llvm::Constant*> Values(2); 1214 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); 1215 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, 1216 Methods.size()); 1217 Values[1] = llvm::ConstantArray::get(AT, Methods); 1218 llvm::Constant *Init = llvm::ConstantStruct::get(Values); 1219 1220 llvm::GlobalVariable *GV = 1221 new llvm::GlobalVariable(Init->getType(), false, 1222 llvm::GlobalValue::InternalLinkage, 1223 Init, Name, &CGM.getModule()); 1224 GV->setSection(Section); 1225 UsedGlobals.push_back(GV); 1226 return llvm::ConstantExpr::getBitCast(GV, 1227 ObjCTypes.MethodDescriptionListPtrTy); 1228} 1229 1230/* 1231 struct _objc_category { 1232 char *category_name; 1233 char *class_name; 1234 struct _objc_method_list *instance_methods; 1235 struct _objc_method_list *class_methods; 1236 struct _objc_protocol_list *protocols; 1237 uint32_t size; // <rdar://4585769> 1238 struct _objc_property_list *instance_properties; 1239 }; 1240 */ 1241void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { 1242 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.CategoryTy); 1243 1244 // FIXME: This is poor design, the OCD should have a pointer to the 1245 // category decl. Additionally, note that Category can be null for 1246 // the @implementation w/o an @interface case. Sema should just 1247 // create one for us as it does for @implementation so everyone else 1248 // can live life under a clear blue sky. 1249 const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); 1250 const ObjCCategoryDecl *Category = 1251 Interface->FindCategoryDeclaration(OCD->getIdentifier()); 1252 std::string ExtName(Interface->getNameAsString() + "_" + 1253 OCD->getNameAsString()); 1254 1255 std::vector<llvm::Constant*> InstanceMethods, ClassMethods; 1256 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), 1257 e = OCD->instmeth_end(); i != e; ++i) { 1258 // Instance methods should always be defined. 1259 InstanceMethods.push_back(GetMethodConstant(*i)); 1260 } 1261 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), 1262 e = OCD->classmeth_end(); i != e; ++i) { 1263 // Class methods should always be defined. 1264 ClassMethods.push_back(GetMethodConstant(*i)); 1265 } 1266 1267 std::vector<llvm::Constant*> Values(7); 1268 Values[0] = GetClassName(OCD->getIdentifier()); 1269 Values[1] = GetClassName(Interface->getIdentifier()); 1270 Values[2] = 1271 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") + 1272 ExtName, 1273 "__OBJC,__cat_inst_meth,regular,no_dead_strip", 1274 InstanceMethods); 1275 Values[3] = 1276 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName, 1277 "__OBJC,__cat_class_meth,regular,no_dead_strip", 1278 ClassMethods); 1279 if (Category) { 1280 Values[4] = 1281 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName, 1282 Category->protocol_begin(), 1283 Category->protocol_end()); 1284 } else { 1285 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); 1286 } 1287 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); 1288 1289 // If there is no category @interface then there can be no properties. 1290 if (Category) { 1291 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName, 1292 OCD, Category, ObjCTypes); 1293 } else { 1294 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); 1295 } 1296 1297 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, 1298 Values); 1299 1300 llvm::GlobalVariable *GV = 1301 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false, 1302 llvm::GlobalValue::InternalLinkage, 1303 Init, 1304 std::string("\01L_OBJC_CATEGORY_")+ExtName, 1305 &CGM.getModule()); 1306 GV->setSection("__OBJC,__category,regular,no_dead_strip"); 1307 UsedGlobals.push_back(GV); 1308 DefinedCategories.push_back(GV); 1309} 1310 1311// FIXME: Get from somewhere? 1312enum ClassFlags { 1313 eClassFlags_Factory = 0x00001, 1314 eClassFlags_Meta = 0x00002, 1315 // <rdr://5142207> 1316 eClassFlags_HasCXXStructors = 0x02000, 1317 eClassFlags_Hidden = 0x20000, 1318 eClassFlags_ABI2_Hidden = 0x00010, 1319 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> 1320}; 1321 1322// <rdr://5142207&4705298&4843145> 1323static bool IsClassHidden(const ObjCInterfaceDecl *ID) { 1324 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) { 1325 // FIXME: Support -fvisibility 1326 switch (attr->getVisibility()) { 1327 default: 1328 assert(0 && "Unknown visibility"); 1329 return false; 1330 case VisibilityAttr::DefaultVisibility: 1331 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here? 1332 return false; 1333 case VisibilityAttr::HiddenVisibility: 1334 return true; 1335 } 1336 } else { 1337 return false; // FIXME: Support -fvisibility 1338 } 1339} 1340 1341/* 1342 struct _objc_class { 1343 Class isa; 1344 Class super_class; 1345 const char *name; 1346 long version; 1347 long info; 1348 long instance_size; 1349 struct _objc_ivar_list *ivars; 1350 struct _objc_method_list *methods; 1351 struct _objc_cache *cache; 1352 struct _objc_protocol_list *protocols; 1353 // Objective-C 1.0 extensions (<rdr://4585769>) 1354 const char *ivar_layout; 1355 struct _objc_class_ext *ext; 1356 }; 1357 1358 See EmitClassExtension(); 1359 */ 1360void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { 1361 DefinedSymbols.insert(ID->getIdentifier()); 1362 1363 std::string ClassName = ID->getNameAsString(); 1364 // FIXME: Gross 1365 ObjCInterfaceDecl *Interface = 1366 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); 1367 llvm::Constant *Protocols = 1368 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(), 1369 Interface->protocol_begin(), 1370 Interface->protocol_end()); 1371 const llvm::Type *InterfaceTy = 1372 CGM.getTypes().ConvertType(CGM.getContext().buildObjCInterfaceType(Interface)); 1373 unsigned Flags = eClassFlags_Factory; 1374 unsigned Size = CGM.getTargetData().getTypePaddedSize(InterfaceTy); 1375 1376 // FIXME: Set CXX-structors flag. 1377 if (IsClassHidden(ID->getClassInterface())) 1378 Flags |= eClassFlags_Hidden; 1379 1380 std::vector<llvm::Constant*> InstanceMethods, ClassMethods; 1381 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), 1382 e = ID->instmeth_end(); i != e; ++i) { 1383 // Instance methods should always be defined. 1384 InstanceMethods.push_back(GetMethodConstant(*i)); 1385 } 1386 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), 1387 e = ID->classmeth_end(); i != e; ++i) { 1388 // Class methods should always be defined. 1389 ClassMethods.push_back(GetMethodConstant(*i)); 1390 } 1391 1392 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), 1393 e = ID->propimpl_end(); i != e; ++i) { 1394 ObjCPropertyImplDecl *PID = *i; 1395 1396 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { 1397 ObjCPropertyDecl *PD = PID->getPropertyDecl(); 1398 1399 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) 1400 if (llvm::Constant *C = GetMethodConstant(MD)) 1401 InstanceMethods.push_back(C); 1402 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) 1403 if (llvm::Constant *C = GetMethodConstant(MD)) 1404 InstanceMethods.push_back(C); 1405 } 1406 } 1407 1408 std::vector<llvm::Constant*> Values(12); 1409 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods); 1410 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { 1411 // Record a reference to the super class. 1412 LazySymbols.insert(Super->getIdentifier()); 1413 1414 Values[ 1] = 1415 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), 1416 ObjCTypes.ClassPtrTy); 1417 } else { 1418 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); 1419 } 1420 Values[ 2] = GetClassName(ID->getIdentifier()); 1421 // Version is always 0. 1422 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); 1423 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); 1424 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); 1425 Values[ 6] = EmitIvarList(ID, false); 1426 Values[ 7] = 1427 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(), 1428 "__OBJC,__inst_meth,regular,no_dead_strip", 1429 InstanceMethods); 1430 // cache is always NULL. 1431 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); 1432 Values[ 9] = Protocols; 1433 // FIXME: Set ivar_layout 1434 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); 1435 Values[11] = EmitClassExtension(ID); 1436 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, 1437 Values); 1438 1439 llvm::GlobalVariable *GV = 1440 new llvm::GlobalVariable(ObjCTypes.ClassTy, false, 1441 llvm::GlobalValue::InternalLinkage, 1442 Init, 1443 std::string("\01L_OBJC_CLASS_")+ClassName, 1444 &CGM.getModule()); 1445 GV->setSection("__OBJC,__class,regular,no_dead_strip"); 1446 UsedGlobals.push_back(GV); 1447 // FIXME: Why? 1448 GV->setAlignment(32); 1449 DefinedClasses.push_back(GV); 1450} 1451 1452llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, 1453 llvm::Constant *Protocols, 1454 const llvm::Type *InterfaceTy, 1455 const ConstantVector &Methods) { 1456 unsigned Flags = eClassFlags_Meta; 1457 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassTy); 1458 1459 if (IsClassHidden(ID->getClassInterface())) 1460 Flags |= eClassFlags_Hidden; 1461 1462 std::vector<llvm::Constant*> Values(12); 1463 // The isa for the metaclass is the root of the hierarchy. 1464 const ObjCInterfaceDecl *Root = ID->getClassInterface(); 1465 while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) 1466 Root = Super; 1467 Values[ 0] = 1468 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), 1469 ObjCTypes.ClassPtrTy); 1470 // The super class for the metaclass is emitted as the name of the 1471 // super class. The runtime fixes this up to point to the 1472 // *metaclass* for the super class. 1473 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { 1474 Values[ 1] = 1475 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), 1476 ObjCTypes.ClassPtrTy); 1477 } else { 1478 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); 1479 } 1480 Values[ 2] = GetClassName(ID->getIdentifier()); 1481 // Version is always 0. 1482 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); 1483 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); 1484 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); 1485 Values[ 6] = EmitIvarList(ID, true); 1486 Values[ 7] = 1487 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), 1488 "__OBJC,__inst_meth,regular,no_dead_strip", 1489 Methods); 1490 // cache is always NULL. 1491 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); 1492 Values[ 9] = Protocols; 1493 // ivar_layout for metaclass is always NULL. 1494 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); 1495 // The class extension is always unused for metaclasses. 1496 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); 1497 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, 1498 Values); 1499 1500 std::string Name("\01L_OBJC_METACLASS_"); 1501 Name += ID->getNameAsCString(); 1502 1503 // Check for a forward reference. 1504 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); 1505 if (GV) { 1506 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && 1507 "Forward metaclass reference has incorrect type."); 1508 GV->setLinkage(llvm::GlobalValue::InternalLinkage); 1509 GV->setInitializer(Init); 1510 } else { 1511 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false, 1512 llvm::GlobalValue::InternalLinkage, 1513 Init, Name, 1514 &CGM.getModule()); 1515 } 1516 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); 1517 UsedGlobals.push_back(GV); 1518 // FIXME: Why? 1519 GV->setAlignment(32); 1520 1521 return GV; 1522} 1523 1524llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { 1525 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); 1526 1527 // FIXME: Should we look these up somewhere other than the 1528 // module. Its a bit silly since we only generate these while 1529 // processing an implementation, so exactly one pointer would work 1530 // if know when we entered/exitted an implementation block. 1531 1532 // Check for an existing forward reference. 1533 // Previously, metaclass with internal linkage may have been defined. 1534 // pass 'true' as 2nd argument so it is returned. 1535 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) { 1536 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && 1537 "Forward metaclass reference has incorrect type."); 1538 return GV; 1539 } else { 1540 // Generate as an external reference to keep a consistent 1541 // module. This will be patched up when we emit the metaclass. 1542 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false, 1543 llvm::GlobalValue::ExternalLinkage, 1544 0, 1545 Name, 1546 &CGM.getModule()); 1547 } 1548} 1549 1550/* 1551 struct objc_class_ext { 1552 uint32_t size; 1553 const char *weak_ivar_layout; 1554 struct _objc_property_list *properties; 1555 }; 1556*/ 1557llvm::Constant * 1558CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { 1559 uint64_t Size = 1560 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassExtensionTy); 1561 1562 std::vector<llvm::Constant*> Values(3); 1563 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); 1564 // FIXME: Output weak_ivar_layout string. 1565 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); 1566 Values[2] = EmitPropertyList("\01L_OBJC_$_PROP_LIST_" + ID->getNameAsString(), 1567 ID, ID->getClassInterface(), ObjCTypes); 1568 1569 // Return null if no extension bits are used. 1570 if (Values[1]->isNullValue() && Values[2]->isNullValue()) 1571 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); 1572 1573 llvm::Constant *Init = 1574 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); 1575 llvm::GlobalVariable *GV = 1576 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false, 1577 llvm::GlobalValue::InternalLinkage, 1578 Init, 1579 "\01L_OBJC_CLASSEXT_" + ID->getNameAsString(), 1580 &CGM.getModule()); 1581 // No special section, but goes in llvm.used 1582 UsedGlobals.push_back(GV); 1583 1584 return GV; 1585} 1586 1587/// countInheritedIvars - count number of ivars in class and its super class(s) 1588/// 1589static int countInheritedIvars(const ObjCInterfaceDecl *OI) { 1590 int count = 0; 1591 if (!OI) 1592 return 0; 1593 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass(); 1594 if (SuperClass) 1595 count += countInheritedIvars(SuperClass); 1596 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), 1597 E = OI->ivar_end(); I != E; ++I) 1598 ++count; 1599 return count; 1600} 1601 1602/// getInterfaceDeclForIvar - Get the interface declaration node where 1603/// this ivar is declared in. 1604/// FIXME. Ideally, this info should be in the ivar node. But currently 1605/// it is not and prevailing wisdom is that ASTs should not have more 1606/// info than is absolutely needed, even though this info reflects the 1607/// source language. 1608/// 1609static const ObjCInterfaceDecl *getInterfaceDeclForIvar( 1610 const ObjCInterfaceDecl *OI, 1611 const ObjCIvarDecl *IVD) { 1612 if (!OI) 1613 return 0; 1614 assert(isa<ObjCInterfaceDecl>(OI) && "OI is not an interface"); 1615 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(), 1616 E = OI->ivar_end(); I != E; ++I) 1617 if ((*I)->getIdentifier() == IVD->getIdentifier()) 1618 return OI; 1619 return getInterfaceDeclForIvar(OI->getSuperClass(), IVD); 1620} 1621 1622/* 1623 struct objc_ivar { 1624 char *ivar_name; 1625 char *ivar_type; 1626 int ivar_offset; 1627 }; 1628 1629 struct objc_ivar_list { 1630 int ivar_count; 1631 struct objc_ivar list[count]; 1632 }; 1633 */ 1634llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, 1635 bool ForClass) { 1636 std::vector<llvm::Constant*> Ivars, Ivar(3); 1637 1638 // When emitting the root class GCC emits ivar entries for the 1639 // actual class structure. It is not clear if we need to follow this 1640 // behavior; for now lets try and get away with not doing it. If so, 1641 // the cleanest solution would be to make up an ObjCInterfaceDecl 1642 // for the class. 1643 if (ForClass) 1644 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); 1645 1646 ObjCInterfaceDecl *OID = 1647 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); 1648 const llvm::Type *InterfaceTy = 1649 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(OID)); 1650 const llvm::StructLayout *Layout = 1651 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy)); 1652 1653 RecordDecl::field_iterator ifield, pfield; 1654 const RecordDecl *RD = GetFirstIvarInRecord(OID, ifield, pfield); 1655 for (RecordDecl::field_iterator e = RD->field_end(); ifield != e; ++ifield) { 1656 FieldDecl *Field = *ifield; 1657 unsigned Offset = Layout->getElementOffset(CGM.getTypes(). 1658 getLLVMFieldNo(Field)); 1659 if (Field->getIdentifier()) 1660 Ivar[0] = GetMethodVarName(Field->getIdentifier()); 1661 else 1662 Ivar[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); 1663 std::string TypeStr; 1664 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); 1665 Ivar[1] = GetMethodVarType(TypeStr); 1666 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset); 1667 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); 1668 } 1669 1670 // Return null for empty list. 1671 if (Ivars.empty()) 1672 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); 1673 1674 std::vector<llvm::Constant*> Values(2); 1675 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); 1676 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, 1677 Ivars.size()); 1678 Values[1] = llvm::ConstantArray::get(AT, Ivars); 1679 llvm::Constant *Init = llvm::ConstantStruct::get(Values); 1680 1681 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" : 1682 "\01L_OBJC_INSTANCE_VARIABLES_"); 1683 llvm::GlobalVariable *GV = 1684 new llvm::GlobalVariable(Init->getType(), false, 1685 llvm::GlobalValue::InternalLinkage, 1686 Init, 1687 Prefix + ID->getNameAsString(), 1688 &CGM.getModule()); 1689 if (ForClass) { 1690 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip"); 1691 // FIXME: Why is this only here? 1692 GV->setAlignment(32); 1693 } else { 1694 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip"); 1695 } 1696 UsedGlobals.push_back(GV); 1697 return llvm::ConstantExpr::getBitCast(GV, 1698 ObjCTypes.IvarListPtrTy); 1699} 1700 1701/* 1702 struct objc_method { 1703 SEL method_name; 1704 char *method_types; 1705 void *method; 1706 }; 1707 1708 struct objc_method_list { 1709 struct objc_method_list *obsolete; 1710 int count; 1711 struct objc_method methods_list[count]; 1712 }; 1713*/ 1714 1715/// GetMethodConstant - Return a struct objc_method constant for the 1716/// given method if it has been defined. The result is null if the 1717/// method has not been defined. The return value has type MethodPtrTy. 1718llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { 1719 // FIXME: Use DenseMap::lookup 1720 llvm::Function *Fn = MethodDefinitions[MD]; 1721 if (!Fn) 1722 return 0; 1723 1724 std::vector<llvm::Constant*> Method(3); 1725 Method[0] = 1726 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), 1727 ObjCTypes.SelectorPtrTy); 1728 Method[1] = GetMethodVarType(MD); 1729 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); 1730 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); 1731} 1732 1733llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name, 1734 const char *Section, 1735 const ConstantVector &Methods) { 1736 // Return null for empty list. 1737 if (Methods.empty()) 1738 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); 1739 1740 std::vector<llvm::Constant*> Values(3); 1741 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); 1742 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); 1743 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, 1744 Methods.size()); 1745 Values[2] = llvm::ConstantArray::get(AT, Methods); 1746 llvm::Constant *Init = llvm::ConstantStruct::get(Values); 1747 1748 llvm::GlobalVariable *GV = 1749 new llvm::GlobalVariable(Init->getType(), false, 1750 llvm::GlobalValue::InternalLinkage, 1751 Init, 1752 Name, 1753 &CGM.getModule()); 1754 GV->setSection(Section); 1755 UsedGlobals.push_back(GV); 1756 return llvm::ConstantExpr::getBitCast(GV, 1757 ObjCTypes.MethodListPtrTy); 1758} 1759 1760llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, 1761 const ObjCContainerDecl *CD) { 1762 std::string Name; 1763 GetNameForMethod(OMD, CD, Name); 1764 1765 CodeGenTypes &Types = CGM.getTypes(); 1766 const llvm::FunctionType *MethodTy = 1767 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); 1768 llvm::Function *Method = 1769 llvm::Function::Create(MethodTy, 1770 llvm::GlobalValue::InternalLinkage, 1771 Name, 1772 &CGM.getModule()); 1773 MethodDefinitions.insert(std::make_pair(OMD, Method)); 1774 1775 return Method; 1776} 1777 1778llvm::Function *CGObjCMac::ModuleInitFunction() { 1779 // Abuse this interface function as a place to finalize. 1780 FinishModule(); 1781 1782 return NULL; 1783} 1784 1785llvm::Function *CGObjCMac::GetPropertyGetFunction() { 1786 return ObjCTypes.GetPropertyFn; 1787} 1788 1789llvm::Function *CGObjCMac::GetPropertySetFunction() { 1790 return ObjCTypes.SetPropertyFn; 1791} 1792 1793llvm::Function *CGObjCMac::EnumerationMutationFunction() 1794{ 1795 return ObjCTypes.EnumerationMutationFn; 1796} 1797 1798/* 1799 1800Objective-C setjmp-longjmp (sjlj) Exception Handling 1801-- 1802 1803The basic framework for a @try-catch-finally is as follows: 1804{ 1805 objc_exception_data d; 1806 id _rethrow = null; 1807 bool _call_try_exit = true; 1808 1809 objc_exception_try_enter(&d); 1810 if (!setjmp(d.jmp_buf)) { 1811 ... try body ... 1812 } else { 1813 // exception path 1814 id _caught = objc_exception_extract(&d); 1815 1816 // enter new try scope for handlers 1817 if (!setjmp(d.jmp_buf)) { 1818 ... match exception and execute catch blocks ... 1819 1820 // fell off end, rethrow. 1821 _rethrow = _caught; 1822 ... jump-through-finally to finally_rethrow ... 1823 } else { 1824 // exception in catch block 1825 _rethrow = objc_exception_extract(&d); 1826 _call_try_exit = false; 1827 ... jump-through-finally to finally_rethrow ... 1828 } 1829 } 1830 ... jump-through-finally to finally_end ... 1831 1832finally: 1833 if (_call_try_exit) 1834 objc_exception_try_exit(&d); 1835 1836 ... finally block .... 1837 ... dispatch to finally destination ... 1838 1839finally_rethrow: 1840 objc_exception_throw(_rethrow); 1841 1842finally_end: 1843} 1844 1845This framework differs slightly from the one gcc uses, in that gcc 1846uses _rethrow to determine if objc_exception_try_exit should be called 1847and if the object should be rethrown. This breaks in the face of 1848throwing nil and introduces unnecessary branches. 1849 1850We specialize this framework for a few particular circumstances: 1851 1852 - If there are no catch blocks, then we avoid emitting the second 1853 exception handling context. 1854 1855 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id 1856 e)) we avoid emitting the code to rethrow an uncaught exception. 1857 1858 - FIXME: If there is no @finally block we can do a few more 1859 simplifications. 1860 1861Rethrows and Jumps-Through-Finally 1862-- 1863 1864Support for implicit rethrows and jumping through the finally block is 1865handled by storing the current exception-handling context in 1866ObjCEHStack. 1867 1868In order to implement proper @finally semantics, we support one basic 1869mechanism for jumping through the finally block to an arbitrary 1870destination. Constructs which generate exits from a @try or @catch 1871block use this mechanism to implement the proper semantics by chaining 1872jumps, as necessary. 1873 1874This mechanism works like the one used for indirect goto: we 1875arbitrarily assign an ID to each destination and store the ID for the 1876destination in a variable prior to entering the finally block. At the 1877end of the finally block we simply create a switch to the proper 1878destination. 1879 1880Code gen for @synchronized(expr) stmt; 1881Effectively generating code for: 1882objc_sync_enter(expr); 1883@try stmt @finally { objc_sync_exit(expr); } 1884*/ 1885 1886void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, 1887 const Stmt &S) { 1888 bool isTry = isa<ObjCAtTryStmt>(S); 1889 // Create various blocks we refer to for handling @finally. 1890 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); 1891 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit"); 1892 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit"); 1893 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); 1894 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); 1895 1896 // Push an EH context entry, used for handling rethrows and jumps 1897 // through finally. 1898 CGF.PushCleanupBlock(FinallyBlock); 1899 1900 CGF.ObjCEHValueStack.push_back(0); 1901 1902 // Allocate memory for the exception data and rethrow pointer. 1903 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, 1904 "exceptiondata.ptr"); 1905 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy, 1906 "_rethrow"); 1907 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty, 1908 "_call_try_exit"); 1909 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr); 1910 1911 if (!isTry) { 1912 // For @synchronized, call objc_sync_enter(sync.expr) 1913 llvm::Value *Arg = CGF.EmitScalarExpr( 1914 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); 1915 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy); 1916 CGF.Builder.CreateCall(ObjCTypes.SyncEnterFn, Arg); 1917 } 1918 1919 // Enter a new try block and call setjmp. 1920 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData); 1921 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0, 1922 "jmpbufarray"); 1923 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp"); 1924 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn, 1925 JmpBufPtr, "result"); 1926 1927 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); 1928 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); 1929 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"), 1930 TryHandler, TryBlock); 1931 1932 // Emit the @try block. 1933 CGF.EmitBlock(TryBlock); 1934 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() 1935 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); 1936 CGF.EmitBranchThroughCleanup(FinallyEnd); 1937 1938 // Emit the "exception in @try" block. 1939 CGF.EmitBlock(TryHandler); 1940 1941 // Retrieve the exception object. We may emit multiple blocks but 1942 // nothing can cross this so the value is already in SSA form. 1943 llvm::Value *Caught = CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn, 1944 ExceptionData, 1945 "caught"); 1946 CGF.ObjCEHValueStack.back() = Caught; 1947 if (!isTry) 1948 { 1949 CGF.Builder.CreateStore(Caught, RethrowPtr); 1950 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); 1951 CGF.EmitBranchThroughCleanup(FinallyRethrow); 1952 } 1953 else if (const ObjCAtCatchStmt* CatchStmt = 1954 cast<ObjCAtTryStmt>(S).getCatchStmts()) 1955 { 1956 // Enter a new exception try block (in case a @catch block throws 1957 // an exception). 1958 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryEnterFn, ExceptionData); 1959 1960 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.SetJmpFn, 1961 JmpBufPtr, "result"); 1962 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"); 1963 1964 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch"); 1965 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler"); 1966 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); 1967 1968 CGF.EmitBlock(CatchBlock); 1969 1970 // Handle catch list. As a special case we check if everything is 1971 // matched and avoid generating code for falling off the end if 1972 // so. 1973 bool AllMatched = false; 1974 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { 1975 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch"); 1976 1977 const DeclStmt *CatchParam = 1978 cast_or_null<DeclStmt>(CatchStmt->getCatchParamStmt()); 1979 const VarDecl *VD = 0; 1980 const PointerType *PT = 0; 1981 1982 // catch(...) always matches. 1983 if (!CatchParam) { 1984 AllMatched = true; 1985 } else { 1986 VD = cast<VarDecl>(CatchParam->getSolitaryDecl()); 1987 PT = VD->getType()->getAsPointerType(); 1988 1989 // catch(id e) always matches. 1990 // FIXME: For the time being we also match id<X>; this should 1991 // be rejected by Sema instead. 1992 if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) || 1993 VD->getType()->isObjCQualifiedIdType()) 1994 AllMatched = true; 1995 } 1996 1997 if (AllMatched) { 1998 if (CatchParam) { 1999 CGF.EmitStmt(CatchParam); 2000 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); 2001 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(VD)); 2002 } 2003 2004 CGF.EmitStmt(CatchStmt->getCatchBody()); 2005 CGF.EmitBranchThroughCleanup(FinallyEnd); 2006 break; 2007 } 2008 2009 assert(PT && "Unexpected non-pointer type in @catch"); 2010 QualType T = PT->getPointeeType(); 2011 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType(); 2012 assert(ObjCType && "Catch parameter must have Objective-C type!"); 2013 2014 // Check if the @catch block matches the exception object. 2015 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl()); 2016 2017 llvm::Value *Match = CGF.Builder.CreateCall2(ObjCTypes.ExceptionMatchFn, 2018 Class, Caught, "match"); 2019 2020 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched"); 2021 2022 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), 2023 MatchedBlock, NextCatchBlock); 2024 2025 // Emit the @catch block. 2026 CGF.EmitBlock(MatchedBlock); 2027 CGF.EmitStmt(CatchParam); 2028 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); 2029 2030 llvm::Value *Tmp = 2031 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(VD->getType()), 2032 "tmp"); 2033 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(VD)); 2034 2035 CGF.EmitStmt(CatchStmt->getCatchBody()); 2036 CGF.EmitBranchThroughCleanup(FinallyEnd); 2037 2038 CGF.EmitBlock(NextCatchBlock); 2039 } 2040 2041 if (!AllMatched) { 2042 // None of the handlers caught the exception, so store it to be 2043 // rethrown at the end of the @finally block. 2044 CGF.Builder.CreateStore(Caught, RethrowPtr); 2045 CGF.EmitBranchThroughCleanup(FinallyRethrow); 2046 } 2047 2048 // Emit the exception handler for the @catch blocks. 2049 CGF.EmitBlock(CatchHandler); 2050 CGF.Builder.CreateStore(CGF.Builder.CreateCall(ObjCTypes.ExceptionExtractFn, 2051 ExceptionData), 2052 RethrowPtr); 2053 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); 2054 CGF.EmitBranchThroughCleanup(FinallyRethrow); 2055 } else { 2056 CGF.Builder.CreateStore(Caught, RethrowPtr); 2057 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr); 2058 CGF.EmitBranchThroughCleanup(FinallyRethrow); 2059 } 2060 2061 // Pop the exception-handling stack entry. It is important to do 2062 // this now, because the code in the @finally block is not in this 2063 // context. 2064 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); 2065 2066 CGF.ObjCEHValueStack.pop_back(); 2067 2068 // Emit the @finally block. 2069 CGF.EmitBlock(FinallyBlock); 2070 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp"); 2071 2072 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit); 2073 2074 CGF.EmitBlock(FinallyExit); 2075 CGF.Builder.CreateCall(ObjCTypes.ExceptionTryExitFn, ExceptionData); 2076 2077 CGF.EmitBlock(FinallyNoExit); 2078 if (isTry) { 2079 if (const ObjCAtFinallyStmt* FinallyStmt = 2080 cast<ObjCAtTryStmt>(S).getFinallyStmt()) 2081 CGF.EmitStmt(FinallyStmt->getFinallyBody()); 2082 } 2083 else { 2084 // For @synchronized objc_sync_exit(expr); As finally's sole statement. 2085 // For @synchronized, call objc_sync_enter(sync.expr) 2086 llvm::Value *Arg = CGF.EmitScalarExpr( 2087 cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); 2088 Arg = CGF.Builder.CreateBitCast(Arg, ObjCTypes.ObjectPtrTy); 2089 CGF.Builder.CreateCall(ObjCTypes.SyncExitFn, Arg); 2090 } 2091 2092 // Emit the switch block 2093 if (Info.SwitchBlock) 2094 CGF.EmitBlock(Info.SwitchBlock); 2095 if (Info.EndBlock) 2096 CGF.EmitBlock(Info.EndBlock); 2097 2098 CGF.EmitBlock(FinallyRethrow); 2099 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, 2100 CGF.Builder.CreateLoad(RethrowPtr)); 2101 CGF.Builder.CreateUnreachable(); 2102 2103 CGF.EmitBlock(FinallyEnd); 2104} 2105 2106void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, 2107 const ObjCAtThrowStmt &S) { 2108 llvm::Value *ExceptionAsObject; 2109 2110 if (const Expr *ThrowExpr = S.getThrowExpr()) { 2111 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); 2112 ExceptionAsObject = 2113 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); 2114 } else { 2115 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && 2116 "Unexpected rethrow outside @catch block."); 2117 ExceptionAsObject = CGF.ObjCEHValueStack.back(); 2118 } 2119 2120 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject); 2121 CGF.Builder.CreateUnreachable(); 2122 2123 // Clear the insertion point to indicate we are in unreachable code. 2124 CGF.Builder.ClearInsertionPoint(); 2125} 2126 2127/// EmitObjCWeakRead - Code gen for loading value of a __weak 2128/// object: objc_read_weak (id *src) 2129/// 2130llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, 2131 llvm::Value *AddrWeakObj) 2132{ 2133 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); 2134 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn, 2135 AddrWeakObj, "weakread"); 2136 return read_weak; 2137} 2138 2139/// EmitObjCWeakAssign - Code gen for assigning to a __weak object. 2140/// objc_assign_weak (id src, id *dst) 2141/// 2142void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, 2143 llvm::Value *src, llvm::Value *dst) 2144{ 2145 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); 2146 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); 2147 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn, 2148 src, dst, "weakassign"); 2149 return; 2150} 2151 2152/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. 2153/// objc_assign_global (id src, id *dst) 2154/// 2155void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, 2156 llvm::Value *src, llvm::Value *dst) 2157{ 2158 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); 2159 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); 2160 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn, 2161 src, dst, "globalassign"); 2162 return; 2163} 2164 2165/// EmitObjCIvarAssign - Code gen for assigning to a __strong object. 2166/// objc_assign_ivar (id src, id *dst) 2167/// 2168void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, 2169 llvm::Value *src, llvm::Value *dst) 2170{ 2171 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); 2172 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); 2173 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn, 2174 src, dst, "assignivar"); 2175 return; 2176} 2177 2178/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. 2179/// objc_assign_strongCast (id src, id *dst) 2180/// 2181void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, 2182 llvm::Value *src, llvm::Value *dst) 2183{ 2184 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); 2185 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); 2186 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn, 2187 src, dst, "weakassign"); 2188 return; 2189} 2190 2191/// EmitObjCValueForIvar - Code Gen for ivar reference. 2192/// 2193LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, 2194 QualType ObjectTy, 2195 llvm::Value *BaseValue, 2196 const ObjCIvarDecl *Ivar, 2197 const FieldDecl *Field, 2198 unsigned CVRQualifiers) { 2199 if (Ivar->isBitField()) 2200 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field), 2201 CVRQualifiers); 2202 // TODO: Add a special case for isa (index 0) 2203 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field); 2204 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp"); 2205 LValue LV = LValue::MakeAddr(V, 2206 Ivar->getType().getCVRQualifiers()|CVRQualifiers, 2207 CGM.getContext().getObjCGCAttrKind(Ivar->getType())); 2208 LValue::SetObjCIvar(LV, true); 2209 return LV; 2210} 2211 2212llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, 2213 ObjCInterfaceDecl *Interface, 2214 const ObjCIvarDecl *Ivar) { 2215 const llvm::Type *InterfaceLTy = 2216 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface)); 2217 const llvm::StructLayout *Layout = 2218 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceLTy)); 2219 FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar); 2220 uint64_t Offset = 2221 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); 2222 2223 return llvm::ConstantInt::get( 2224 CGM.getTypes().ConvertType(CGM.getContext().LongTy), 2225 Offset); 2226} 2227 2228/* *** Private Interface *** */ 2229 2230/// EmitImageInfo - Emit the image info marker used to encode some module 2231/// level information. 2232/// 2233/// See: <rdr://4810609&4810587&4810587> 2234/// struct IMAGE_INFO { 2235/// unsigned version; 2236/// unsigned flags; 2237/// }; 2238enum ImageInfoFlags { 2239 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies 2240 eImageInfo_GarbageCollected = (1 << 1), 2241 eImageInfo_GCOnly = (1 << 2) 2242}; 2243 2244void CGObjCMac::EmitImageInfo() { 2245 unsigned version = 0; // Version is unused? 2246 unsigned flags = 0; 2247 2248 // FIXME: Fix and continue? 2249 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) 2250 flags |= eImageInfo_GarbageCollected; 2251 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) 2252 flags |= eImageInfo_GCOnly; 2253 2254 // Emitted as int[2]; 2255 llvm::Constant *values[2] = { 2256 llvm::ConstantInt::get(llvm::Type::Int32Ty, version), 2257 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags) 2258 }; 2259 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2); 2260 llvm::GlobalVariable *GV = 2261 new llvm::GlobalVariable(AT, true, 2262 llvm::GlobalValue::InternalLinkage, 2263 llvm::ConstantArray::get(AT, values, 2), 2264 "\01L_OBJC_IMAGE_INFO", 2265 &CGM.getModule()); 2266 2267 if (ObjCABI == 1) { 2268 GV->setSection("__OBJC, __image_info,regular"); 2269 } else { 2270 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); 2271 } 2272 2273 UsedGlobals.push_back(GV); 2274} 2275 2276 2277// struct objc_module { 2278// unsigned long version; 2279// unsigned long size; 2280// const char *name; 2281// Symtab symtab; 2282// }; 2283 2284// FIXME: Get from somewhere 2285static const int ModuleVersion = 7; 2286 2287void CGObjCMac::EmitModuleInfo() { 2288 uint64_t Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.ModuleTy); 2289 2290 std::vector<llvm::Constant*> Values(4); 2291 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); 2292 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); 2293 // This used to be the filename, now it is unused. <rdr://4327263> 2294 Values[2] = GetClassName(&CGM.getContext().Idents.get("")); 2295 Values[3] = EmitModuleSymbols(); 2296 2297 llvm::GlobalVariable *GV = 2298 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false, 2299 llvm::GlobalValue::InternalLinkage, 2300 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, 2301 Values), 2302 "\01L_OBJC_MODULES", 2303 &CGM.getModule()); 2304 GV->setSection("__OBJC,__module_info,regular,no_dead_strip"); 2305 UsedGlobals.push_back(GV); 2306} 2307 2308llvm::Constant *CGObjCMac::EmitModuleSymbols() { 2309 unsigned NumClasses = DefinedClasses.size(); 2310 unsigned NumCategories = DefinedCategories.size(); 2311 2312 // Return null if no symbols were defined. 2313 if (!NumClasses && !NumCategories) 2314 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); 2315 2316 std::vector<llvm::Constant*> Values(5); 2317 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); 2318 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); 2319 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); 2320 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); 2321 2322 // The runtime expects exactly the list of defined classes followed 2323 // by the list of defined categories, in a single array. 2324 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); 2325 for (unsigned i=0; i<NumClasses; i++) 2326 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], 2327 ObjCTypes.Int8PtrTy); 2328 for (unsigned i=0; i<NumCategories; i++) 2329 Symbols[NumClasses + i] = 2330 llvm::ConstantExpr::getBitCast(DefinedCategories[i], 2331 ObjCTypes.Int8PtrTy); 2332 2333 Values[4] = 2334 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, 2335 NumClasses + NumCategories), 2336 Symbols); 2337 2338 llvm::Constant *Init = llvm::ConstantStruct::get(Values); 2339 2340 llvm::GlobalVariable *GV = 2341 new llvm::GlobalVariable(Init->getType(), false, 2342 llvm::GlobalValue::InternalLinkage, 2343 Init, 2344 "\01L_OBJC_SYMBOLS", 2345 &CGM.getModule()); 2346 GV->setSection("__OBJC,__symbols,regular,no_dead_strip"); 2347 UsedGlobals.push_back(GV); 2348 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); 2349} 2350 2351llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, 2352 const ObjCInterfaceDecl *ID) { 2353 LazySymbols.insert(ID->getIdentifier()); 2354 2355 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; 2356 2357 if (!Entry) { 2358 llvm::Constant *Casted = 2359 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), 2360 ObjCTypes.ClassPtrTy); 2361 Entry = 2362 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false, 2363 llvm::GlobalValue::InternalLinkage, 2364 Casted, "\01L_OBJC_CLASS_REFERENCES_", 2365 &CGM.getModule()); 2366 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip"); 2367 UsedGlobals.push_back(Entry); 2368 } 2369 2370 return Builder.CreateLoad(Entry, false, "tmp"); 2371} 2372 2373llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) { 2374 llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; 2375 2376 if (!Entry) { 2377 llvm::Constant *Casted = 2378 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), 2379 ObjCTypes.SelectorPtrTy); 2380 Entry = 2381 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false, 2382 llvm::GlobalValue::InternalLinkage, 2383 Casted, "\01L_OBJC_SELECTOR_REFERENCES_", 2384 &CGM.getModule()); 2385 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip"); 2386 UsedGlobals.push_back(Entry); 2387 } 2388 2389 return Builder.CreateLoad(Entry, false, "tmp"); 2390} 2391 2392llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { 2393 llvm::GlobalVariable *&Entry = ClassNames[Ident]; 2394 2395 if (!Entry) { 2396 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName()); 2397 Entry = 2398 new llvm::GlobalVariable(C->getType(), false, 2399 llvm::GlobalValue::InternalLinkage, 2400 C, "\01L_OBJC_CLASS_NAME_", 2401 &CGM.getModule()); 2402 Entry->setSection("__TEXT,__cstring,cstring_literals"); 2403 UsedGlobals.push_back(Entry); 2404 } 2405 2406 return getConstantGEP(Entry, 0, 0); 2407} 2408 2409llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { 2410 llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; 2411 2412 if (!Entry) { 2413 // FIXME: Avoid std::string copying. 2414 llvm::Constant *C = llvm::ConstantArray::get(Sel.getAsString()); 2415 Entry = 2416 new llvm::GlobalVariable(C->getType(), false, 2417 llvm::GlobalValue::InternalLinkage, 2418 C, "\01L_OBJC_METH_VAR_NAME_", 2419 &CGM.getModule()); 2420 Entry->setSection("__TEXT,__cstring,cstring_literals"); 2421 UsedGlobals.push_back(Entry); 2422 } 2423 2424 return getConstantGEP(Entry, 0, 0); 2425} 2426 2427// FIXME: Merge into a single cstring creation function. 2428llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { 2429 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); 2430} 2431 2432// FIXME: Merge into a single cstring creation function. 2433llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) { 2434 return GetMethodVarName(&CGM.getContext().Idents.get(Name)); 2435} 2436 2437llvm::Constant *CGObjCCommonMac::GetMethodVarType(const std::string &Name) { 2438 llvm::GlobalVariable *&Entry = MethodVarTypes[Name]; 2439 2440 if (!Entry) { 2441 llvm::Constant *C = llvm::ConstantArray::get(Name); 2442 Entry = 2443 new llvm::GlobalVariable(C->getType(), false, 2444 llvm::GlobalValue::InternalLinkage, 2445 C, "\01L_OBJC_METH_VAR_TYPE_", 2446 &CGM.getModule()); 2447 Entry->setSection("__TEXT,__cstring,cstring_literals"); 2448 UsedGlobals.push_back(Entry); 2449 } 2450 2451 return getConstantGEP(Entry, 0, 0); 2452} 2453 2454// FIXME: Merge into a single cstring creation function. 2455llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) { 2456 std::string TypeStr; 2457 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), 2458 TypeStr); 2459 return GetMethodVarType(TypeStr); 2460} 2461 2462// FIXME: Merge into a single cstring creation function. 2463llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { 2464 llvm::GlobalVariable *&Entry = PropertyNames[Ident]; 2465 2466 if (!Entry) { 2467 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName()); 2468 Entry = 2469 new llvm::GlobalVariable(C->getType(), false, 2470 llvm::GlobalValue::InternalLinkage, 2471 C, "\01L_OBJC_PROP_NAME_ATTR_", 2472 &CGM.getModule()); 2473 Entry->setSection("__TEXT,__cstring,cstring_literals"); 2474 UsedGlobals.push_back(Entry); 2475 } 2476 2477 return getConstantGEP(Entry, 0, 0); 2478} 2479 2480// FIXME: Merge into a single cstring creation function. 2481// FIXME: This Decl should be more precise. 2482llvm::Constant *CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, 2483 const Decl *Container) { 2484 std::string TypeStr; 2485 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); 2486 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); 2487} 2488 2489void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, 2490 const ObjCContainerDecl *CD, 2491 std::string &NameOut) { 2492 // FIXME: Find the mangling GCC uses. 2493 NameOut = (D->isInstanceMethod() ? "-" : "+"); 2494 NameOut += '['; 2495 assert (CD && "Missing container decl in GetNameForMethod"); 2496 NameOut += CD->getNameAsString(); 2497 // FIXME. For a method in a category, (CAT_NAME) is inserted here. 2498 // Right now! there is not enough info. to do this. 2499 NameOut += ' '; 2500 NameOut += D->getSelector().getAsString(); 2501 NameOut += ']'; 2502} 2503 2504/// GetFirstIvarInRecord - This routine returns the record for the 2505/// implementation of the fiven class OID. It also returns field 2506/// corresponding to the first ivar in the class in FIV. It also 2507/// returns the one before the first ivar. 2508/// 2509const RecordDecl *CGObjCCommonMac::GetFirstIvarInRecord( 2510 const ObjCInterfaceDecl *OID, 2511 RecordDecl::field_iterator &FIV, 2512 RecordDecl::field_iterator &PIV) { 2513 int countSuperClassIvars = countInheritedIvars(OID->getSuperClass()); 2514 const RecordDecl *RD = CGM.getContext().addRecordToClass(OID); 2515 RecordDecl::field_iterator ifield = RD->field_begin(); 2516 RecordDecl::field_iterator pfield = RD->field_end(); 2517 while (countSuperClassIvars-- > 0) { 2518 pfield = ifield; 2519 ++ifield; 2520 } 2521 FIV = ifield; 2522 PIV = pfield; 2523 return RD; 2524} 2525 2526void CGObjCMac::FinishModule() { 2527 EmitModuleInfo(); 2528 2529 // Emit the dummy bodies for any protocols which were referenced but 2530 // never defined. 2531 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator 2532 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) { 2533 if (i->second->hasInitializer()) 2534 continue; 2535 2536 std::vector<llvm::Constant*> Values(5); 2537 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); 2538 Values[1] = GetClassName(i->first); 2539 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); 2540 Values[3] = Values[4] = 2541 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); 2542 i->second->setLinkage(llvm::GlobalValue::InternalLinkage); 2543 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, 2544 Values)); 2545 } 2546 2547 std::vector<llvm::Constant*> Used; 2548 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), 2549 e = UsedGlobals.end(); i != e; ++i) { 2550 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); 2551 } 2552 2553 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); 2554 llvm::GlobalValue *GV = 2555 new llvm::GlobalVariable(AT, false, 2556 llvm::GlobalValue::AppendingLinkage, 2557 llvm::ConstantArray::get(AT, Used), 2558 "llvm.used", 2559 &CGM.getModule()); 2560 2561 GV->setSection("llvm.metadata"); 2562 2563 // Add assembler directives to add lazy undefined symbol references 2564 // for classes which are referenced but not defined. This is 2565 // important for correct linker interaction. 2566 2567 // FIXME: Uh, this isn't particularly portable. 2568 std::stringstream s; 2569 2570 if (!CGM.getModule().getModuleInlineAsm().empty()) 2571 s << "\n"; 2572 2573 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(), 2574 e = LazySymbols.end(); i != e; ++i) { 2575 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n"; 2576 } 2577 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(), 2578 e = DefinedSymbols.end(); i != e; ++i) { 2579 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n" 2580 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n"; 2581 } 2582 2583 CGM.getModule().appendModuleInlineAsm(s.str()); 2584} 2585 2586CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) 2587 : CGObjCCommonMac(cgm), 2588 ObjCTypes(cgm) 2589{ 2590 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; 2591 ObjCABI = 2; 2592} 2593 2594/* *** */ 2595 2596ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) 2597: CGM(cgm) 2598{ 2599 CodeGen::CodeGenTypes &Types = CGM.getTypes(); 2600 ASTContext &Ctx = CGM.getContext(); 2601 2602 ShortTy = Types.ConvertType(Ctx.ShortTy); 2603 IntTy = Types.ConvertType(Ctx.IntTy); 2604 LongTy = Types.ConvertType(Ctx.LongTy); 2605 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 2606 2607 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); 2608 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); 2609 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); 2610 2611 // FIXME: It would be nice to unify this with the opaque type, so 2612 // that the IR comes out a bit cleaner. 2613 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); 2614 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); 2615 2616 // I'm not sure I like this. The implicit coordination is a bit 2617 // gross. We should solve this in a reasonable fashion because this 2618 // is a pretty common task (match some runtime data structure with 2619 // an LLVM data structure). 2620 2621 // FIXME: This is leaked. 2622 // FIXME: Merge with rewriter code? 2623 2624 // struct _objc_super { 2625 // id self; 2626 // Class cls; 2627 // } 2628 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, 2629 SourceLocation(), 2630 &Ctx.Idents.get("_objc_super")); 2631 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, 2632 Ctx.getObjCIdType(), 0, false)); 2633 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, 2634 Ctx.getObjCClassType(), 0, false)); 2635 RD->completeDefinition(Ctx); 2636 2637 SuperCTy = Ctx.getTagDeclType(RD); 2638 SuperPtrCTy = Ctx.getPointerType(SuperCTy); 2639 2640 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); 2641 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); 2642 2643 // struct _prop_t { 2644 // char *name; 2645 // char *attributes; 2646 // } 2647 PropertyTy = llvm::StructType::get(Int8PtrTy, 2648 Int8PtrTy, 2649 NULL); 2650 CGM.getModule().addTypeName("struct._prop_t", 2651 PropertyTy); 2652 2653 // struct _prop_list_t { 2654 // uint32_t entsize; // sizeof(struct _prop_t) 2655 // uint32_t count_of_properties; 2656 // struct _prop_t prop_list[count_of_properties]; 2657 // } 2658 PropertyListTy = llvm::StructType::get(IntTy, 2659 IntTy, 2660 llvm::ArrayType::get(PropertyTy, 0), 2661 NULL); 2662 CGM.getModule().addTypeName("struct._prop_list_t", 2663 PropertyListTy); 2664 // struct _prop_list_t * 2665 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); 2666 2667 // struct _objc_method { 2668 // SEL _cmd; 2669 // char *method_type; 2670 // char *_imp; 2671 // } 2672 MethodTy = llvm::StructType::get(SelectorPtrTy, 2673 Int8PtrTy, 2674 Int8PtrTy, 2675 NULL); 2676 CGM.getModule().addTypeName("struct._objc_method", MethodTy); 2677 2678 // struct _objc_cache * 2679 CacheTy = llvm::OpaqueType::get(); 2680 CGM.getModule().addTypeName("struct._objc_cache", CacheTy); 2681 CachePtrTy = llvm::PointerType::getUnqual(CacheTy); 2682 2683 // Property manipulation functions. 2684 2685 QualType IdType = Ctx.getObjCIdType(); 2686 QualType SelType = Ctx.getObjCSelType(); 2687 llvm::SmallVector<QualType,16> Params; 2688 const llvm::FunctionType *FTy; 2689 2690 // id objc_getProperty (id, SEL, ptrdiff_t, bool) 2691 Params.push_back(IdType); 2692 Params.push_back(SelType); 2693 Params.push_back(Ctx.LongTy); 2694 Params.push_back(Ctx.BoolTy); 2695 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), 2696 false); 2697 GetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); 2698 2699 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) 2700 Params.clear(); 2701 Params.push_back(IdType); 2702 Params.push_back(SelType); 2703 Params.push_back(Ctx.LongTy); 2704 Params.push_back(IdType); 2705 Params.push_back(Ctx.BoolTy); 2706 Params.push_back(Ctx.BoolTy); 2707 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); 2708 SetPropertyFn = CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); 2709 2710 // Enumeration mutation. 2711 2712 // void objc_enumerationMutation (id) 2713 Params.clear(); 2714 Params.push_back(IdType); 2715 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); 2716 EnumerationMutationFn = CGM.CreateRuntimeFunction(FTy, 2717 "objc_enumerationMutation"); 2718 2719 // gc's API 2720 // id objc_read_weak (id *) 2721 Params.clear(); 2722 Params.push_back(Ctx.getPointerType(IdType)); 2723 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false); 2724 GcReadWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); 2725 2726 // id objc_assign_weak (id, id *) 2727 Params.clear(); 2728 Params.push_back(IdType); 2729 Params.push_back(Ctx.getPointerType(IdType)); 2730 2731 FTy = Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false); 2732 GcAssignWeakFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); 2733 GcAssignGlobalFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); 2734 GcAssignIvarFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); 2735 GcAssignStrongCastFn = 2736 CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); 2737 2738 // void objc_exception_throw(id) 2739 Params.clear(); 2740 Params.push_back(IdType); 2741 2742 FTy = Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); 2743 2744 ExceptionThrowFn = 2745 CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); 2746} 2747 2748ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) 2749 : ObjCCommonTypesHelper(cgm) 2750{ 2751 // struct _objc_method_description { 2752 // SEL name; 2753 // char *types; 2754 // } 2755 MethodDescriptionTy = 2756 llvm::StructType::get(SelectorPtrTy, 2757 Int8PtrTy, 2758 NULL); 2759 CGM.getModule().addTypeName("struct._objc_method_description", 2760 MethodDescriptionTy); 2761 2762 // struct _objc_method_description_list { 2763 // int count; 2764 // struct _objc_method_description[1]; 2765 // } 2766 MethodDescriptionListTy = 2767 llvm::StructType::get(IntTy, 2768 llvm::ArrayType::get(MethodDescriptionTy, 0), 2769 NULL); 2770 CGM.getModule().addTypeName("struct._objc_method_description_list", 2771 MethodDescriptionListTy); 2772 2773 // struct _objc_method_description_list * 2774 MethodDescriptionListPtrTy = 2775 llvm::PointerType::getUnqual(MethodDescriptionListTy); 2776 2777 // Protocol description structures 2778 2779 // struct _objc_protocol_extension { 2780 // uint32_t size; // sizeof(struct _objc_protocol_extension) 2781 // struct _objc_method_description_list *optional_instance_methods; 2782 // struct _objc_method_description_list *optional_class_methods; 2783 // struct _objc_property_list *instance_properties; 2784 // } 2785 ProtocolExtensionTy = 2786 llvm::StructType::get(IntTy, 2787 MethodDescriptionListPtrTy, 2788 MethodDescriptionListPtrTy, 2789 PropertyListPtrTy, 2790 NULL); 2791 CGM.getModule().addTypeName("struct._objc_protocol_extension", 2792 ProtocolExtensionTy); 2793 2794 // struct _objc_protocol_extension * 2795 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); 2796 2797 // Handle recursive construction of Protocol and ProtocolList types 2798 2799 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(); 2800 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); 2801 2802 const llvm::Type *T = 2803 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder), 2804 LongTy, 2805 llvm::ArrayType::get(ProtocolTyHolder, 0), 2806 NULL); 2807 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); 2808 2809 // struct _objc_protocol { 2810 // struct _objc_protocol_extension *isa; 2811 // char *protocol_name; 2812 // struct _objc_protocol **_objc_protocol_list; 2813 // struct _objc_method_description_list *instance_methods; 2814 // struct _objc_method_description_list *class_methods; 2815 // } 2816 T = llvm::StructType::get(ProtocolExtensionPtrTy, 2817 Int8PtrTy, 2818 llvm::PointerType::getUnqual(ProtocolListTyHolder), 2819 MethodDescriptionListPtrTy, 2820 MethodDescriptionListPtrTy, 2821 NULL); 2822 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); 2823 2824 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); 2825 CGM.getModule().addTypeName("struct._objc_protocol_list", 2826 ProtocolListTy); 2827 // struct _objc_protocol_list * 2828 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); 2829 2830 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); 2831 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy); 2832 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); 2833 2834 // Class description structures 2835 2836 // struct _objc_ivar { 2837 // char *ivar_name; 2838 // char *ivar_type; 2839 // int ivar_offset; 2840 // } 2841 IvarTy = llvm::StructType::get(Int8PtrTy, 2842 Int8PtrTy, 2843 IntTy, 2844 NULL); 2845 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); 2846 2847 // struct _objc_ivar_list * 2848 IvarListTy = llvm::OpaqueType::get(); 2849 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); 2850 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); 2851 2852 // struct _objc_method_list * 2853 MethodListTy = llvm::OpaqueType::get(); 2854 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); 2855 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); 2856 2857 // struct _objc_class_extension * 2858 ClassExtensionTy = 2859 llvm::StructType::get(IntTy, 2860 Int8PtrTy, 2861 PropertyListPtrTy, 2862 NULL); 2863 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); 2864 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); 2865 2866 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); 2867 2868 // struct _objc_class { 2869 // Class isa; 2870 // Class super_class; 2871 // char *name; 2872 // long version; 2873 // long info; 2874 // long instance_size; 2875 // struct _objc_ivar_list *ivars; 2876 // struct _objc_method_list *methods; 2877 // struct _objc_cache *cache; 2878 // struct _objc_protocol_list *protocols; 2879 // char *ivar_layout; 2880 // struct _objc_class_ext *ext; 2881 // }; 2882 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), 2883 llvm::PointerType::getUnqual(ClassTyHolder), 2884 Int8PtrTy, 2885 LongTy, 2886 LongTy, 2887 LongTy, 2888 IvarListPtrTy, 2889 MethodListPtrTy, 2890 CachePtrTy, 2891 ProtocolListPtrTy, 2892 Int8PtrTy, 2893 ClassExtensionPtrTy, 2894 NULL); 2895 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); 2896 2897 ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); 2898 CGM.getModule().addTypeName("struct._objc_class", ClassTy); 2899 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); 2900 2901 // struct _objc_category { 2902 // char *category_name; 2903 // char *class_name; 2904 // struct _objc_method_list *instance_method; 2905 // struct _objc_method_list *class_method; 2906 // uint32_t size; // sizeof(struct _objc_category) 2907 // struct _objc_property_list *instance_properties;// category's @property 2908 // } 2909 CategoryTy = llvm::StructType::get(Int8PtrTy, 2910 Int8PtrTy, 2911 MethodListPtrTy, 2912 MethodListPtrTy, 2913 ProtocolListPtrTy, 2914 IntTy, 2915 PropertyListPtrTy, 2916 NULL); 2917 CGM.getModule().addTypeName("struct._objc_category", CategoryTy); 2918 2919 // Global metadata structures 2920 2921 // struct _objc_symtab { 2922 // long sel_ref_cnt; 2923 // SEL *refs; 2924 // short cls_def_cnt; 2925 // short cat_def_cnt; 2926 // char *defs[cls_def_cnt + cat_def_cnt]; 2927 // } 2928 SymtabTy = llvm::StructType::get(LongTy, 2929 SelectorPtrTy, 2930 ShortTy, 2931 ShortTy, 2932 llvm::ArrayType::get(Int8PtrTy, 0), 2933 NULL); 2934 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); 2935 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); 2936 2937 // struct _objc_module { 2938 // long version; 2939 // long size; // sizeof(struct _objc_module) 2940 // char *name; 2941 // struct _objc_symtab* symtab; 2942 // } 2943 ModuleTy = 2944 llvm::StructType::get(LongTy, 2945 LongTy, 2946 Int8PtrTy, 2947 SymtabPtrTy, 2948 NULL); 2949 CGM.getModule().addTypeName("struct._objc_module", ModuleTy); 2950 2951 // Message send functions. 2952 2953 // id objc_msgSend (id, SEL, ...) 2954 std::vector<const llvm::Type*> Params; 2955 Params.push_back(ObjectPtrTy); 2956 Params.push_back(SelectorPtrTy); 2957 MessageSendFn = 2958 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, 2959 Params, 2960 true), 2961 "objc_msgSend"); 2962 2963 // id objc_msgSend_stret (id, SEL, ...) 2964 Params.clear(); 2965 Params.push_back(ObjectPtrTy); 2966 Params.push_back(SelectorPtrTy); 2967 MessageSendStretFn = 2968 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, 2969 Params, 2970 true), 2971 "objc_msgSend_stret"); 2972 2973 // 2974 Params.clear(); 2975 Params.push_back(ObjectPtrTy); 2976 Params.push_back(SelectorPtrTy); 2977 // FIXME: This should be long double on x86_64? 2978 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...) 2979 MessageSendFpretFn = 2980 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy, 2981 Params, 2982 true), 2983 "objc_msgSend_fpret"); 2984 2985 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...) 2986 Params.clear(); 2987 Params.push_back(SuperPtrTy); 2988 Params.push_back(SelectorPtrTy); 2989 MessageSendSuperFn = 2990 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, 2991 Params, 2992 true), 2993 "objc_msgSendSuper"); 2994 2995 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super, 2996 // SEL op, ...) 2997 Params.clear(); 2998 Params.push_back(Int8PtrTy); 2999 Params.push_back(SuperPtrTy); 3000 Params.push_back(SelectorPtrTy); 3001 MessageSendSuperStretFn = 3002 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, 3003 Params, 3004 true), 3005 "objc_msgSendSuper_stret"); 3006 3007 // There is no objc_msgSendSuper_fpret? How can that work? 3008 MessageSendSuperFpretFn = MessageSendSuperFn; 3009 3010 // FIXME: This is the size of the setjmp buffer and should be 3011 // target specific. 18 is what's used on 32-bit X86. 3012 uint64_t SetJmpBufferSize = 18; 3013 3014 // Exceptions 3015 const llvm::Type *StackPtrTy = 3016 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4); 3017 3018 ExceptionDataTy = 3019 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty, 3020 SetJmpBufferSize), 3021 StackPtrTy, NULL); 3022 CGM.getModule().addTypeName("struct._objc_exception_data", 3023 ExceptionDataTy); 3024 3025 Params.clear(); 3026 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); 3027 ExceptionTryEnterFn = 3028 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, 3029 Params, 3030 false), 3031 "objc_exception_try_enter"); 3032 ExceptionTryExitFn = 3033 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, 3034 Params, 3035 false), 3036 "objc_exception_try_exit"); 3037 ExceptionExtractFn = 3038 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, 3039 Params, 3040 false), 3041 "objc_exception_extract"); 3042 3043 Params.clear(); 3044 Params.push_back(ClassPtrTy); 3045 Params.push_back(ObjectPtrTy); 3046 ExceptionMatchFn = 3047 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, 3048 Params, 3049 false), 3050 "objc_exception_match"); 3051 3052 // synchronized APIs 3053 // void objc_sync_enter (id) 3054 Params.clear(); 3055 Params.push_back(ObjectPtrTy); 3056 SyncEnterFn = 3057 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, 3058 Params, 3059 false), 3060 "objc_sync_enter"); 3061 // void objc_sync_exit (id) 3062 SyncExitFn = 3063 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy, 3064 Params, 3065 false), 3066 "objc_sync_exit"); 3067 3068 3069 Params.clear(); 3070 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty)); 3071 SetJmpFn = 3072 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty, 3073 Params, 3074 false), 3075 "_setjmp"); 3076 3077} 3078 3079ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) 3080: ObjCCommonTypesHelper(cgm) 3081{ 3082 // struct _method_list_t { 3083 // uint32_t entsize; // sizeof(struct _objc_method) 3084 // uint32_t method_count; 3085 // struct _objc_method method_list[method_count]; 3086 // } 3087 MethodListnfABITy = llvm::StructType::get(IntTy, 3088 IntTy, 3089 llvm::ArrayType::get(MethodTy, 0), 3090 NULL); 3091 CGM.getModule().addTypeName("struct.__method_list_t", 3092 MethodListnfABITy); 3093 // struct method_list_t * 3094 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); 3095 3096 // struct _protocol_t { 3097 // id isa; // NULL 3098 // const char * const protocol_name; 3099 // const struct _protocol_list_t * protocol_list; // super protocols 3100 // const struct method_list_t * const instance_methods; 3101 // const struct method_list_t * const class_methods; 3102 // const struct method_list_t *optionalInstanceMethods; 3103 // const struct method_list_t *optionalClassMethods; 3104 // const struct _prop_list_t * properties; 3105 // const uint32_t size; // sizeof(struct _protocol_t) 3106 // const uint32_t flags; // = 0 3107 // } 3108 3109 // Holder for struct _protocol_list_t * 3110 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); 3111 3112 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy, 3113 Int8PtrTy, 3114 llvm::PointerType::getUnqual( 3115 ProtocolListTyHolder), 3116 MethodListnfABIPtrTy, 3117 MethodListnfABIPtrTy, 3118 MethodListnfABIPtrTy, 3119 MethodListnfABIPtrTy, 3120 PropertyListPtrTy, 3121 IntTy, 3122 IntTy, 3123 NULL); 3124 CGM.getModule().addTypeName("struct._protocol_t", 3125 ProtocolnfABITy); 3126 3127 // struct _protocol_t* 3128 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); 3129 3130 // struct _protocol_list_t { 3131 // long protocol_count; // Note, this is 32/64 bit 3132 // struct _protocol_t *[protocol_count]; 3133 // } 3134 ProtocolListnfABITy = llvm::StructType::get(LongTy, 3135 llvm::ArrayType::get( 3136 ProtocolnfABIPtrTy, 0), 3137 NULL); 3138 CGM.getModule().addTypeName("struct._objc_protocol_list", 3139 ProtocolListnfABITy); 3140 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo( 3141 ProtocolListnfABITy); 3142 3143 // struct _objc_protocol_list* 3144 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); 3145 3146 // struct _ivar_t { 3147 // unsigned long int *offset; // pointer to ivar offset location 3148 // char *name; 3149 // char *type; 3150 // uint32_t alignment; 3151 // uint32_t size; 3152 // } 3153 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy), 3154 Int8PtrTy, 3155 Int8PtrTy, 3156 IntTy, 3157 IntTy, 3158 NULL); 3159 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy); 3160 3161 // struct _ivar_list_t { 3162 // uint32 entsize; // sizeof(struct _ivar_t) 3163 // uint32 count; 3164 // struct _iver_t list[count]; 3165 // } 3166 IvarListnfABITy = llvm::StructType::get(IntTy, 3167 IntTy, 3168 llvm::ArrayType::get( 3169 IvarnfABITy, 0), 3170 NULL); 3171 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy); 3172 3173 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); 3174 3175 // struct _class_ro_t { 3176 // uint32_t const flags; 3177 // uint32_t const instanceStart; 3178 // uint32_t const instanceSize; 3179 // uint32_t const reserved; // only when building for 64bit targets 3180 // const uint8_t * const ivarLayout; 3181 // const char *const name; 3182 // const struct _method_list_t * const baseMethods; 3183 // const struct _objc_protocol_list *const baseProtocols; 3184 // const struct _ivar_list_t *const ivars; 3185 // const uint8_t * const weakIvarLayout; 3186 // const struct _prop_list_t * const properties; 3187 // } 3188 3189 // FIXME. Add 'reserved' field in 64bit abi mode! 3190 ClassRonfABITy = llvm::StructType::get(IntTy, 3191 IntTy, 3192 IntTy, 3193 Int8PtrTy, 3194 Int8PtrTy, 3195 MethodListnfABIPtrTy, 3196 ProtocolListnfABIPtrTy, 3197 IvarListnfABIPtrTy, 3198 Int8PtrTy, 3199 PropertyListPtrTy, 3200 NULL); 3201 CGM.getModule().addTypeName("struct._class_ro_t", 3202 ClassRonfABITy); 3203 3204 // ImpnfABITy - LLVM for id (*)(id, SEL, ...) 3205 std::vector<const llvm::Type*> Params; 3206 Params.push_back(ObjectPtrTy); 3207 Params.push_back(SelectorPtrTy); 3208 ImpnfABITy = llvm::PointerType::getUnqual( 3209 llvm::FunctionType::get(ObjectPtrTy, Params, false)); 3210 3211 // struct _class_t { 3212 // struct _class_t *isa; 3213 // struct _class_t * const superclass; 3214 // void *cache; 3215 // IMP *vtable; 3216 // struct class_ro_t *ro; 3217 // } 3218 3219 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); 3220 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), 3221 llvm::PointerType::getUnqual(ClassTyHolder), 3222 CachePtrTy, 3223 llvm::PointerType::getUnqual(ImpnfABITy), 3224 llvm::PointerType::getUnqual( 3225 ClassRonfABITy), 3226 NULL); 3227 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy); 3228 3229 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo( 3230 ClassnfABITy); 3231 3232 // LLVM for struct _class_t * 3233 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); 3234 3235 // struct _category_t { 3236 // const char * const name; 3237 // struct _class_t *const cls; 3238 // const struct _method_list_t * const instance_methods; 3239 // const struct _method_list_t * const class_methods; 3240 // const struct _protocol_list_t * const protocols; 3241 // const struct _prop_list_t * const properties; 3242 // } 3243 CategorynfABITy = llvm::StructType::get(Int8PtrTy, 3244 ClassnfABIPtrTy, 3245 MethodListnfABIPtrTy, 3246 MethodListnfABIPtrTy, 3247 ProtocolListnfABIPtrTy, 3248 PropertyListPtrTy, 3249 NULL); 3250 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy); 3251 3252 // New types for nonfragile abi messaging. 3253 CodeGen::CodeGenTypes &Types = CGM.getTypes(); 3254 ASTContext &Ctx = CGM.getContext(); 3255 3256 // MessageRefTy - LLVM for: 3257 // struct _message_ref_t { 3258 // IMP messenger; 3259 // SEL name; 3260 // }; 3261 3262 // First the clang type for struct _message_ref_t 3263 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, 3264 SourceLocation(), 3265 &Ctx.Idents.get("_message_ref_t")); 3266 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, 3267 Ctx.VoidPtrTy, 0, false)); 3268 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, 3269 Ctx.getObjCSelType(), 0, false)); 3270 RD->completeDefinition(Ctx); 3271 3272 MessageRefCTy = Ctx.getTagDeclType(RD); 3273 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); 3274 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); 3275 3276 // MessageRefPtrTy - LLVM for struct _message_ref_t* 3277 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); 3278 3279 // SuperMessageRefTy - LLVM for: 3280 // struct _super_message_ref_t { 3281 // SUPER_IMP messenger; 3282 // SEL name; 3283 // }; 3284 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy, 3285 SelectorPtrTy, 3286 NULL); 3287 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy); 3288 3289 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* 3290 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); 3291 3292 // id objc_msgSend_fixup (id, struct message_ref_t*, ...) 3293 Params.clear(); 3294 Params.push_back(ObjectPtrTy); 3295 Params.push_back(MessageRefPtrTy); 3296 MessengerTy = llvm::FunctionType::get(ObjectPtrTy, 3297 Params, 3298 true); 3299 MessageSendFixupFn = 3300 CGM.CreateRuntimeFunction(MessengerTy, 3301 "objc_msgSend_fixup"); 3302 3303 // id objc_msgSend_fpret_fixup (id, struct message_ref_t*, ...) 3304 MessageSendFpretFixupFn = 3305 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, 3306 Params, 3307 true), 3308 "objc_msgSend_fpret_fixup"); 3309 3310 // id objc_msgSend_stret_fixup (id, struct message_ref_t*, ...) 3311 MessageSendStretFixupFn = 3312 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, 3313 Params, 3314 true), 3315 "objc_msgSend_stret_fixup"); 3316 3317 // id objc_msgSendId_fixup (id, struct message_ref_t*, ...) 3318 MessageSendIdFixupFn = 3319 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, 3320 Params, 3321 true), 3322 "objc_msgSendId_fixup"); 3323 3324 3325 // id objc_msgSendId_stret_fixup (id, struct message_ref_t*, ...) 3326 MessageSendIdStretFixupFn = 3327 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, 3328 Params, 3329 true), 3330 "objc_msgSendId_stret_fixup"); 3331 3332 // id objc_msgSendSuper2_fixup (struct objc_super *, 3333 // struct _super_message_ref_t*, ...) 3334 Params.clear(); 3335 Params.push_back(SuperPtrTy); 3336 Params.push_back(SuperMessageRefPtrTy); 3337 MessageSendSuper2FixupFn = 3338 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, 3339 Params, 3340 true), 3341 "objc_msgSendSuper2_fixup"); 3342 3343 3344 // id objc_msgSendSuper2_stret_fixup (struct objc_super *, 3345 // struct _super_message_ref_t*, ...) 3346 MessageSendSuper2StretFixupFn = 3347 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, 3348 Params, 3349 true), 3350 "objc_msgSendSuper2_stret_fixup"); 3351 3352} 3353 3354llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { 3355 FinishNonFragileABIModule(); 3356 3357 return NULL; 3358} 3359 3360void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { 3361 // nonfragile abi has no module definition. 3362 3363 // Build list of all implemented classe addresses in array 3364 // L_OBJC_LABEL_CLASS_$. 3365 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CLASS_$ 3366 // list of 'nonlazy' implementations (defined as those with a +load{} 3367 // method!!). 3368 unsigned NumClasses = DefinedClasses.size(); 3369 if (NumClasses) { 3370 std::vector<llvm::Constant*> Symbols(NumClasses); 3371 for (unsigned i=0; i<NumClasses; i++) 3372 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], 3373 ObjCTypes.Int8PtrTy); 3374 llvm::Constant* Init = 3375 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, 3376 NumClasses), 3377 Symbols); 3378 3379 llvm::GlobalVariable *GV = 3380 new llvm::GlobalVariable(Init->getType(), false, 3381 llvm::GlobalValue::InternalLinkage, 3382 Init, 3383 "\01L_OBJC_LABEL_CLASS_$", 3384 &CGM.getModule()); 3385 GV->setSection("__DATA, __objc_classlist, regular, no_dead_strip"); 3386 UsedGlobals.push_back(GV); 3387 } 3388 3389 // Build list of all implemented category addresses in array 3390 // L_OBJC_LABEL_CATEGORY_$. 3391 // FIXME. Also generate in L_OBJC_LABEL_NONLAZY_CATEGORY_$ 3392 // list of 'nonlazy' category implementations (defined as those with a +load{} 3393 // method!!). 3394 unsigned NumCategory = DefinedCategories.size(); 3395 if (NumCategory) { 3396 std::vector<llvm::Constant*> Symbols(NumCategory); 3397 for (unsigned i=0; i<NumCategory; i++) 3398 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedCategories[i], 3399 ObjCTypes.Int8PtrTy); 3400 llvm::Constant* Init = 3401 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, 3402 NumCategory), 3403 Symbols); 3404 3405 llvm::GlobalVariable *GV = 3406 new llvm::GlobalVariable(Init->getType(), false, 3407 llvm::GlobalValue::InternalLinkage, 3408 Init, 3409 "\01L_OBJC_LABEL_CATEGORY_$", 3410 &CGM.getModule()); 3411 GV->setSection("__DATA, __objc_catlist, regular, no_dead_strip"); 3412 UsedGlobals.push_back(GV); 3413 } 3414 3415 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags }; 3416 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0 3417 std::vector<llvm::Constant*> Values(2); 3418 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0); 3419 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0); 3420 llvm::Constant* Init = llvm::ConstantArray::get( 3421 llvm::ArrayType::get(ObjCTypes.IntTy, 2), 3422 Values); 3423 llvm::GlobalVariable *IMGV = 3424 new llvm::GlobalVariable(Init->getType(), false, 3425 llvm::GlobalValue::InternalLinkage, 3426 Init, 3427 "\01L_OBJC_IMAGE_INFO", 3428 &CGM.getModule()); 3429 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); 3430 UsedGlobals.push_back(IMGV); 3431 3432 std::vector<llvm::Constant*> Used; 3433 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), 3434 e = UsedGlobals.end(); i != e; ++i) { 3435 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); 3436 } 3437 3438 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); 3439 llvm::GlobalValue *GV = 3440 new llvm::GlobalVariable(AT, false, 3441 llvm::GlobalValue::AppendingLinkage, 3442 llvm::ConstantArray::get(AT, Used), 3443 "llvm.used", 3444 &CGM.getModule()); 3445 3446 GV->setSection("llvm.metadata"); 3447 3448} 3449 3450// Metadata flags 3451enum MetaDataDlags { 3452 CLS = 0x0, 3453 CLS_META = 0x1, 3454 CLS_ROOT = 0x2, 3455 OBJC2_CLS_HIDDEN = 0x10, 3456 CLS_EXCEPTION = 0x20 3457}; 3458/// BuildClassRoTInitializer - generate meta-data for: 3459/// struct _class_ro_t { 3460/// uint32_t const flags; 3461/// uint32_t const instanceStart; 3462/// uint32_t const instanceSize; 3463/// uint32_t const reserved; // only when building for 64bit targets 3464/// const uint8_t * const ivarLayout; 3465/// const char *const name; 3466/// const struct _method_list_t * const baseMethods; 3467/// const struct _protocol_list_t *const baseProtocols; 3468/// const struct _ivar_list_t *const ivars; 3469/// const uint8_t * const weakIvarLayout; 3470/// const struct _prop_list_t * const properties; 3471/// } 3472/// 3473llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( 3474 unsigned flags, 3475 unsigned InstanceStart, 3476 unsigned InstanceSize, 3477 const ObjCImplementationDecl *ID) { 3478 std::string ClassName = ID->getNameAsString(); 3479 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets! 3480 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); 3481 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); 3482 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); 3483 // FIXME. For 64bit targets add 0 here. 3484 // FIXME. ivarLayout is currently null! 3485 Values[ 3] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); 3486 Values[ 4] = GetClassName(ID->getIdentifier()); 3487 // const struct _method_list_t * const baseMethods; 3488 std::vector<llvm::Constant*> Methods; 3489 std::string MethodListName("\01l_OBJC_$_"); 3490 if (flags & CLS_META) { 3491 MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); 3492 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), 3493 e = ID->classmeth_end(); i != e; ++i) { 3494 // Class methods should always be defined. 3495 Methods.push_back(GetMethodConstant(*i)); 3496 } 3497 } else { 3498 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); 3499 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), 3500 e = ID->instmeth_end(); i != e; ++i) { 3501 // Instance methods should always be defined. 3502 Methods.push_back(GetMethodConstant(*i)); 3503 } 3504 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), 3505 e = ID->propimpl_end(); i != e; ++i) { 3506 ObjCPropertyImplDecl *PID = *i; 3507 3508 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ 3509 ObjCPropertyDecl *PD = PID->getPropertyDecl(); 3510 3511 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) 3512 if (llvm::Constant *C = GetMethodConstant(MD)) 3513 Methods.push_back(C); 3514 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) 3515 if (llvm::Constant *C = GetMethodConstant(MD)) 3516 Methods.push_back(C); 3517 } 3518 } 3519 } 3520 Values[ 5] = EmitMethodList(MethodListName, 3521 "__DATA, __objc_const", Methods); 3522 3523 const ObjCInterfaceDecl *OID = ID->getClassInterface(); 3524 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); 3525 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" 3526 + OID->getNameAsString(), 3527 OID->protocol_begin(), 3528 OID->protocol_end()); 3529 3530 if (flags & CLS_META) 3531 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); 3532 else 3533 Values[ 7] = EmitIvarList(ID); 3534 // FIXME. weakIvarLayout is currently null. 3535 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); 3536 if (flags & CLS_META) 3537 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); 3538 else 3539 Values[ 9] = 3540 EmitPropertyList( 3541 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), 3542 ID, ID->getClassInterface(), ObjCTypes); 3543 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, 3544 Values); 3545 llvm::GlobalVariable *CLASS_RO_GV = 3546 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false, 3547 llvm::GlobalValue::InternalLinkage, 3548 Init, 3549 (flags & CLS_META) ? 3550 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : 3551 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName, 3552 &CGM.getModule()); 3553 CLASS_RO_GV->setAlignment( 3554 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy)); 3555 CLASS_RO_GV->setSection("__DATA, __objc_const"); 3556 UsedGlobals.push_back(CLASS_RO_GV); 3557 return CLASS_RO_GV; 3558 3559} 3560 3561/// BuildClassMetaData - This routine defines that to-level meta-data 3562/// for the given ClassName for: 3563/// struct _class_t { 3564/// struct _class_t *isa; 3565/// struct _class_t * const superclass; 3566/// void *cache; 3567/// IMP *vtable; 3568/// struct class_ro_t *ro; 3569/// } 3570/// 3571llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( 3572 std::string &ClassName, 3573 llvm::Constant *IsAGV, 3574 llvm::Constant *SuperClassGV, 3575 llvm::Constant *ClassRoGV, 3576 bool HiddenVisibility) { 3577 std::vector<llvm::Constant*> Values(5); 3578 Values[0] = IsAGV; 3579 Values[1] = SuperClassGV 3580 ? SuperClassGV 3581 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); 3582 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar 3583 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar 3584 Values[4] = ClassRoGV; // &CLASS_RO_GV 3585 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, 3586 Values); 3587 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(ClassName); 3588 if (GV) 3589 GV->setInitializer(Init); 3590 else 3591 GV = 3592 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, 3593 llvm::GlobalValue::ExternalLinkage, 3594 Init, 3595 ClassName, 3596 &CGM.getModule()); 3597 GV->setSection("__DATA, __objc_data"); 3598 GV->setAlignment( 3599 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy)); 3600 if (HiddenVisibility) 3601 GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 3602 UsedGlobals.push_back(GV); 3603 return GV; 3604} 3605 3606void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { 3607 std::string ClassName = ID->getNameAsString(); 3608 if (!ObjCEmptyCacheVar) { 3609 ObjCEmptyCacheVar = new llvm::GlobalVariable( 3610 ObjCTypes.CacheTy, 3611 false, 3612 llvm::GlobalValue::ExternalLinkage, 3613 0, 3614 "\01__objc_empty_cache", 3615 &CGM.getModule()); 3616 UsedGlobals.push_back(ObjCEmptyCacheVar); 3617 3618 ObjCEmptyVtableVar = new llvm::GlobalVariable( 3619 ObjCTypes.ImpnfABITy, 3620 false, 3621 llvm::GlobalValue::ExternalLinkage, 3622 0, 3623 "\01__objc_empty_vtable", 3624 &CGM.getModule()); 3625 UsedGlobals.push_back(ObjCEmptyVtableVar); 3626 } 3627 assert(ID->getClassInterface() && 3628 "CGObjCNonFragileABIMac::GenerateClass - class is 0"); 3629 uint32_t InstanceStart = 3630 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ClassnfABITy); 3631 uint32_t InstanceSize = InstanceStart; 3632 uint32_t flags = CLS_META; 3633 std::string ObjCMetaClassName("\01_OBJC_METACLASS_$_"); 3634 std::string ObjCClassName("\01_OBJC_CLASS_$_"); 3635 3636 llvm::GlobalVariable *SuperClassGV, *IsAGV; 3637 3638 bool classIsHidden = IsClassHidden(ID->getClassInterface()); 3639 if (classIsHidden) 3640 flags |= OBJC2_CLS_HIDDEN; 3641 if (!ID->getClassInterface()->getSuperClass()) { 3642 // class is root 3643 flags |= CLS_ROOT; 3644 std::string SuperClassName = ObjCClassName + ClassName; 3645 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName); 3646 if (!SuperClassGV) { 3647 SuperClassGV = 3648 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, 3649 llvm::GlobalValue::ExternalLinkage, 3650 0, 3651 SuperClassName, 3652 &CGM.getModule()); 3653 UsedGlobals.push_back(SuperClassGV); 3654 } 3655 std::string IsAClassName = ObjCMetaClassName + ClassName; 3656 IsAGV = CGM.getModule().getGlobalVariable(IsAClassName); 3657 if (!IsAGV) { 3658 IsAGV = 3659 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, 3660 llvm::GlobalValue::ExternalLinkage, 3661 0, 3662 IsAClassName, 3663 &CGM.getModule()); 3664 UsedGlobals.push_back(IsAGV); 3665 } 3666 } else { 3667 // Has a root. Current class is not a root. 3668 std::string RootClassName = 3669 ID->getClassInterface()->getSuperClass()->getNameAsString(); 3670 std::string SuperClassName = ObjCMetaClassName + RootClassName; 3671 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName); 3672 if (!SuperClassGV) { 3673 SuperClassGV = 3674 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, 3675 llvm::GlobalValue::ExternalLinkage, 3676 0, 3677 SuperClassName, 3678 &CGM.getModule()); 3679 UsedGlobals.push_back(SuperClassGV); 3680 } 3681 IsAGV = SuperClassGV; 3682 } 3683 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, 3684 InstanceStart, 3685 InstanceSize,ID); 3686 std::string TClassName = ObjCMetaClassName + ClassName; 3687 llvm::GlobalVariable *MetaTClass = 3688 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, 3689 classIsHidden); 3690 3691 // Metadata for the class 3692 flags = CLS; 3693 if (classIsHidden) 3694 flags |= OBJC2_CLS_HIDDEN; 3695 if (!ID->getClassInterface()->getSuperClass()) { 3696 flags |= CLS_ROOT; 3697 SuperClassGV = 0; 3698 } 3699 else { 3700 // Has a root. Current class is not a root. 3701 std::string RootClassName = 3702 ID->getClassInterface()->getSuperClass()->getNameAsString(); 3703 std::string SuperClassName = ObjCClassName + RootClassName; 3704 SuperClassGV = CGM.getModule().getGlobalVariable(SuperClassName); 3705 if (!SuperClassGV) { 3706 SuperClassGV = 3707 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, 3708 llvm::GlobalValue::ExternalLinkage, 3709 0, 3710 SuperClassName, 3711 &CGM.getModule()); 3712 UsedGlobals.push_back(SuperClassGV); 3713 } 3714 } 3715 3716 InstanceStart = InstanceSize = 0; 3717 if (ObjCInterfaceDecl *OID = 3718 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface())) { 3719 // FIXME. Share this with the one in EmitIvarList. 3720 const llvm::Type *InterfaceTy = 3721 CGM.getTypes().ConvertType(CGM.getContext().buildObjCInterfaceType(OID)); 3722 const llvm::StructLayout *Layout = 3723 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy)); 3724 3725 RecordDecl::field_iterator firstField, lastField; 3726 const RecordDecl *RD = GetFirstIvarInRecord(OID, firstField, lastField); 3727 3728 for (RecordDecl::field_iterator e = RD->field_end(), 3729 ifield = firstField; ifield != e; ++ifield) 3730 lastField = ifield; 3731 3732 if (lastField != RD->field_end()) { 3733 FieldDecl *Field = *lastField; 3734 const llvm::Type *FieldTy = 3735 CGM.getTypes().ConvertTypeForMem(Field->getType()); 3736 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); 3737 InstanceSize = Layout->getElementOffset( 3738 CGM.getTypes().getLLVMFieldNo(Field)) + 3739 Size; 3740 if (firstField == RD->field_end()) 3741 InstanceStart = InstanceSize; 3742 else 3743 InstanceStart = Layout->getElementOffset(CGM.getTypes(). 3744 getLLVMFieldNo(*firstField)); 3745 } 3746 } 3747 CLASS_RO_GV = BuildClassRoTInitializer(flags, 3748 InstanceStart, 3749 InstanceSize, 3750 ID); 3751 3752 TClassName = ObjCClassName + ClassName; 3753 llvm::GlobalVariable *ClassMD = 3754 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, 3755 classIsHidden); 3756 DefinedClasses.push_back(ClassMD); 3757} 3758 3759/// GenerateProtocolRef - This routine is called to generate code for 3760/// a protocol reference expression; as in: 3761/// @code 3762/// @protocol(Proto1); 3763/// @endcode 3764/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 3765/// which will hold address of the protocol meta-data. 3766/// 3767llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, 3768 const ObjCProtocolDecl *PD) { 3769 3770 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), 3771 ObjCTypes.ExternalProtocolPtrTy); 3772 3773 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); 3774 ProtocolName += PD->getNameAsCString(); 3775 3776 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); 3777 if (PTGV) 3778 return Builder.CreateLoad(PTGV, false, "tmp"); 3779 PTGV = new llvm::GlobalVariable( 3780 Init->getType(), false, 3781 llvm::GlobalValue::WeakLinkage, 3782 Init, 3783 ProtocolName, 3784 &CGM.getModule()); 3785 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); 3786 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); 3787 UsedGlobals.push_back(PTGV); 3788 return Builder.CreateLoad(PTGV, false, "tmp"); 3789} 3790 3791/// GenerateCategory - Build metadata for a category implementation. 3792/// struct _category_t { 3793/// const char * const name; 3794/// struct _class_t *const cls; 3795/// const struct _method_list_t * const instance_methods; 3796/// const struct _method_list_t * const class_methods; 3797/// const struct _protocol_list_t * const protocols; 3798/// const struct _prop_list_t * const properties; 3799/// } 3800/// 3801void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) 3802{ 3803 const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); 3804 const char *Prefix = "\01l_OBJC_$_CATEGORY_"; 3805 std::string ExtCatName(Prefix + Interface->getNameAsString()+ 3806 "_$_" + OCD->getNameAsString()); 3807 std::string ExtClassName("\01_OBJC_CLASS_$_" + Interface->getNameAsString()); 3808 3809 std::vector<llvm::Constant*> Values(6); 3810 Values[0] = GetClassName(OCD->getIdentifier()); 3811 // meta-class entry symbol 3812 llvm::GlobalVariable *ClassGV = 3813 CGM.getModule().getGlobalVariable(ExtClassName); 3814 if (!ClassGV) 3815 ClassGV = 3816 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, 3817 llvm::GlobalValue::ExternalLinkage, 3818 0, 3819 ExtClassName, 3820 &CGM.getModule()); 3821 UsedGlobals.push_back(ClassGV); 3822 Values[1] = ClassGV; 3823 std::vector<llvm::Constant*> Methods; 3824 std::string MethodListName(Prefix); 3825 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + 3826 "_$_" + OCD->getNameAsString(); 3827 3828 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), 3829 e = OCD->instmeth_end(); i != e; ++i) { 3830 // Instance methods should always be defined. 3831 Methods.push_back(GetMethodConstant(*i)); 3832 } 3833 3834 Values[2] = EmitMethodList(MethodListName, 3835 "__DATA, __objc_const", 3836 Methods); 3837 3838 MethodListName = Prefix; 3839 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + 3840 OCD->getNameAsString(); 3841 Methods.clear(); 3842 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), 3843 e = OCD->classmeth_end(); i != e; ++i) { 3844 // Class methods should always be defined. 3845 Methods.push_back(GetMethodConstant(*i)); 3846 } 3847 3848 Values[3] = EmitMethodList(MethodListName, 3849 "__DATA, __objc_const", 3850 Methods); 3851 const ObjCCategoryDecl *Category = 3852 Interface->FindCategoryDeclaration(OCD->getIdentifier()); 3853 if (Category) { 3854 std::string ExtName(Interface->getNameAsString() + "_$_" + 3855 OCD->getNameAsString()); 3856 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" 3857 + Interface->getNameAsString() + "_$_" 3858 + Category->getNameAsString(), 3859 Category->protocol_begin(), 3860 Category->protocol_end()); 3861 Values[5] = 3862 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, 3863 OCD, Category, ObjCTypes); 3864 } 3865 else { 3866 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); 3867 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); 3868 } 3869 3870 llvm::Constant *Init = 3871 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, 3872 Values); 3873 llvm::GlobalVariable *GCATV 3874 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy, 3875 false, 3876 llvm::GlobalValue::InternalLinkage, 3877 Init, 3878 ExtCatName, 3879 &CGM.getModule()); 3880 GCATV->setAlignment( 3881 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy)); 3882 GCATV->setSection("__DATA, __objc_const"); 3883 UsedGlobals.push_back(GCATV); 3884 DefinedCategories.push_back(GCATV); 3885} 3886 3887/// GetMethodConstant - Return a struct objc_method constant for the 3888/// given method if it has been defined. The result is null if the 3889/// method has not been defined. The return value has type MethodPtrTy. 3890llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( 3891 const ObjCMethodDecl *MD) { 3892 // FIXME: Use DenseMap::lookup 3893 llvm::Function *Fn = MethodDefinitions[MD]; 3894 if (!Fn) 3895 return 0; 3896 3897 std::vector<llvm::Constant*> Method(3); 3898 Method[0] = 3899 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), 3900 ObjCTypes.SelectorPtrTy); 3901 Method[1] = GetMethodVarType(MD); 3902 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); 3903 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); 3904} 3905 3906/// EmitMethodList - Build meta-data for method declarations 3907/// struct _method_list_t { 3908/// uint32_t entsize; // sizeof(struct _objc_method) 3909/// uint32_t method_count; 3910/// struct _objc_method method_list[method_count]; 3911/// } 3912/// 3913llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList( 3914 const std::string &Name, 3915 const char *Section, 3916 const ConstantVector &Methods) { 3917 // Return null for empty list. 3918 if (Methods.empty()) 3919 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); 3920 3921 std::vector<llvm::Constant*> Values(3); 3922 // sizeof(struct _objc_method) 3923 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.MethodTy); 3924 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); 3925 // method_count 3926 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); 3927 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, 3928 Methods.size()); 3929 Values[2] = llvm::ConstantArray::get(AT, Methods); 3930 llvm::Constant *Init = llvm::ConstantStruct::get(Values); 3931 3932 llvm::GlobalVariable *GV = 3933 new llvm::GlobalVariable(Init->getType(), false, 3934 llvm::GlobalValue::InternalLinkage, 3935 Init, 3936 Name, 3937 &CGM.getModule()); 3938 GV->setAlignment( 3939 CGM.getTargetData().getPrefTypeAlignment(Init->getType())); 3940 GV->setSection(Section); 3941 UsedGlobals.push_back(GV); 3942 return llvm::ConstantExpr::getBitCast(GV, 3943 ObjCTypes.MethodListnfABIPtrTy); 3944} 3945 3946/// ObjCIvarOffsetVariable - Returns the ivar offset variable for 3947/// the given ivar. 3948/// 3949llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable( 3950 std::string &Name, 3951 const ObjCInterfaceDecl *ID, 3952 const ObjCIvarDecl *Ivar) { 3953 Name += "\01_OBJC_IVAR_$_" + 3954 getInterfaceDeclForIvar(ID, Ivar)->getNameAsString() + '.' 3955 + Ivar->getNameAsString(); 3956 llvm::GlobalVariable *IvarOffsetGV = 3957 CGM.getModule().getGlobalVariable(Name); 3958 if (!IvarOffsetGV) 3959 IvarOffsetGV = 3960 new llvm::GlobalVariable(ObjCTypes.LongTy, 3961 false, 3962 llvm::GlobalValue::ExternalLinkage, 3963 0, 3964 Name, 3965 &CGM.getModule()); 3966 return IvarOffsetGV; 3967} 3968 3969llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar( 3970 const ObjCInterfaceDecl *ID, 3971 const ObjCIvarDecl *Ivar, 3972 unsigned long int Offset) { 3973 3974 assert(ID && "EmitIvarOffsetVar - null interface decl."); 3975 std::string ExternalName("\01_OBJC_IVAR_$_" + ID->getNameAsString() + '.' 3976 + Ivar->getNameAsString()); 3977 llvm::Constant *Init = llvm::ConstantInt::get(ObjCTypes.LongTy, Offset); 3978 3979 llvm::GlobalVariable *IvarOffsetGV = 3980 CGM.getModule().getGlobalVariable(ExternalName); 3981 if (IvarOffsetGV) { 3982 // ivar offset symbol already built due to user code referencing it. 3983 IvarOffsetGV->setAlignment( 3984 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy)); 3985 IvarOffsetGV->setInitializer(Init); 3986 IvarOffsetGV->setSection("__DATA, __objc_const"); 3987 UsedGlobals.push_back(IvarOffsetGV); 3988 return IvarOffsetGV; 3989 } 3990 3991 IvarOffsetGV = 3992 new llvm::GlobalVariable(Init->getType(), 3993 false, 3994 llvm::GlobalValue::ExternalLinkage, 3995 Init, 3996 ExternalName, 3997 &CGM.getModule()); 3998 IvarOffsetGV->setAlignment( 3999 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy)); 4000 // @private and @package have hidden visibility. 4001 bool globalVisibility = (Ivar->getAccessControl() == ObjCIvarDecl::Public || 4002 Ivar->getAccessControl() == ObjCIvarDecl::Protected); 4003 if (!globalVisibility) 4004 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); 4005 else 4006 if (IsClassHidden(ID)) 4007 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); 4008 4009 IvarOffsetGV->setSection("__DATA, __objc_const"); 4010 UsedGlobals.push_back(IvarOffsetGV); 4011 return IvarOffsetGV; 4012} 4013 4014/// EmitIvarList - Emit the ivar list for the given 4015/// implementation. If ForClass is true the list of class ivars 4016/// (i.e. metaclass ivars) is emitted, otherwise the list of 4017/// interface ivars will be emitted. The return value has type 4018/// IvarListnfABIPtrTy. 4019/// struct _ivar_t { 4020/// unsigned long int *offset; // pointer to ivar offset location 4021/// char *name; 4022/// char *type; 4023/// uint32_t alignment; 4024/// uint32_t size; 4025/// } 4026/// struct _ivar_list_t { 4027/// uint32 entsize; // sizeof(struct _ivar_t) 4028/// uint32 count; 4029/// struct _iver_t list[count]; 4030/// } 4031/// 4032llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( 4033 const ObjCImplementationDecl *ID) { 4034 4035 std::vector<llvm::Constant*> Ivars, Ivar(5); 4036 4037 const ObjCInterfaceDecl *OID = ID->getClassInterface(); 4038 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); 4039 4040 // FIXME. Consolidate this with similar code in GenerateClass. 4041 const llvm::Type *InterfaceTy = 4042 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType( 4043 const_cast<ObjCInterfaceDecl*>(OID))); 4044 const llvm::StructLayout *Layout = 4045 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy)); 4046 4047 RecordDecl::field_iterator i,p; 4048 const RecordDecl *RD = GetFirstIvarInRecord(OID, i,p); 4049 ObjCInterfaceDecl::ivar_iterator I = OID->ivar_begin(); 4050 4051 for (RecordDecl::field_iterator e = RD->field_end(); i != e; ++i) { 4052 FieldDecl *Field = *i; 4053 unsigned long offset = Layout->getElementOffset(CGM.getTypes(). 4054 getLLVMFieldNo(Field)); 4055 const ObjCIvarDecl *ivarDecl = *I++; 4056 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), ivarDecl, offset); 4057 if (Field->getIdentifier()) 4058 Ivar[1] = GetMethodVarName(Field->getIdentifier()); 4059 else 4060 Ivar[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); 4061 std::string TypeStr; 4062 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); 4063 Ivar[2] = GetMethodVarType(TypeStr); 4064 const llvm::Type *FieldTy = 4065 CGM.getTypes().ConvertTypeForMem(Field->getType()); 4066 unsigned Size = CGM.getTargetData().getTypePaddedSize(FieldTy); 4067 unsigned Align = CGM.getContext().getPreferredTypeAlign( 4068 Field->getType().getTypePtr()) >> 3; 4069 Align = llvm::Log2_32(Align); 4070 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); 4071 // NOTE. Size of a bitfield does not match gcc's, because of the way 4072 // bitfields are treated special in each. But I am told that 'size' 4073 // for bitfield ivars is ignored by the runtime so it does not matter. 4074 // (even if it matters, some day, there is enough info. to get the bitfield 4075 // right! 4076 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); 4077 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); 4078 } 4079 // Return null for empty list. 4080 if (Ivars.empty()) 4081 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); 4082 std::vector<llvm::Constant*> Values(3); 4083 unsigned Size = CGM.getTargetData().getTypePaddedSize(ObjCTypes.IvarnfABITy); 4084 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); 4085 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); 4086 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, 4087 Ivars.size()); 4088 Values[2] = llvm::ConstantArray::get(AT, Ivars); 4089 llvm::Constant *Init = llvm::ConstantStruct::get(Values); 4090 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; 4091 llvm::GlobalVariable *GV = 4092 new llvm::GlobalVariable(Init->getType(), false, 4093 llvm::GlobalValue::InternalLinkage, 4094 Init, 4095 Prefix + OID->getNameAsString(), 4096 &CGM.getModule()); 4097 GV->setAlignment( 4098 CGM.getTargetData().getPrefTypeAlignment(Init->getType())); 4099 GV->setSection("__DATA, __objc_const"); 4100 4101 UsedGlobals.push_back(GV); 4102 return llvm::ConstantExpr::getBitCast(GV, 4103 ObjCTypes.IvarListnfABIPtrTy); 4104} 4105 4106llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( 4107 const ObjCProtocolDecl *PD) { 4108 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; 4109 4110 if (!Entry) { 4111 // We use the initializer as a marker of whether this is a forward 4112 // reference or not. At module finalization we add the empty 4113 // contents for protocols which were referenced but never defined. 4114 Entry = 4115 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, 4116 llvm::GlobalValue::ExternalLinkage, 4117 0, 4118 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(), 4119 &CGM.getModule()); 4120 Entry->setSection("__DATA,__datacoal_nt,coalesced"); 4121 UsedGlobals.push_back(Entry); 4122 } 4123 4124 return Entry; 4125} 4126 4127/// GetOrEmitProtocol - Generate the protocol meta-data: 4128/// @code 4129/// struct _protocol_t { 4130/// id isa; // NULL 4131/// const char * const protocol_name; 4132/// const struct _protocol_list_t * protocol_list; // super protocols 4133/// const struct method_list_t * const instance_methods; 4134/// const struct method_list_t * const class_methods; 4135/// const struct method_list_t *optionalInstanceMethods; 4136/// const struct method_list_t *optionalClassMethods; 4137/// const struct _prop_list_t * properties; 4138/// const uint32_t size; // sizeof(struct _protocol_t) 4139/// const uint32_t flags; // = 0 4140/// } 4141/// @endcode 4142/// 4143 4144llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( 4145 const ObjCProtocolDecl *PD) { 4146 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; 4147 4148 // Early exit if a defining object has already been generated. 4149 if (Entry && Entry->hasInitializer()) 4150 return Entry; 4151 4152 const char *ProtocolName = PD->getNameAsCString(); 4153 4154 // Construct method lists. 4155 std::vector<llvm::Constant*> InstanceMethods, ClassMethods; 4156 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; 4157 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(), 4158 e = PD->instmeth_end(); i != e; ++i) { 4159 ObjCMethodDecl *MD = *i; 4160 llvm::Constant *C = GetMethodDescriptionConstant(MD); 4161 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { 4162 OptInstanceMethods.push_back(C); 4163 } else { 4164 InstanceMethods.push_back(C); 4165 } 4166 } 4167 4168 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(), 4169 e = PD->classmeth_end(); i != e; ++i) { 4170 ObjCMethodDecl *MD = *i; 4171 llvm::Constant *C = GetMethodDescriptionConstant(MD); 4172 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { 4173 OptClassMethods.push_back(C); 4174 } else { 4175 ClassMethods.push_back(C); 4176 } 4177 } 4178 4179 std::vector<llvm::Constant*> Values(10); 4180 // isa is NULL 4181 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); 4182 Values[1] = GetClassName(PD->getIdentifier()); 4183 Values[2] = EmitProtocolList( 4184 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(), 4185 PD->protocol_begin(), 4186 PD->protocol_end()); 4187 4188 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" 4189 + PD->getNameAsString(), 4190 "__DATA, __objc_const", 4191 InstanceMethods); 4192 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" 4193 + PD->getNameAsString(), 4194 "__DATA, __objc_const", 4195 ClassMethods); 4196 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" 4197 + PD->getNameAsString(), 4198 "__DATA, __objc_const", 4199 OptInstanceMethods); 4200 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" 4201 + PD->getNameAsString(), 4202 "__DATA, __objc_const", 4203 OptClassMethods); 4204 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(), 4205 0, PD, ObjCTypes); 4206 uint32_t Size = 4207 CGM.getTargetData().getTypePaddedSize(ObjCTypes.ProtocolnfABITy); 4208 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); 4209 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); 4210 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, 4211 Values); 4212 4213 if (Entry) { 4214 // Already created, fix the linkage and update the initializer. 4215 Entry->setLinkage(llvm::GlobalValue::WeakLinkage); 4216 Entry->setInitializer(Init); 4217 } else { 4218 Entry = 4219 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false, 4220 llvm::GlobalValue::WeakLinkage, 4221 Init, 4222 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName, 4223 &CGM.getModule()); 4224 Entry->setAlignment( 4225 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy)); 4226 Entry->setSection("__DATA,__datacoal_nt,coalesced"); 4227 } 4228 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); 4229 4230 // Use this protocol meta-data to build protocol list table in section 4231 // __DATA, __objc_protolist 4232 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable( 4233 ObjCTypes.ProtocolnfABIPtrTy, false, 4234 llvm::GlobalValue::WeakLinkage, 4235 Entry, 4236 std::string("\01l_OBJC_LABEL_PROTOCOL_$_") 4237 +ProtocolName, 4238 &CGM.getModule()); 4239 PTGV->setAlignment( 4240 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); 4241 PTGV->setSection("__DATA, __objc_protolist"); 4242 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); 4243 UsedGlobals.push_back(PTGV); 4244 return Entry; 4245} 4246 4247/// EmitProtocolList - Generate protocol list meta-data: 4248/// @code 4249/// struct _protocol_list_t { 4250/// long protocol_count; // Note, this is 32/64 bit 4251/// struct _protocol_t[protocol_count]; 4252/// } 4253/// @endcode 4254/// 4255llvm::Constant * 4256CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name, 4257 ObjCProtocolDecl::protocol_iterator begin, 4258 ObjCProtocolDecl::protocol_iterator end) { 4259 std::vector<llvm::Constant*> ProtocolRefs; 4260 4261 // Just return null for empty protocol lists 4262 if (begin == end) 4263 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); 4264 4265 // FIXME: We shouldn't need to do this lookup here, should we? 4266 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); 4267 if (GV) 4268 return llvm::ConstantExpr::getBitCast(GV, 4269 ObjCTypes.ProtocolListnfABIPtrTy); 4270 4271 for (; begin != end; ++begin) 4272 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? 4273 4274 // This list is null terminated. 4275 ProtocolRefs.push_back(llvm::Constant::getNullValue( 4276 ObjCTypes.ProtocolnfABIPtrTy)); 4277 4278 std::vector<llvm::Constant*> Values(2); 4279 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); 4280 Values[1] = 4281 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, 4282 ProtocolRefs.size()), 4283 ProtocolRefs); 4284 4285 llvm::Constant *Init = llvm::ConstantStruct::get(Values); 4286 GV = new llvm::GlobalVariable(Init->getType(), false, 4287 llvm::GlobalValue::InternalLinkage, 4288 Init, 4289 Name, 4290 &CGM.getModule()); 4291 GV->setSection("__DATA, __objc_const"); 4292 GV->setAlignment( 4293 CGM.getTargetData().getPrefTypeAlignment(Init->getType())); 4294 UsedGlobals.push_back(GV); 4295 return llvm::ConstantExpr::getBitCast(GV, 4296 ObjCTypes.ProtocolListnfABIPtrTy); 4297} 4298 4299/// GetMethodDescriptionConstant - This routine build following meta-data: 4300/// struct _objc_method { 4301/// SEL _cmd; 4302/// char *method_type; 4303/// char *_imp; 4304/// } 4305 4306llvm::Constant * 4307CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { 4308 std::vector<llvm::Constant*> Desc(3); 4309 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), 4310 ObjCTypes.SelectorPtrTy); 4311 Desc[1] = GetMethodVarType(MD); 4312 // Protocol methods have no implementation. So, this entry is always NULL. 4313 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); 4314 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); 4315} 4316 4317/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. 4318/// This code gen. amounts to generating code for: 4319/// @code 4320/// (type *)((char *)base + _OBJC_IVAR_$_.ivar; 4321/// @encode 4322/// 4323LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( 4324 CodeGen::CodeGenFunction &CGF, 4325 QualType ObjectTy, 4326 llvm::Value *BaseValue, 4327 const ObjCIvarDecl *Ivar, 4328 const FieldDecl *Field, 4329 unsigned CVRQualifiers) { 4330 assert(ObjectTy->isObjCInterfaceType() && 4331 "CGObjCNonFragileABIMac::EmitObjCValueForIvar"); 4332 ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); 4333 std::string ExternalName; 4334 llvm::GlobalVariable *IvarOffsetGV = 4335 ObjCIvarOffsetVariable(ExternalName, ID, Ivar); 4336 4337 // (char *) BaseValue 4338 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, 4339 ObjCTypes.Int8PtrTy); 4340 llvm::Value *Offset = CGF.Builder.CreateLoad(IvarOffsetGV); 4341 // (char*)BaseValue + Offset_symbol 4342 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr"); 4343 // (type *)((char*)BaseValue + Offset_symbol) 4344 const llvm::Type *IvarTy = 4345 CGM.getTypes().ConvertType(Ivar->getType()); 4346 llvm::Type *ptrIvarTy = llvm::PointerType::getUnqual(IvarTy); 4347 V = CGF.Builder.CreateBitCast(V, ptrIvarTy); 4348 4349 if (Ivar->isBitField()) 4350 return CGF.EmitLValueForBitfield(V, const_cast<FieldDecl *>(Field), 4351 CVRQualifiers); 4352 4353 LValue LV = LValue::MakeAddr(V, 4354 Ivar->getType().getCVRQualifiers()|CVRQualifiers, 4355 CGM.getContext().getObjCGCAttrKind(Ivar->getType())); 4356 LValue::SetObjCIvar(LV, true); 4357 return LV; 4358} 4359 4360llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( 4361 CodeGen::CodeGenFunction &CGF, 4362 ObjCInterfaceDecl *Interface, 4363 const ObjCIvarDecl *Ivar) { 4364 std::string ExternalName; 4365 llvm::GlobalVariable *IvarOffsetGV = 4366 ObjCIvarOffsetVariable(ExternalName, Interface, Ivar); 4367 4368 return CGF.Builder.CreateLoad(IvarOffsetGV, false, "ivar"); 4369} 4370 4371CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend( 4372 CodeGen::CodeGenFunction &CGF, 4373 QualType ResultType, 4374 Selector Sel, 4375 llvm::Value *Receiver, 4376 QualType Arg0Ty, 4377 bool IsSuper, 4378 const CallArgList &CallArgs) { 4379 // FIXME. Even though IsSuper is passes. This function doese not 4380 // handle calls to 'super' receivers. 4381 CodeGenTypes &Types = CGM.getTypes(); 4382 llvm::Value *Arg0 = Receiver; 4383 if (!IsSuper) 4384 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); 4385 4386 // Find the message function name. 4387 // FIXME. This is too much work to get the ABI-specific result type 4388 // needed to find the message name. 4389 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, 4390 llvm::SmallVector<QualType, 16>()); 4391 llvm::Constant *Fn; 4392 std::string Name("\01l_"); 4393 if (CGM.ReturnTypeUsesSret(FnInfo)) { 4394#if 0 4395 // unlike what is documented. gcc never generates this API!! 4396 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { 4397 Fn = ObjCTypes.MessageSendIdStretFixupFn; 4398 // FIXME. Is there a better way of getting these names. 4399 // They are available in RuntimeFunctions vector pair. 4400 Name += "objc_msgSendId_stret_fixup"; 4401 } 4402 else 4403#endif 4404 if (IsSuper) { 4405 Fn = ObjCTypes.MessageSendSuper2StretFixupFn; 4406 Name += "objc_msgSendSuper2_stret_fixup"; 4407 } 4408 else 4409 { 4410 Fn = ObjCTypes.MessageSendStretFixupFn; 4411 Name += "objc_msgSend_stret_fixup"; 4412 } 4413 } 4414 else if (ResultType->isFloatingType() && 4415 // Selection of frret API only happens in 32bit nonfragile ABI. 4416 CGM.getTargetData().getTypePaddedSize(ObjCTypes.LongTy) == 4) { 4417 Fn = ObjCTypes.MessageSendFpretFixupFn; 4418 Name += "objc_msgSend_fpret_fixup"; 4419 } 4420 else { 4421#if 0 4422// unlike what is documented. gcc never generates this API!! 4423 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { 4424 Fn = ObjCTypes.MessageSendIdFixupFn; 4425 Name += "objc_msgSendId_fixup"; 4426 } 4427 else 4428#endif 4429 if (IsSuper) { 4430 Fn = ObjCTypes.MessageSendSuper2FixupFn; 4431 Name += "objc_msgSendSuper2_fixup"; 4432 } 4433 else 4434 { 4435 Fn = ObjCTypes.MessageSendFixupFn; 4436 Name += "objc_msgSend_fixup"; 4437 } 4438 } 4439 Name += '_'; 4440 std::string SelName(Sel.getAsString()); 4441 // Replace all ':' in selector name with '_' ouch! 4442 for(unsigned i = 0; i < SelName.size(); i++) 4443 if (SelName[i] == ':') 4444 SelName[i] = '_'; 4445 Name += SelName; 4446 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); 4447 if (!GV) { 4448 // Build messafe ref table entry. 4449 std::vector<llvm::Constant*> Values(2); 4450 Values[0] = Fn; 4451 Values[1] = GetMethodVarName(Sel); 4452 llvm::Constant *Init = llvm::ConstantStruct::get(Values); 4453 GV = new llvm::GlobalVariable(Init->getType(), false, 4454 llvm::GlobalValue::WeakLinkage, 4455 Init, 4456 Name, 4457 &CGM.getModule()); 4458 GV->setVisibility(llvm::GlobalValue::HiddenVisibility); 4459 GV->setAlignment( 4460 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.MessageRefTy)); 4461 GV->setSection("__DATA, __objc_msgrefs, coalesced"); 4462 UsedGlobals.push_back(GV); 4463 } 4464 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy); 4465 4466 CallArgList ActualArgs; 4467 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); 4468 ActualArgs.push_back(std::make_pair(RValue::get(Arg1), 4469 ObjCTypes.MessageRefCPtrTy)); 4470 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); 4471 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs); 4472 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0); 4473 Callee = CGF.Builder.CreateLoad(Callee); 4474 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true); 4475 Callee = CGF.Builder.CreateBitCast(Callee, 4476 llvm::PointerType::getUnqual(FTy)); 4477 return CGF.EmitCall(FnInfo1, Callee, ActualArgs); 4478} 4479 4480/// Generate code for a message send expression in the nonfragile abi. 4481CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend( 4482 CodeGen::CodeGenFunction &CGF, 4483 QualType ResultType, 4484 Selector Sel, 4485 llvm::Value *Receiver, 4486 bool IsClassMessage, 4487 const CallArgList &CallArgs) { 4488 return EmitMessageSend(CGF, ResultType, Sel, 4489 Receiver, CGF.getContext().getObjCIdType(), 4490 false, CallArgs); 4491} 4492 4493llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, 4494 const ObjCInterfaceDecl *ID, 4495 bool IsSuper) { 4496 4497 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; 4498 4499 if (!Entry) { 4500 std::string ClassName("\01_OBJC_CLASS_$_" + ID->getNameAsString()); 4501 llvm::GlobalVariable *ClassGV = 4502 CGM.getModule().getGlobalVariable(ClassName); 4503 if (!ClassGV) { 4504 ClassGV = 4505 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, 4506 llvm::GlobalValue::ExternalLinkage, 4507 0, 4508 ClassName, 4509 &CGM.getModule()); 4510 UsedGlobals.push_back(ClassGV); 4511 } 4512 Entry = 4513 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, 4514 llvm::GlobalValue::InternalLinkage, 4515 ClassGV, 4516 IsSuper ? "\01L_OBJC_CLASSLIST_SUP_REFS_$_" 4517 : "\01L_OBJC_CLASSLIST_REFERENCES_$_", 4518 &CGM.getModule()); 4519 Entry->setAlignment( 4520 CGM.getTargetData().getPrefTypeAlignment( 4521 ObjCTypes.ClassnfABIPtrTy)); 4522 4523 if (IsSuper) 4524 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip"); 4525 else 4526 Entry->setSection("__OBJC,__objc_classrefs,regular,no_dead_strip"); 4527 UsedGlobals.push_back(Entry); 4528 } 4529 4530 return Builder.CreateLoad(Entry, false, "tmp"); 4531} 4532 4533/// EmitMetaClassRef - Return a Value * of the address of _class_t 4534/// meta-data 4535/// 4536llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, 4537 const ObjCInterfaceDecl *ID) { 4538 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; 4539 if (Entry) 4540 return Builder.CreateLoad(Entry, false, "tmp"); 4541 4542 std::string MetaClassName("\01_OBJC_METACLASS_$_" + ID->getNameAsString()); 4543 llvm::GlobalVariable *MetaClassGV = 4544 CGM.getModule().getGlobalVariable(MetaClassName); 4545 if (!MetaClassGV) { 4546 MetaClassGV = 4547 new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false, 4548 llvm::GlobalValue::ExternalLinkage, 4549 0, 4550 MetaClassName, 4551 &CGM.getModule()); 4552 UsedGlobals.push_back(MetaClassGV); 4553 } 4554 4555 Entry = 4556 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false, 4557 llvm::GlobalValue::InternalLinkage, 4558 MetaClassGV, 4559 "\01L_OBJC_CLASSLIST_SUP_REFS_$_", 4560 &CGM.getModule()); 4561 Entry->setAlignment( 4562 CGM.getTargetData().getPrefTypeAlignment( 4563 ObjCTypes.ClassnfABIPtrTy)); 4564 4565 Entry->setSection("__OBJC,__objc_superrefs,regular,no_dead_strip"); 4566 UsedGlobals.push_back(Entry); 4567 4568 return Builder.CreateLoad(Entry, false, "tmp"); 4569} 4570 4571/// GetClass - Return a reference to the class for the given interface 4572/// decl. 4573llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, 4574 const ObjCInterfaceDecl *ID) { 4575 return EmitClassRef(Builder, ID); 4576} 4577 4578/// Generates a message send where the super is the receiver. This is 4579/// a message send to self with special delivery semantics indicating 4580/// which class's method should be called. 4581CodeGen::RValue 4582CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, 4583 QualType ResultType, 4584 Selector Sel, 4585 const ObjCInterfaceDecl *Class, 4586 llvm::Value *Receiver, 4587 bool IsClassMessage, 4588 const CodeGen::CallArgList &CallArgs) { 4589 // ... 4590 // Create and init a super structure; this is a (receiver, class) 4591 // pair we will pass to objc_msgSendSuper. 4592 llvm::Value *ObjCSuper = 4593 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); 4594 4595 llvm::Value *ReceiverAsObject = 4596 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); 4597 CGF.Builder.CreateStore(ReceiverAsObject, 4598 CGF.Builder.CreateStructGEP(ObjCSuper, 0)); 4599 4600 // If this is a class message the metaclass is passed as the target. 4601 llvm::Value *Target = 4602 IsClassMessage ? EmitMetaClassRef(CGF.Builder, Class) 4603 : EmitClassRef(CGF.Builder, Class, true); 4604 4605 // FIXME: We shouldn't need to do this cast, rectify the ASTContext 4606 // and ObjCTypes types. 4607 const llvm::Type *ClassTy = 4608 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); 4609 Target = CGF.Builder.CreateBitCast(Target, ClassTy); 4610 CGF.Builder.CreateStore(Target, 4611 CGF.Builder.CreateStructGEP(ObjCSuper, 1)); 4612 4613 return EmitMessageSend(CGF, ResultType, Sel, 4614 ObjCSuper, ObjCTypes.SuperPtrCTy, 4615 true, CallArgs); 4616} 4617 4618llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, 4619 Selector Sel) { 4620 llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; 4621 4622 if (!Entry) { 4623 llvm::Constant *Casted = 4624 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), 4625 ObjCTypes.SelectorPtrTy); 4626 Entry = 4627 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false, 4628 llvm::GlobalValue::InternalLinkage, 4629 Casted, "\01L_OBJC_SELECTOR_REFERENCES_", 4630 &CGM.getModule()); 4631 Entry->setSection("__DATA,__objc_selrefs,literal_pointers,no_dead_strip"); 4632 UsedGlobals.push_back(Entry); 4633 } 4634 4635 return Builder.CreateLoad(Entry, false, "tmp"); 4636} 4637/// EmitObjCIvarAssign - Code gen for assigning to a __strong object. 4638/// objc_assign_ivar (id src, id *dst) 4639/// 4640void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, 4641 llvm::Value *src, llvm::Value *dst) 4642{ 4643 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); 4644 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); 4645 CGF.Builder.CreateCall2(ObjCTypes.GcAssignIvarFn, 4646 src, dst, "assignivar"); 4647 return; 4648} 4649 4650/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. 4651/// objc_assign_strongCast (id src, id *dst) 4652/// 4653void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( 4654 CodeGen::CodeGenFunction &CGF, 4655 llvm::Value *src, llvm::Value *dst) 4656{ 4657 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); 4658 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); 4659 CGF.Builder.CreateCall2(ObjCTypes.GcAssignStrongCastFn, 4660 src, dst, "weakassign"); 4661 return; 4662} 4663 4664/// EmitObjCWeakRead - Code gen for loading value of a __weak 4665/// object: objc_read_weak (id *src) 4666/// 4667llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( 4668 CodeGen::CodeGenFunction &CGF, 4669 llvm::Value *AddrWeakObj) 4670{ 4671 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); 4672 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.GcReadWeakFn, 4673 AddrWeakObj, "weakread"); 4674 return read_weak; 4675} 4676 4677/// EmitObjCWeakAssign - Code gen for assigning to a __weak object. 4678/// objc_assign_weak (id src, id *dst) 4679/// 4680void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, 4681 llvm::Value *src, llvm::Value *dst) 4682{ 4683 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); 4684 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); 4685 CGF.Builder.CreateCall2(ObjCTypes.GcAssignWeakFn, 4686 src, dst, "weakassign"); 4687 return; 4688} 4689 4690/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. 4691/// objc_assign_global (id src, id *dst) 4692/// 4693void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, 4694 llvm::Value *src, llvm::Value *dst) 4695{ 4696 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); 4697 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); 4698 CGF.Builder.CreateCall2(ObjCTypes.GcAssignGlobalFn, 4699 src, dst, "globalassign"); 4700 return; 4701} 4702 4703/// EmitThrowStmt - Generate code for a throw statement. 4704void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, 4705 const ObjCAtThrowStmt &S) { 4706 llvm::Value *ExceptionAsObject; 4707 4708 if (const Expr *ThrowExpr = S.getThrowExpr()) { 4709 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); 4710 ExceptionAsObject = 4711 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); 4712 4713 CGF.Builder.CreateCall(ObjCTypes.ExceptionThrowFn, ExceptionAsObject); 4714 CGF.Builder.CreateUnreachable(); 4715 } else { 4716 CGF.ErrorUnsupported(&S, "rethrow statement"); 4717 } 4718 4719 // Clear the insertion point to indicate we are in unreachable code. 4720 CGF.Builder.ClearInsertionPoint(); 4721} 4722 4723/* *** */ 4724 4725CodeGen::CGObjCRuntime * 4726CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { 4727 return new CGObjCMac(CGM); 4728} 4729 4730CodeGen::CGObjCRuntime * 4731CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) { 4732 return new CGObjCNonFragileABIMac(CGM); 4733} 4734