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