CGObjCMac.cpp revision c56f34a1c1779de15330bdb3eec39b3418802d47
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/Support/IRBuilder.h" 25#include "llvm/Target/TargetData.h" 26#include <sstream> 27 28using namespace clang; 29 30namespace { 31 32 typedef std::vector<llvm::Constant*> ConstantVector; 33 34 // FIXME: We should find a nicer way to make the labels for 35 // metadata, string concatenation is lame. 36 37/// ObjCTypesHelper - Helper class that encapsulates lazy 38/// construction of varies types used during ObjC generation. 39class ObjCTypesHelper { 40private: 41 CodeGen::CodeGenModule &CGM; 42 43 llvm::Function *MessageSendFn, *MessageSendStretFn; 44 llvm::Function *MessageSendSuperFn, *MessageSendSuperStretFn; 45 46public: 47 const llvm::Type *ShortTy, *IntTy, *LongTy; 48 const llvm::Type *Int8PtrTy; 49 50 /// ObjectPtrTy - LLVM type for object handles (typeof(id)) 51 const llvm::Type *ObjectPtrTy; 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 /// SuperTy - LLVM type for struct objc_super. 59 const llvm::StructType *SuperTy; 60 /// SuperPtrTy - LLVM type for struct objc_super *. 61 const llvm::Type *SuperPtrTy; 62 63 /// SymtabTy - LLVM type for struct objc_symtab. 64 const llvm::StructType *SymtabTy; 65 /// SymtabPtrTy - LLVM type for struct objc_symtab *. 66 const llvm::Type *SymtabPtrTy; 67 /// ModuleTy - LLVM type for struct objc_module. 68 const llvm::StructType *ModuleTy; 69 70 /// ProtocolTy - LLVM type for struct objc_protocol. 71 const llvm::StructType *ProtocolTy; 72 /// ProtocolPtrTy - LLVM type for struct objc_protocol *. 73 const llvm::Type *ProtocolPtrTy; 74 /// ProtocolExtensionTy - LLVM type for struct 75 /// objc_protocol_extension. 76 const llvm::StructType *ProtocolExtensionTy; 77 /// ProtocolExtensionTy - LLVM type for struct 78 /// objc_protocol_extension *. 79 const llvm::Type *ProtocolExtensionPtrTy; 80 /// MethodDescriptionTy - LLVM type for struct 81 /// objc_method_description. 82 const llvm::StructType *MethodDescriptionTy; 83 /// MethodDescriptionListTy - LLVM type for struct 84 /// objc_method_description_list. 85 const llvm::StructType *MethodDescriptionListTy; 86 /// MethodDescriptionListPtrTy - LLVM type for struct 87 /// objc_method_description_list *. 88 const llvm::Type *MethodDescriptionListPtrTy; 89 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t 90 /// in GCC parlance). 91 const llvm::StructType *PropertyTy; 92 /// PropertyListTy - LLVM type for struct objc_property_list 93 /// (_prop_list_t in GCC parlance). 94 const llvm::StructType *PropertyListTy; 95 /// PropertyListPtrTy - LLVM type for struct objc_property_list*. 96 const llvm::Type *PropertyListPtrTy; 97 /// ProtocolListTy - LLVM type for struct objc_property_list. 98 const llvm::Type *ProtocolListTy; 99 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. 100 const llvm::Type *ProtocolListPtrTy; 101 /// CategoryTy - LLVM type for struct objc_category. 102 const llvm::StructType *CategoryTy; 103 /// ClassTy - LLVM type for struct objc_class. 104 const llvm::StructType *ClassTy; 105 /// ClassPtrTy - LLVM type for struct objc_class *. 106 const llvm::Type *ClassPtrTy; 107 /// ClassExtensionTy - LLVM type for struct objc_class_ext. 108 const llvm::StructType *ClassExtensionTy; 109 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. 110 const llvm::Type *ClassExtensionPtrTy; 111 /// CacheTy - LLVM type for struct objc_cache. 112 const llvm::Type *CacheTy; 113 /// CachePtrTy - LLVM type for struct objc_cache *. 114 const llvm::Type *CachePtrTy; 115 // IvarTy - LLVM type for struct objc_ivar. 116 const llvm::StructType *IvarTy; 117 /// IvarListTy - LLVM type for struct objc_ivar_list. 118 const llvm::Type *IvarListTy; 119 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. 120 const llvm::Type *IvarListPtrTy; 121 // MethodTy - LLVM type for struct objc_method. 122 const llvm::StructType *MethodTy; 123 /// MethodListTy - LLVM type for struct objc_method_list. 124 const llvm::Type *MethodListTy; 125 /// MethodListPtrTy - LLVM type for struct objc_method_list *. 126 const llvm::Type *MethodListPtrTy; 127 128public: 129 ObjCTypesHelper(CodeGen::CodeGenModule &cgm); 130 ~ObjCTypesHelper(); 131 132 llvm::Value *getMessageSendFn(bool IsSuper, const llvm::Type *ReturnTy); 133}; 134 135class CGObjCMac : public CodeGen::CGObjCRuntime { 136private: 137 CodeGen::CodeGenModule &CGM; 138 ObjCTypesHelper ObjCTypes; 139 /// ObjCABI - FIXME: Not sure yet. 140 unsigned ObjCABI; 141 142 /// LazySymbols - Symbols to generate a lazy reference for. See 143 /// DefinedSymbols and FinishModule(). 144 std::set<IdentifierInfo*> LazySymbols; 145 146 /// DefinedSymbols - External symbols which are defined by this 147 /// module. The symbols in this list and LazySymbols are used to add 148 /// special linker symbols which ensure that Objective-C modules are 149 /// linked properly. 150 std::set<IdentifierInfo*> DefinedSymbols; 151 152 /// ClassNames - uniqued class names. 153 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; 154 155 /// MethodVarNames - uniqued method variable names. 156 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; 157 158 /// MethodVarTypes - uniqued method type signatures. We have to use 159 /// a StringMap here because have no other unique reference. 160 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; 161 162 /// MethodDefinitions - map of methods which have been defined in 163 /// this translation unit. 164 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; 165 166 /// PropertyNames - uniqued method variable names. 167 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; 168 169 /// ClassReferences - uniqued class references. 170 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; 171 172 /// SelectorReferences - uniqued selector references. 173 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; 174 175 /// Protocols - Protocols for which an objc_protocol structure has 176 /// been emitted. Forward declarations are handled by creating an 177 /// empty structure whose initializer is filled in when/if defined. 178 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; 179 180 /// DefinedClasses - List of defined classes. 181 std::vector<llvm::GlobalValue*> DefinedClasses; 182 183 /// DefinedCategories - List of defined categories. 184 std::vector<llvm::GlobalValue*> DefinedCategories; 185 186 /// UsedGlobals - List of globals to pack into the llvm.used metadata 187 /// to prevent them from being clobbered. 188 std::vector<llvm::GlobalVariable*> UsedGlobals; 189 190 /// EmitImageInfo - Emit the image info marker used to encode some module 191 /// level information. 192 void EmitImageInfo(); 193 194 /// EmitModuleInfo - Another marker encoding module level 195 /// information. 196 void EmitModuleInfo(); 197 198 /// EmitModuleSymols - Emit module symbols, the list of defined 199 /// classes and categories. The result has type SymtabPtrTy. 200 llvm::Constant *EmitModuleSymbols(); 201 202 /// FinishModule - Write out global data structures at the end of 203 /// processing a translation unit. 204 void FinishModule(); 205 206 /// EmitClassExtension - Generate the class extension structure used 207 /// to store the weak ivar layout and properties. The return value 208 /// has type ClassExtensionPtrTy. 209 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); 210 211 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, 212 /// for the given class. 213 llvm::Value *EmitClassRef(llvm::IRBuilder<> &Builder, 214 const ObjCInterfaceDecl *ID); 215 216 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, 217 const ObjCMessageExpr *E, 218 llvm::Value *Arg0, 219 bool IsSuper); 220 221 /// EmitIvarList - Emit the ivar list for the given 222 /// implementation. If ForClass is true the list of class ivars 223 /// (i.e. metaclass ivars) is emitted, otherwise the list of 224 /// interface ivars will be emitted. The return value has type 225 /// IvarListPtrTy. 226 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, 227 bool ForClass, 228 const llvm::Type *InterfaceTy); 229 230 /// EmitMetaClass - Emit a forward reference to the class structure 231 /// for the metaclass of the given interface. The return value has 232 /// type ClassPtrTy. 233 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); 234 235 /// EmitMetaClass - Emit a class structure for the metaclass of the 236 /// given implementation. The return value has type ClassPtrTy. 237 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, 238 llvm::Constant *Protocols, 239 const llvm::Type *InterfaceTy, 240 const ConstantVector &Methods); 241 242 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); 243 244 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); 245 246 /// EmitMethodList - Emit the method list for the given 247 /// implementation. The return value has type MethodListPtrTy. 248 llvm::Constant *EmitMethodList(const std::string &Name, 249 const char *Section, 250 const ConstantVector &Methods); 251 252 /// EmitMethodDescList - Emit a method description list for a list of 253 /// method declarations. 254 /// - TypeName: The name for the type containing the methods. 255 /// - IsProtocol: True iff these methods are for a protocol. 256 /// - ClassMethds: True iff these are class methods. 257 /// - Required: When true, only "required" methods are 258 /// listed. Similarly, when false only "optional" methods are 259 /// listed. For classes this should always be true. 260 /// - begin, end: The method list to output. 261 /// 262 /// The return value has type MethodDescriptionListPtrTy. 263 llvm::Constant *EmitMethodDescList(const std::string &Name, 264 const char *Section, 265 const ConstantVector &Methods); 266 267 /// EmitPropertyList - Emit the given property list. The return 268 /// value has type PropertyListPtrTy. 269 llvm::Constant *EmitPropertyList(const std::string &Name, 270 const Decl *Container, 271 ObjCPropertyDecl * const *begin, 272 ObjCPropertyDecl * const *end); 273 274 /// EmitProtocolExtension - Generate the protocol extension 275 /// structure used to store optional instance and class methods, and 276 /// protocol properties. The return value has type 277 /// ProtocolExtensionPtrTy. 278 llvm::Constant * 279 EmitProtocolExtension(const ObjCProtocolDecl *PD, 280 const ConstantVector &OptInstanceMethods, 281 const ConstantVector &OptClassMethods); 282 283 /// EmitProtocolList - Generate the list of referenced 284 /// protocols. The return value has type ProtocolListPtrTy. 285 llvm::Constant *EmitProtocolList(const std::string &Name, 286 ObjCProtocolDecl::protocol_iterator begin, 287 ObjCProtocolDecl::protocol_iterator end); 288 289 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, 290 /// for the given selector. 291 llvm::Value *EmitSelector(llvm::IRBuilder<> &Builder, Selector Sel); 292 293 /// GetProtocolRef - Return a reference to the internal protocol 294 /// description, creating an empty one if it has not been 295 /// defined. The return value has type pointer-to ProtocolTy. 296 llvm::GlobalVariable *GetProtocolRef(const ObjCProtocolDecl *PD); 297 298 /// GetClassName - Return a unique constant for the given selector's 299 /// name. The return value has type char *. 300 llvm::Constant *GetClassName(IdentifierInfo *Ident); 301 302 /// GetMethodVarName - Return a unique constant for the given 303 /// selector's name. The return value has type char *. 304 llvm::Constant *GetMethodVarName(Selector Sel); 305 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); 306 llvm::Constant *GetMethodVarName(const std::string &Name); 307 308 /// GetMethodVarType - Return a unique constant for the given 309 /// selector's name. The return value has type char *. 310 311 // FIXME: This is a horrible name. 312 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); 313 llvm::Constant *GetMethodVarType(const std::string &Name); 314 315 /// GetPropertyName - Return a unique constant for the given 316 /// name. The return value has type char *. 317 llvm::Constant *GetPropertyName(IdentifierInfo *Ident); 318 319 // FIXME: This can be dropped once string functions are unified. 320 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, 321 const Decl *Container); 322 323 /// GetNameForMethod - Return a name for the given method. 324 /// \param[out] NameOut - The return value. 325 void GetNameForMethod(const ObjCMethodDecl *OMD, 326 std::string &NameOut); 327 328public: 329 CGObjCMac(CodeGen::CodeGenModule &cgm); 330 virtual llvm::Constant *GenerateConstantString(const std::string &String); 331 332 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, 333 const ObjCMessageExpr *E, 334 llvm::Value *Receiver, 335 bool IsClassMessage); 336 337 virtual CodeGen::RValue 338 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, 339 const ObjCMessageExpr *E, 340 const ObjCInterfaceDecl *Class, 341 llvm::Value *Receiver, 342 bool IsClassMessage); 343 344 virtual llvm::Value *GetClass(llvm::IRBuilder<> &Builder, 345 const ObjCInterfaceDecl *ID); 346 347 virtual llvm::Value *GetSelector(llvm::IRBuilder<> &Builder, Selector Sel); 348 349 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD); 350 351 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); 352 353 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); 354 355 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<> &Builder, 356 const ObjCProtocolDecl *PD); 357 358 virtual void GenerateProtocol(const ObjCProtocolDecl *PD); 359 360 virtual llvm::Function *ModuleInitFunction(); 361}; 362} // end anonymous namespace 363 364/* *** Helper Functions *** */ 365 366/// getConstantGEP() - Help routine to construct simple GEPs. 367static llvm::Constant *getConstantGEP(llvm::Constant *C, 368 unsigned idx0, 369 unsigned idx1) { 370 llvm::Value *Idxs[] = { 371 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0), 372 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1) 373 }; 374 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2); 375} 376 377/* *** CGObjCMac Public Interface *** */ 378 379CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) 380 : CGM(cgm), 381 ObjCTypes(cgm), 382 ObjCABI(1) 383{ 384 // FIXME: How does this get set in GCC? And what does it even mean? 385 if (ObjCTypes.LongTy != CGM.getTypes().ConvertType(CGM.getContext().IntTy)) 386 ObjCABI = 2; 387 388 EmitImageInfo(); 389} 390 391/// GetClass - Return a reference to the class for the given interface 392/// decl. 393llvm::Value *CGObjCMac::GetClass(llvm::IRBuilder<> &Builder, 394 const ObjCInterfaceDecl *ID) { 395 return EmitClassRef(Builder, ID); 396} 397 398/// GetSelector - Return the pointer to the unique'd string for this selector. 399llvm::Value *CGObjCMac::GetSelector(llvm::IRBuilder<> &Builder, Selector Sel) { 400 return EmitSelector(Builder, Sel); 401} 402 403/// Generate a constant CFString object. 404/* 405 struct __builtin_CFString { 406 const int *isa; // point to __CFConstantStringClassReference 407 int flags; 408 const char *str; 409 long length; 410 }; 411*/ 412 413llvm::Constant *CGObjCMac::GenerateConstantString(const std::string &String) { 414 return CGM.GetAddrOfConstantCFString(String); 415} 416 417/// Generates a message send where the super is the receiver. This is 418/// a message send to self with special delivery semantics indicating 419/// which class's method should be called. 420CodeGen::RValue 421CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, 422 const ObjCMessageExpr *E, 423 const ObjCInterfaceDecl *Class, 424 llvm::Value *Receiver, 425 bool IsClassMessage) { 426 // Create and init a super structure; this is a (receiver, class) 427 // pair we will pass to objc_msgSendSuper. 428 llvm::Value *ObjCSuper = 429 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); 430 llvm::Value *ReceiverAsObject = 431 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); 432 CGF.Builder.CreateStore(ReceiverAsObject, 433 CGF.Builder.CreateStructGEP(ObjCSuper, 0)); 434 435 // If this is a class message the metaclass is passed as the target. 436 llvm::Value *Target; 437 if (IsClassMessage) { 438 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); 439 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); 440 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); 441 Target = Super; 442 } else { 443 Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); 444 } 445 CGF.Builder.CreateStore(Target, 446 CGF.Builder.CreateStructGEP(ObjCSuper, 1)); 447 448 return EmitMessageSend(CGF, E, ObjCSuper, true); 449} 450 451/// Generate code for a message send expression. 452CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, 453 const ObjCMessageExpr *E, 454 llvm::Value *Receiver, 455 bool IsClassMessage) { 456 llvm::Value *Arg0 = 457 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy, "tmp"); 458 return EmitMessageSend(CGF, E, Arg0, false); 459} 460 461CodeGen::RValue CGObjCMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF, 462 const ObjCMessageExpr *E, 463 llvm::Value *Arg0, 464 bool IsSuper) { 465 llvm::Value *Args[2]; 466 Args[0] = Arg0; 467 Args[1] = EmitSelector(CGF.Builder, E->getSelector()); 468 469 // FIXME: This is a hack, we are implicitly coordinating with 470 // EmitCallExprExt, which will move the return type to the first 471 // parameter and set the structure return flag. See 472 // getMessageSendFn(). 473 474 475 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(E->getType()); 476 return CGF.EmitCallExprExt(ObjCTypes.getMessageSendFn(IsSuper, ReturnTy), 477 E->getType(), 478 E->arg_begin(), 479 E->arg_end(), 480 Args, 2); 481} 482 483llvm::Value *CGObjCMac::GenerateProtocolRef(llvm::IRBuilder<> &Builder, 484 const ObjCProtocolDecl *PD) { 485 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), 486 ObjCTypes.ExternalProtocolPtrTy); 487} 488 489/* 490 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions 491 struct _objc_protocol { 492 struct _objc_protocol_extension *isa; 493 char *protocol_name; 494 struct _objc_protocol_list *protocol_list; 495 struct _objc__method_prototype_list *instance_methods; 496 struct _objc__method_prototype_list *class_methods 497 }; 498 499 See EmitProtocolExtension(). 500*/ 501void CGObjCMac::GenerateProtocol(const ObjCProtocolDecl *PD) { 502 // FIXME: I don't understand why gcc generates this, or where it is 503 // resolved. Investigate. Its also wasteful to look this up over and 504 // over. 505 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); 506 507 const char *ProtocolName = PD->getName(); 508 509 // Construct method lists. 510 std::vector<llvm::Constant*> InstanceMethods, ClassMethods; 511 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; 512 for (ObjCProtocolDecl::instmeth_iterator i = PD->instmeth_begin(), 513 e = PD->instmeth_end(); i != e; ++i) { 514 ObjCMethodDecl *MD = *i; 515 llvm::Constant *C = GetMethodDescriptionConstant(MD); 516 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { 517 OptInstanceMethods.push_back(C); 518 } else { 519 InstanceMethods.push_back(C); 520 } 521 } 522 523 for (ObjCProtocolDecl::classmeth_iterator i = PD->classmeth_begin(), 524 e = PD->classmeth_end(); i != e; ++i) { 525 ObjCMethodDecl *MD = *i; 526 llvm::Constant *C = GetMethodDescriptionConstant(MD); 527 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { 528 OptClassMethods.push_back(C); 529 } else { 530 ClassMethods.push_back(C); 531 } 532 } 533 534 std::vector<llvm::Constant*> Values(5); 535 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); 536 Values[1] = GetClassName(PD->getIdentifier()); 537 Values[2] = 538 EmitProtocolList(std::string("\01L_OBJC_PROTOCOL_REFS_")+PD->getName(), 539 PD->protocol_begin(), 540 PD->protocol_end()); 541 Values[3] = 542 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_") 543 + PD->getName(), 544 "__OBJC,__cat_inst_meth,regular,no_dead_strip", 545 InstanceMethods); 546 Values[4] = 547 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_CLASS_METHODS_") 548 + PD->getName(), 549 "__OBJC,__cat_cls_meth,regular,no_dead_strip", 550 ClassMethods); 551 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, 552 Values); 553 554 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; 555 if (Entry) { 556 // Already created, just update the initializer 557 Entry->setInitializer(Init); 558 } else { 559 Entry = 560 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, 561 llvm::GlobalValue::InternalLinkage, 562 Init, 563 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName, 564 &CGM.getModule()); 565 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); 566 UsedGlobals.push_back(Entry); 567 // FIXME: Is this necessary? Why only for protocol? 568 Entry->setAlignment(4); 569 } 570} 571 572llvm::GlobalVariable *CGObjCMac::GetProtocolRef(const ObjCProtocolDecl *PD) { 573 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; 574 575 if (!Entry) { 576 std::vector<llvm::Constant*> Values(5); 577 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); 578 Values[1] = GetClassName(PD->getIdentifier()); 579 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); 580 Values[3] = Values[4] = 581 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); 582 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, 583 Values); 584 585 Entry = 586 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false, 587 llvm::GlobalValue::InternalLinkage, 588 Init, 589 std::string("\01L_OBJC_PROTOCOL_")+PD->getName(), 590 &CGM.getModule()); 591 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); 592 UsedGlobals.push_back(Entry); 593 // FIXME: Is this necessary? Why only for protocol? 594 Entry->setAlignment(4); 595 } 596 597 return Entry; 598} 599 600/* 601 struct _objc_protocol_extension { 602 uint32_t size; 603 struct objc_method_description_list *optional_instance_methods; 604 struct objc_method_description_list *optional_class_methods; 605 struct objc_property_list *instance_properties; 606 }; 607*/ 608llvm::Constant * 609CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, 610 const ConstantVector &OptInstanceMethods, 611 const ConstantVector &OptClassMethods) { 612 uint64_t Size = 613 CGM.getTargetData().getABITypeSize(ObjCTypes.ProtocolExtensionTy); 614 std::vector<llvm::Constant*> Values(4); 615 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); 616 Values[1] = 617 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_") 618 + PD->getName(), 619 "__OBJC,__cat_inst_meth,regular,no_dead_strip", 620 OptInstanceMethods); 621 Values[2] = 622 EmitMethodDescList(std::string("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_") 623 + PD->getName(), 624 "__OBJC,__cat_cls_meth,regular,no_dead_strip", 625 OptClassMethods); 626 Values[3] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_PROTO_LIST_") + 627 PD->getName(), 628 0, 629 PD->classprop_begin(), 630 PD->classprop_end()); 631 632 // Return null if no extension bits are used. 633 if (Values[1]->isNullValue() && Values[2]->isNullValue() && 634 Values[3]->isNullValue()) 635 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); 636 637 llvm::Constant *Init = 638 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); 639 llvm::GlobalVariable *GV = 640 new llvm::GlobalVariable(ObjCTypes.ProtocolExtensionTy, false, 641 llvm::GlobalValue::InternalLinkage, 642 Init, 643 (std::string("\01L_OBJC_PROTOCOLEXT_") + 644 PD->getName()), 645 &CGM.getModule()); 646 // No special section, but goes in llvm.used 647 UsedGlobals.push_back(GV); 648 649 return GV; 650} 651 652/* 653 struct objc_protocol_list { 654 struct objc_protocol_list *next; 655 long count; 656 Protocol *list[]; 657 }; 658*/ 659llvm::Constant * 660CGObjCMac::EmitProtocolList(const std::string &Name, 661 ObjCProtocolDecl::protocol_iterator begin, 662 ObjCProtocolDecl::protocol_iterator end) { 663 std::vector<llvm::Constant*> ProtocolRefs; 664 665 for (; begin != end; ++begin) 666 ProtocolRefs.push_back(GetProtocolRef(*begin)); 667 668 // Just return null for empty protocol lists 669 if (ProtocolRefs.empty()) 670 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); 671 672 // This list is null terminated. 673 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); 674 675 std::vector<llvm::Constant*> Values(3); 676 // This field is only used by the runtime. 677 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); 678 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); 679 Values[2] = 680 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, 681 ProtocolRefs.size()), 682 ProtocolRefs); 683 684 llvm::Constant *Init = llvm::ConstantStruct::get(Values); 685 llvm::GlobalVariable *GV = 686 new llvm::GlobalVariable(Init->getType(), false, 687 llvm::GlobalValue::InternalLinkage, 688 Init, 689 Name, 690 &CGM.getModule()); 691 GV->setSection("__OBJC,__cat_cls_meth,regular,no_dead_strip"); 692 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); 693} 694 695/* 696 struct _objc_property { 697 const char * const name; 698 const char * const attributes; 699 }; 700 701 struct _objc_property_list { 702 uint32_t entsize; // sizeof (struct _objc_property) 703 uint32_t prop_count; 704 struct _objc_property[prop_count]; 705 }; 706*/ 707llvm::Constant *CGObjCMac::EmitPropertyList(const std::string &Name, 708 const Decl *Container, 709 ObjCPropertyDecl * const *begin, 710 ObjCPropertyDecl * const *end) { 711 std::vector<llvm::Constant*> Properties, Prop(2); 712 for (; begin != end; ++begin) { 713 const ObjCPropertyDecl *PD = *begin; 714 Prop[0] = GetPropertyName(PD->getIdentifier()); 715 Prop[1] = GetPropertyTypeString(PD, Container); 716 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, 717 Prop)); 718 } 719 720 // Return null for empty list. 721 if (Properties.empty()) 722 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); 723 724 unsigned PropertySize = 725 CGM.getTargetData().getABITypeSize(ObjCTypes.PropertyTy); 726 std::vector<llvm::Constant*> Values(3); 727 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); 728 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); 729 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, 730 Properties.size()); 731 Values[2] = llvm::ConstantArray::get(AT, Properties); 732 llvm::Constant *Init = llvm::ConstantStruct::get(Values); 733 734 llvm::GlobalVariable *GV = 735 new llvm::GlobalVariable(Init->getType(), false, 736 llvm::GlobalValue::InternalLinkage, 737 Init, 738 Name, 739 &CGM.getModule()); 740 // No special section on property lists? 741 UsedGlobals.push_back(GV); 742 return llvm::ConstantExpr::getBitCast(GV, 743 ObjCTypes.PropertyListPtrTy); 744 745} 746 747/* 748 struct objc_method_description_list { 749 int count; 750 struct objc_method_description list[]; 751 }; 752*/ 753llvm::Constant * 754CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { 755 std::vector<llvm::Constant*> Desc(2); 756 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), 757 ObjCTypes.SelectorPtrTy); 758 Desc[1] = GetMethodVarType(MD); 759 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, 760 Desc); 761} 762 763llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name, 764 const char *Section, 765 const ConstantVector &Methods) { 766 // Return null for empty list. 767 if (Methods.empty()) 768 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); 769 770 std::vector<llvm::Constant*> Values(2); 771 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); 772 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, 773 Methods.size()); 774 Values[1] = llvm::ConstantArray::get(AT, Methods); 775 llvm::Constant *Init = llvm::ConstantStruct::get(Values); 776 777 llvm::GlobalVariable *GV = 778 new llvm::GlobalVariable(Init->getType(), false, 779 llvm::GlobalValue::InternalLinkage, 780 Init, Name, &CGM.getModule()); 781 GV->setSection(Section); 782 UsedGlobals.push_back(GV); 783 return llvm::ConstantExpr::getBitCast(GV, 784 ObjCTypes.MethodDescriptionListPtrTy); 785} 786 787/* 788 struct _objc_category { 789 char *category_name; 790 char *class_name; 791 struct _objc_method_list *instance_methods; 792 struct _objc_method_list *class_methods; 793 struct _objc_protocol_list *protocols; 794 uint32_t size; // <rdar://4585769> 795 struct _objc_property_list *instance_properties; 796 }; 797 */ 798void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { 799 unsigned Size = CGM.getTargetData().getABITypeSize(ObjCTypes.CategoryTy); 800 801 // FIXME: This is poor design, the OCD should have a pointer to the 802 // category decl. Additionally, note that Category can be null for 803 // the @implementation w/o an @interface case. Sema should just 804 // create one for us as it does for @implementation so everyone else 805 // can live life under a clear blue sky. 806 const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); 807 const ObjCCategoryDecl *Category = 808 Interface->FindCategoryDeclaration(OCD->getIdentifier()); 809 std::string ExtName(std::string(Interface->getName()) + 810 "_" + 811 OCD->getName()); 812 813 std::vector<llvm::Constant*> InstanceMethods, ClassMethods; 814 for (ObjCCategoryImplDecl::instmeth_iterator i = OCD->instmeth_begin(), 815 e = OCD->instmeth_end(); i != e; ++i) { 816 // Instance methods should always be defined. 817 InstanceMethods.push_back(GetMethodConstant(*i)); 818 } 819 for (ObjCCategoryImplDecl::classmeth_iterator i = OCD->classmeth_begin(), 820 e = OCD->classmeth_end(); i != e; ++i) { 821 // Class methods should always be defined. 822 ClassMethods.push_back(GetMethodConstant(*i)); 823 } 824 825 std::vector<llvm::Constant*> Values(7); 826 Values[0] = GetClassName(OCD->getIdentifier()); 827 Values[1] = GetClassName(Interface->getIdentifier()); 828 Values[2] = 829 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") + 830 ExtName, 831 "__OBJC,__cat_inst_meth,regular,no_dead_strip", 832 InstanceMethods); 833 Values[3] = 834 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName, 835 "__OBJC,__cat_class_meth,regular,no_dead_strip", 836 ClassMethods); 837 if (Category) { 838 Values[4] = 839 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName, 840 Category->protocol_begin(), 841 Category->protocol_end()); 842 } else { 843 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); 844 } 845 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); 846 847 // If there is no category @interface then there can be no properties. 848 if (Category) { 849 Values[6] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + ExtName, 850 OCD, 851 Category->classprop_begin(), 852 Category->classprop_end()); 853 } else { 854 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); 855 } 856 857 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, 858 Values); 859 860 llvm::GlobalVariable *GV = 861 new llvm::GlobalVariable(ObjCTypes.CategoryTy, false, 862 llvm::GlobalValue::InternalLinkage, 863 Init, 864 std::string("\01L_OBJC_CATEGORY_")+ExtName, 865 &CGM.getModule()); 866 GV->setSection("__OBJC,__category,regular,no_dead_strip"); 867 UsedGlobals.push_back(GV); 868 DefinedCategories.push_back(GV); 869} 870 871// FIXME: Get from somewhere? 872enum ClassFlags { 873 eClassFlags_Factory = 0x00001, 874 eClassFlags_Meta = 0x00002, 875 // <rdr://5142207> 876 eClassFlags_HasCXXStructors = 0x02000, 877 eClassFlags_Hidden = 0x20000, 878 eClassFlags_ABI2_Hidden = 0x00010, 879 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> 880}; 881 882// <rdr://5142207&4705298&4843145> 883static bool IsClassHidden(const ObjCInterfaceDecl *ID) { 884 if (const VisibilityAttr *attr = ID->getAttr<VisibilityAttr>()) { 885 // FIXME: Support -fvisibility 886 switch (attr->getVisibility()) { 887 default: 888 assert(0 && "Unknown visibility"); 889 return false; 890 case VisibilityAttr::DefaultVisibility: 891 case VisibilityAttr::ProtectedVisibility: // FIXME: What do we do here? 892 return false; 893 case VisibilityAttr::HiddenVisibility: 894 return true; 895 } 896 } else { 897 return false; // FIXME: Support -fvisibility 898 } 899} 900 901/* 902 struct _objc_class { 903 Class isa; 904 Class super_class; 905 const char *name; 906 long version; 907 long info; 908 long instance_size; 909 struct _objc_ivar_list *ivars; 910 struct _objc_method_list *methods; 911 struct _objc_cache *cache; 912 struct _objc_protocol_list *protocols; 913 // Objective-C 1.0 extensions (<rdr://4585769>) 914 const char *ivar_layout; 915 struct _objc_class_ext *ext; 916 }; 917 918 See EmitClassExtension(); 919 */ 920void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { 921 DefinedSymbols.insert(ID->getIdentifier()); 922 923 const char *ClassName = ID->getName(); 924 // FIXME: Gross 925 ObjCInterfaceDecl *Interface = 926 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); 927 llvm::Constant *Protocols = 928 EmitProtocolList(std::string("\01L_OBJC_CLASS_PROTOCOLS_") + ID->getName(), 929 Interface->protocol_begin(), 930 Interface->protocol_end()); 931 const llvm::Type *InterfaceTy = 932 CGM.getTypes().ConvertType(CGM.getContext().getObjCInterfaceType(Interface)); 933 unsigned Flags = eClassFlags_Factory; 934 unsigned Size = CGM.getTargetData().getABITypeSize(InterfaceTy); 935 936 // FIXME: Set CXX-structors flag. 937 if (IsClassHidden(ID->getClassInterface())) 938 Flags |= eClassFlags_Hidden; 939 940 std::vector<llvm::Constant*> InstanceMethods, ClassMethods; 941 for (ObjCImplementationDecl::instmeth_iterator i = ID->instmeth_begin(), 942 e = ID->instmeth_end(); i != e; ++i) { 943 // Instance methods should always be defined. 944 InstanceMethods.push_back(GetMethodConstant(*i)); 945 } 946 for (ObjCImplementationDecl::classmeth_iterator i = ID->classmeth_begin(), 947 e = ID->classmeth_end(); i != e; ++i) { 948 // Class methods should always be defined. 949 ClassMethods.push_back(GetMethodConstant(*i)); 950 } 951 952 for (ObjCImplementationDecl::propimpl_iterator i = ID->propimpl_begin(), 953 e = ID->propimpl_end(); i != e; ++i) { 954 ObjCPropertyImplDecl *PID = *i; 955 956 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { 957 ObjCPropertyDecl *PD = PID->getPropertyDecl(); 958 959 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) 960 if (llvm::Constant *C = GetMethodConstant(MD)) 961 InstanceMethods.push_back(C); 962 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) 963 if (llvm::Constant *C = GetMethodConstant(MD)) 964 InstanceMethods.push_back(C); 965 } 966 } 967 968 std::vector<llvm::Constant*> Values(12); 969 Values[ 0] = EmitMetaClass(ID, Protocols, InterfaceTy, ClassMethods); 970 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { 971 // Record a reference to the super class. 972 LazySymbols.insert(Super->getIdentifier()); 973 974 Values[ 1] = 975 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), 976 ObjCTypes.ClassPtrTy); 977 } else { 978 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); 979 } 980 Values[ 2] = GetClassName(ID->getIdentifier()); 981 // Version is always 0. 982 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); 983 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); 984 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); 985 Values[ 6] = EmitIvarList(ID, false, InterfaceTy); 986 Values[ 7] = 987 EmitMethodList(std::string("\01L_OBJC_INSTANCE_METHODS_") + ID->getName(), 988 "__OBJC,__inst_meth,regular,no_dead_strip", 989 InstanceMethods); 990 // cache is always NULL. 991 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); 992 Values[ 9] = Protocols; 993 // FIXME: Set ivar_layout 994 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); 995 Values[11] = EmitClassExtension(ID); 996 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, 997 Values); 998 999 llvm::GlobalVariable *GV = 1000 new llvm::GlobalVariable(ObjCTypes.ClassTy, false, 1001 llvm::GlobalValue::InternalLinkage, 1002 Init, 1003 std::string("\01L_OBJC_CLASS_")+ClassName, 1004 &CGM.getModule()); 1005 GV->setSection("__OBJC,__class,regular,no_dead_strip"); 1006 UsedGlobals.push_back(GV); 1007 // FIXME: Why? 1008 GV->setAlignment(32); 1009 DefinedClasses.push_back(GV); 1010} 1011 1012llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, 1013 llvm::Constant *Protocols, 1014 const llvm::Type *InterfaceTy, 1015 const ConstantVector &Methods) { 1016 const char *ClassName = ID->getName(); 1017 unsigned Flags = eClassFlags_Meta; 1018 unsigned Size = CGM.getTargetData().getABITypeSize(ObjCTypes.ClassTy); 1019 1020 if (IsClassHidden(ID->getClassInterface())) 1021 Flags |= eClassFlags_Hidden; 1022 1023 std::vector<llvm::Constant*> Values(12); 1024 // The isa for the metaclass is the root of the hierarchy. 1025 const ObjCInterfaceDecl *Root = ID->getClassInterface(); 1026 while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) 1027 Root = Super; 1028 Values[ 0] = 1029 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), 1030 ObjCTypes.ClassPtrTy); 1031 // The super class for the metaclass is emitted as the name of the 1032 // super class. The runtime fixes this up to point to the 1033 // *metaclass* for the super class. 1034 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { 1035 Values[ 1] = 1036 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), 1037 ObjCTypes.ClassPtrTy); 1038 } else { 1039 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); 1040 } 1041 Values[ 2] = GetClassName(ID->getIdentifier()); 1042 // Version is always 0. 1043 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); 1044 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); 1045 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); 1046 Values[ 6] = EmitIvarList(ID, true, InterfaceTy); 1047 Values[ 7] = 1048 EmitMethodList(std::string("\01L_OBJC_CLASS_METHODS_") + ID->getName(), 1049 "__OBJC,__inst_meth,regular,no_dead_strip", 1050 Methods); 1051 // cache is always NULL. 1052 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); 1053 Values[ 9] = Protocols; 1054 // ivar_layout for metaclass is always NULL. 1055 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); 1056 // The class extension is always unused for metaclasses. 1057 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); 1058 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, 1059 Values); 1060 1061 std::string Name("\01L_OBJC_METACLASS_"); 1062 Name += ClassName; 1063 1064 // Check for a forward reference. 1065 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); 1066 if (GV) { 1067 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && 1068 "Forward metaclass reference has incorrect type."); 1069 GV->setLinkage(llvm::GlobalValue::InternalLinkage); 1070 GV->setInitializer(Init); 1071 } else { 1072 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false, 1073 llvm::GlobalValue::InternalLinkage, 1074 Init, Name, 1075 &CGM.getModule()); 1076 } 1077 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); 1078 UsedGlobals.push_back(GV); 1079 // FIXME: Why? 1080 GV->setAlignment(32); 1081 1082 return GV; 1083} 1084 1085llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { 1086 std::string Name("\01L_OBJC_METACLASS_"); 1087 Name += ID->getName(); 1088 1089 // FIXME: Should we look these up somewhere other than the 1090 // module. Its a bit silly since we only generate these while 1091 // processing an implementation, so exactly one pointer would work 1092 // if know when we entered/exitted an implementation block. 1093 1094 // Check for an existing forward reference. 1095 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name)) { 1096 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && 1097 "Forward metaclass reference has incorrect type."); 1098 return GV; 1099 } else { 1100 // Generate as an external reference to keep a consistent 1101 // module. This will be patched up when we emit the metaclass. 1102 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false, 1103 llvm::GlobalValue::ExternalLinkage, 1104 0, 1105 Name, 1106 &CGM.getModule()); 1107 } 1108} 1109 1110/* 1111 struct objc_class_ext { 1112 uint32_t size; 1113 const char *weak_ivar_layout; 1114 struct _objc_property_list *properties; 1115 }; 1116*/ 1117llvm::Constant * 1118CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { 1119 uint64_t Size = 1120 CGM.getTargetData().getABITypeSize(ObjCTypes.ClassExtensionTy); 1121 1122 std::vector<llvm::Constant*> Values(3); 1123 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); 1124 // FIXME: Output weak_ivar_layout string. 1125 Values[1] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); 1126 Values[2] = EmitPropertyList(std::string("\01L_OBJC_$_PROP_LIST_") + 1127 ID->getName(), 1128 ID, 1129 ID->getClassInterface()->classprop_begin(), 1130 ID->getClassInterface()->classprop_end()); 1131 1132 // Return null if no extension bits are used. 1133 if (Values[1]->isNullValue() && Values[2]->isNullValue()) 1134 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); 1135 1136 llvm::Constant *Init = 1137 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); 1138 llvm::GlobalVariable *GV = 1139 new llvm::GlobalVariable(ObjCTypes.ClassExtensionTy, false, 1140 llvm::GlobalValue::InternalLinkage, 1141 Init, 1142 (std::string("\01L_OBJC_CLASSEXT_") + 1143 ID->getName()), 1144 &CGM.getModule()); 1145 // No special section, but goes in llvm.used 1146 UsedGlobals.push_back(GV); 1147 1148 return GV; 1149} 1150 1151/* 1152 struct objc_ivar { 1153 char *ivar_name; 1154 char *ivar_type; 1155 int ivar_offset; 1156 }; 1157 1158 struct objc_ivar_list { 1159 int ivar_count; 1160 struct objc_ivar list[count]; 1161 }; 1162 */ 1163llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, 1164 bool ForClass, 1165 const llvm::Type *InterfaceTy) { 1166 std::vector<llvm::Constant*> Ivars, Ivar(3); 1167 1168 // When emitting the root class GCC emits ivar entries for the 1169 // actual class structure. It is not clear if we need to follow this 1170 // behavior; for now lets try and get away with not doing it. If so, 1171 // the cleanest solution would be to make up an ObjCInterfaceDecl 1172 // for the class. 1173 if (ForClass) 1174 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); 1175 1176 const llvm::StructLayout *Layout = 1177 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceTy)); 1178 for (ObjCInterfaceDecl::ivar_iterator 1179 i = ID->getClassInterface()->ivar_begin(), 1180 e = ID->getClassInterface()->ivar_end(); i != e; ++i) { 1181 ObjCIvarDecl *V = *i; 1182 unsigned Offset = 1183 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(V)); 1184 std::string TypeStr; 1185 llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes; 1186 Ivar[0] = GetMethodVarName(V->getIdentifier()); 1187 CGM.getContext().getObjCEncodingForType(V->getType(), TypeStr, 1188 EncodingRecordTypes); 1189 Ivar[1] = GetMethodVarType(TypeStr); 1190 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, Offset); 1191 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, 1192 Ivar)); 1193 } 1194 1195 // Return null for empty list. 1196 if (Ivars.empty()) 1197 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); 1198 1199 std::vector<llvm::Constant*> Values(2); 1200 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); 1201 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, 1202 Ivars.size()); 1203 Values[1] = llvm::ConstantArray::get(AT, Ivars); 1204 llvm::Constant *Init = llvm::ConstantStruct::get(Values); 1205 1206 const char *Prefix = (ForClass ? "\01L_OBJC_CLASS_VARIABLES_" : 1207 "\01L_OBJC_INSTANCE_VARIABLES_"); 1208 llvm::GlobalVariable *GV = 1209 new llvm::GlobalVariable(Init->getType(), false, 1210 llvm::GlobalValue::InternalLinkage, 1211 Init, 1212 std::string(Prefix) + ID->getName(), 1213 &CGM.getModule()); 1214 if (ForClass) { 1215 GV->setSection("__OBJC,__cls_vars,regular,no_dead_strip"); 1216 // FIXME: Why is this only here? 1217 GV->setAlignment(32); 1218 } else { 1219 GV->setSection("__OBJC,__instance_vars,regular,no_dead_strip"); 1220 } 1221 UsedGlobals.push_back(GV); 1222 return llvm::ConstantExpr::getBitCast(GV, 1223 ObjCTypes.IvarListPtrTy); 1224} 1225 1226/* 1227 struct objc_method { 1228 SEL method_name; 1229 char *method_types; 1230 void *method; 1231 }; 1232 1233 struct objc_method_list { 1234 struct objc_method_list *obsolete; 1235 int count; 1236 struct objc_method methods_list[count]; 1237 }; 1238*/ 1239 1240/// GetMethodConstant - Return a struct objc_method constant for the 1241/// given method if it has been defined. The result is null if the 1242/// method has not been defined. The return value has type MethodPtrTy. 1243llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { 1244 // FIXME: Use DenseMap::lookup 1245 llvm::Function *Fn = MethodDefinitions[MD]; 1246 if (!Fn) 1247 return 0; 1248 1249 std::vector<llvm::Constant*> Method(3); 1250 Method[0] = 1251 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), 1252 ObjCTypes.SelectorPtrTy); 1253 Method[1] = GetMethodVarType(MD); 1254 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); 1255 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); 1256} 1257 1258llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name, 1259 const char *Section, 1260 const ConstantVector &Methods) { 1261 // Return null for empty list. 1262 if (Methods.empty()) 1263 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); 1264 1265 std::vector<llvm::Constant*> Values(3); 1266 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); 1267 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); 1268 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, 1269 Methods.size()); 1270 Values[2] = llvm::ConstantArray::get(AT, Methods); 1271 llvm::Constant *Init = llvm::ConstantStruct::get(Values); 1272 1273 llvm::GlobalVariable *GV = 1274 new llvm::GlobalVariable(Init->getType(), false, 1275 llvm::GlobalValue::InternalLinkage, 1276 Init, 1277 Name, 1278 &CGM.getModule()); 1279 GV->setSection(Section); 1280 UsedGlobals.push_back(GV); 1281 return llvm::ConstantExpr::getBitCast(GV, 1282 ObjCTypes.MethodListPtrTy); 1283} 1284 1285llvm::Function *CGObjCMac::GenerateMethod(const ObjCMethodDecl *OMD) { 1286 const llvm::Type *ReturnTy = 1287 CGM.getTypes().ConvertReturnType(OMD->getResultType()); 1288 const llvm::Type *SelfTy = 1289 CGM.getTypes().ConvertType(OMD->getSelfDecl()->getType()); 1290 1291 std::vector<const llvm::Type*> ArgTys; 1292 ArgTys.reserve(1 + 2 + OMD->param_size()); 1293 1294 // FIXME: This is not something we should have to be dealing with 1295 // here. 1296 bool useStructRet = 1297 CodeGen::CodeGenFunction::hasAggregateLLVMType(OMD->getResultType()); 1298 if (useStructRet) { 1299 ArgTys.push_back(llvm::PointerType::getUnqual(ReturnTy)); 1300 ReturnTy = llvm::Type::VoidTy; 1301 } 1302 1303 // Implicit arguments 1304 ArgTys.push_back(SelfTy); 1305 ArgTys.push_back(ObjCTypes.SelectorPtrTy); 1306 1307 for (ObjCMethodDecl::param_const_iterator 1308 i = OMD->param_begin(), e = OMD->param_end(); 1309 i != e; ++i) { 1310 const llvm::Type *Ty = CGM.getTypes().ConvertType((*i)->getType()); 1311 if (Ty->isSingleValueType()) { 1312 ArgTys.push_back(Ty); 1313 } else { 1314 ArgTys.push_back(llvm::PointerType::getUnqual(Ty)); 1315 } 1316 } 1317 1318 std::string Name; 1319 GetNameForMethod(OMD, Name); 1320 1321 llvm::Function *Method = 1322 llvm::Function::Create(llvm::FunctionType::get(ReturnTy, 1323 ArgTys, 1324 OMD->isVariadic()), 1325 llvm::GlobalValue::InternalLinkage, 1326 Name, 1327 &CGM.getModule()); 1328 MethodDefinitions.insert(std::make_pair(OMD, Method)); 1329 1330 unsigned Offset = 3; // Return plus self and selector implicit args. 1331 if (useStructRet) { 1332 Method->addParamAttr(1, llvm::ParamAttr::StructRet); 1333 ++Offset; 1334 } 1335 1336 // FIXME: This is horrible, we need to be reusing the machinery in 1337 // CodeGenModule.cpp (SetFunctionAttributes). 1338 for (ObjCMethodDecl::param_const_iterator 1339 i = OMD->param_begin(), e = OMD->param_end(); 1340 i != e; ++i, ++Offset) { 1341 const llvm::Type *Ty = CGM.getTypes().ConvertType((*i)->getType()); 1342 if (!Ty->isSingleValueType()) 1343 Method->addParamAttr(Offset, llvm::ParamAttr::ByVal); 1344 } 1345 1346 return Method; 1347} 1348 1349llvm::Function *CGObjCMac::ModuleInitFunction() { 1350 // Abuse this interface function as a place to finalize. 1351 FinishModule(); 1352 1353 return NULL; 1354} 1355 1356/* *** Private Interface *** */ 1357 1358/// EmitImageInfo - Emit the image info marker used to encode some module 1359/// level information. 1360/// 1361/// See: <rdr://4810609&4810587&4810587> 1362/// struct IMAGE_INFO { 1363/// unsigned version; 1364/// unsigned flags; 1365/// }; 1366enum ImageInfoFlags { 1367 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies 1368 eImageInfo_GarbageCollected = (1 << 1), 1369 eImageInfo_GCOnly = (1 << 2) 1370}; 1371 1372void CGObjCMac::EmitImageInfo() { 1373 unsigned version = 0; // Version is unused? 1374 unsigned flags = 0; 1375 1376 // FIXME: Fix and continue? 1377 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) 1378 flags |= eImageInfo_GarbageCollected; 1379 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) 1380 flags |= eImageInfo_GCOnly; 1381 1382 // Emitted as int[2]; 1383 llvm::Constant *values[2] = { 1384 llvm::ConstantInt::get(llvm::Type::Int32Ty, version), 1385 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags) 1386 }; 1387 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2); 1388 llvm::GlobalVariable *GV = 1389 new llvm::GlobalVariable(AT, true, 1390 llvm::GlobalValue::InternalLinkage, 1391 llvm::ConstantArray::get(AT, values, 2), 1392 "\01L_OBJC_IMAGE_INFO", 1393 &CGM.getModule()); 1394 1395 if (ObjCABI == 1) { 1396 GV->setSection("__OBJC, __image_info,regular"); 1397 } else { 1398 GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); 1399 } 1400 1401 UsedGlobals.push_back(GV); 1402} 1403 1404 1405// struct objc_module { 1406// unsigned long version; 1407// unsigned long size; 1408// const char *name; 1409// Symtab symtab; 1410// }; 1411 1412// FIXME: Get from somewhere 1413static const int ModuleVersion = 7; 1414 1415void CGObjCMac::EmitModuleInfo() { 1416 uint64_t Size = CGM.getTargetData().getABITypeSize(ObjCTypes.ModuleTy); 1417 1418 std::vector<llvm::Constant*> Values(4); 1419 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); 1420 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); 1421 // This used to be the filename, now it is unused. <rdr://4327263> 1422 Values[2] = GetClassName(&CGM.getContext().Idents.get("")); 1423 Values[3] = EmitModuleSymbols(); 1424 1425 llvm::GlobalVariable *GV = 1426 new llvm::GlobalVariable(ObjCTypes.ModuleTy, false, 1427 llvm::GlobalValue::InternalLinkage, 1428 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, 1429 Values), 1430 "\01L_OBJC_MODULES", 1431 &CGM.getModule()); 1432 GV->setSection("__OBJC,__module_info,regular,no_dead_strip"); 1433 UsedGlobals.push_back(GV); 1434} 1435 1436llvm::Constant *CGObjCMac::EmitModuleSymbols() { 1437 unsigned NumClasses = DefinedClasses.size(); 1438 unsigned NumCategories = DefinedCategories.size(); 1439 1440 // Return null if no symbols were defined. 1441 if (!NumClasses && !NumCategories) 1442 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); 1443 1444 std::vector<llvm::Constant*> Values(5); 1445 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); 1446 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); 1447 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); 1448 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); 1449 1450 // The runtime expects exactly the list of defined classes followed 1451 // by the list of defined categories, in a single array. 1452 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); 1453 for (unsigned i=0; i<NumClasses; i++) 1454 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], 1455 ObjCTypes.Int8PtrTy); 1456 for (unsigned i=0; i<NumCategories; i++) 1457 Symbols[NumClasses + i] = 1458 llvm::ConstantExpr::getBitCast(DefinedCategories[i], 1459 ObjCTypes.Int8PtrTy); 1460 1461 Values[4] = 1462 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, 1463 NumClasses + NumCategories), 1464 Symbols); 1465 1466 llvm::Constant *Init = llvm::ConstantStruct::get(Values); 1467 1468 llvm::GlobalVariable *GV = 1469 new llvm::GlobalVariable(Init->getType(), false, 1470 llvm::GlobalValue::InternalLinkage, 1471 Init, 1472 "\01L_OBJC_SYMBOLS", 1473 &CGM.getModule()); 1474 GV->setSection("__OBJC,__symbols,regular,no_dead_strip"); 1475 UsedGlobals.push_back(GV); 1476 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); 1477} 1478 1479llvm::Value *CGObjCMac::EmitClassRef(llvm::IRBuilder<> &Builder, 1480 const ObjCInterfaceDecl *ID) { 1481 LazySymbols.insert(ID->getIdentifier()); 1482 1483 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; 1484 1485 if (!Entry) { 1486 llvm::Constant *Casted = 1487 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), 1488 ObjCTypes.ClassPtrTy); 1489 Entry = 1490 new llvm::GlobalVariable(ObjCTypes.ClassPtrTy, false, 1491 llvm::GlobalValue::InternalLinkage, 1492 Casted, "\01L_OBJC_CLASS_REFERENCES_", 1493 &CGM.getModule()); 1494 Entry->setSection("__OBJC,__cls_refs,literal_pointers,no_dead_strip"); 1495 UsedGlobals.push_back(Entry); 1496 } 1497 1498 return Builder.CreateLoad(Entry, false, "tmp"); 1499} 1500 1501llvm::Value *CGObjCMac::EmitSelector(llvm::IRBuilder<> &Builder, Selector Sel) { 1502 llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; 1503 1504 if (!Entry) { 1505 llvm::Constant *Casted = 1506 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), 1507 ObjCTypes.SelectorPtrTy); 1508 Entry = 1509 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false, 1510 llvm::GlobalValue::InternalLinkage, 1511 Casted, "\01L_OBJC_SELECTOR_REFERENCES_", 1512 &CGM.getModule()); 1513 Entry->setSection("__OBJC,__message_refs,literal_pointers,no_dead_strip"); 1514 UsedGlobals.push_back(Entry); 1515 } 1516 1517 return Builder.CreateLoad(Entry, false, "tmp"); 1518} 1519 1520llvm::Constant *CGObjCMac::GetClassName(IdentifierInfo *Ident) { 1521 llvm::GlobalVariable *&Entry = ClassNames[Ident]; 1522 1523 if (!Entry) { 1524 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName()); 1525 Entry = 1526 new llvm::GlobalVariable(C->getType(), false, 1527 llvm::GlobalValue::InternalLinkage, 1528 C, "\01L_OBJC_CLASS_NAME_", 1529 &CGM.getModule()); 1530 Entry->setSection("__TEXT,__cstring,cstring_literals"); 1531 UsedGlobals.push_back(Entry); 1532 } 1533 1534 return getConstantGEP(Entry, 0, 0); 1535} 1536 1537llvm::Constant *CGObjCMac::GetMethodVarName(Selector Sel) { 1538 llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; 1539 1540 if (!Entry) { 1541 llvm::Constant *C = llvm::ConstantArray::get(Sel.getName()); 1542 Entry = 1543 new llvm::GlobalVariable(C->getType(), false, 1544 llvm::GlobalValue::InternalLinkage, 1545 C, "\01L_OBJC_METH_VAR_NAME_", 1546 &CGM.getModule()); 1547 Entry->setSection("__TEXT,__cstring,cstring_literals"); 1548 UsedGlobals.push_back(Entry); 1549 } 1550 1551 return getConstantGEP(Entry, 0, 0); 1552} 1553 1554// FIXME: Merge into a single cstring creation function. 1555llvm::Constant *CGObjCMac::GetMethodVarName(IdentifierInfo *ID) { 1556 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); 1557} 1558 1559// FIXME: Merge into a single cstring creation function. 1560llvm::Constant *CGObjCMac::GetMethodVarName(const std::string &Name) { 1561 return GetMethodVarName(&CGM.getContext().Idents.get(Name)); 1562} 1563 1564llvm::Constant *CGObjCMac::GetMethodVarType(const std::string &Name) { 1565 llvm::GlobalVariable *&Entry = MethodVarTypes[Name]; 1566 1567 if (!Entry) { 1568 llvm::Constant *C = llvm::ConstantArray::get(Name); 1569 Entry = 1570 new llvm::GlobalVariable(C->getType(), false, 1571 llvm::GlobalValue::InternalLinkage, 1572 C, "\01L_OBJC_METH_VAR_TYPE_", 1573 &CGM.getModule()); 1574 Entry->setSection("__TEXT,__cstring,cstring_literals"); 1575 UsedGlobals.push_back(Entry); 1576 } 1577 1578 return getConstantGEP(Entry, 0, 0); 1579} 1580 1581// FIXME: Merge into a single cstring creation function. 1582llvm::Constant *CGObjCMac::GetMethodVarType(const ObjCMethodDecl *D) { 1583 std::string TypeStr; 1584 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), 1585 TypeStr); 1586 return GetMethodVarType(TypeStr); 1587} 1588 1589// FIXME: Merge into a single cstring creation function. 1590llvm::Constant *CGObjCMac::GetPropertyName(IdentifierInfo *Ident) { 1591 llvm::GlobalVariable *&Entry = PropertyNames[Ident]; 1592 1593 if (!Entry) { 1594 llvm::Constant *C = llvm::ConstantArray::get(Ident->getName()); 1595 Entry = 1596 new llvm::GlobalVariable(C->getType(), false, 1597 llvm::GlobalValue::InternalLinkage, 1598 C, "\01L_OBJC_PROP_NAME_ATTR_", 1599 &CGM.getModule()); 1600 Entry->setSection("__TEXT,__cstring,cstring_literals"); 1601 UsedGlobals.push_back(Entry); 1602 } 1603 1604 return getConstantGEP(Entry, 0, 0); 1605} 1606 1607// FIXME: Merge into a single cstring creation function. 1608// FIXME: This Decl should be more precise. 1609llvm::Constant *CGObjCMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, 1610 const Decl *Container) { 1611 std::string TypeStr; 1612 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); 1613 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); 1614} 1615 1616void CGObjCMac::GetNameForMethod(const ObjCMethodDecl *D, 1617 std::string &NameOut) { 1618 // FIXME: Find the mangling GCC uses. 1619 std::stringstream s; 1620 s << (D->isInstance() ? "-" : "+"); 1621 s << "["; 1622 s << D->getClassInterface()->getName(); 1623 s << " "; 1624 s << D->getSelector().getName(); 1625 s << "]"; 1626 NameOut = s.str(); 1627} 1628 1629void CGObjCMac::FinishModule() { 1630 EmitModuleInfo(); 1631 1632 std::vector<llvm::Constant*> Used; 1633 1634 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(), 1635 e = UsedGlobals.end(); i != e; ++i) { 1636 Used.push_back(llvm::ConstantExpr::getBitCast(*i, ObjCTypes.Int8PtrTy)); 1637 } 1638 1639 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy, Used.size()); 1640 llvm::GlobalValue *GV = 1641 new llvm::GlobalVariable(AT, false, 1642 llvm::GlobalValue::AppendingLinkage, 1643 llvm::ConstantArray::get(AT, Used), 1644 "llvm.used", 1645 &CGM.getModule()); 1646 1647 GV->setSection("llvm.metadata"); 1648 1649 // Add assembler directives to add lazy undefined symbol references 1650 // for classes which are referenced but not defined. This is 1651 // important for correct linker interaction. 1652 1653 // FIXME: Uh, this isn't particularly portable. 1654 std::stringstream s; 1655 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(), 1656 e = LazySymbols.end(); i != e; ++i) { 1657 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n"; 1658 } 1659 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(), 1660 e = DefinedSymbols.end(); i != e; ++i) { 1661 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n" 1662 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n"; 1663 } 1664 CGM.getModule().appendModuleInlineAsm(s.str()); 1665} 1666 1667/* *** */ 1668 1669ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) 1670 : CGM(cgm) 1671{ 1672 CodeGen::CodeGenTypes &Types = CGM.getTypes(); 1673 ASTContext &Ctx = CGM.getContext(); 1674 1675 ShortTy = Types.ConvertType(Ctx.ShortTy); 1676 IntTy = Types.ConvertType(Ctx.IntTy); 1677 LongTy = Types.ConvertType(Ctx.LongTy); 1678 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 1679 1680 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); 1681 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); 1682 1683 // FIXME: It would be nice to unify this with the opaque type, so 1684 // that the IR comes out a bit cleaner. 1685 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); 1686 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); 1687 1688 MethodDescriptionTy = 1689 llvm::StructType::get(SelectorPtrTy, 1690 Int8PtrTy, 1691 NULL); 1692 CGM.getModule().addTypeName("struct._objc_method_description", 1693 MethodDescriptionTy); 1694 1695 MethodDescriptionListTy = 1696 llvm::StructType::get(IntTy, 1697 llvm::ArrayType::get(MethodDescriptionTy, 0), 1698 NULL); 1699 CGM.getModule().addTypeName("struct._objc_method_description_list", 1700 MethodDescriptionListTy); 1701 MethodDescriptionListPtrTy = 1702 llvm::PointerType::getUnqual(MethodDescriptionListTy); 1703 1704 PropertyTy = llvm::StructType::get(Int8PtrTy, 1705 Int8PtrTy, 1706 NULL); 1707 CGM.getModule().addTypeName("struct._objc_property", 1708 PropertyTy); 1709 1710 PropertyListTy = llvm::StructType::get(IntTy, 1711 IntTy, 1712 llvm::ArrayType::get(PropertyTy, 0), 1713 NULL); 1714 CGM.getModule().addTypeName("struct._objc_property_list", 1715 PropertyListTy); 1716 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); 1717 1718 // Protocol description structures 1719 1720 ProtocolExtensionTy = 1721 llvm::StructType::get(Types.ConvertType(Ctx.IntTy), 1722 llvm::PointerType::getUnqual(MethodDescriptionListTy), 1723 llvm::PointerType::getUnqual(MethodDescriptionListTy), 1724 PropertyListPtrTy, 1725 NULL); 1726 CGM.getModule().addTypeName("struct._objc_protocol_extension", 1727 ProtocolExtensionTy); 1728 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); 1729 1730 // Handle recursive construction of Protocl and ProtocolList types 1731 1732 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(); 1733 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(); 1734 1735 T = llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder), 1736 LongTy, 1737 llvm::ArrayType::get(ProtocolTyHolder, 0), 1738 NULL); 1739 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); 1740 1741 T = llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolExtensionTy), 1742 Int8PtrTy, 1743 llvm::PointerType::getUnqual(ProtocolListTyHolder), 1744 MethodDescriptionListPtrTy, 1745 MethodDescriptionListPtrTy, 1746 NULL); 1747 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); 1748 1749 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); 1750 CGM.getModule().addTypeName("struct._objc_protocol_list", 1751 ProtocolListTy); 1752 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); 1753 1754 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); 1755 CGM.getModule().addTypeName("struct.__objc_protocol", ProtocolTy); 1756 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); 1757 1758 // Class description structures 1759 1760 IvarTy = llvm::StructType::get(Int8PtrTy, 1761 Int8PtrTy, 1762 IntTy, 1763 NULL); 1764 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); 1765 1766 IvarListTy = llvm::OpaqueType::get(); 1767 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); 1768 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); 1769 1770 MethodTy = llvm::StructType::get(SelectorPtrTy, 1771 Int8PtrTy, 1772 Int8PtrTy, 1773 NULL); 1774 CGM.getModule().addTypeName("struct._objc_method", MethodTy); 1775 1776 MethodListTy = llvm::OpaqueType::get(); 1777 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); 1778 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); 1779 1780 CacheTy = llvm::OpaqueType::get(); 1781 CGM.getModule().addTypeName("struct._objc_cache", CacheTy); 1782 CachePtrTy = llvm::PointerType::getUnqual(CacheTy); 1783 1784 ClassExtensionTy = 1785 llvm::StructType::get(IntTy, 1786 Int8PtrTy, 1787 PropertyListPtrTy, 1788 NULL); 1789 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); 1790 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); 1791 1792 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(); 1793 1794 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder), 1795 llvm::PointerType::getUnqual(ClassTyHolder), 1796 Int8PtrTy, 1797 LongTy, 1798 LongTy, 1799 LongTy, 1800 IvarListPtrTy, 1801 MethodListPtrTy, 1802 CachePtrTy, 1803 ProtocolListPtrTy, 1804 Int8PtrTy, 1805 ClassExtensionPtrTy, 1806 NULL); 1807 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); 1808 1809 ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); 1810 CGM.getModule().addTypeName("struct._objc_class", ClassTy); 1811 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); 1812 1813 CategoryTy = llvm::StructType::get(Int8PtrTy, 1814 Int8PtrTy, 1815 MethodListPtrTy, 1816 MethodListPtrTy, 1817 ProtocolListPtrTy, 1818 IntTy, 1819 PropertyListPtrTy, 1820 NULL); 1821 CGM.getModule().addTypeName("struct._objc_category", CategoryTy); 1822 1823 SuperTy = 1824 llvm::StructType::get(ObjectPtrTy, 1825 ClassPtrTy, 1826 NULL); 1827 CGM.getModule().addTypeName("struct._objc_super", 1828 SuperTy); 1829 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); 1830 1831 // Global metadata structures 1832 1833 SymtabTy = llvm::StructType::get(LongTy, 1834 SelectorPtrTy, 1835 ShortTy, 1836 ShortTy, 1837 llvm::ArrayType::get(Int8PtrTy, 0), 1838 NULL); 1839 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); 1840 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); 1841 1842 ModuleTy = 1843 llvm::StructType::get(LongTy, 1844 LongTy, 1845 Int8PtrTy, 1846 SymtabPtrTy, 1847 NULL); 1848 CGM.getModule().addTypeName("struct._objc_module", ModuleTy); 1849 1850 // Message send functions 1851 1852 std::vector<const llvm::Type*> Params; 1853 Params.push_back(ObjectPtrTy); 1854 Params.push_back(SelectorPtrTy); 1855 MessageSendFn = llvm::Function::Create(llvm::FunctionType::get(ObjectPtrTy, 1856 Params, 1857 true), 1858 llvm::Function::ExternalLinkage, 1859 "objc_msgSend", 1860 &CGM.getModule()); 1861 1862 Params.clear(); 1863 Params.push_back(Int8PtrTy); 1864 Params.push_back(ObjectPtrTy); 1865 Params.push_back(SelectorPtrTy); 1866 MessageSendStretFn = 1867 llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy, 1868 Params, 1869 true), 1870 llvm::Function::ExternalLinkage, 1871 "objc_msgSend_stret", 1872 &CGM.getModule()); 1873 1874 Params.clear(); 1875 Params.push_back(SuperPtrTy); 1876 Params.push_back(SelectorPtrTy); 1877 MessageSendSuperFn = 1878 llvm::Function::Create(llvm::FunctionType::get(ObjectPtrTy, 1879 Params, 1880 true), 1881 llvm::Function::ExternalLinkage, 1882 "objc_msgSendSuper", 1883 &CGM.getModule()); 1884 1885 Params.clear(); 1886 Params.push_back(Int8PtrTy); 1887 Params.push_back(SuperPtrTy); 1888 Params.push_back(SelectorPtrTy); 1889 MessageSendSuperStretFn = 1890 llvm::Function::Create(llvm::FunctionType::get(llvm::Type::VoidTy, 1891 Params, 1892 true), 1893 llvm::Function::ExternalLinkage, 1894 "objc_msgSendSuper_stret", 1895 &CGM.getModule()); 1896} 1897 1898ObjCTypesHelper::~ObjCTypesHelper() { 1899} 1900 1901llvm::Value *ObjCTypesHelper::getMessageSendFn(bool IsSuper, 1902 const llvm::Type *ReturnTy) { 1903 llvm::Function *F; 1904 llvm::FunctionType *CallFTy; 1905 1906 // FIXME: Should we be caching any of this? 1907 if (!ReturnTy->isSingleValueType()) { 1908 F = IsSuper ? MessageSendSuperStretFn : MessageSendStretFn; 1909 std::vector<const llvm::Type*> Params(3); 1910 Params[0] = llvm::PointerType::getUnqual(ReturnTy); 1911 Params[1] = IsSuper ? SuperPtrTy : ObjectPtrTy; 1912 Params[2] = SelectorPtrTy; 1913 CallFTy = llvm::FunctionType::get(llvm::Type::VoidTy, Params, true); 1914 } else { // XXX floating point? 1915 F = IsSuper ? MessageSendSuperFn : MessageSendFn; 1916 std::vector<const llvm::Type*> Params(2); 1917 Params[0] = IsSuper ? SuperPtrTy : ObjectPtrTy; 1918 Params[1] = SelectorPtrTy; 1919 CallFTy = llvm::FunctionType::get(ReturnTy, Params, true); 1920 } 1921 1922 return llvm::ConstantExpr::getBitCast(F, 1923 llvm::PointerType::getUnqual(CallFTy)); 1924} 1925 1926/* *** */ 1927 1928CodeGen::CGObjCRuntime * 1929CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { 1930 return new CGObjCMac(CGM); 1931} 1932