CGObjCGNU.cpp revision 91a0b51438e5259a79d68d7450c453827beed9e2
1//===------- CGObjCGNU.cpp - Emit LLVM Code from ASTs for a Module --------===// 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 GNU runtime. The 11// class in this file generates structures used by the GNU Objective-C runtime 12// library. These structures are defined in objc/objc.h and objc/objc-api.h in 13// the GNU runtime distribution. 14// 15//===----------------------------------------------------------------------===// 16 17#include "CGObjCRuntime.h" 18#include "CodeGenModule.h" 19#include "CodeGenFunction.h" 20#include "clang/AST/ASTContext.h" 21#include "clang/AST/Decl.h" 22#include "clang/AST/DeclObjC.h" 23#include "llvm/Module.h" 24#include "llvm/ADT/SmallVector.h" 25#include "llvm/ADT/StringMap.h" 26#include "llvm/Support/Compiler.h" 27#include "llvm/Target/TargetData.h" 28#include <map> 29 30 31using namespace clang; 32using namespace CodeGen; 33using llvm::dyn_cast; 34 35// The version of the runtime that this class targets. Must match the version 36// in the runtime. 37static const int RuntimeVersion = 8; 38static const int ProtocolVersion = 2; 39 40namespace { 41class CGObjCGNU : public CodeGen::CGObjCRuntime { 42private: 43 CodeGen::CodeGenModule &CGM; 44 llvm::Module &TheModule; 45 const llvm::PointerType *SelectorTy; 46 const llvm::PointerType *PtrToInt8Ty; 47 const llvm::FunctionType *IMPTy; 48 const llvm::PointerType *IdTy; 49 const llvm::IntegerType *IntTy; 50 const llvm::PointerType *PtrTy; 51 const llvm::IntegerType *LongTy; 52 const llvm::PointerType *PtrToIntTy; 53 std::vector<llvm::Constant*> Classes; 54 std::vector<llvm::Constant*> Categories; 55 std::vector<llvm::Constant*> ConstantStrings; 56 llvm::Function *LoadFunction; 57 llvm::StringMap<llvm::Constant*> ExistingProtocols; 58 typedef std::pair<std::string, std::string> TypedSelector; 59 std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors; 60 llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors; 61 // Some zeros used for GEPs in lots of places. 62 llvm::Constant *Zeros[2]; 63 llvm::Constant *NULLPtr; 64private: 65 llvm::Constant *GenerateIvarList( 66 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames, 67 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes, 68 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets); 69 llvm::Constant *GenerateMethodList(const std::string &ClassName, 70 const std::string &CategoryName, 71 const llvm::SmallVectorImpl<Selector> &MethodSels, 72 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes, 73 bool isClassMethodList); 74 llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName); 75 llvm::Constant *GenerateProtocolList( 76 const llvm::SmallVectorImpl<std::string> &Protocols); 77 llvm::Constant *GenerateClassStructure( 78 llvm::Constant *MetaClass, 79 llvm::Constant *SuperClass, 80 unsigned info, 81 const char *Name, 82 llvm::Constant *Version, 83 llvm::Constant *InstanceSize, 84 llvm::Constant *IVars, 85 llvm::Constant *Methods, 86 llvm::Constant *Protocols); 87 llvm::Constant *GenerateProtocolMethodList( 88 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames, 89 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes); 90 llvm::Constant *MakeConstantString(const std::string &Str, const std::string 91 &Name=""); 92 llvm::Constant *MakeGlobal(const llvm::StructType *Ty, 93 std::vector<llvm::Constant*> &V, const std::string &Name=""); 94 llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty, 95 std::vector<llvm::Constant*> &V, const std::string &Name=""); 96public: 97 CGObjCGNU(CodeGen::CodeGenModule &cgm); 98 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *); 99 virtual CodeGen::RValue 100 GenerateMessageSend(CodeGen::CodeGenFunction &CGF, 101 QualType ResultType, 102 Selector Sel, 103 llvm::Value *Receiver, 104 bool IsClassMessage, 105 const CallArgList &CallArgs); 106 virtual CodeGen::RValue 107 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, 108 QualType ResultType, 109 Selector Sel, 110 const ObjCInterfaceDecl *Class, 111 bool isCategoryImpl, 112 llvm::Value *Receiver, 113 bool IsClassMessage, 114 const CallArgList &CallArgs); 115 virtual llvm::Value *GetClass(CGBuilderTy &Builder, 116 const ObjCInterfaceDecl *OID); 117 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); 118 119 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, 120 const ObjCContainerDecl *CD); 121 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); 122 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); 123 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, 124 const ObjCProtocolDecl *PD); 125 virtual void GenerateProtocol(const ObjCProtocolDecl *PD); 126 virtual llvm::Function *ModuleInitFunction(); 127 virtual llvm::Function *GetPropertyGetFunction(); 128 virtual llvm::Function *GetPropertySetFunction(); 129 virtual llvm::Function *EnumerationMutationFunction(); 130 131 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, 132 const Stmt &S); 133 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, 134 const ObjCAtThrowStmt &S); 135 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, 136 llvm::Value *AddrWeakObj); 137 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, 138 llvm::Value *src, llvm::Value *dst); 139 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, 140 llvm::Value *src, llvm::Value *dest); 141 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, 142 llvm::Value *src, llvm::Value *dest); 143 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, 144 llvm::Value *src, llvm::Value *dest); 145 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, 146 QualType ObjectTy, 147 llvm::Value *BaseValue, 148 const ObjCIvarDecl *Ivar, 149 const FieldDecl *Field, 150 unsigned CVRQualifiers); 151 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, 152 ObjCInterfaceDecl *Interface, 153 const ObjCIvarDecl *Ivar); 154}; 155} // end anonymous namespace 156 157 158 159static std::string SymbolNameForClass(const std::string &ClassName) { 160 return ".objc_class_" + ClassName; 161} 162 163static std::string SymbolNameForMethod(const std::string &ClassName, const 164 std::string &CategoryName, const std::string &MethodName, bool isClassMethod) 165{ 166 return "._objc_method_" + ClassName +"("+CategoryName+")"+ 167 (isClassMethod ? "+" : "-") + MethodName; 168} 169 170CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm) 171 : CGM(cgm), TheModule(CGM.getModule()) { 172 IntTy = cast<llvm::IntegerType>( 173 CGM.getTypes().ConvertType(CGM.getContext().IntTy)); 174 LongTy = cast<llvm::IntegerType>( 175 CGM.getTypes().ConvertType(CGM.getContext().LongTy)); 176 177 Zeros[0] = llvm::ConstantInt::get(LongTy, 0); 178 Zeros[1] = Zeros[0]; 179 NULLPtr = llvm::ConstantPointerNull::get( 180 llvm::PointerType::getUnqual(llvm::Type::Int8Ty)); 181 // C string type. Used in lots of places. 182 PtrToInt8Ty = 183 llvm::PointerType::getUnqual(llvm::Type::Int8Ty); 184 // Get the selector Type. 185 SelectorTy = cast<llvm::PointerType>( 186 CGM.getTypes().ConvertType(CGM.getContext().getObjCSelType())); 187 188 PtrToIntTy = llvm::PointerType::getUnqual(IntTy); 189 PtrTy = PtrToInt8Ty; 190 191 // Object type 192 IdTy = cast<llvm::PointerType>( 193 CGM.getTypes().ConvertType(CGM.getContext().getObjCIdType())); 194 195 // IMP type 196 std::vector<const llvm::Type*> IMPArgs; 197 IMPArgs.push_back(IdTy); 198 IMPArgs.push_back(SelectorTy); 199 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true); 200} 201// This has to perform the lookup every time, since posing and related 202// techniques can modify the name -> class mapping. 203llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder, 204 const ObjCInterfaceDecl *OID) { 205 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString()); 206 ClassName = Builder.CreateStructGEP(ClassName, 0); 207 208 std::vector<const llvm::Type*> Params(1, PtrToInt8Ty); 209 llvm::Constant *ClassLookupFn = 210 CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, 211 Params, 212 true), 213 "objc_lookup_class"); 214 return Builder.CreateCall(ClassLookupFn, ClassName); 215} 216 217/// GetSelector - Return the pointer to the unique'd string for this selector. 218llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) { 219 // FIXME: uniquing on the string is wasteful, unique on Sel instead! 220 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()]; 221 if (US == 0) 222 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy), 223 llvm::GlobalValue::InternalLinkage, 224 ".objc_untyped_selector_alias", 225 NULL, &TheModule); 226 227 return Builder.CreateLoad(US); 228 229} 230 231llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str, 232 const std::string &Name) { 233 llvm::Constant * ConstStr = llvm::ConstantArray::get(Str); 234 ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true, 235 llvm::GlobalValue::InternalLinkage, 236 ConstStr, Name, &TheModule); 237 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2); 238} 239llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty, 240 std::vector<llvm::Constant*> &V, const std::string &Name) { 241 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V); 242 return new llvm::GlobalVariable(Ty, false, 243 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule); 244} 245llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty, 246 std::vector<llvm::Constant*> &V, const std::string &Name) { 247 llvm::Constant *C = llvm::ConstantArray::get(Ty, V); 248 return new llvm::GlobalVariable(Ty, false, 249 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule); 250} 251 252/// Generate an NSConstantString object. 253//TODO: In case there are any crazy people still using the GNU runtime without 254//an OpenStep implementation, this should let them select their own class for 255//constant strings. 256llvm::Constant *CGObjCGNU::GenerateConstantString(const ObjCStringLiteral *SL) { 257 std::string Str(SL->getString()->getStrData(), 258 SL->getString()->getByteLength()); 259 std::vector<llvm::Constant*> Ivars; 260 Ivars.push_back(NULLPtr); 261 Ivars.push_back(MakeConstantString(Str)); 262 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size())); 263 llvm::Constant *ObjCStr = MakeGlobal( 264 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL), 265 Ivars, ".objc_str"); 266 ConstantStrings.push_back( 267 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty)); 268 return ObjCStr; 269} 270 271///Generates a message send where the super is the receiver. This is a message 272///send to self with special delivery semantics indicating which class's method 273///should be called. 274CodeGen::RValue 275CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, 276 QualType ResultType, 277 Selector Sel, 278 const ObjCInterfaceDecl *Class, 279 bool isCategoryImpl, 280 llvm::Value *Receiver, 281 bool IsClassMessage, 282 const CallArgList &CallArgs) { 283 llvm::Value *cmd = GetSelector(CGF.Builder, Sel); 284 285 CallArgList ActualArgs; 286 287 ActualArgs.push_back( 288 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)), 289 CGF.getContext().getObjCIdType())); 290 ActualArgs.push_back(std::make_pair(RValue::get(cmd), 291 CGF.getContext().getObjCSelType())); 292 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); 293 294 CodeGenTypes &Types = CGM.getTypes(); 295 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); 296 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false); 297 298 299 const ObjCInterfaceDecl *SuperClass = Class->getSuperClass(); 300 // TODO: This should be cached, not looked up every time. 301 llvm::Value *ReceiverClass = GetClass(CGF.Builder, SuperClass); 302 303 304 // Construct the structure used to look up the IMP 305 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(), 306 IdTy, NULL); 307 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy); 308 309 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0)); 310 CGF.Builder.CreateStore(ReceiverClass, CGF.Builder.CreateStructGEP(ObjCSuper, 1)); 311 312 // Get the IMP 313 std::vector<const llvm::Type*> Params; 314 Params.push_back(llvm::PointerType::getUnqual(ObjCSuperTy)); 315 Params.push_back(SelectorTy); 316 llvm::Constant *lookupFunction = 317 CGM.CreateRuntimeFunction(llvm::FunctionType::get( 318 llvm::PointerType::getUnqual(impType), Params, true), 319 "objc_msg_lookup_super"); 320 321 llvm::Value *lookupArgs[] = {ObjCSuper, cmd}; 322 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs, 323 lookupArgs+2); 324 325 return CGF.EmitCall(FnInfo, imp, ActualArgs); 326} 327 328/// Generate code for a message send expression. 329CodeGen::RValue 330CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, 331 QualType ResultType, 332 Selector Sel, 333 llvm::Value *Receiver, 334 bool IsClassMessage, 335 const CallArgList &CallArgs) { 336 llvm::Value *cmd = GetSelector(CGF.Builder, Sel); 337 CallArgList ActualArgs; 338 339 ActualArgs.push_back( 340 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)), 341 CGF.getContext().getObjCIdType())); 342 ActualArgs.push_back(std::make_pair(RValue::get(cmd), 343 CGF.getContext().getObjCSelType())); 344 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); 345 346 CodeGenTypes &Types = CGM.getTypes(); 347 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); 348 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false); 349 350 std::vector<const llvm::Type*> Params; 351 Params.push_back(Receiver->getType()); 352 Params.push_back(SelectorTy); 353 llvm::Constant *lookupFunction = 354 CGM.CreateRuntimeFunction(llvm::FunctionType::get( 355 llvm::PointerType::getUnqual(impType), Params, true), 356 "objc_msg_lookup"); 357 358 llvm::Value *imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd); 359 360 return CGF.EmitCall(FnInfo, imp, ActualArgs); 361} 362 363/// Generates a MethodList. Used in construction of a objc_class and 364/// objc_category structures. 365llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName, 366 const std::string &CategoryName, 367 const llvm::SmallVectorImpl<Selector> &MethodSels, 368 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes, 369 bool isClassMethodList) { 370 // Get the method structure type. 371 llvm::StructType *ObjCMethodTy = llvm::StructType::get( 372 PtrToInt8Ty, // Really a selector, but the runtime creates it us. 373 PtrToInt8Ty, // Method types 374 llvm::PointerType::getUnqual(IMPTy), //Method pointer 375 NULL); 376 std::vector<llvm::Constant*> Methods; 377 std::vector<llvm::Constant*> Elements; 378 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) { 379 Elements.clear(); 380 llvm::Constant *C = 381 CGM.GetAddrOfConstantCString(MethodSels[i].getAsString()); 382 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2)); 383 Elements.push_back( 384 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2)); 385 llvm::Constant *Method = 386 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName, 387 MethodSels[i].getAsString(), 388 isClassMethodList)); 389 Method = llvm::ConstantExpr::getBitCast(Method, 390 llvm::PointerType::getUnqual(IMPTy)); 391 Elements.push_back(Method); 392 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements)); 393 } 394 395 // Array of method structures 396 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy, 397 MethodSels.size()); 398 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy, 399 Methods); 400 401 // Structure containing list pointer, array and array count 402 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields; 403 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get(); 404 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy); 405 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy, 406 IntTy, 407 ObjCMethodArrayTy, 408 NULL); 409 // Refine next pointer type to concrete type 410 llvm::cast<llvm::OpaqueType>( 411 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy); 412 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get()); 413 414 Methods.clear(); 415 Methods.push_back(llvm::ConstantPointerNull::get( 416 llvm::PointerType::getUnqual(ObjCMethodListTy))); 417 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 418 MethodTypes.size())); 419 Methods.push_back(MethodArray); 420 421 // Create an instance of the structure 422 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list"); 423} 424 425/// Generates an IvarList. Used in construction of a objc_class. 426llvm::Constant *CGObjCGNU::GenerateIvarList( 427 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames, 428 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes, 429 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) { 430 // Get the method structure type. 431 llvm::StructType *ObjCIvarTy = llvm::StructType::get( 432 PtrToInt8Ty, 433 PtrToInt8Ty, 434 IntTy, 435 NULL); 436 std::vector<llvm::Constant*> Ivars; 437 std::vector<llvm::Constant*> Elements; 438 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) { 439 Elements.clear(); 440 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i], 441 Zeros, 2)); 442 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i], 443 Zeros, 2)); 444 Elements.push_back(IvarOffsets[i]); 445 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements)); 446 } 447 448 // Array of method structures 449 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy, 450 IvarNames.size()); 451 452 453 Elements.clear(); 454 Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size())); 455 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars)); 456 // Structure containing array and array count 457 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy, 458 ObjCIvarArrayTy, 459 NULL); 460 461 // Create an instance of the structure 462 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list"); 463} 464 465/// Generate a class structure 466llvm::Constant *CGObjCGNU::GenerateClassStructure( 467 llvm::Constant *MetaClass, 468 llvm::Constant *SuperClass, 469 unsigned info, 470 const char *Name, 471 llvm::Constant *Version, 472 llvm::Constant *InstanceSize, 473 llvm::Constant *IVars, 474 llvm::Constant *Methods, 475 llvm::Constant *Protocols) { 476 // Set up the class structure 477 // Note: Several of these are char*s when they should be ids. This is 478 // because the runtime performs this translation on load. 479 llvm::StructType *ClassTy = llvm::StructType::get( 480 PtrToInt8Ty, // class_pointer 481 PtrToInt8Ty, // super_class 482 PtrToInt8Ty, // name 483 LongTy, // version 484 LongTy, // info 485 LongTy, // instance_size 486 IVars->getType(), // ivars 487 Methods->getType(), // methods 488 // These are all filled in by the runtime, so we pretend 489 PtrTy, // dtable 490 PtrTy, // subclass_list 491 PtrTy, // sibling_class 492 PtrTy, // protocols 493 PtrTy, // gc_object_type 494 NULL); 495 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0); 496 llvm::Constant *NullP = 497 llvm::ConstantPointerNull::get(PtrTy); 498 // Fill in the structure 499 std::vector<llvm::Constant*> Elements; 500 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty)); 501 Elements.push_back(SuperClass); 502 Elements.push_back(MakeConstantString(Name, ".class_name")); 503 Elements.push_back(Zero); 504 Elements.push_back(llvm::ConstantInt::get(LongTy, info)); 505 Elements.push_back(InstanceSize); 506 Elements.push_back(IVars); 507 Elements.push_back(Methods); 508 Elements.push_back(NullP); 509 Elements.push_back(NullP); 510 Elements.push_back(NullP); 511 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy)); 512 Elements.push_back(NullP); 513 // Create an instance of the structure 514 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name)); 515} 516 517llvm::Constant *CGObjCGNU::GenerateProtocolMethodList( 518 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames, 519 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) { 520 // Get the method structure type. 521 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get( 522 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us. 523 PtrToInt8Ty, 524 NULL); 525 std::vector<llvm::Constant*> Methods; 526 std::vector<llvm::Constant*> Elements; 527 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) { 528 Elements.clear(); 529 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i], 530 Zeros, 2)); 531 Elements.push_back( 532 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2)); 533 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements)); 534 } 535 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy, 536 MethodNames.size()); 537 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods); 538 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get( 539 IntTy, ObjCMethodArrayTy, NULL); 540 Methods.clear(); 541 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size())); 542 Methods.push_back(Array); 543 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list"); 544} 545// Create the protocol list structure used in classes, categories and so on 546llvm::Constant *CGObjCGNU::GenerateProtocolList( 547 const llvm::SmallVectorImpl<std::string> &Protocols) { 548 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty, 549 Protocols.size()); 550 llvm::StructType *ProtocolListTy = llvm::StructType::get( 551 PtrTy, //Should be a recurisve pointer, but it's always NULL here. 552 LongTy,//FIXME: Should be size_t 553 ProtocolArrayTy, 554 NULL); 555 std::vector<llvm::Constant*> Elements; 556 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end(); 557 iter != endIter ; iter++) { 558 llvm::Constant *protocol = ExistingProtocols[*iter]; 559 if (!protocol) 560 protocol = GenerateEmptyProtocol(*iter); 561 llvm::Constant *Ptr = 562 llvm::ConstantExpr::getBitCast(protocol, PtrToInt8Ty); 563 Elements.push_back(Ptr); 564 } 565 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy, 566 Elements); 567 Elements.clear(); 568 Elements.push_back(NULLPtr); 569 Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size())); 570 Elements.push_back(ProtocolArray); 571 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list"); 572} 573 574llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder, 575 const ObjCProtocolDecl *PD) { 576 llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()]; 577 const llvm::Type *T = 578 CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType()); 579 return Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T)); 580} 581 582llvm::Constant *CGObjCGNU::GenerateEmptyProtocol( 583 const std::string &ProtocolName) { 584 llvm::SmallVector<std::string, 0> EmptyStringVector; 585 llvm::SmallVector<llvm::Constant*, 0> EmptyConstantVector; 586 587 llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector); 588 llvm::Constant *InstanceMethodList = 589 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector); 590 llvm::Constant *ClassMethodList = 591 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector); 592 // Protocols are objects containing lists of the methods implemented and 593 // protocols adopted. 594 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy, 595 PtrToInt8Ty, 596 ProtocolList->getType(), 597 InstanceMethodList->getType(), 598 ClassMethodList->getType(), 599 NULL); 600 std::vector<llvm::Constant*> Elements; 601 // The isa pointer must be set to a magic number so the runtime knows it's 602 // the correct layout. 603 Elements.push_back(llvm::ConstantExpr::getIntToPtr( 604 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy)); 605 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name")); 606 Elements.push_back(ProtocolList); 607 Elements.push_back(InstanceMethodList); 608 Elements.push_back(ClassMethodList); 609 return MakeGlobal(ProtocolTy, Elements, ".objc_protocol"); 610} 611 612void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) { 613 ASTContext &Context = CGM.getContext(); 614 std::string ProtocolName = PD->getNameAsString(); 615 llvm::SmallVector<std::string, 16> Protocols; 616 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(), 617 E = PD->protocol_end(); PI != E; ++PI) 618 Protocols.push_back((*PI)->getNameAsString()); 619 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames; 620 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes; 621 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(), 622 E = PD->instmeth_end(); iter != E; iter++) { 623 std::string TypeStr; 624 Context.getObjCEncodingForMethodDecl(*iter, TypeStr); 625 InstanceMethodNames.push_back( 626 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString())); 627 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); 628 } 629 // Collect information about class methods: 630 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames; 631 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes; 632 for (ObjCProtocolDecl::classmeth_iterator iter = PD->classmeth_begin(), 633 endIter = PD->classmeth_end() ; iter != endIter ; iter++) { 634 std::string TypeStr; 635 Context.getObjCEncodingForMethodDecl((*iter),TypeStr); 636 ClassMethodNames.push_back( 637 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString())); 638 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); 639 } 640 641 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols); 642 llvm::Constant *InstanceMethodList = 643 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes); 644 llvm::Constant *ClassMethodList = 645 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes); 646 // Protocols are objects containing lists of the methods implemented and 647 // protocols adopted. 648 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy, 649 PtrToInt8Ty, 650 ProtocolList->getType(), 651 InstanceMethodList->getType(), 652 ClassMethodList->getType(), 653 NULL); 654 std::vector<llvm::Constant*> Elements; 655 // The isa pointer must be set to a magic number so the runtime knows it's 656 // the correct layout. 657 Elements.push_back(llvm::ConstantExpr::getIntToPtr( 658 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy)); 659 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name")); 660 Elements.push_back(ProtocolList); 661 Elements.push_back(InstanceMethodList); 662 Elements.push_back(ClassMethodList); 663 ExistingProtocols[ProtocolName] = 664 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements, 665 ".objc_protocol"), IdTy); 666} 667 668void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) { 669 std::string ClassName = OCD->getClassInterface()->getNameAsString(); 670 std::string CategoryName = OCD->getNameAsString(); 671 // Collect information about instance methods 672 llvm::SmallVector<Selector, 16> InstanceMethodSels; 673 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes; 674 for (ObjCCategoryImplDecl::instmeth_iterator iter = OCD->instmeth_begin(), 675 endIter = OCD->instmeth_end() ; iter != endIter ; iter++) { 676 InstanceMethodSels.push_back((*iter)->getSelector()); 677 std::string TypeStr; 678 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr); 679 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); 680 } 681 682 // Collect information about class methods 683 llvm::SmallVector<Selector, 16> ClassMethodSels; 684 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes; 685 for (ObjCCategoryImplDecl::classmeth_iterator iter = OCD->classmeth_begin(), 686 endIter = OCD->classmeth_end() ; iter != endIter ; iter++) { 687 ClassMethodSels.push_back((*iter)->getSelector()); 688 std::string TypeStr; 689 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr); 690 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); 691 } 692 693 // Collect the names of referenced protocols 694 llvm::SmallVector<std::string, 16> Protocols; 695 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface(); 696 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols(); 697 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(), 698 E = Protos.end(); I != E; ++I) 699 Protocols.push_back((*I)->getNameAsString()); 700 701 std::vector<llvm::Constant*> Elements; 702 Elements.push_back(MakeConstantString(CategoryName)); 703 Elements.push_back(MakeConstantString(ClassName)); 704 // Instance method list 705 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList( 706 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes, 707 false), PtrTy)); 708 // Class method list 709 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList( 710 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true), 711 PtrTy)); 712 // Protocol list 713 Elements.push_back(llvm::ConstantExpr::getBitCast( 714 GenerateProtocolList(Protocols), PtrTy)); 715 Categories.push_back(llvm::ConstantExpr::getBitCast( 716 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy, 717 PtrTy, PtrTy, NULL), Elements), PtrTy)); 718} 719 720void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) { 721 ASTContext &Context = CGM.getContext(); 722 723 // Get the superclass name. 724 const ObjCInterfaceDecl * SuperClassDecl = 725 OID->getClassInterface()->getSuperClass(); 726 std::string SuperClassName; 727 if (SuperClassDecl) 728 SuperClassName = SuperClassDecl->getNameAsString(); 729 730 // Get the class name 731 ObjCInterfaceDecl *ClassDecl = 732 const_cast<ObjCInterfaceDecl *>(OID->getClassInterface()); 733 std::string ClassName = ClassDecl->getNameAsString(); 734 735 // This is required by newer ObjC runtimes. 736 assert(!LateBoundIVars() &&"Late-bound instance variables not yet supported"); 737 738 // Get the size of instances. For runtimes that support late-bound instances 739 // this should probably be something different (size just of instance 740 // varaibles in this class, not superclasses?). 741 const llvm::Type *ObjTy = 742 CGM.getTypes().ConvertType(Context.getObjCInterfaceType(ClassDecl)); 743 int instanceSize = CGM.getTargetData().getTypePaddedSize(ObjTy); 744 745 // Collect information about instance variables. 746 llvm::SmallVector<llvm::Constant*, 16> IvarNames; 747 llvm::SmallVector<llvm::Constant*, 16> IvarTypes; 748 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets; 749 const llvm::StructLayout *Layout = 750 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(ObjTy)); 751 ObjTy = llvm::PointerType::getUnqual(ObjTy); 752 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(), 753 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) { 754 // Store the name 755 IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter) 756 ->getNameAsString())); 757 // Get the type encoding for this ivar 758 std::string TypeStr; 759 Context.getObjCEncodingForType((*iter)->getType(), TypeStr); 760 IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); 761 // Get the offset 762 FieldDecl *Field = ClassDecl->lookupFieldDeclForIvar(Context, (*iter)); 763 int offset = 764 (int)Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); 765 IvarOffsets.push_back( 766 llvm::ConstantInt::get(llvm::Type::Int32Ty, offset)); 767 } 768 769 // Collect information about instance methods 770 llvm::SmallVector<Selector, 16> InstanceMethodSels; 771 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes; 772 for (ObjCImplementationDecl::instmeth_iterator iter = OID->instmeth_begin(), 773 endIter = OID->instmeth_end() ; iter != endIter ; iter++) { 774 InstanceMethodSels.push_back((*iter)->getSelector()); 775 std::string TypeStr; 776 Context.getObjCEncodingForMethodDecl((*iter),TypeStr); 777 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); 778 } 779 780 // Collect information about class methods 781 llvm::SmallVector<Selector, 16> ClassMethodSels; 782 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes; 783 for (ObjCImplementationDecl::classmeth_iterator iter = OID->classmeth_begin(), 784 endIter = OID->classmeth_end() ; iter != endIter ; iter++) { 785 ClassMethodSels.push_back((*iter)->getSelector()); 786 std::string TypeStr; 787 Context.getObjCEncodingForMethodDecl((*iter),TypeStr); 788 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); 789 } 790 // Collect the names of referenced protocols 791 llvm::SmallVector<std::string, 16> Protocols; 792 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols(); 793 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(), 794 E = Protos.end(); I != E; ++I) 795 Protocols.push_back((*I)->getNameAsString()); 796 797 798 799 // Get the superclass pointer. 800 llvm::Constant *SuperClass; 801 if (!SuperClassName.empty()) { 802 SuperClass = MakeConstantString(SuperClassName, ".super_class_name"); 803 } else { 804 SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty); 805 } 806 // Empty vector used to construct empty method lists 807 llvm::SmallVector<llvm::Constant*, 1> empty; 808 // Generate the method and instance variable lists 809 llvm::Constant *MethodList = GenerateMethodList(ClassName, "", 810 InstanceMethodSels, InstanceMethodTypes, false); 811 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "", 812 ClassMethodSels, ClassMethodTypes, true); 813 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes, 814 IvarOffsets); 815 //Generate metaclass for class methods 816 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr, 817 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList( 818 empty, empty, empty), ClassMethodList, NULLPtr); 819 // Generate the class structure 820 llvm::Constant *ClassStruct = 821 GenerateClassStructure(MetaClassStruct, SuperClass, 0x1L, 822 ClassName.c_str(), 0, 823 llvm::ConstantInt::get(LongTy, instanceSize), IvarList, 824 MethodList, GenerateProtocolList(Protocols)); 825 // Add class structure to list to be added to the symtab later 826 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty); 827 Classes.push_back(ClassStruct); 828} 829 830llvm::Function *CGObjCGNU::ModuleInitFunction() { 831 // Only emit an ObjC load function if no Objective-C stuff has been called 832 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() && 833 ExistingProtocols.empty() && TypedSelectors.empty() && 834 UntypedSelectors.empty()) 835 return NULL; 836 837 const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>( 838 SelectorTy->getElementType()); 839 const llvm::Type *SelStructPtrTy = SelectorTy; 840 bool isSelOpaque = false; 841 if (SelStructTy == 0) { 842 SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, NULL); 843 SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy); 844 isSelOpaque = true; 845 } 846 847 // Name the ObjC types to make the IR a bit easier to read 848 TheModule.addTypeName(".objc_selector", SelStructPtrTy); 849 TheModule.addTypeName(".objc_id", IdTy); 850 TheModule.addTypeName(".objc_imp", IMPTy); 851 852 std::vector<llvm::Constant*> Elements; 853 // Generate statics list: 854 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty, 855 ConstantStrings.size() + 1); 856 ConstantStrings.push_back(NULLPtr); 857 Elements.push_back(MakeConstantString("NSConstantString", 858 ".objc_static_class_name")); 859 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings)); 860 llvm::StructType *StaticsListTy = 861 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL); 862 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy); 863 llvm::Constant *Statics = 864 MakeGlobal(StaticsListTy, Elements, ".objc_statics"); 865 llvm::ArrayType *StaticsListArrayTy = 866 llvm::ArrayType::get(StaticsListPtrTy, 2); 867 Elements.clear(); 868 Elements.push_back(Statics); 869 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy)); 870 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr"); 871 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy); 872 // Array of classes, categories, and constant objects 873 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty, 874 Classes.size() + Categories.size() + 2); 875 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy, 876 llvm::Type::Int16Ty, 877 llvm::Type::Int16Ty, 878 ClassListTy, NULL); 879 880 Elements.clear(); 881 // Pointer to an array of selectors used in this module. 882 std::vector<llvm::Constant*> Selectors; 883 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator 884 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end(); 885 iter != iterEnd ; ++iter) { 886 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name")); 887 Elements.push_back(MakeConstantString(iter->first.second, 888 ".objc_sel_types")); 889 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements)); 890 Elements.clear(); 891 } 892 for (llvm::StringMap<llvm::GlobalAlias*>::iterator 893 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end(); 894 iter != iterEnd; ++iter) { 895 Elements.push_back( 896 MakeConstantString(iter->getKeyData(), ".objc_sel_name")); 897 Elements.push_back(NULLPtr); 898 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements)); 899 Elements.clear(); 900 } 901 Elements.push_back(NULLPtr); 902 Elements.push_back(NULLPtr); 903 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements)); 904 Elements.clear(); 905 // Number of static selectors 906 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() )); 907 llvm::Constant *SelectorList = MakeGlobal( 908 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors, 909 ".objc_selector_list"); 910 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList, 911 SelStructPtrTy)); 912 913 // Now that all of the static selectors exist, create pointers to them. 914 int index = 0; 915 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator 916 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end(); 917 iter != iterEnd; ++iter) { 918 llvm::Constant *Idxs[] = {Zeros[0], 919 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]}; 920 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy, 921 true, llvm::GlobalValue::InternalLinkage, 922 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2), 923 ".objc_sel_ptr", &TheModule); 924 // If selectors are defined as an opaque type, cast the pointer to this 925 // type. 926 if (isSelOpaque) { 927 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr, 928 llvm::PointerType::getUnqual(SelectorTy)); 929 } 930 (*iter).second->setAliasee(SelPtr); 931 } 932 for (llvm::StringMap<llvm::GlobalAlias*>::iterator 933 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end(); 934 iter != iterEnd; iter++) { 935 llvm::Constant *Idxs[] = {Zeros[0], 936 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]}; 937 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy, true, 938 llvm::GlobalValue::InternalLinkage, 939 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2), 940 ".objc_sel_ptr", &TheModule); 941 // If selectors are defined as an opaque type, cast the pointer to this 942 // type. 943 if (isSelOpaque) { 944 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr, 945 llvm::PointerType::getUnqual(SelectorTy)); 946 } 947 (*iter).second->setAliasee(SelPtr); 948 } 949 // Number of classes defined. 950 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty, 951 Classes.size())); 952 // Number of categories defined 953 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty, 954 Categories.size())); 955 // Create an array of classes, then categories, then static object instances 956 Classes.insert(Classes.end(), Categories.begin(), Categories.end()); 957 // NULL-terminated list of static object instances (mainly constant strings) 958 Classes.push_back(Statics); 959 Classes.push_back(NULLPtr); 960 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes); 961 Elements.push_back(ClassList); 962 // Construct the symbol table 963 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements); 964 965 // The symbol table is contained in a module which has some version-checking 966 // constants 967 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy, 968 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL); 969 Elements.clear(); 970 // Runtime version used for compatibility checking. 971 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion)); 972 // sizeof(ModuleTy) 973 llvm::TargetData td = llvm::TargetData::TargetData(&TheModule); 974 Elements.push_back(llvm::ConstantInt::get(LongTy, td.getTypeSizeInBits(ModuleTy)/8)); 975 //FIXME: Should be the path to the file where this module was declared 976 Elements.push_back(NULLPtr); 977 Elements.push_back(SymTab); 978 llvm::Value *Module = MakeGlobal(ModuleTy, Elements); 979 980 // Create the load function calling the runtime entry point with the module 981 // structure 982 std::vector<const llvm::Type*> VoidArgs; 983 llvm::Function * LoadFunction = llvm::Function::Create( 984 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false), 985 llvm::GlobalValue::InternalLinkage, ".objc_load_function", 986 &TheModule); 987 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction); 988 CGBuilderTy Builder; 989 Builder.SetInsertPoint(EntryBB); 990 991 std::vector<const llvm::Type*> Params(1, 992 llvm::PointerType::getUnqual(ModuleTy)); 993 llvm::Value *Register = CGM.CreateRuntimeFunction(llvm::FunctionType::get( 994 llvm::Type::VoidTy, Params, true), "__objc_exec_class"); 995 Builder.CreateCall(Register, Module); 996 Builder.CreateRetVoid(); 997 return LoadFunction; 998} 999 1000llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD, 1001 const ObjCContainerDecl *CD) { 1002 const ObjCCategoryImplDecl *OCD = 1003 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext()); 1004 std::string CategoryName = OCD ? OCD->getNameAsString() : ""; 1005 std::string ClassName = OMD->getClassInterface()->getNameAsString(); 1006 std::string MethodName = OMD->getSelector().getAsString(); 1007 bool isClassMethod = !OMD->isInstanceMethod(); 1008 1009 CodeGenTypes &Types = CGM.getTypes(); 1010 const llvm::FunctionType *MethodTy = 1011 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); 1012 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName, 1013 MethodName, isClassMethod); 1014 1015 llvm::Function *Method = llvm::Function::Create(MethodTy, 1016 llvm::GlobalValue::InternalLinkage, 1017 FunctionName, 1018 &TheModule); 1019 return Method; 1020} 1021 1022llvm::Function *CGObjCGNU::GetPropertyGetFunction() { 1023 return 0; 1024} 1025 1026llvm::Function *CGObjCGNU::GetPropertySetFunction() { 1027 return 0; 1028} 1029 1030llvm::Function *CGObjCGNU::EnumerationMutationFunction() { 1031 std::vector<const llvm::Type*> Params(1, IdTy); 1032 return cast<llvm::Function>(CGM.CreateRuntimeFunction( 1033 llvm::FunctionType::get(llvm::Type::VoidTy, Params, true), 1034 "objc_enumerationMutation")); 1035} 1036 1037void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, 1038 const Stmt &S) { 1039 CGF.ErrorUnsupported(&S, "@try/@synchronized statement"); 1040} 1041 1042void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, 1043 const ObjCAtThrowStmt &S) { 1044 CGF.ErrorUnsupported(&S, "@throw statement"); 1045} 1046 1047llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, 1048 llvm::Value *AddrWeakObj) 1049{ 1050 return 0; 1051} 1052 1053void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, 1054 llvm::Value *src, llvm::Value *dst) 1055{ 1056 return; 1057} 1058 1059void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, 1060 llvm::Value *src, llvm::Value *dst) 1061{ 1062 return; 1063} 1064 1065void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, 1066 llvm::Value *src, llvm::Value *dst) 1067{ 1068 return; 1069} 1070 1071void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, 1072 llvm::Value *src, llvm::Value *dst) 1073{ 1074 return; 1075} 1076 1077LValue CGObjCGNU::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, 1078 QualType ObjectTy, 1079 llvm::Value *BaseValue, 1080 const ObjCIvarDecl *Ivar, 1081 const FieldDecl *Field, 1082 unsigned CVRQualifiers) { 1083 if (Ivar->isBitField()) 1084 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field), 1085 CVRQualifiers); 1086 // TODO: Add a special case for isa (index 0) 1087 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field); 1088 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp"); 1089 LValue LV = LValue::MakeAddr(V, 1090 Ivar->getType().getCVRQualifiers()|CVRQualifiers); 1091 LValue::SetObjCIvar(LV, true); 1092 return LV; 1093} 1094 1095llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, 1096 ObjCInterfaceDecl *Interface, 1097 const ObjCIvarDecl *Ivar) { 1098 const llvm::Type *InterfaceLTy = 1099 CGM.getTypes().ConvertType( 1100 CGM.getContext().getObjCInterfaceType(Interface)); 1101 const llvm::StructLayout *Layout = 1102 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceLTy)); 1103 FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar); 1104 uint64_t Offset = 1105 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); 1106 1107 return llvm::ConstantInt::get( 1108 CGM.getTypes().ConvertType(CGM.getContext().LongTy), 1109 Offset); 1110} 1111 1112CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){ 1113 return new CGObjCGNU(CGM); 1114} 1115