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" |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 20 | #include "CGException.h" |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 21 | |
Chris Lattner | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 22 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 23 | #include "clang/AST/Decl.h" |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 25 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | 16f0049 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 26 | #include "clang/AST/StmtObjC.h" |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 27 | |
| 28 | #include "llvm/Intrinsics.h" |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 29 | #include "llvm/Module.h" |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 30 | #include "llvm/LLVMContext.h" |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallVector.h" |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/StringMap.h" |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Compiler.h" |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 35 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 36 | #include <map> |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 37 | |
| 38 | |
Chris Lattner | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 39 | using namespace clang; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 40 | using namespace CodeGen; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 41 | using llvm::dyn_cast; |
| 42 | |
| 43 | // The version of the runtime that this class targets. Must match the version |
| 44 | // in the runtime. |
| 45 | static const int RuntimeVersion = 8; |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 46 | static const int NonFragileRuntimeVersion = 9; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 47 | static const int ProtocolVersion = 2; |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 48 | static const int NonFragileProtocolVersion = 3; |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 50 | namespace { |
Chris Lattner | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 51 | class CGObjCGNU : public CodeGen::CGObjCRuntime { |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 52 | private: |
Chris Lattner | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 53 | CodeGen::CodeGenModule &CGM; |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 54 | llvm::Module &TheModule; |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 55 | const llvm::PointerType *SelectorTy; |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 56 | const llvm::IntegerType *Int8Ty; |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 57 | const llvm::PointerType *PtrToInt8Ty; |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 58 | const llvm::FunctionType *IMPTy; |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 59 | const llvm::PointerType *IdTy; |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 60 | const llvm::PointerType *PtrToIdTy; |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 61 | CanQualType ASTIdTy; |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 62 | const llvm::IntegerType *IntTy; |
| 63 | const llvm::PointerType *PtrTy; |
| 64 | const llvm::IntegerType *LongTy; |
| 65 | const llvm::PointerType *PtrToIntTy; |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 66 | llvm::GlobalAlias *ClassPtrAlias; |
| 67 | llvm::GlobalAlias *MetaClassPtrAlias; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 68 | std::vector<llvm::Constant*> Classes; |
| 69 | std::vector<llvm::Constant*> Categories; |
| 70 | std::vector<llvm::Constant*> ConstantStrings; |
David Chisnall | 48272a0 | 2010-01-27 12:49:23 +0000 | [diff] [blame] | 71 | llvm::StringMap<llvm::Constant*> ObjCStrings; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 72 | llvm::Function *LoadFunction; |
| 73 | llvm::StringMap<llvm::Constant*> ExistingProtocols; |
| 74 | typedef std::pair<std::string, std::string> TypedSelector; |
| 75 | std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors; |
| 76 | llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors; |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 77 | // Selectors that we don't emit in GC mode |
| 78 | Selector RetainSel, ReleaseSel, AutoreleaseSel; |
| 79 | // Functions used for GC. |
| 80 | llvm::Constant *IvarAssignFn, *StrongCastAssignFn, *MemMoveFn, *WeakReadFn, |
| 81 | *WeakAssignFn, *GlobalAssignFn; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 82 | // Some zeros used for GEPs in lots of places. |
| 83 | llvm::Constant *Zeros[2]; |
| 84 | llvm::Constant *NULLPtr; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 85 | llvm::LLVMContext &VMContext; |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 86 | /// Metadata kind used to tie method lookups to message sends. |
| 87 | unsigned msgSendMDKind; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 88 | private: |
| 89 | llvm::Constant *GenerateIvarList( |
| 90 | const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames, |
| 91 | const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes, |
| 92 | const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets); |
| 93 | llvm::Constant *GenerateMethodList(const std::string &ClassName, |
| 94 | const std::string &CategoryName, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | const llvm::SmallVectorImpl<Selector> &MethodSels, |
| 96 | const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 97 | bool isClassMethodList); |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 98 | llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 99 | llvm::Constant *GeneratePropertyList(const ObjCImplementationDecl *OID, |
| 100 | llvm::SmallVectorImpl<Selector> &InstanceMethodSels, |
| 101 | llvm::SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 102 | llvm::Constant *GenerateProtocolList( |
| 103 | const llvm::SmallVectorImpl<std::string> &Protocols); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 104 | // To ensure that all protocols are seen by the runtime, we add a category on |
| 105 | // a class defined in the runtime, declaring no methods, but adopting the |
| 106 | // protocols. |
| 107 | void GenerateProtocolHolderCategory(void); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 108 | llvm::Constant *GenerateClassStructure( |
| 109 | llvm::Constant *MetaClass, |
| 110 | llvm::Constant *SuperClass, |
| 111 | unsigned info, |
Chris Lattner | d002cc6 | 2008-06-26 04:47:04 +0000 | [diff] [blame] | 112 | const char *Name, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 113 | llvm::Constant *Version, |
| 114 | llvm::Constant *InstanceSize, |
| 115 | llvm::Constant *IVars, |
| 116 | llvm::Constant *Methods, |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 117 | llvm::Constant *Protocols, |
| 118 | llvm::Constant *IvarOffsets, |
David Chisnall | 8c757f9 | 2010-04-28 14:29:56 +0000 | [diff] [blame] | 119 | llvm::Constant *Properties, |
| 120 | bool isMeta=false); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 121 | llvm::Constant *GenerateProtocolMethodList( |
| 122 | const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames, |
| 123 | const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes); |
| 124 | llvm::Constant *MakeConstantString(const std::string &Str, const std::string |
| 125 | &Name=""); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 126 | llvm::Constant *ExportUniqueString(const std::string &Str, const std::string |
| 127 | prefix); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 128 | llvm::Constant *MakeGlobal(const llvm::StructType *Ty, |
Benjamin Kramer | 74a8bbf | 2010-02-09 19:31:24 +0000 | [diff] [blame] | 129 | std::vector<llvm::Constant*> &V, llvm::StringRef Name="", |
David Chisnall | 41d63ed | 2010-01-08 00:14:31 +0000 | [diff] [blame] | 130 | llvm::GlobalValue::LinkageTypes linkage=llvm::GlobalValue::InternalLinkage); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 131 | llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty, |
Benjamin Kramer | 74a8bbf | 2010-02-09 19:31:24 +0000 | [diff] [blame] | 132 | std::vector<llvm::Constant*> &V, llvm::StringRef Name="", |
David Chisnall | 41d63ed | 2010-01-08 00:14:31 +0000 | [diff] [blame] | 133 | llvm::GlobalValue::LinkageTypes linkage=llvm::GlobalValue::InternalLinkage); |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 134 | llvm::GlobalVariable *ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID, |
| 135 | const ObjCIvarDecl *Ivar); |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 136 | void EmitClassRef(const std::string &className); |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 137 | llvm::Value* EnforceType(CGBuilderTy B, llvm::Value *V, const llvm::Type *Ty){ |
| 138 | if (V->getType() == Ty) return V; |
| 139 | return B.CreateBitCast(V, Ty); |
| 140 | } |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 141 | public: |
Chris Lattner | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 142 | CGObjCGNU(CodeGen::CodeGenModule &cgm); |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 143 | virtual llvm::Constant *GenerateConstantString(const StringLiteral *); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | virtual CodeGen::RValue |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 145 | GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 146 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 147 | QualType ResultType, |
| 148 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 149 | llvm::Value *Receiver, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 150 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 151 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 152 | const ObjCMethodDecl *Method); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 153 | virtual CodeGen::RValue |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 154 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 155 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 156 | QualType ResultType, |
| 157 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 158 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 159 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 160 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 161 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 162 | const CallArgList &CallArgs, |
| 163 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 164 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 165 | const ObjCInterfaceDecl *OID); |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 166 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 167 | bool lval = false); |
Daniel Dunbar | 6d5a1c2 | 2010-02-03 20:11:42 +0000 | [diff] [blame] | 168 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl |
| 169 | *Method); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 170 | virtual llvm::Constant *GetEHType(QualType T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 171 | |
| 172 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 173 | const ObjCContainerDecl *CD); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 174 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
| 175 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 176 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 177 | const ObjCProtocolDecl *PD); |
| 178 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 179 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 180 | virtual llvm::Function *GetPropertyGetFunction(); |
| 181 | virtual llvm::Function *GetPropertySetFunction(); |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 182 | virtual llvm::Function *GetCopyStructFunction(); |
Daniel Dunbar | 309a436 | 2009-07-24 07:40:24 +0000 | [diff] [blame] | 183 | virtual llvm::Constant *EnumerationMutationFunction(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 184 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 185 | virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 186 | const ObjCAtTryStmt &S); |
| 187 | virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 188 | const ObjCAtSynchronizedStmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 189 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 190 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 191 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 192 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 193 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 194 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 195 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 196 | llvm::Value *src, llvm::Value *dest, |
| 197 | bool threadlocal=false); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 198 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 199 | llvm::Value *src, llvm::Value *dest, |
| 200 | llvm::Value *ivarOffset); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 201 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 202 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 203 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | llvm::Value *DestPtr, |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 205 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 206 | llvm::Value *Size); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 207 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 208 | QualType ObjectTy, |
| 209 | llvm::Value *BaseValue, |
| 210 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 211 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 212 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 213 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 214 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | 89ecd41 | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 215 | virtual llvm::Constant *GCBlockLayout(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | c5904b4 | 2010-09-11 01:27:29 +0000 | [diff] [blame] | 216 | const llvm::SmallVectorImpl<const Expr *> &) { |
Fariborz Jahanian | 89ecd41 | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 217 | return NULLPtr; |
| 218 | } |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 219 | }; |
| 220 | } // end anonymous namespace |
| 221 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 222 | |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 223 | /// Emits a reference to a dummy variable which is emitted with each class. |
| 224 | /// This ensures that a linker error will be generated when trying to link |
| 225 | /// together modules where a referenced class is not defined. |
Mike Stump | bb1c860 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 226 | void CGObjCGNU::EmitClassRef(const std::string &className) { |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 227 | std::string symbolRef = "__objc_class_ref_" + className; |
| 228 | // Don't emit two copies of the same symbol |
Mike Stump | bb1c860 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 229 | if (TheModule.getGlobalVariable(symbolRef)) |
| 230 | return; |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 231 | std::string symbolName = "__objc_class_name_" + className; |
| 232 | llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(symbolName); |
| 233 | if (!ClassSymbol) { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 234 | ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false, |
| 235 | llvm::GlobalValue::ExternalLinkage, 0, symbolName); |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 236 | } |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 237 | new llvm::GlobalVariable(TheModule, ClassSymbol->getType(), true, |
Chris Lattner | f35271b | 2009-08-05 05:25:18 +0000 | [diff] [blame] | 238 | llvm::GlobalValue::WeakAnyLinkage, ClassSymbol, symbolRef); |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 239 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 240 | |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 241 | static std::string SymbolNameForMethod(const std::string &ClassName, const |
| 242 | std::string &CategoryName, const std::string &MethodName, bool isClassMethod) |
| 243 | { |
David Chisnall | d346736 | 2010-01-14 14:08:19 +0000 | [diff] [blame] | 244 | std::string MethodNameColonStripped = MethodName; |
| 245 | std::replace(MethodNameColonStripped.begin(), MethodNameColonStripped.end(), |
| 246 | ':', '_'); |
| 247 | return std::string(isClassMethod ? "_c_" : "_i_") + ClassName + "_" + |
| 248 | CategoryName + "_" + MethodNameColonStripped; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 249 | } |
David Chisnall | 87935a8 | 2010-05-08 20:58:05 +0000 | [diff] [blame] | 250 | static std::string MangleSelectorTypes(const std::string &TypeString) { |
| 251 | std::string Mangled = TypeString; |
David Chisnall | 22b8827 | 2010-05-09 01:01:43 +0000 | [diff] [blame] | 252 | // Simple mangling to avoid breaking when we mix JIT / static code. |
| 253 | // Not part of the ABI, subject to change without notice. |
David Chisnall | 87935a8 | 2010-05-08 20:58:05 +0000 | [diff] [blame] | 254 | std::replace(Mangled.begin(), Mangled.end(), '@', '_'); |
David Chisnall | 22b8827 | 2010-05-09 01:01:43 +0000 | [diff] [blame] | 255 | std::replace(Mangled.begin(), Mangled.end(), ':', 'J'); |
| 256 | std::replace(Mangled.begin(), Mangled.end(), '*', 'e'); |
| 257 | std::replace(Mangled.begin(), Mangled.end(), '#', 'E'); |
| 258 | std::replace(Mangled.begin(), Mangled.end(), ':', 'j'); |
| 259 | std::replace(Mangled.begin(), Mangled.end(), '(', 'g'); |
| 260 | std::replace(Mangled.begin(), Mangled.end(), ')', 'G'); |
| 261 | std::replace(Mangled.begin(), Mangled.end(), '[', 'h'); |
| 262 | std::replace(Mangled.begin(), Mangled.end(), ']', 'H'); |
David Chisnall | 87935a8 | 2010-05-08 20:58:05 +0000 | [diff] [blame] | 263 | return Mangled; |
| 264 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 265 | |
Chris Lattner | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 266 | CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm) |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 267 | : CGM(cgm), TheModule(CGM.getModule()), ClassPtrAlias(0), |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 268 | MetaClassPtrAlias(0), VMContext(cgm.getLLVMContext()) { |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 269 | |
| 270 | msgSendMDKind = VMContext.getMDKindID("GNUObjCMessageSend"); |
| 271 | |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 272 | IntTy = cast<llvm::IntegerType>( |
| 273 | CGM.getTypes().ConvertType(CGM.getContext().IntTy)); |
| 274 | LongTy = cast<llvm::IntegerType>( |
| 275 | CGM.getTypes().ConvertType(CGM.getContext().LongTy)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 276 | |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 277 | Int8Ty = llvm::Type::getInt8Ty(VMContext); |
| 278 | // C string type. Used in lots of places. |
| 279 | PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty); |
| 280 | |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 281 | Zeros[0] = llvm::ConstantInt::get(LongTy, 0); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 282 | Zeros[1] = Zeros[0]; |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 283 | NULLPtr = llvm::ConstantPointerNull::get(PtrToInt8Ty); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 284 | // Get the selector Type. |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 285 | QualType selTy = CGM.getContext().getObjCSelType(); |
| 286 | if (QualType() == selTy) { |
| 287 | SelectorTy = PtrToInt8Ty; |
| 288 | } else { |
| 289 | SelectorTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(selTy)); |
| 290 | } |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 291 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 292 | PtrToIntTy = llvm::PointerType::getUnqual(IntTy); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 293 | PtrTy = PtrToInt8Ty; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 295 | // Object type |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 296 | ASTIdTy = CGM.getContext().getCanonicalType(CGM.getContext().getObjCIdType()); |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 297 | if (QualType() == ASTIdTy) { |
| 298 | IdTy = PtrToInt8Ty; |
| 299 | } else { |
| 300 | IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy)); |
| 301 | } |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 302 | PtrToIdTy = llvm::PointerType::getUnqual(IdTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 304 | // IMP type |
| 305 | std::vector<const llvm::Type*> IMPArgs; |
| 306 | IMPArgs.push_back(IdTy); |
| 307 | IMPArgs.push_back(SelectorTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 308 | IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true); |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 309 | |
| 310 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) { |
| 311 | // Get selectors needed in GC mode |
| 312 | RetainSel = GetNullarySelector("retain", CGM.getContext()); |
| 313 | ReleaseSel = GetNullarySelector("release", CGM.getContext()); |
| 314 | AutoreleaseSel = GetNullarySelector("autorelease", CGM.getContext()); |
| 315 | |
| 316 | // Get functions needed in GC mode |
| 317 | |
| 318 | // id objc_assign_ivar(id, id, ptrdiff_t); |
| 319 | std::vector<const llvm::Type*> Args(1, IdTy); |
| 320 | Args.push_back(PtrToIdTy); |
| 321 | // FIXME: ptrdiff_t |
| 322 | Args.push_back(LongTy); |
| 323 | llvm::FunctionType *FTy = llvm::FunctionType::get(IdTy, Args, false); |
| 324 | IvarAssignFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 325 | // id objc_assign_strongCast (id, id*) |
| 326 | Args.pop_back(); |
| 327 | FTy = llvm::FunctionType::get(IdTy, Args, false); |
| 328 | StrongCastAssignFn = |
| 329 | CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
| 330 | // id objc_assign_global(id, id*); |
| 331 | FTy = llvm::FunctionType::get(IdTy, Args, false); |
| 332 | GlobalAssignFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 333 | // id objc_assign_weak(id, id*); |
| 334 | FTy = llvm::FunctionType::get(IdTy, Args, false); |
| 335 | WeakAssignFn = CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 336 | // id objc_read_weak(id*); |
| 337 | Args.clear(); |
| 338 | Args.push_back(PtrToIdTy); |
| 339 | FTy = llvm::FunctionType::get(IdTy, Args, false); |
| 340 | WeakReadFn = CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
| 341 | // void *objc_memmove_collectable(void*, void *, size_t); |
| 342 | Args.clear(); |
| 343 | Args.push_back(PtrToInt8Ty); |
| 344 | Args.push_back(PtrToInt8Ty); |
| 345 | // FIXME: size_t |
| 346 | Args.push_back(LongTy); |
| 347 | FTy = llvm::FunctionType::get(IdTy, Args, false); |
| 348 | MemMoveFn = CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable"); |
| 349 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 350 | } |
Mike Stump | bb1c860 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 351 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 352 | // This has to perform the lookup every time, since posing and related |
| 353 | // techniques can modify the name -> class mapping. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 354 | llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 355 | const ObjCInterfaceDecl *OID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 356 | llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString()); |
David Chisnall | 41d63ed | 2010-01-08 00:14:31 +0000 | [diff] [blame] | 357 | // With the incompatible ABI, this will need to be replaced with a direct |
| 358 | // reference to the class symbol. For the compatible nonfragile ABI we are |
| 359 | // still performing this lookup at run time but emitting the symbol for the |
| 360 | // class externally so that we can make the switch later. |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 361 | EmitClassRef(OID->getNameAsString()); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 362 | ClassName = Builder.CreateStructGEP(ClassName, 0); |
| 363 | |
Fariborz Jahanian | 26c8294 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 364 | std::vector<const llvm::Type*> Params(1, PtrToInt8Ty); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 365 | llvm::Constant *ClassLookupFn = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 366 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, |
Fariborz Jahanian | 26c8294 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 367 | Params, |
| 368 | true), |
| 369 | "objc_lookup_class"); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 370 | return Builder.CreateCall(ClassLookupFn, ClassName); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 373 | llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 374 | bool lval) { |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 375 | llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()]; |
Chris Lattner | 8e67b63 | 2008-06-26 04:37:12 +0000 | [diff] [blame] | 376 | if (US == 0) |
Daniel Dunbar | 6d5a1c2 | 2010-02-03 20:11:42 +0000 | [diff] [blame] | 377 | US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy), |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 378 | llvm::GlobalValue::PrivateLinkage, |
| 379 | ".objc_untyped_selector_alias"+Sel.getAsString(), |
Chris Lattner | 8e67b63 | 2008-06-26 04:37:12 +0000 | [diff] [blame] | 380 | NULL, &TheModule); |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 381 | if (lval) |
| 382 | return US; |
Daniel Dunbar | 6d5a1c2 | 2010-02-03 20:11:42 +0000 | [diff] [blame] | 383 | return Builder.CreateLoad(US); |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Daniel Dunbar | 6d5a1c2 | 2010-02-03 20:11:42 +0000 | [diff] [blame] | 386 | llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 387 | *Method) { |
| 388 | |
| 389 | std::string SelName = Method->getSelector().getAsString(); |
| 390 | std::string SelTypes; |
| 391 | CGM.getContext().getObjCEncodingForMethodDecl(Method, SelTypes); |
| 392 | // Typed selectors |
| 393 | TypedSelector Selector = TypedSelector(SelName, |
| 394 | SelTypes); |
| 395 | |
| 396 | // If it's already cached, return it. |
Mike Stump | bb1c860 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 397 | if (TypedSelectors[Selector]) { |
Daniel Dunbar | 6d5a1c2 | 2010-02-03 20:11:42 +0000 | [diff] [blame] | 398 | return Builder.CreateLoad(TypedSelectors[Selector]); |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | // If it isn't, cache it. |
| 402 | llvm::GlobalAlias *Sel = new llvm::GlobalAlias( |
Daniel Dunbar | 6d5a1c2 | 2010-02-03 20:11:42 +0000 | [diff] [blame] | 403 | llvm::PointerType::getUnqual(SelectorTy), |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 404 | llvm::GlobalValue::PrivateLinkage, ".objc_selector_alias" + SelName, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 405 | NULL, &TheModule); |
| 406 | TypedSelectors[Selector] = Sel; |
| 407 | |
Daniel Dunbar | 6d5a1c2 | 2010-02-03 20:11:42 +0000 | [diff] [blame] | 408 | return Builder.CreateLoad(Sel); |
Chris Lattner | 8e67b63 | 2008-06-26 04:37:12 +0000 | [diff] [blame] | 409 | } |
| 410 | |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 411 | llvm::Constant *CGObjCGNU::GetEHType(QualType T) { |
| 412 | llvm_unreachable("asking for catch type for ObjC type in GNU runtime"); |
| 413 | return 0; |
| 414 | } |
| 415 | |
Chris Lattner | 5e7dcc6 | 2008-06-26 04:44:19 +0000 | [diff] [blame] | 416 | llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str, |
| 417 | const std::string &Name) { |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 418 | llvm::Constant *ConstStr = CGM.GetAddrOfConstantCString(Str, Name.c_str()); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 419 | return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 420 | } |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 421 | llvm::Constant *CGObjCGNU::ExportUniqueString(const std::string &Str, |
| 422 | const std::string prefix) { |
| 423 | std::string name = prefix + Str; |
| 424 | llvm::Constant *ConstStr = TheModule.getGlobalVariable(name); |
| 425 | if (!ConstStr) { |
| 426 | llvm::Constant *value = llvm::ConstantArray::get(VMContext, Str, true); |
| 427 | ConstStr = new llvm::GlobalVariable(TheModule, value->getType(), true, |
| 428 | llvm::GlobalValue::LinkOnceODRLinkage, value, prefix + Str); |
| 429 | } |
| 430 | return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2); |
| 431 | } |
Mike Stump | bb1c860 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 432 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 433 | llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty, |
Benjamin Kramer | 74a8bbf | 2010-02-09 19:31:24 +0000 | [diff] [blame] | 434 | std::vector<llvm::Constant*> &V, llvm::StringRef Name, |
David Chisnall | 41d63ed | 2010-01-08 00:14:31 +0000 | [diff] [blame] | 435 | llvm::GlobalValue::LinkageTypes linkage) { |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 436 | llvm::Constant *C = llvm::ConstantStruct::get(Ty, V); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 437 | return new llvm::GlobalVariable(TheModule, Ty, false, |
| 438 | llvm::GlobalValue::InternalLinkage, C, Name); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 439 | } |
Mike Stump | bb1c860 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 440 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 441 | llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty, |
Benjamin Kramer | 74a8bbf | 2010-02-09 19:31:24 +0000 | [diff] [blame] | 442 | std::vector<llvm::Constant*> &V, llvm::StringRef Name, |
David Chisnall | 41d63ed | 2010-01-08 00:14:31 +0000 | [diff] [blame] | 443 | llvm::GlobalValue::LinkageTypes linkage) { |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 444 | llvm::Constant *C = llvm::ConstantArray::get(Ty, V); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 445 | return new llvm::GlobalVariable(TheModule, Ty, false, |
Mike Stump | bb1c860 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 446 | llvm::GlobalValue::InternalLinkage, C, Name); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /// Generate an NSConstantString object. |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 450 | llvm::Constant *CGObjCGNU::GenerateConstantString(const StringLiteral *SL) { |
David Chisnall | 48272a0 | 2010-01-27 12:49:23 +0000 | [diff] [blame] | 451 | |
Benjamin Kramer | 2f4eaef | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 452 | std::string Str = SL->getString().str(); |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 453 | |
David Chisnall | 48272a0 | 2010-01-27 12:49:23 +0000 | [diff] [blame] | 454 | // Look for an existing one |
| 455 | llvm::StringMap<llvm::Constant*>::iterator old = ObjCStrings.find(Str); |
| 456 | if (old != ObjCStrings.end()) |
| 457 | return old->getValue(); |
| 458 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 459 | std::vector<llvm::Constant*> Ivars; |
| 460 | Ivars.push_back(NULLPtr); |
Chris Lattner | 13fd7e5 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 461 | Ivars.push_back(MakeConstantString(Str)); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 462 | Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size())); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 463 | llvm::Constant *ObjCStr = MakeGlobal( |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 464 | llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL), |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 465 | Ivars, ".objc_str"); |
David Chisnall | 48272a0 | 2010-01-27 12:49:23 +0000 | [diff] [blame] | 466 | ObjCStr = llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty); |
| 467 | ObjCStrings[Str] = ObjCStr; |
| 468 | ConstantStrings.push_back(ObjCStr); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 469 | return ObjCStr; |
| 470 | } |
| 471 | |
| 472 | ///Generates a message send where the super is the receiver. This is a message |
| 473 | ///send to self with special delivery semantics indicating which class's method |
| 474 | ///should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 475 | CodeGen::RValue |
| 476 | CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 477 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 478 | QualType ResultType, |
| 479 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 480 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 481 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 482 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 483 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 484 | const CallArgList &CallArgs, |
| 485 | const ObjCMethodDecl *Method) { |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 486 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) { |
| 487 | if (Sel == RetainSel || Sel == AutoreleaseSel) { |
| 488 | return RValue::get(Receiver); |
| 489 | } |
| 490 | if (Sel == ReleaseSel) { |
| 491 | return RValue::get(0); |
| 492 | } |
| 493 | } |
David Chisnall | db83194 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 494 | |
| 495 | CGBuilderTy &Builder = CGF.Builder; |
| 496 | llvm::Value *cmd = GetSelector(Builder, Sel); |
| 497 | |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 498 | |
| 499 | CallArgList ActualArgs; |
| 500 | |
| 501 | ActualArgs.push_back( |
David Chisnall | db83194 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 502 | std::make_pair(RValue::get(Builder.CreateBitCast(Receiver, IdTy)), |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 503 | ASTIdTy)); |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 504 | ActualArgs.push_back(std::make_pair(RValue::get(cmd), |
| 505 | CGF.getContext().getObjCSelType())); |
| 506 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
| 507 | |
| 508 | CodeGenTypes &Types = CGM.getTypes(); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 509 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 510 | FunctionType::ExtInfo()); |
Daniel Dunbar | 6793966 | 2009-09-17 04:01:40 +0000 | [diff] [blame] | 511 | const llvm::FunctionType *impType = |
| 512 | Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false); |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 513 | |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 514 | llvm::Value *ReceiverClass = 0; |
Chris Lattner | 48e6e7e | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 515 | if (isCategoryImpl) { |
| 516 | llvm::Constant *classLookupFunction = 0; |
| 517 | std::vector<const llvm::Type*> Params; |
| 518 | Params.push_back(PtrTy); |
| 519 | if (IsClassMessage) { |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 520 | classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get( |
Chris Lattner | 48e6e7e | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 521 | IdTy, Params, true), "objc_get_meta_class"); |
| 522 | } else { |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 523 | classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get( |
Chris Lattner | 48e6e7e | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 524 | IdTy, Params, true), "objc_get_class"); |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 525 | } |
David Chisnall | db83194 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 526 | ReceiverClass = Builder.CreateCall(classLookupFunction, |
Chris Lattner | 48e6e7e | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 527 | MakeConstantString(Class->getNameAsString())); |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 528 | } else { |
Chris Lattner | 48e6e7e | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 529 | // Set up global aliases for the metaclass or class pointer if they do not |
| 530 | // already exist. These will are forward-references which will be set to |
Mike Stump | bb1c860 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 531 | // pointers to the class and metaclass structure created for the runtime |
| 532 | // load function. To send a message to super, we look up the value of the |
Chris Lattner | 48e6e7e | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 533 | // super_class pointer from either the class or metaclass structure. |
| 534 | if (IsClassMessage) { |
| 535 | if (!MetaClassPtrAlias) { |
| 536 | MetaClassPtrAlias = new llvm::GlobalAlias(IdTy, |
| 537 | llvm::GlobalValue::InternalLinkage, ".objc_metaclass_ref" + |
| 538 | Class->getNameAsString(), NULL, &TheModule); |
| 539 | } |
| 540 | ReceiverClass = MetaClassPtrAlias; |
| 541 | } else { |
| 542 | if (!ClassPtrAlias) { |
| 543 | ClassPtrAlias = new llvm::GlobalAlias(IdTy, |
| 544 | llvm::GlobalValue::InternalLinkage, ".objc_class_ref" + |
| 545 | Class->getNameAsString(), NULL, &TheModule); |
| 546 | } |
| 547 | ReceiverClass = ClassPtrAlias; |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 548 | } |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 549 | } |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 550 | // Cast the pointer to a simplified version of the class structure |
David Chisnall | db83194 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 551 | ReceiverClass = Builder.CreateBitCast(ReceiverClass, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 552 | llvm::PointerType::getUnqual( |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 553 | llvm::StructType::get(VMContext, IdTy, IdTy, NULL))); |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 554 | // Get the superclass pointer |
David Chisnall | db83194 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 555 | ReceiverClass = Builder.CreateStructGEP(ReceiverClass, 1); |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 556 | // Load the superclass pointer |
David Chisnall | db83194 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 557 | ReceiverClass = Builder.CreateLoad(ReceiverClass); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 558 | // Construct the structure used to look up the IMP |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 559 | llvm::StructType *ObjCSuperTy = llvm::StructType::get(VMContext, |
| 560 | Receiver->getType(), IdTy, NULL); |
David Chisnall | db83194 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 561 | llvm::Value *ObjCSuper = Builder.CreateAlloca(ObjCSuperTy); |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 562 | |
David Chisnall | db83194 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 563 | Builder.CreateStore(Receiver, Builder.CreateStructGEP(ObjCSuper, 0)); |
| 564 | Builder.CreateStore(ReceiverClass, Builder.CreateStructGEP(ObjCSuper, 1)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 565 | |
| 566 | // Get the IMP |
Fariborz Jahanian | 26c8294 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 567 | std::vector<const llvm::Type*> Params; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 568 | Params.push_back(llvm::PointerType::getUnqual(ObjCSuperTy)); |
Fariborz Jahanian | 26c8294 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 569 | Params.push_back(SelectorTy); |
David Chisnall | db83194 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 570 | |
| 571 | llvm::Value *lookupArgs[] = {ObjCSuper, cmd}; |
| 572 | llvm::Value *imp; |
| 573 | |
| 574 | if (CGM.getContext().getLangOptions().ObjCNonFragileABI) { |
| 575 | // The lookup function returns a slot, which can be safely cached. |
| 576 | llvm::Type *SlotTy = llvm::StructType::get(VMContext, PtrTy, PtrTy, PtrTy, |
| 577 | IntTy, llvm::PointerType::getUnqual(impType), NULL); |
| 578 | |
| 579 | llvm::Constant *lookupFunction = |
| 580 | CGM.CreateRuntimeFunction(llvm::FunctionType::get( |
| 581 | llvm::PointerType::getUnqual(SlotTy), Params, true), |
| 582 | "objc_slot_lookup_super"); |
| 583 | |
| 584 | llvm::CallInst *slot = Builder.CreateCall(lookupFunction, lookupArgs, |
| 585 | lookupArgs+2); |
| 586 | slot->setOnlyReadsMemory(); |
| 587 | |
| 588 | imp = Builder.CreateLoad(Builder.CreateStructGEP(slot, 4)); |
| 589 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 590 | llvm::Constant *lookupFunction = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 591 | CGM.CreateRuntimeFunction(llvm::FunctionType::get( |
| 592 | llvm::PointerType::getUnqual(impType), Params, true), |
Fariborz Jahanian | 26c8294 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 593 | "objc_msg_lookup_super"); |
David Chisnall | db83194 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 594 | imp = Builder.CreateCall(lookupFunction, lookupArgs, lookupArgs+2); |
| 595 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 596 | |
David Chisnall | dd5c98f | 2010-05-01 11:15:56 +0000 | [diff] [blame] | 597 | llvm::Value *impMD[] = { |
| 598 | llvm::MDString::get(VMContext, Sel.getAsString()), |
| 599 | llvm::MDString::get(VMContext, Class->getSuperClass()->getNameAsString()), |
| 600 | llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsClassMessage) |
| 601 | }; |
| 602 | llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD, 3); |
| 603 | |
David Chisnall | 4b02afc | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 604 | llvm::Instruction *call; |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 605 | RValue msgRet = CGF.EmitCall(FnInfo, imp, Return, ActualArgs, |
David Chisnall | 4b02afc | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 606 | 0, &call); |
| 607 | call->setMetadata(msgSendMDKind, node); |
| 608 | return msgRet; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 611 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 612 | CodeGen::RValue |
| 613 | CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 614 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 615 | QualType ResultType, |
| 616 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 617 | llvm::Value *Receiver, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 618 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 619 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 620 | const ObjCMethodDecl *Method) { |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 621 | // Strip out message sends to retain / release in GC mode |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 622 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) { |
| 623 | if (Sel == RetainSel || Sel == AutoreleaseSel) { |
| 624 | return RValue::get(Receiver); |
| 625 | } |
| 626 | if (Sel == ReleaseSel) { |
| 627 | return RValue::get(0); |
| 628 | } |
| 629 | } |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 630 | |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 631 | CGBuilderTy &Builder = CGF.Builder; |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 632 | |
| 633 | // If the return type is something that goes in an integer register, the |
| 634 | // runtime will handle 0 returns. For other cases, we fill in the 0 value |
| 635 | // ourselves. |
| 636 | // |
| 637 | // The language spec says the result of this kind of message send is |
| 638 | // undefined, but lots of people seem to have forgotten to read that |
| 639 | // paragraph and insist on sending messages to nil that have structure |
| 640 | // returns. With GCC, this generates a random return value (whatever happens |
| 641 | // to be on the stack / in those registers at the time) on most platforms, |
| 642 | // and generates a SegV on SPARC. With LLVM it corrupts the stack. |
| 643 | bool isPointerSizedReturn = false; |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 644 | if (ResultType->isAnyPointerType() || |
| 645 | ResultType->isIntegralOrEnumerationType() || ResultType->isVoidType()) |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 646 | isPointerSizedReturn = true; |
| 647 | |
| 648 | llvm::BasicBlock *startBB = 0; |
| 649 | llvm::BasicBlock *messageBB = 0; |
David Chisnall | a54da05 | 2010-05-20 13:45:48 +0000 | [diff] [blame] | 650 | llvm::BasicBlock *continueBB = 0; |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 651 | |
| 652 | if (!isPointerSizedReturn) { |
| 653 | startBB = Builder.GetInsertBlock(); |
| 654 | messageBB = CGF.createBasicBlock("msgSend"); |
David Chisnall | a54da05 | 2010-05-20 13:45:48 +0000 | [diff] [blame] | 655 | continueBB = CGF.createBasicBlock("continue"); |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 656 | |
| 657 | llvm::Value *isNil = Builder.CreateICmpEQ(Receiver, |
| 658 | llvm::Constant::getNullValue(Receiver->getType())); |
David Chisnall | a54da05 | 2010-05-20 13:45:48 +0000 | [diff] [blame] | 659 | Builder.CreateCondBr(isNil, continueBB, messageBB); |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 660 | CGF.EmitBlock(messageBB); |
| 661 | } |
| 662 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 663 | IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy)); |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 664 | llvm::Value *cmd; |
| 665 | if (Method) |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 666 | cmd = GetSelector(Builder, Method); |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 667 | else |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 668 | cmd = GetSelector(Builder, Sel); |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 669 | CallArgList ActualArgs; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 670 | |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 671 | Receiver = Builder.CreateBitCast(Receiver, IdTy); |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 672 | ActualArgs.push_back( |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 673 | std::make_pair(RValue::get(Receiver), ASTIdTy)); |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 674 | ActualArgs.push_back(std::make_pair(RValue::get(cmd), |
| 675 | CGF.getContext().getObjCSelType())); |
| 676 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
| 677 | |
| 678 | CodeGenTypes &Types = CGM.getTypes(); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 679 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 680 | FunctionType::ExtInfo()); |
Daniel Dunbar | 6793966 | 2009-09-17 04:01:40 +0000 | [diff] [blame] | 681 | const llvm::FunctionType *impType = |
| 682 | Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false); |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 683 | |
David Chisnall | 63e742b | 2010-05-01 12:56:56 +0000 | [diff] [blame] | 684 | llvm::Value *impMD[] = { |
| 685 | llvm::MDString::get(VMContext, Sel.getAsString()), |
| 686 | llvm::MDString::get(VMContext, Class ? Class->getNameAsString() :""), |
| 687 | llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), Class!=0) |
| 688 | }; |
| 689 | llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD, 3); |
| 690 | |
| 691 | |
Fariborz Jahanian | 34e6577 | 2009-05-22 20:17:16 +0000 | [diff] [blame] | 692 | llvm::Value *imp; |
Fariborz Jahanian | 34e6577 | 2009-05-22 20:17:16 +0000 | [diff] [blame] | 693 | // For sender-aware dispatch, we pass the sender as the third argument to a |
| 694 | // lookup function. When sending messages from C code, the sender is nil. |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 695 | // objc_msg_lookup_sender(id *receiver, SEL selector, id sender); |
| 696 | if (CGM.getContext().getLangOptions().ObjCNonFragileABI) { |
| 697 | |
| 698 | std::vector<const llvm::Type*> Params; |
| 699 | llvm::Value *ReceiverPtr = CGF.CreateTempAlloca(Receiver->getType()); |
| 700 | Builder.CreateStore(Receiver, ReceiverPtr); |
| 701 | Params.push_back(ReceiverPtr->getType()); |
| 702 | Params.push_back(SelectorTy); |
Fariborz Jahanian | 34e6577 | 2009-05-22 20:17:16 +0000 | [diff] [blame] | 703 | llvm::Value *self; |
Fariborz Jahanian | 26c8294 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 704 | |
David Chisnall | f69ea95 | 2010-07-21 15:28:28 +0000 | [diff] [blame] | 705 | if (isa<ObjCMethodDecl>(CGF.CurCodeDecl)) { |
Fariborz Jahanian | 34e6577 | 2009-05-22 20:17:16 +0000 | [diff] [blame] | 706 | self = CGF.LoadObjCSelf(); |
| 707 | } else { |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 708 | self = llvm::ConstantPointerNull::get(IdTy); |
Fariborz Jahanian | 34e6577 | 2009-05-22 20:17:16 +0000 | [diff] [blame] | 709 | } |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 710 | |
Fariborz Jahanian | 34e6577 | 2009-05-22 20:17:16 +0000 | [diff] [blame] | 711 | Params.push_back(self->getType()); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 712 | |
| 713 | // The lookup function returns a slot, which can be safely cached. |
| 714 | llvm::Type *SlotTy = llvm::StructType::get(VMContext, PtrTy, PtrTy, PtrTy, |
| 715 | IntTy, llvm::PointerType::getUnqual(impType), NULL); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 716 | llvm::Constant *lookupFunction = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 717 | CGM.CreateRuntimeFunction(llvm::FunctionType::get( |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 718 | llvm::PointerType::getUnqual(SlotTy), Params, true), |
Fariborz Jahanian | 34e6577 | 2009-05-22 20:17:16 +0000 | [diff] [blame] | 719 | "objc_msg_lookup_sender"); |
| 720 | |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 721 | // The lookup function is guaranteed not to capture the receiver pointer. |
| 722 | if (llvm::Function *LookupFn = dyn_cast<llvm::Function>(lookupFunction)) { |
| 723 | LookupFn->setDoesNotCapture(1); |
| 724 | } |
| 725 | |
David Chisnall | 866163b | 2010-04-30 13:36:12 +0000 | [diff] [blame] | 726 | llvm::CallInst *slot = |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 727 | Builder.CreateCall3(lookupFunction, ReceiverPtr, cmd, self); |
David Chisnall | 866163b | 2010-04-30 13:36:12 +0000 | [diff] [blame] | 728 | slot->setOnlyReadsMemory(); |
David Chisnall | 63e742b | 2010-05-01 12:56:56 +0000 | [diff] [blame] | 729 | slot->setMetadata(msgSendMDKind, node); |
David Chisnall | 866163b | 2010-04-30 13:36:12 +0000 | [diff] [blame] | 730 | |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 731 | imp = Builder.CreateLoad(Builder.CreateStructGEP(slot, 4)); |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 732 | |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 733 | // The lookup function may have changed the receiver, so make sure we use |
| 734 | // the new one. |
David Chisnall | 41d0c7a | 2010-07-21 12:55:25 +0000 | [diff] [blame] | 735 | ActualArgs[0] = std::make_pair(RValue::get( |
| 736 | Builder.CreateLoad(ReceiverPtr, true)), ASTIdTy); |
Fariborz Jahanian | 34e6577 | 2009-05-22 20:17:16 +0000 | [diff] [blame] | 737 | } else { |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 738 | std::vector<const llvm::Type*> Params; |
| 739 | Params.push_back(Receiver->getType()); |
| 740 | Params.push_back(SelectorTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 741 | llvm::Constant *lookupFunction = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 742 | CGM.CreateRuntimeFunction(llvm::FunctionType::get( |
| 743 | llvm::PointerType::getUnqual(impType), Params, true), |
Fariborz Jahanian | 34e6577 | 2009-05-22 20:17:16 +0000 | [diff] [blame] | 744 | "objc_msg_lookup"); |
| 745 | |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 746 | imp = Builder.CreateCall2(lookupFunction, Receiver, cmd); |
David Chisnall | 63e742b | 2010-05-01 12:56:56 +0000 | [diff] [blame] | 747 | cast<llvm::CallInst>(imp)->setMetadata(msgSendMDKind, node); |
Fariborz Jahanian | 34e6577 | 2009-05-22 20:17:16 +0000 | [diff] [blame] | 748 | } |
David Chisnall | 4b02afc | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 749 | llvm::Instruction *call; |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 750 | RValue msgRet = CGF.EmitCall(FnInfo, imp, Return, ActualArgs, |
David Chisnall | 4b02afc | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 751 | 0, &call); |
| 752 | call->setMetadata(msgSendMDKind, node); |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 753 | |
David Chisnall | a54da05 | 2010-05-20 13:45:48 +0000 | [diff] [blame] | 754 | |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 755 | if (!isPointerSizedReturn) { |
David Chisnall | a54da05 | 2010-05-20 13:45:48 +0000 | [diff] [blame] | 756 | messageBB = CGF.Builder.GetInsertBlock(); |
| 757 | CGF.Builder.CreateBr(continueBB); |
| 758 | CGF.EmitBlock(continueBB); |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 759 | if (msgRet.isScalar()) { |
| 760 | llvm::Value *v = msgRet.getScalarVal(); |
| 761 | llvm::PHINode *phi = Builder.CreatePHI(v->getType()); |
| 762 | phi->addIncoming(v, messageBB); |
| 763 | phi->addIncoming(llvm::Constant::getNullValue(v->getType()), startBB); |
| 764 | msgRet = RValue::get(phi); |
| 765 | } else if (msgRet.isAggregate()) { |
| 766 | llvm::Value *v = msgRet.getAggregateAddr(); |
| 767 | llvm::PHINode *phi = Builder.CreatePHI(v->getType()); |
| 768 | const llvm::PointerType *RetTy = cast<llvm::PointerType>(v->getType()); |
David Chisnall | 866163b | 2010-04-30 13:36:12 +0000 | [diff] [blame] | 769 | llvm::AllocaInst *NullVal = |
| 770 | CGF.CreateTempAlloca(RetTy->getElementType(), "null"); |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 771 | CGF.InitTempAlloca(NullVal, |
| 772 | llvm::Constant::getNullValue(RetTy->getElementType())); |
| 773 | phi->addIncoming(v, messageBB); |
| 774 | phi->addIncoming(NullVal, startBB); |
| 775 | msgRet = RValue::getAggregate(phi); |
| 776 | } else /* isComplex() */ { |
| 777 | std::pair<llvm::Value*,llvm::Value*> v = msgRet.getComplexVal(); |
| 778 | llvm::PHINode *phi = Builder.CreatePHI(v.first->getType()); |
| 779 | phi->addIncoming(v.first, messageBB); |
| 780 | phi->addIncoming(llvm::Constant::getNullValue(v.first->getType()), |
| 781 | startBB); |
| 782 | llvm::PHINode *phi2 = Builder.CreatePHI(v.second->getType()); |
| 783 | phi2->addIncoming(v.second, messageBB); |
| 784 | phi2->addIncoming(llvm::Constant::getNullValue(v.second->getType()), |
| 785 | startBB); |
| 786 | msgRet = RValue::getComplex(phi, phi2); |
| 787 | } |
| 788 | } |
| 789 | return msgRet; |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 790 | } |
| 791 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 792 | /// Generates a MethodList. Used in construction of a objc_class and |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 793 | /// objc_category structures. |
| 794 | llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 795 | const std::string &CategoryName, |
| 796 | const llvm::SmallVectorImpl<Selector> &MethodSels, |
| 797 | const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 798 | bool isClassMethodList) { |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 799 | if (MethodSels.empty()) |
| 800 | return NULLPtr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 801 | // Get the method structure type. |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 802 | llvm::StructType *ObjCMethodTy = llvm::StructType::get(VMContext, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 803 | PtrToInt8Ty, // Really a selector, but the runtime creates it us. |
| 804 | PtrToInt8Ty, // Method types |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 805 | llvm::PointerType::getUnqual(IMPTy), //Method pointer |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 806 | NULL); |
| 807 | std::vector<llvm::Constant*> Methods; |
| 808 | std::vector<llvm::Constant*> Elements; |
| 809 | for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) { |
| 810 | Elements.clear(); |
Fariborz Jahanian | 1e64a95 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 811 | if (llvm::Constant *Method = |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 812 | TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName, |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 813 | MethodSels[i].getAsString(), |
Fariborz Jahanian | 1e64a95 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 814 | isClassMethodList))) { |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 815 | llvm::Constant *C = MakeConstantString(MethodSels[i].getAsString()); |
| 816 | Elements.push_back(C); |
| 817 | Elements.push_back(MethodTypes[i]); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 818 | Method = llvm::ConstantExpr::getBitCast(Method, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 819 | llvm::PointerType::getUnqual(IMPTy)); |
Fariborz Jahanian | 1e64a95 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 820 | Elements.push_back(Method); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 821 | Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements)); |
Fariborz Jahanian | 1e64a95 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 822 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | // Array of method structures |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 826 | llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy, |
Fariborz Jahanian | 1e64a95 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 827 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 828 | llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy, |
Chris Lattner | fba6763 | 2008-06-26 04:52:29 +0000 | [diff] [blame] | 829 | Methods); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 830 | |
| 831 | // Structure containing list pointer, array and array count |
| 832 | llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields; |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 833 | llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get(VMContext); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 834 | llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy); |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 835 | llvm::StructType *ObjCMethodListTy = llvm::StructType::get(VMContext, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 836 | NextPtrTy, |
| 837 | IntTy, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 838 | ObjCMethodArrayTy, |
| 839 | NULL); |
| 840 | // Refine next pointer type to concrete type |
| 841 | llvm::cast<llvm::OpaqueType>( |
| 842 | OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy); |
| 843 | ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get()); |
| 844 | |
| 845 | Methods.clear(); |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 846 | Methods.push_back(llvm::ConstantPointerNull::get( |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 847 | llvm::PointerType::getUnqual(ObjCMethodListTy))); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 848 | Methods.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 849 | MethodTypes.size())); |
| 850 | Methods.push_back(MethodArray); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 851 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 852 | // Create an instance of the structure |
| 853 | return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list"); |
| 854 | } |
| 855 | |
| 856 | /// Generates an IvarList. Used in construction of a objc_class. |
| 857 | llvm::Constant *CGObjCGNU::GenerateIvarList( |
| 858 | const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames, |
| 859 | const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes, |
| 860 | const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) { |
David Chisnall | 1804463 | 2009-11-16 19:05:54 +0000 | [diff] [blame] | 861 | if (IvarNames.size() == 0) |
| 862 | return NULLPtr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | // Get the method structure type. |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 864 | llvm::StructType *ObjCIvarTy = llvm::StructType::get(VMContext, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 865 | PtrToInt8Ty, |
| 866 | PtrToInt8Ty, |
| 867 | IntTy, |
| 868 | NULL); |
| 869 | std::vector<llvm::Constant*> Ivars; |
| 870 | std::vector<llvm::Constant*> Elements; |
| 871 | for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) { |
| 872 | Elements.clear(); |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 873 | Elements.push_back(IvarNames[i]); |
| 874 | Elements.push_back(IvarTypes[i]); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 875 | Elements.push_back(IvarOffsets[i]); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 876 | Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | // Array of method structures |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 880 | llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 881 | IvarNames.size()); |
| 882 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 883 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 884 | Elements.clear(); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 885 | Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size())); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 886 | Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 887 | // Structure containing array and array count |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 888 | llvm::StructType *ObjCIvarListTy = llvm::StructType::get(VMContext, IntTy, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 889 | ObjCIvarArrayTy, |
| 890 | NULL); |
| 891 | |
| 892 | // Create an instance of the structure |
| 893 | return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list"); |
| 894 | } |
| 895 | |
| 896 | /// Generate a class structure |
| 897 | llvm::Constant *CGObjCGNU::GenerateClassStructure( |
| 898 | llvm::Constant *MetaClass, |
| 899 | llvm::Constant *SuperClass, |
| 900 | unsigned info, |
Chris Lattner | d002cc6 | 2008-06-26 04:47:04 +0000 | [diff] [blame] | 901 | const char *Name, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 902 | llvm::Constant *Version, |
| 903 | llvm::Constant *InstanceSize, |
| 904 | llvm::Constant *IVars, |
| 905 | llvm::Constant *Methods, |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 906 | llvm::Constant *Protocols, |
| 907 | llvm::Constant *IvarOffsets, |
David Chisnall | 8c757f9 | 2010-04-28 14:29:56 +0000 | [diff] [blame] | 908 | llvm::Constant *Properties, |
| 909 | bool isMeta) { |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 910 | // Set up the class structure |
| 911 | // Note: Several of these are char*s when they should be ids. This is |
| 912 | // because the runtime performs this translation on load. |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 913 | // |
| 914 | // Fields marked New ABI are part of the GNUstep runtime. We emit them |
| 915 | // anyway; the classes will still work with the GNU runtime, they will just |
| 916 | // be ignored. |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 917 | llvm::StructType *ClassTy = llvm::StructType::get(VMContext, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 918 | PtrToInt8Ty, // class_pointer |
| 919 | PtrToInt8Ty, // super_class |
| 920 | PtrToInt8Ty, // name |
| 921 | LongTy, // version |
| 922 | LongTy, // info |
| 923 | LongTy, // instance_size |
| 924 | IVars->getType(), // ivars |
| 925 | Methods->getType(), // methods |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 926 | // These are all filled in by the runtime, so we pretend |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 927 | PtrTy, // dtable |
| 928 | PtrTy, // subclass_list |
| 929 | PtrTy, // sibling_class |
| 930 | PtrTy, // protocols |
| 931 | PtrTy, // gc_object_type |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 932 | // New ABI: |
| 933 | LongTy, // abi_version |
| 934 | IvarOffsets->getType(), // ivar_offsets |
| 935 | Properties->getType(), // properties |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 936 | NULL); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 937 | llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 938 | // Fill in the structure |
| 939 | std::vector<llvm::Constant*> Elements; |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 940 | Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 941 | Elements.push_back(SuperClass); |
Chris Lattner | d002cc6 | 2008-06-26 04:47:04 +0000 | [diff] [blame] | 942 | Elements.push_back(MakeConstantString(Name, ".class_name")); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 943 | Elements.push_back(Zero); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 944 | Elements.push_back(llvm::ConstantInt::get(LongTy, info)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 945 | Elements.push_back(InstanceSize); |
| 946 | Elements.push_back(IVars); |
| 947 | Elements.push_back(Methods); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 948 | Elements.push_back(NULLPtr); |
| 949 | Elements.push_back(NULLPtr); |
| 950 | Elements.push_back(NULLPtr); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 951 | Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy)); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 952 | Elements.push_back(NULLPtr); |
| 953 | Elements.push_back(Zero); |
| 954 | Elements.push_back(IvarOffsets); |
| 955 | Elements.push_back(Properties); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 956 | // Create an instance of the structure |
David Chisnall | 41d63ed | 2010-01-08 00:14:31 +0000 | [diff] [blame] | 957 | // This is now an externally visible symbol, so that we can speed up class |
| 958 | // messages in the next ABI. |
David Chisnall | 8c757f9 | 2010-04-28 14:29:56 +0000 | [diff] [blame] | 959 | return MakeGlobal(ClassTy, Elements, (isMeta ? "_OBJC_METACLASS_": |
| 960 | "_OBJC_CLASS_") + std::string(Name), llvm::GlobalValue::ExternalLinkage); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | llvm::Constant *CGObjCGNU::GenerateProtocolMethodList( |
| 964 | const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames, |
| 965 | const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 966 | // Get the method structure type. |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 967 | llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(VMContext, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 968 | PtrToInt8Ty, // Really a selector, but the runtime does the casting for us. |
| 969 | PtrToInt8Ty, |
| 970 | NULL); |
| 971 | std::vector<llvm::Constant*> Methods; |
| 972 | std::vector<llvm::Constant*> Elements; |
| 973 | for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) { |
| 974 | Elements.clear(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 975 | Elements.push_back(MethodNames[i]); |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 976 | Elements.push_back(MethodTypes[i]); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 977 | Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 978 | } |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 979 | llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 980 | MethodNames.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 981 | llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, |
Mike Stump | bb1c860 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 982 | Methods); |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 983 | llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(VMContext, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 984 | IntTy, ObjCMethodArrayTy, NULL); |
| 985 | Methods.clear(); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 986 | Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size())); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 987 | Methods.push_back(Array); |
| 988 | return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list"); |
| 989 | } |
Mike Stump | bb1c860 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 990 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 991 | // Create the protocol list structure used in classes, categories and so on |
| 992 | llvm::Constant *CGObjCGNU::GenerateProtocolList( |
| 993 | const llvm::SmallVectorImpl<std::string> &Protocols) { |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 994 | llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 995 | Protocols.size()); |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 996 | llvm::StructType *ProtocolListTy = llvm::StructType::get(VMContext, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 997 | PtrTy, //Should be a recurisve pointer, but it's always NULL here. |
| 998 | LongTy,//FIXME: Should be size_t |
| 999 | ProtocolArrayTy, |
| 1000 | NULL); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | std::vector<llvm::Constant*> Elements; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1002 | for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end(); |
| 1003 | iter != endIter ; iter++) { |
David Chisnall | ff80fab | 2009-11-20 14:50:59 +0000 | [diff] [blame] | 1004 | llvm::Constant *protocol = 0; |
| 1005 | llvm::StringMap<llvm::Constant*>::iterator value = |
| 1006 | ExistingProtocols.find(*iter); |
| 1007 | if (value == ExistingProtocols.end()) { |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1008 | protocol = GenerateEmptyProtocol(*iter); |
David Chisnall | ff80fab | 2009-11-20 14:50:59 +0000 | [diff] [blame] | 1009 | } else { |
| 1010 | protocol = value->getValue(); |
| 1011 | } |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1012 | llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(protocol, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1013 | PtrToInt8Ty); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1014 | Elements.push_back(Ptr); |
| 1015 | } |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 1016 | llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1017 | Elements); |
| 1018 | Elements.clear(); |
| 1019 | Elements.push_back(NULLPtr); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1020 | Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size())); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1021 | Elements.push_back(ProtocolArray); |
| 1022 | return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list"); |
| 1023 | } |
| 1024 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1025 | llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1026 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1027 | llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1028 | const llvm::Type *T = |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1029 | CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1030 | return Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T)); |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | llvm::Constant *CGObjCGNU::GenerateEmptyProtocol( |
| 1034 | const std::string &ProtocolName) { |
| 1035 | llvm::SmallVector<std::string, 0> EmptyStringVector; |
| 1036 | llvm::SmallVector<llvm::Constant*, 0> EmptyConstantVector; |
| 1037 | |
| 1038 | llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1039 | llvm::Constant *MethodList = |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1040 | GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector); |
| 1041 | // Protocols are objects containing lists of the methods implemented and |
| 1042 | // protocols adopted. |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1043 | llvm::StructType *ProtocolTy = llvm::StructType::get(VMContext, IdTy, |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1044 | PtrToInt8Ty, |
| 1045 | ProtocolList->getType(), |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1046 | MethodList->getType(), |
| 1047 | MethodList->getType(), |
| 1048 | MethodList->getType(), |
| 1049 | MethodList->getType(), |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1050 | NULL); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1051 | std::vector<llvm::Constant*> Elements; |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1052 | // The isa pointer must be set to a magic number so the runtime knows it's |
| 1053 | // the correct layout. |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1054 | int Version = CGM.getContext().getLangOptions().ObjCNonFragileABI ? |
| 1055 | NonFragileProtocolVersion : ProtocolVersion; |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1056 | Elements.push_back(llvm::ConstantExpr::getIntToPtr( |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1057 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Version), IdTy)); |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1058 | Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name")); |
| 1059 | Elements.push_back(ProtocolList); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1060 | Elements.push_back(MethodList); |
| 1061 | Elements.push_back(MethodList); |
| 1062 | Elements.push_back(MethodList); |
| 1063 | Elements.push_back(MethodList); |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1064 | return MakeGlobal(ProtocolTy, Elements, ".objc_protocol"); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1067 | void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) { |
| 1068 | ASTContext &Context = CGM.getContext(); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1069 | std::string ProtocolName = PD->getNameAsString(); |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1070 | llvm::SmallVector<std::string, 16> Protocols; |
| 1071 | for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(), |
| 1072 | E = PD->protocol_end(); PI != E; ++PI) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1073 | Protocols.push_back((*PI)->getNameAsString()); |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1074 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames; |
| 1075 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes; |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1076 | llvm::SmallVector<llvm::Constant*, 16> OptionalInstanceMethodNames; |
| 1077 | llvm::SmallVector<llvm::Constant*, 16> OptionalInstanceMethodTypes; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1078 | for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(), |
| 1079 | E = PD->instmeth_end(); iter != E; iter++) { |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1080 | std::string TypeStr; |
| 1081 | Context.getObjCEncodingForMethodDecl(*iter, TypeStr); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1082 | if ((*iter)->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1083 | InstanceMethodNames.push_back( |
| 1084 | MakeConstantString((*iter)->getSelector().getAsString())); |
| 1085 | InstanceMethodTypes.push_back(MakeConstantString(TypeStr)); |
| 1086 | } else { |
| 1087 | OptionalInstanceMethodNames.push_back( |
| 1088 | MakeConstantString((*iter)->getSelector().getAsString())); |
| 1089 | OptionalInstanceMethodTypes.push_back(MakeConstantString(TypeStr)); |
| 1090 | } |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1091 | } |
| 1092 | // Collect information about class methods: |
| 1093 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames; |
| 1094 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes; |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1095 | llvm::SmallVector<llvm::Constant*, 16> OptionalClassMethodNames; |
| 1096 | llvm::SmallVector<llvm::Constant*, 16> OptionalClassMethodTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1097 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1098 | iter = PD->classmeth_begin(), endIter = PD->classmeth_end(); |
| 1099 | iter != endIter ; iter++) { |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1100 | std::string TypeStr; |
| 1101 | Context.getObjCEncodingForMethodDecl((*iter),TypeStr); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1102 | if ((*iter)->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1103 | ClassMethodNames.push_back( |
| 1104 | MakeConstantString((*iter)->getSelector().getAsString())); |
| 1105 | ClassMethodTypes.push_back(MakeConstantString(TypeStr)); |
| 1106 | } else { |
| 1107 | OptionalClassMethodNames.push_back( |
| 1108 | MakeConstantString((*iter)->getSelector().getAsString())); |
| 1109 | OptionalClassMethodTypes.push_back(MakeConstantString(TypeStr)); |
| 1110 | } |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1111 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1112 | |
| 1113 | llvm::Constant *ProtocolList = GenerateProtocolList(Protocols); |
| 1114 | llvm::Constant *InstanceMethodList = |
| 1115 | GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes); |
| 1116 | llvm::Constant *ClassMethodList = |
| 1117 | GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1118 | llvm::Constant *OptionalInstanceMethodList = |
| 1119 | GenerateProtocolMethodList(OptionalInstanceMethodNames, |
| 1120 | OptionalInstanceMethodTypes); |
| 1121 | llvm::Constant *OptionalClassMethodList = |
| 1122 | GenerateProtocolMethodList(OptionalClassMethodNames, |
| 1123 | OptionalClassMethodTypes); |
| 1124 | |
| 1125 | // Property metadata: name, attributes, isSynthesized, setter name, setter |
| 1126 | // types, getter name, getter types. |
| 1127 | // The isSynthesized value is always set to 0 in a protocol. It exists to |
| 1128 | // simplify the runtime library by allowing it to use the same data |
| 1129 | // structures for protocol metadata everywhere. |
| 1130 | llvm::StructType *PropertyMetadataTy = llvm::StructType::get(VMContext, |
| 1131 | PtrToInt8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty, |
| 1132 | PtrToInt8Ty, NULL); |
| 1133 | std::vector<llvm::Constant*> Properties; |
| 1134 | std::vector<llvm::Constant*> OptionalProperties; |
| 1135 | |
| 1136 | // Add all of the property methods need adding to the method list and to the |
| 1137 | // property metadata list. |
| 1138 | for (ObjCContainerDecl::prop_iterator |
| 1139 | iter = PD->prop_begin(), endIter = PD->prop_end(); |
| 1140 | iter != endIter ; iter++) { |
| 1141 | std::vector<llvm::Constant*> Fields; |
| 1142 | ObjCPropertyDecl *property = (*iter); |
| 1143 | |
| 1144 | Fields.push_back(MakeConstantString(property->getNameAsString())); |
| 1145 | Fields.push_back(llvm::ConstantInt::get(Int8Ty, |
| 1146 | property->getPropertyAttributes())); |
| 1147 | Fields.push_back(llvm::ConstantInt::get(Int8Ty, 0)); |
| 1148 | if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) { |
| 1149 | std::string TypeStr; |
| 1150 | Context.getObjCEncodingForMethodDecl(getter,TypeStr); |
| 1151 | llvm::Constant *TypeEncoding = MakeConstantString(TypeStr); |
| 1152 | InstanceMethodTypes.push_back(TypeEncoding); |
| 1153 | Fields.push_back(MakeConstantString(getter->getSelector().getAsString())); |
| 1154 | Fields.push_back(TypeEncoding); |
| 1155 | } else { |
| 1156 | Fields.push_back(NULLPtr); |
| 1157 | Fields.push_back(NULLPtr); |
| 1158 | } |
| 1159 | if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) { |
| 1160 | std::string TypeStr; |
| 1161 | Context.getObjCEncodingForMethodDecl(setter,TypeStr); |
| 1162 | llvm::Constant *TypeEncoding = MakeConstantString(TypeStr); |
| 1163 | InstanceMethodTypes.push_back(TypeEncoding); |
| 1164 | Fields.push_back(MakeConstantString(setter->getSelector().getAsString())); |
| 1165 | Fields.push_back(TypeEncoding); |
| 1166 | } else { |
| 1167 | Fields.push_back(NULLPtr); |
| 1168 | Fields.push_back(NULLPtr); |
| 1169 | } |
| 1170 | if (property->getPropertyImplementation() == ObjCPropertyDecl::Optional) { |
| 1171 | OptionalProperties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields)); |
| 1172 | } else { |
| 1173 | Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields)); |
| 1174 | } |
| 1175 | } |
| 1176 | llvm::Constant *PropertyArray = llvm::ConstantArray::get( |
| 1177 | llvm::ArrayType::get(PropertyMetadataTy, Properties.size()), Properties); |
| 1178 | llvm::Constant* PropertyListInitFields[] = |
| 1179 | {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray}; |
| 1180 | |
| 1181 | llvm::Constant *PropertyListInit = |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 1182 | llvm::ConstantStruct::get(VMContext, PropertyListInitFields, 3, false); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1183 | llvm::Constant *PropertyList = new llvm::GlobalVariable(TheModule, |
| 1184 | PropertyListInit->getType(), false, llvm::GlobalValue::InternalLinkage, |
| 1185 | PropertyListInit, ".objc_property_list"); |
| 1186 | |
| 1187 | llvm::Constant *OptionalPropertyArray = |
| 1188 | llvm::ConstantArray::get(llvm::ArrayType::get(PropertyMetadataTy, |
| 1189 | OptionalProperties.size()) , OptionalProperties); |
| 1190 | llvm::Constant* OptionalPropertyListInitFields[] = { |
| 1191 | llvm::ConstantInt::get(IntTy, OptionalProperties.size()), NULLPtr, |
| 1192 | OptionalPropertyArray }; |
| 1193 | |
| 1194 | llvm::Constant *OptionalPropertyListInit = |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 1195 | llvm::ConstantStruct::get(VMContext, OptionalPropertyListInitFields, 3, false); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1196 | llvm::Constant *OptionalPropertyList = new llvm::GlobalVariable(TheModule, |
| 1197 | OptionalPropertyListInit->getType(), false, |
| 1198 | llvm::GlobalValue::InternalLinkage, OptionalPropertyListInit, |
| 1199 | ".objc_property_list"); |
| 1200 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1201 | // Protocols are objects containing lists of the methods implemented and |
| 1202 | // protocols adopted. |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1203 | llvm::StructType *ProtocolTy = llvm::StructType::get(VMContext, IdTy, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1204 | PtrToInt8Ty, |
| 1205 | ProtocolList->getType(), |
| 1206 | InstanceMethodList->getType(), |
| 1207 | ClassMethodList->getType(), |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1208 | OptionalInstanceMethodList->getType(), |
| 1209 | OptionalClassMethodList->getType(), |
| 1210 | PropertyList->getType(), |
| 1211 | OptionalPropertyList->getType(), |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1212 | NULL); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1213 | std::vector<llvm::Constant*> Elements; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1214 | // The isa pointer must be set to a magic number so the runtime knows it's |
| 1215 | // the correct layout. |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1216 | int Version = CGM.getContext().getLangOptions().ObjCNonFragileABI ? |
| 1217 | NonFragileProtocolVersion : ProtocolVersion; |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1218 | Elements.push_back(llvm::ConstantExpr::getIntToPtr( |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1219 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Version), IdTy)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1220 | Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name")); |
| 1221 | Elements.push_back(ProtocolList); |
| 1222 | Elements.push_back(InstanceMethodList); |
| 1223 | Elements.push_back(ClassMethodList); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1224 | Elements.push_back(OptionalInstanceMethodList); |
| 1225 | Elements.push_back(OptionalClassMethodList); |
| 1226 | Elements.push_back(PropertyList); |
| 1227 | Elements.push_back(OptionalPropertyList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1228 | ExistingProtocols[ProtocolName] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1229 | llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1230 | ".objc_protocol"), IdTy); |
| 1231 | } |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1232 | void CGObjCGNU::GenerateProtocolHolderCategory(void) { |
| 1233 | // Collect information about instance methods |
| 1234 | llvm::SmallVector<Selector, 1> MethodSels; |
| 1235 | llvm::SmallVector<llvm::Constant*, 1> MethodTypes; |
| 1236 | |
| 1237 | std::vector<llvm::Constant*> Elements; |
| 1238 | const std::string ClassName = "__ObjC_Protocol_Holder_Ugly_Hack"; |
| 1239 | const std::string CategoryName = "AnotherHack"; |
| 1240 | Elements.push_back(MakeConstantString(CategoryName)); |
| 1241 | Elements.push_back(MakeConstantString(ClassName)); |
| 1242 | // Instance method list |
| 1243 | Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList( |
| 1244 | ClassName, CategoryName, MethodSels, MethodTypes, false), PtrTy)); |
| 1245 | // Class method list |
| 1246 | Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList( |
| 1247 | ClassName, CategoryName, MethodSels, MethodTypes, true), PtrTy)); |
| 1248 | // Protocol list |
| 1249 | llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrTy, |
| 1250 | ExistingProtocols.size()); |
| 1251 | llvm::StructType *ProtocolListTy = llvm::StructType::get(VMContext, |
| 1252 | PtrTy, //Should be a recurisve pointer, but it's always NULL here. |
| 1253 | LongTy,//FIXME: Should be size_t |
| 1254 | ProtocolArrayTy, |
| 1255 | NULL); |
| 1256 | std::vector<llvm::Constant*> ProtocolElements; |
| 1257 | for (llvm::StringMapIterator<llvm::Constant*> iter = |
| 1258 | ExistingProtocols.begin(), endIter = ExistingProtocols.end(); |
| 1259 | iter != endIter ; iter++) { |
| 1260 | llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(iter->getValue(), |
| 1261 | PtrTy); |
| 1262 | ProtocolElements.push_back(Ptr); |
| 1263 | } |
| 1264 | llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy, |
| 1265 | ProtocolElements); |
| 1266 | ProtocolElements.clear(); |
| 1267 | ProtocolElements.push_back(NULLPtr); |
| 1268 | ProtocolElements.push_back(llvm::ConstantInt::get(LongTy, |
| 1269 | ExistingProtocols.size())); |
| 1270 | ProtocolElements.push_back(ProtocolArray); |
| 1271 | Elements.push_back(llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolListTy, |
| 1272 | ProtocolElements, ".objc_protocol_list"), PtrTy)); |
| 1273 | Categories.push_back(llvm::ConstantExpr::getBitCast( |
| 1274 | MakeGlobal(llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty, |
| 1275 | PtrTy, PtrTy, PtrTy, NULL), Elements), PtrTy)); |
| 1276 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1277 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1278 | void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1279 | std::string ClassName = OCD->getClassInterface()->getNameAsString(); |
| 1280 | std::string CategoryName = OCD->getNameAsString(); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1281 | // Collect information about instance methods |
| 1282 | llvm::SmallVector<Selector, 16> InstanceMethodSels; |
| 1283 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1284 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1285 | iter = OCD->instmeth_begin(), endIter = OCD->instmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1286 | iter != endIter ; iter++) { |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1287 | InstanceMethodSels.push_back((*iter)->getSelector()); |
| 1288 | std::string TypeStr; |
| 1289 | CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr); |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1290 | InstanceMethodTypes.push_back(MakeConstantString(TypeStr)); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | // Collect information about class methods |
| 1294 | llvm::SmallVector<Selector, 16> ClassMethodSels; |
| 1295 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1296 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1297 | iter = OCD->classmeth_begin(), endIter = OCD->classmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1298 | iter != endIter ; iter++) { |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1299 | ClassMethodSels.push_back((*iter)->getSelector()); |
| 1300 | std::string TypeStr; |
| 1301 | CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr); |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1302 | ClassMethodTypes.push_back(MakeConstantString(TypeStr)); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | // Collect the names of referenced protocols |
| 1306 | llvm::SmallVector<std::string, 16> Protocols; |
David Chisnall | ad9e06d | 2010-03-13 22:20:45 +0000 | [diff] [blame] | 1307 | const ObjCCategoryDecl *CatDecl = OCD->getCategoryDecl(); |
| 1308 | const ObjCList<ObjCProtocolDecl> &Protos = CatDecl->getReferencedProtocols(); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1309 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(), |
| 1310 | E = Protos.end(); I != E; ++I) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1311 | Protocols.push_back((*I)->getNameAsString()); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1312 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1313 | std::vector<llvm::Constant*> Elements; |
| 1314 | Elements.push_back(MakeConstantString(CategoryName)); |
| 1315 | Elements.push_back(MakeConstantString(ClassName)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1316 | // Instance method list |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1317 | Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList( |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 1318 | ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1319 | false), PtrTy)); |
| 1320 | // Class method list |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1321 | Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList( |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 1322 | ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true), |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1323 | PtrTy)); |
| 1324 | // Protocol list |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1325 | Elements.push_back(llvm::ConstantExpr::getBitCast( |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1326 | GenerateProtocolList(Protocols), PtrTy)); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1327 | Categories.push_back(llvm::ConstantExpr::getBitCast( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1328 | MakeGlobal(llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty, |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1329 | PtrTy, PtrTy, PtrTy, NULL), Elements), PtrTy)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1330 | } |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1331 | |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1332 | llvm::Constant *CGObjCGNU::GeneratePropertyList(const ObjCImplementationDecl *OID, |
| 1333 | llvm::SmallVectorImpl<Selector> &InstanceMethodSels, |
| 1334 | llvm::SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes) { |
| 1335 | ASTContext &Context = CGM.getContext(); |
| 1336 | // |
| 1337 | // Property metadata: name, attributes, isSynthesized, setter name, setter |
| 1338 | // types, getter name, getter types. |
| 1339 | llvm::StructType *PropertyMetadataTy = llvm::StructType::get(VMContext, |
| 1340 | PtrToInt8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty, |
| 1341 | PtrToInt8Ty, NULL); |
| 1342 | std::vector<llvm::Constant*> Properties; |
| 1343 | |
| 1344 | |
| 1345 | // Add all of the property methods need adding to the method list and to the |
| 1346 | // property metadata list. |
| 1347 | for (ObjCImplDecl::propimpl_iterator |
| 1348 | iter = OID->propimpl_begin(), endIter = OID->propimpl_end(); |
| 1349 | iter != endIter ; iter++) { |
| 1350 | std::vector<llvm::Constant*> Fields; |
| 1351 | ObjCPropertyDecl *property = (*iter)->getPropertyDecl(); |
David Chisnall | 42ba04a | 2010-02-26 01:11:38 +0000 | [diff] [blame] | 1352 | ObjCPropertyImplDecl *propertyImpl = *iter; |
| 1353 | bool isSynthesized = (propertyImpl->getPropertyImplementation() == |
| 1354 | ObjCPropertyImplDecl::Synthesize); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1355 | |
| 1356 | Fields.push_back(MakeConstantString(property->getNameAsString())); |
| 1357 | Fields.push_back(llvm::ConstantInt::get(Int8Ty, |
| 1358 | property->getPropertyAttributes())); |
David Chisnall | 42ba04a | 2010-02-26 01:11:38 +0000 | [diff] [blame] | 1359 | Fields.push_back(llvm::ConstantInt::get(Int8Ty, isSynthesized)); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1360 | if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) { |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1361 | std::string TypeStr; |
| 1362 | Context.getObjCEncodingForMethodDecl(getter,TypeStr); |
| 1363 | llvm::Constant *TypeEncoding = MakeConstantString(TypeStr); |
David Chisnall | 42ba04a | 2010-02-26 01:11:38 +0000 | [diff] [blame] | 1364 | if (isSynthesized) { |
| 1365 | InstanceMethodTypes.push_back(TypeEncoding); |
| 1366 | InstanceMethodSels.push_back(getter->getSelector()); |
| 1367 | } |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1368 | Fields.push_back(MakeConstantString(getter->getSelector().getAsString())); |
| 1369 | Fields.push_back(TypeEncoding); |
| 1370 | } else { |
| 1371 | Fields.push_back(NULLPtr); |
| 1372 | Fields.push_back(NULLPtr); |
| 1373 | } |
| 1374 | if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) { |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1375 | std::string TypeStr; |
| 1376 | Context.getObjCEncodingForMethodDecl(setter,TypeStr); |
| 1377 | llvm::Constant *TypeEncoding = MakeConstantString(TypeStr); |
David Chisnall | 42ba04a | 2010-02-26 01:11:38 +0000 | [diff] [blame] | 1378 | if (isSynthesized) { |
| 1379 | InstanceMethodTypes.push_back(TypeEncoding); |
| 1380 | InstanceMethodSels.push_back(setter->getSelector()); |
| 1381 | } |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1382 | Fields.push_back(MakeConstantString(setter->getSelector().getAsString())); |
| 1383 | Fields.push_back(TypeEncoding); |
| 1384 | } else { |
| 1385 | Fields.push_back(NULLPtr); |
| 1386 | Fields.push_back(NULLPtr); |
| 1387 | } |
| 1388 | Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields)); |
| 1389 | } |
| 1390 | llvm::ArrayType *PropertyArrayTy = |
| 1391 | llvm::ArrayType::get(PropertyMetadataTy, Properties.size()); |
| 1392 | llvm::Constant *PropertyArray = llvm::ConstantArray::get(PropertyArrayTy, |
| 1393 | Properties); |
| 1394 | llvm::Constant* PropertyListInitFields[] = |
| 1395 | {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray}; |
| 1396 | |
| 1397 | llvm::Constant *PropertyListInit = |
Nick Lewycky | 0d36dd2 | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 1398 | llvm::ConstantStruct::get(VMContext, PropertyListInitFields, 3, false); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1399 | return new llvm::GlobalVariable(TheModule, PropertyListInit->getType(), false, |
| 1400 | llvm::GlobalValue::InternalLinkage, PropertyListInit, |
| 1401 | ".objc_property_list"); |
| 1402 | } |
| 1403 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1404 | void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) { |
| 1405 | ASTContext &Context = CGM.getContext(); |
| 1406 | |
| 1407 | // Get the superclass name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1408 | const ObjCInterfaceDecl * SuperClassDecl = |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1409 | OID->getClassInterface()->getSuperClass(); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1410 | std::string SuperClassName; |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 1411 | if (SuperClassDecl) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1412 | SuperClassName = SuperClassDecl->getNameAsString(); |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 1413 | EmitClassRef(SuperClassName); |
| 1414 | } |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1415 | |
| 1416 | // Get the class name |
Chris Lattner | 09dc666 | 2009-04-01 02:00:48 +0000 | [diff] [blame] | 1417 | ObjCInterfaceDecl *ClassDecl = |
| 1418 | const_cast<ObjCInterfaceDecl *>(OID->getClassInterface()); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1419 | std::string ClassName = ClassDecl->getNameAsString(); |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 1420 | // Emit the symbol that is used to generate linker errors if this class is |
| 1421 | // referenced in other modules but not declared. |
Fariborz Jahanian | c51db23 | 2009-07-03 15:10:14 +0000 | [diff] [blame] | 1422 | std::string classSymbolName = "__objc_class_name_" + ClassName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1423 | if (llvm::GlobalVariable *symbol = |
Fariborz Jahanian | c51db23 | 2009-07-03 15:10:14 +0000 | [diff] [blame] | 1424 | TheModule.getGlobalVariable(classSymbolName)) { |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1425 | symbol->setInitializer(llvm::ConstantInt::get(LongTy, 0)); |
Fariborz Jahanian | c51db23 | 2009-07-03 15:10:14 +0000 | [diff] [blame] | 1426 | } else { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1427 | new llvm::GlobalVariable(TheModule, LongTy, false, |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1428 | llvm::GlobalValue::ExternalLinkage, llvm::ConstantInt::get(LongTy, 0), |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1429 | classSymbolName); |
Fariborz Jahanian | c51db23 | 2009-07-03 15:10:14 +0000 | [diff] [blame] | 1430 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1431 | |
Daniel Dunbar | 2bebbf0 | 2009-05-03 10:46:44 +0000 | [diff] [blame] | 1432 | // Get the size of instances. |
| 1433 | int instanceSize = Context.getASTObjCImplementationLayout(OID).getSize() / 8; |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1434 | |
| 1435 | // Collect information about instance variables. |
| 1436 | llvm::SmallVector<llvm::Constant*, 16> IvarNames; |
| 1437 | llvm::SmallVector<llvm::Constant*, 16> IvarTypes; |
| 1438 | llvm::SmallVector<llvm::Constant*, 16> IvarOffsets; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1439 | |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1440 | std::vector<llvm::Constant*> IvarOffsetValues; |
| 1441 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1442 | int superInstanceSize = !SuperClassDecl ? 0 : |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 1443 | Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize() / 8; |
| 1444 | // For non-fragile ivars, set the instance size to 0 - {the size of just this |
| 1445 | // class}. The runtime will then set this to the correct value on load. |
| 1446 | if (CGM.getContext().getLangOptions().ObjCNonFragileABI) { |
| 1447 | instanceSize = 0 - (instanceSize - superInstanceSize); |
| 1448 | } |
David Chisnall | 7f63cb0 | 2010-04-19 00:45:34 +0000 | [diff] [blame] | 1449 | |
| 1450 | // Collect declared and synthesized ivars. |
| 1451 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
| 1452 | CGM.getContext().ShallowCollectObjCIvars(ClassDecl, OIvars); |
| 1453 | |
| 1454 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 1455 | ObjCIvarDecl *IVD = OIvars[i]; |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1456 | // Store the name |
David Chisnall | 7f63cb0 | 2010-04-19 00:45:34 +0000 | [diff] [blame] | 1457 | IvarNames.push_back(MakeConstantString(IVD->getNameAsString())); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1458 | // Get the type encoding for this ivar |
| 1459 | std::string TypeStr; |
David Chisnall | 7f63cb0 | 2010-04-19 00:45:34 +0000 | [diff] [blame] | 1460 | Context.getObjCEncodingForType(IVD->getType(), TypeStr); |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1461 | IvarTypes.push_back(MakeConstantString(TypeStr)); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1462 | // Get the offset |
David Chisnall | d901da5 | 2010-04-19 01:37:25 +0000 | [diff] [blame] | 1463 | uint64_t BaseOffset = ComputeIvarBaseOffset(CGM, OID, IVD); |
David Chisnall | aecbf24 | 2009-11-17 19:32:15 +0000 | [diff] [blame] | 1464 | uint64_t Offset = BaseOffset; |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 1465 | if (CGM.getContext().getLangOptions().ObjCNonFragileABI) { |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1466 | Offset = BaseOffset - superInstanceSize; |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 1467 | } |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1468 | IvarOffsets.push_back( |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1469 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Offset)); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1470 | IvarOffsetValues.push_back(new llvm::GlobalVariable(TheModule, IntTy, |
| 1471 | false, llvm::GlobalValue::ExternalLinkage, |
| 1472 | llvm::ConstantInt::get(IntTy, BaseOffset), |
| 1473 | "__objc_ivar_offset_value_" + ClassName +"." + |
David Chisnall | 7f63cb0 | 2010-04-19 00:45:34 +0000 | [diff] [blame] | 1474 | IVD->getNameAsString())); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1475 | } |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1476 | llvm::Constant *IvarOffsetArrayInit = |
| 1477 | llvm::ConstantArray::get(llvm::ArrayType::get(PtrToIntTy, |
| 1478 | IvarOffsetValues.size()), IvarOffsetValues); |
| 1479 | llvm::GlobalVariable *IvarOffsetArray = new llvm::GlobalVariable(TheModule, |
| 1480 | IvarOffsetArrayInit->getType(), false, |
| 1481 | llvm::GlobalValue::InternalLinkage, IvarOffsetArrayInit, |
| 1482 | ".ivar.offsets"); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1483 | |
| 1484 | // Collect information about instance methods |
| 1485 | llvm::SmallVector<Selector, 16> InstanceMethodSels; |
| 1486 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1488 | iter = OID->instmeth_begin(), endIter = OID->instmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1489 | iter != endIter ; iter++) { |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1490 | InstanceMethodSels.push_back((*iter)->getSelector()); |
| 1491 | std::string TypeStr; |
| 1492 | Context.getObjCEncodingForMethodDecl((*iter),TypeStr); |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1493 | InstanceMethodTypes.push_back(MakeConstantString(TypeStr)); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1494 | } |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1495 | |
| 1496 | llvm::Constant *Properties = GeneratePropertyList(OID, InstanceMethodSels, |
| 1497 | InstanceMethodTypes); |
| 1498 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1499 | |
| 1500 | // Collect information about class methods |
| 1501 | llvm::SmallVector<Selector, 16> ClassMethodSels; |
| 1502 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1503 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1504 | iter = OID->classmeth_begin(), endIter = OID->classmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1505 | iter != endIter ; iter++) { |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1506 | ClassMethodSels.push_back((*iter)->getSelector()); |
| 1507 | std::string TypeStr; |
| 1508 | Context.getObjCEncodingForMethodDecl((*iter),TypeStr); |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1509 | ClassMethodTypes.push_back(MakeConstantString(TypeStr)); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1510 | } |
| 1511 | // Collect the names of referenced protocols |
| 1512 | llvm::SmallVector<std::string, 16> Protocols; |
| 1513 | const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols(); |
| 1514 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(), |
| 1515 | E = Protos.end(); I != E; ++I) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1516 | Protocols.push_back((*I)->getNameAsString()); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1517 | |
| 1518 | |
| 1519 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1520 | // Get the superclass pointer. |
| 1521 | llvm::Constant *SuperClass; |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1522 | if (!SuperClassName.empty()) { |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1523 | SuperClass = MakeConstantString(SuperClassName, ".super_class_name"); |
| 1524 | } else { |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 1525 | SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1526 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1527 | // Empty vector used to construct empty method lists |
| 1528 | llvm::SmallVector<llvm::Constant*, 1> empty; |
| 1529 | // Generate the method and instance variable lists |
| 1530 | llvm::Constant *MethodList = GenerateMethodList(ClassName, "", |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 1531 | InstanceMethodSels, InstanceMethodTypes, false); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1532 | llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "", |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 1533 | ClassMethodSels, ClassMethodTypes, true); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1534 | llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes, |
| 1535 | IvarOffsets); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1536 | // Irrespective of whether we are compiling for a fragile or non-fragile ABI, |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1537 | // we emit a symbol containing the offset for each ivar in the class. This |
| 1538 | // allows code compiled for the non-Fragile ABI to inherit from code compiled |
| 1539 | // for the legacy ABI, without causing problems. The converse is also |
| 1540 | // possible, but causes all ivar accesses to be fragile. |
| 1541 | int i = 0; |
| 1542 | // Offset pointer for getting at the correct field in the ivar list when |
| 1543 | // setting up the alias. These are: The base address for the global, the |
| 1544 | // ivar array (second field), the ivar in this list (set for each ivar), and |
| 1545 | // the offset (third field in ivar structure) |
| 1546 | const llvm::Type *IndexTy = llvm::Type::getInt32Ty(VMContext); |
| 1547 | llvm::Constant *offsetPointerIndexes[] = {Zeros[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1548 | llvm::ConstantInt::get(IndexTy, 1), 0, |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1549 | llvm::ConstantInt::get(IndexTy, 2) }; |
| 1550 | |
| 1551 | for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(), |
| 1552 | endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) { |
| 1553 | const std::string Name = "__objc_ivar_offset_" + ClassName + '.' |
| 1554 | +(*iter)->getNameAsString(); |
| 1555 | offsetPointerIndexes[2] = llvm::ConstantInt::get(IndexTy, i++); |
| 1556 | // Get the correct ivar field |
| 1557 | llvm::Constant *offsetValue = llvm::ConstantExpr::getGetElementPtr( |
| 1558 | IvarList, offsetPointerIndexes, 4); |
| 1559 | // Get the existing alias, if one exists. |
| 1560 | llvm::GlobalVariable *offset = TheModule.getNamedGlobal(Name); |
| 1561 | if (offset) { |
| 1562 | offset->setInitializer(offsetValue); |
| 1563 | // If this is the real definition, change its linkage type so that |
| 1564 | // different modules will use this one, rather than their private |
| 1565 | // copy. |
| 1566 | offset->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 1567 | } else { |
| 1568 | // Add a new alias if there isn't one already. |
| 1569 | offset = new llvm::GlobalVariable(TheModule, offsetValue->getType(), |
| 1570 | false, llvm::GlobalValue::ExternalLinkage, offsetValue, Name); |
| 1571 | } |
| 1572 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1573 | //Generate metaclass for class methods |
| 1574 | llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr, |
David Chisnall | 1804463 | 2009-11-16 19:05:54 +0000 | [diff] [blame] | 1575 | NULLPtr, 0x12L, ClassName.c_str(), 0, Zeros[0], GenerateIvarList( |
David Chisnall | 8c757f9 | 2010-04-28 14:29:56 +0000 | [diff] [blame] | 1576 | empty, empty, empty), ClassMethodList, NULLPtr, NULLPtr, NULLPtr, true); |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1577 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1578 | // Generate the class structure |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1579 | llvm::Constant *ClassStruct = |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1580 | GenerateClassStructure(MetaClassStruct, SuperClass, 0x11L, |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1581 | ClassName.c_str(), 0, |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1582 | llvm::ConstantInt::get(LongTy, instanceSize), IvarList, |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1583 | MethodList, GenerateProtocolList(Protocols), IvarOffsetArray, |
| 1584 | Properties); |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1585 | |
| 1586 | // Resolve the class aliases, if they exist. |
| 1587 | if (ClassPtrAlias) { |
| 1588 | ClassPtrAlias->setAliasee( |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1589 | llvm::ConstantExpr::getBitCast(ClassStruct, IdTy)); |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1590 | ClassPtrAlias = 0; |
| 1591 | } |
| 1592 | if (MetaClassPtrAlias) { |
| 1593 | MetaClassPtrAlias->setAliasee( |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1594 | llvm::ConstantExpr::getBitCast(MetaClassStruct, IdTy)); |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1595 | MetaClassPtrAlias = 0; |
| 1596 | } |
| 1597 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1598 | // Add class structure to list to be added to the symtab later |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1599 | ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1600 | Classes.push_back(ClassStruct); |
| 1601 | } |
| 1602 | |
Fariborz Jahanian | c38e9af | 2009-06-23 21:47:46 +0000 | [diff] [blame] | 1603 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1604 | llvm::Function *CGObjCGNU::ModuleInitFunction() { |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1605 | // Only emit an ObjC load function if no Objective-C stuff has been called |
| 1606 | if (Classes.empty() && Categories.empty() && ConstantStrings.empty() && |
| 1607 | ExistingProtocols.empty() && TypedSelectors.empty() && |
Anton Korobeynikov | 5f58b91 | 2008-06-01 15:14:46 +0000 | [diff] [blame] | 1608 | UntypedSelectors.empty()) |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1609 | return NULL; |
Eli Friedman | 1b8956e | 2008-06-01 16:00:02 +0000 | [diff] [blame] | 1610 | |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1611 | // Add all referenced protocols to a category. |
| 1612 | GenerateProtocolHolderCategory(); |
| 1613 | |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 1614 | const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>( |
| 1615 | SelectorTy->getElementType()); |
| 1616 | const llvm::Type *SelStructPtrTy = SelectorTy; |
| 1617 | bool isSelOpaque = false; |
| 1618 | if (SelStructTy == 0) { |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1619 | SelStructTy = llvm::StructType::get(VMContext, PtrToInt8Ty, |
| 1620 | PtrToInt8Ty, NULL); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1621 | SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy); |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 1622 | isSelOpaque = true; |
| 1623 | } |
| 1624 | |
Eli Friedman | 1b8956e | 2008-06-01 16:00:02 +0000 | [diff] [blame] | 1625 | // 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] | 1626 | TheModule.addTypeName(".objc_selector", SelStructPtrTy); |
Eli Friedman | 1b8956e | 2008-06-01 16:00:02 +0000 | [diff] [blame] | 1627 | TheModule.addTypeName(".objc_id", IdTy); |
| 1628 | TheModule.addTypeName(".objc_imp", IMPTy); |
| 1629 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1630 | std::vector<llvm::Constant*> Elements; |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 1631 | llvm::Constant *Statics = NULLPtr; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1632 | // Generate statics list: |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 1633 | if (ConstantStrings.size()) { |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1634 | llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty, |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 1635 | ConstantStrings.size() + 1); |
| 1636 | ConstantStrings.push_back(NULLPtr); |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1637 | |
Daniel Dunbar | 1b09695 | 2009-11-29 02:38:47 +0000 | [diff] [blame] | 1638 | llvm::StringRef StringClass = CGM.getLangOptions().ObjCConstantStringClass; |
| 1639 | if (StringClass.empty()) StringClass = "NXConstantString"; |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1640 | Elements.push_back(MakeConstantString(StringClass, |
| 1641 | ".objc_static_class_name")); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 1642 | Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 1643 | ConstantStrings)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1644 | llvm::StructType *StaticsListTy = |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1645 | llvm::StructType::get(VMContext, PtrToInt8Ty, StaticsArrayTy, NULL); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1646 | llvm::Type *StaticsListPtrTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1647 | llvm::PointerType::getUnqual(StaticsListTy); |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 1648 | Statics = MakeGlobal(StaticsListTy, Elements, ".objc_statics"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1649 | llvm::ArrayType *StaticsListArrayTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1650 | llvm::ArrayType::get(StaticsListPtrTy, 2); |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 1651 | Elements.clear(); |
| 1652 | Elements.push_back(Statics); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1653 | Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy)); |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 1654 | Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr"); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1655 | Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy); |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 1656 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1657 | // Array of classes, categories, and constant objects |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1658 | llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1659 | Classes.size() + Categories.size() + 2); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1660 | llvm::StructType *SymTabTy = llvm::StructType::get(VMContext, |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1661 | LongTy, SelStructPtrTy, |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1662 | llvm::Type::getInt16Ty(VMContext), |
| 1663 | llvm::Type::getInt16Ty(VMContext), |
Chris Lattner | 630404b | 2008-06-26 04:10:42 +0000 | [diff] [blame] | 1664 | ClassListTy, NULL); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1665 | |
| 1666 | Elements.clear(); |
| 1667 | // Pointer to an array of selectors used in this module. |
| 1668 | std::vector<llvm::Constant*> Selectors; |
| 1669 | for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator |
| 1670 | iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end(); |
| 1671 | iter != iterEnd ; ++iter) { |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1672 | Elements.push_back(ExportUniqueString(iter->first.first, ".objc_sel_name")); |
David Chisnall | a7c5b08 | 2009-09-14 19:04:10 +0000 | [diff] [blame] | 1673 | Elements.push_back(MakeConstantString(iter->first.second, |
Chris Lattner | 630404b | 2008-06-26 04:10:42 +0000 | [diff] [blame] | 1674 | ".objc_sel_types")); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1675 | Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1676 | Elements.clear(); |
| 1677 | } |
| 1678 | for (llvm::StringMap<llvm::GlobalAlias*>::iterator |
| 1679 | iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end(); |
Chris Lattner | 630404b | 2008-06-26 04:10:42 +0000 | [diff] [blame] | 1680 | iter != iterEnd; ++iter) { |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1681 | Elements.push_back( |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1682 | ExportUniqueString(iter->getKeyData(), ".objc_sel_name")); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1683 | Elements.push_back(NULLPtr); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1684 | Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1685 | Elements.clear(); |
| 1686 | } |
| 1687 | Elements.push_back(NULLPtr); |
| 1688 | Elements.push_back(NULLPtr); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1689 | Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1690 | Elements.clear(); |
| 1691 | // Number of static selectors |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1692 | Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() )); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1693 | llvm::Constant *SelectorList = MakeGlobal( |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1694 | llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1695 | ".objc_selector_list"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1696 | Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList, |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 1697 | SelStructPtrTy)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1698 | |
| 1699 | // Now that all of the static selectors exist, create pointers to them. |
| 1700 | int index = 0; |
| 1701 | for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator |
| 1702 | iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end(); |
| 1703 | iter != iterEnd; ++iter) { |
| 1704 | llvm::Constant *Idxs[] = {Zeros[0], |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1705 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), index++), Zeros[0]}; |
Daniel Dunbar | 6d5a1c2 | 2010-02-03 20:11:42 +0000 | [diff] [blame] | 1706 | llvm::Constant *SelPtr = new llvm::GlobalVariable(TheModule, SelStructPtrTy, |
David Chisnall | 22b8827 | 2010-05-09 01:01:43 +0000 | [diff] [blame] | 1707 | true, llvm::GlobalValue::LinkOnceODRLinkage, |
| 1708 | llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2), |
| 1709 | MangleSelectorTypes(".objc_sel_ptr"+iter->first.first+"."+ |
| 1710 | iter->first.second)); |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 1711 | // If selectors are defined as an opaque type, cast the pointer to this |
| 1712 | // type. |
| 1713 | if (isSelOpaque) { |
Daniel Dunbar | 6d5a1c2 | 2010-02-03 20:11:42 +0000 | [diff] [blame] | 1714 | SelPtr = llvm::ConstantExpr::getBitCast(SelPtr, |
| 1715 | llvm::PointerType::getUnqual(SelectorTy)); |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 1716 | } |
David Chisnall | 87935a8 | 2010-05-08 20:58:05 +0000 | [diff] [blame] | 1717 | (*iter).second->replaceAllUsesWith(SelPtr); |
| 1718 | (*iter).second->eraseFromParent(); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1719 | } |
| 1720 | for (llvm::StringMap<llvm::GlobalAlias*>::iterator |
| 1721 | iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end(); |
| 1722 | iter != iterEnd; iter++) { |
| 1723 | llvm::Constant *Idxs[] = {Zeros[0], |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1724 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), index++), Zeros[0]}; |
David Chisnall | 87935a8 | 2010-05-08 20:58:05 +0000 | [diff] [blame] | 1725 | llvm::Constant *SelPtr = new llvm::GlobalVariable(TheModule, SelStructPtrTy, |
David Chisnall | 22b8827 | 2010-05-09 01:01:43 +0000 | [diff] [blame] | 1726 | true, llvm::GlobalValue::LinkOnceODRLinkage, |
| 1727 | llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2), |
| 1728 | MangleSelectorTypes(std::string(".objc_sel_ptr")+iter->getKey().str())); |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 1729 | // If selectors are defined as an opaque type, cast the pointer to this |
| 1730 | // type. |
| 1731 | if (isSelOpaque) { |
Daniel Dunbar | 6d5a1c2 | 2010-02-03 20:11:42 +0000 | [diff] [blame] | 1732 | SelPtr = llvm::ConstantExpr::getBitCast(SelPtr, |
| 1733 | llvm::PointerType::getUnqual(SelectorTy)); |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 1734 | } |
David Chisnall | 87935a8 | 2010-05-08 20:58:05 +0000 | [diff] [blame] | 1735 | (*iter).second->replaceAllUsesWith(SelPtr); |
| 1736 | (*iter).second->eraseFromParent(); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1737 | } |
| 1738 | // Number of classes defined. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1739 | Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext), |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1740 | Classes.size())); |
| 1741 | // Number of categories defined |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1742 | Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext), |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1743 | Categories.size())); |
| 1744 | // Create an array of classes, then categories, then static object instances |
| 1745 | Classes.insert(Classes.end(), Categories.begin(), Categories.end()); |
| 1746 | // NULL-terminated list of static object instances (mainly constant strings) |
| 1747 | Classes.push_back(Statics); |
| 1748 | Classes.push_back(NULLPtr); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 1749 | llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1750 | Elements.push_back(ClassList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1751 | // Construct the symbol table |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1752 | llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements); |
| 1753 | |
| 1754 | // The symbol table is contained in a module which has some version-checking |
| 1755 | // constants |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1756 | llvm::StructType * ModuleTy = llvm::StructType::get(VMContext, LongTy, LongTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1757 | PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1758 | Elements.clear(); |
| 1759 | // Runtime version used for compatibility checking. |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 1760 | if (CGM.getContext().getLangOptions().ObjCNonFragileABI) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1761 | Elements.push_back(llvm::ConstantInt::get(LongTy, |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1762 | NonFragileRuntimeVersion)); |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 1763 | } else { |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1764 | Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion)); |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 1765 | } |
Fariborz Jahanian | 91a0b51 | 2009-04-01 19:49:42 +0000 | [diff] [blame] | 1766 | // sizeof(ModuleTy) |
Benjamin Kramer | 74a8bbf | 2010-02-09 19:31:24 +0000 | [diff] [blame] | 1767 | llvm::TargetData td(&TheModule); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1768 | Elements.push_back(llvm::ConstantInt::get(LongTy, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1769 | td.getTypeSizeInBits(ModuleTy)/8)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1770 | //FIXME: Should be the path to the file where this module was declared |
| 1771 | Elements.push_back(NULLPtr); |
| 1772 | Elements.push_back(SymTab); |
| 1773 | llvm::Value *Module = MakeGlobal(ModuleTy, Elements); |
| 1774 | |
| 1775 | // Create the load function calling the runtime entry point with the module |
| 1776 | // structure |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1777 | llvm::Function * LoadFunction = llvm::Function::Create( |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1778 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false), |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1779 | llvm::GlobalValue::InternalLinkage, ".objc_load_function", |
| 1780 | &TheModule); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1781 | llvm::BasicBlock *EntryBB = |
| 1782 | llvm::BasicBlock::Create(VMContext, "entry", LoadFunction); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1783 | CGBuilderTy Builder(VMContext); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1784 | Builder.SetInsertPoint(EntryBB); |
Fariborz Jahanian | 26c8294 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 1785 | |
| 1786 | std::vector<const llvm::Type*> Params(1, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1787 | llvm::PointerType::getUnqual(ModuleTy)); |
| 1788 | llvm::Value *Register = CGM.CreateRuntimeFunction(llvm::FunctionType::get( |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1789 | llvm::Type::getVoidTy(VMContext), Params, true), "__objc_exec_class"); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1790 | Builder.CreateCall(Register, Module); |
| 1791 | Builder.CreateRetVoid(); |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 1792 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1793 | return LoadFunction; |
| 1794 | } |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1795 | |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 1796 | llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1797 | const ObjCContainerDecl *CD) { |
| 1798 | const ObjCCategoryImplDecl *OCD = |
Steve Naroff | 3e0a540 | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 1799 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext()); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1800 | std::string CategoryName = OCD ? OCD->getNameAsString() : ""; |
David Chisnall | 4c8c8e9 | 2010-03-20 19:53:29 +0000 | [diff] [blame] | 1801 | std::string ClassName = CD->getName(); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1802 | std::string MethodName = OMD->getSelector().getAsString(); |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1803 | bool isClassMethod = !OMD->isInstanceMethod(); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1804 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1805 | CodeGenTypes &Types = CGM.getTypes(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1806 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1807 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1808 | std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName, |
| 1809 | MethodName, isClassMethod); |
| 1810 | |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1811 | llvm::Function *Method |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1812 | = llvm::Function::Create(MethodTy, |
| 1813 | llvm::GlobalValue::InternalLinkage, |
| 1814 | FunctionName, |
| 1815 | &TheModule); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 1816 | return Method; |
| 1817 | } |
| 1818 | |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 1819 | llvm::Function *CGObjCGNU::GetPropertyGetFunction() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1820 | std::vector<const llvm::Type*> Params; |
| 1821 | const llvm::Type *BoolTy = |
| 1822 | CGM.getTypes().ConvertType(CGM.getContext().BoolTy); |
| 1823 | Params.push_back(IdTy); |
| 1824 | Params.push_back(SelectorTy); |
David Chisnall | 7f63cb0 | 2010-04-19 00:45:34 +0000 | [diff] [blame] | 1825 | Params.push_back(IntTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1826 | Params.push_back(BoolTy); |
David Chisnall | 7f63cb0 | 2010-04-19 00:45:34 +0000 | [diff] [blame] | 1827 | // void objc_getProperty (id, SEL, int, bool) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1828 | const llvm::FunctionType *FTy = |
| 1829 | llvm::FunctionType::get(IdTy, Params, false); |
| 1830 | return cast<llvm::Function>(CGM.CreateRuntimeFunction(FTy, |
| 1831 | "objc_getProperty")); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | llvm::Function *CGObjCGNU::GetPropertySetFunction() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1835 | std::vector<const llvm::Type*> Params; |
| 1836 | const llvm::Type *BoolTy = |
| 1837 | CGM.getTypes().ConvertType(CGM.getContext().BoolTy); |
| 1838 | Params.push_back(IdTy); |
| 1839 | Params.push_back(SelectorTy); |
David Chisnall | 7f63cb0 | 2010-04-19 00:45:34 +0000 | [diff] [blame] | 1840 | Params.push_back(IntTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1841 | Params.push_back(IdTy); |
| 1842 | Params.push_back(BoolTy); |
| 1843 | Params.push_back(BoolTy); |
David Chisnall | 7f63cb0 | 2010-04-19 00:45:34 +0000 | [diff] [blame] | 1844 | // void objc_setProperty (id, SEL, int, id, bool, bool) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1845 | const llvm::FunctionType *FTy = |
| 1846 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false); |
| 1847 | return cast<llvm::Function>(CGM.CreateRuntimeFunction(FTy, |
| 1848 | "objc_setProperty")); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 1851 | // FIXME. Implement this. |
| 1852 | llvm::Function *CGObjCGNU::GetCopyStructFunction() { |
| 1853 | return 0; |
| 1854 | } |
| 1855 | |
Daniel Dunbar | 309a436 | 2009-07-24 07:40:24 +0000 | [diff] [blame] | 1856 | llvm::Constant *CGObjCGNU::EnumerationMutationFunction() { |
| 1857 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 1858 | ASTContext &Ctx = CGM.getContext(); |
| 1859 | // void objc_enumerationMutation (id) |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 1860 | llvm::SmallVector<CanQualType,1> Params; |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 1861 | Params.push_back(ASTIdTy); |
Daniel Dunbar | 309a436 | 2009-07-24 07:40:24 +0000 | [diff] [blame] | 1862 | const llvm::FunctionType *FTy = |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1863 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1864 | FunctionType::ExtInfo()), false); |
Daniel Dunbar | 309a436 | 2009-07-24 07:40:24 +0000 | [diff] [blame] | 1865 | return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation"); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
John McCall | 740e807 | 2010-07-21 00:41:47 +0000 | [diff] [blame] | 1868 | namespace { |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1869 | struct CallSyncExit : EHScopeStack::Cleanup { |
John McCall | 740e807 | 2010-07-21 00:41:47 +0000 | [diff] [blame] | 1870 | llvm::Value *SyncExitFn; |
| 1871 | llvm::Value *SyncArg; |
| 1872 | CallSyncExit(llvm::Value *SyncExitFn, llvm::Value *SyncArg) |
| 1873 | : SyncExitFn(SyncExitFn), SyncArg(SyncArg) {} |
| 1874 | |
| 1875 | void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) { |
| 1876 | CGF.Builder.CreateCall(SyncExitFn, SyncArg)->setDoesNotThrow(); |
| 1877 | } |
| 1878 | }; |
| 1879 | } |
| 1880 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1881 | void CGObjCGNU::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1882 | const ObjCAtSynchronizedStmt &S) { |
| 1883 | std::vector<const llvm::Type*> Args(1, IdTy); |
| 1884 | llvm::FunctionType *FTy = |
| 1885 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false); |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1886 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1887 | // Evaluate the lock operand. This should dominate the cleanup. |
| 1888 | llvm::Value *SyncArg = |
| 1889 | CGF.EmitScalarExpr(S.getSynchExpr()); |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1890 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1891 | // Acquire the lock. |
| 1892 | llvm::Value *SyncEnter = CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 1893 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, IdTy); |
| 1894 | CGF.Builder.CreateCall(SyncEnter, SyncArg); |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1895 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1896 | // Register an all-paths cleanup to release the lock. |
John McCall | 740e807 | 2010-07-21 00:41:47 +0000 | [diff] [blame] | 1897 | llvm::Value *SyncExit = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
John McCall | 1f0fca5 | 2010-07-21 07:22:38 +0000 | [diff] [blame] | 1898 | CGF.EHStack.pushCleanup<CallSyncExit>(NormalAndEHCleanup, SyncExit, SyncArg); |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1899 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1900 | // Emit the body of the statement. |
| 1901 | CGF.EmitStmt(S.getSynchBody()); |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1902 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1903 | // Pop the lock-release cleanup. |
| 1904 | CGF.PopCleanupBlock(); |
| 1905 | } |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1906 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1907 | namespace { |
| 1908 | struct CatchHandler { |
| 1909 | const VarDecl *Variable; |
| 1910 | const Stmt *Body; |
| 1911 | llvm::BasicBlock *Block; |
| 1912 | llvm::Value *TypeInfo; |
| 1913 | }; |
| 1914 | } |
David Chisnall | 0faa516 | 2009-12-24 02:26:34 +0000 | [diff] [blame] | 1915 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1916 | void CGObjCGNU::EmitTryStmt(CodeGen::CodeGenFunction &CGF, |
| 1917 | const ObjCAtTryStmt &S) { |
| 1918 | // Unlike the Apple non-fragile runtimes, which also uses |
| 1919 | // unwind-based zero cost exceptions, the GNU Objective C runtime's |
| 1920 | // EH support isn't a veneer over C++ EH. Instead, exception |
| 1921 | // objects are created by __objc_exception_throw and destroyed by |
| 1922 | // the personality function; this avoids the need for bracketing |
| 1923 | // catch handlers with calls to __blah_begin_catch/__blah_end_catch |
| 1924 | // (or even _Unwind_DeleteException), but probably doesn't |
| 1925 | // interoperate very well with foreign exceptions. |
| 1926 | |
| 1927 | // Jump destination for falling out of catch bodies. |
| 1928 | CodeGenFunction::JumpDest Cont; |
| 1929 | if (S.getNumCatchStmts()) |
| 1930 | Cont = CGF.getJumpDestInCurrentScope("eh.cont"); |
| 1931 | |
| 1932 | // We handle @finally statements by pushing them as a cleanup |
| 1933 | // before entering the catch. |
| 1934 | CodeGenFunction::FinallyInfo FinallyInfo; |
| 1935 | if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt()) { |
| 1936 | std::vector<const llvm::Type*> Args(1, IdTy); |
| 1937 | llvm::FunctionType *FTy = |
| 1938 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false); |
| 1939 | llvm::Constant *Rethrow = |
| 1940 | CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
| 1941 | |
| 1942 | FinallyInfo = CGF.EnterFinallyBlock(Finally->getFinallyBody(), 0, 0, |
| 1943 | Rethrow); |
David Chisnall | 0faa516 | 2009-12-24 02:26:34 +0000 | [diff] [blame] | 1944 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1945 | |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1946 | llvm::SmallVector<CatchHandler, 8> Handlers; |
| 1947 | |
| 1948 | // Enter the catch, if there is one. |
| 1949 | if (S.getNumCatchStmts()) { |
| 1950 | for (unsigned I = 0, N = S.getNumCatchStmts(); I != N; ++I) { |
| 1951 | const ObjCAtCatchStmt *CatchStmt = S.getCatchStmt(I); |
| 1952 | const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl(); |
| 1953 | |
| 1954 | Handlers.push_back(CatchHandler()); |
| 1955 | CatchHandler &Handler = Handlers.back(); |
| 1956 | Handler.Variable = CatchDecl; |
| 1957 | Handler.Body = CatchStmt->getCatchBody(); |
| 1958 | Handler.Block = CGF.createBasicBlock("catch"); |
| 1959 | |
| 1960 | // @catch() and @catch(id) both catch any ObjC exception. |
| 1961 | // Treat them as catch-alls. |
| 1962 | // FIXME: this is what this code was doing before, but should 'id' |
| 1963 | // really be catching foreign exceptions? |
| 1964 | if (!CatchDecl |
| 1965 | || CatchDecl->getType()->isObjCIdType() |
| 1966 | || CatchDecl->getType()->isObjCQualifiedIdType()) { |
| 1967 | |
| 1968 | Handler.TypeInfo = 0; // catch-all |
| 1969 | |
| 1970 | // Don't consider any other catches. |
| 1971 | break; |
| 1972 | } |
| 1973 | |
| 1974 | // All other types should be Objective-C interface pointer types. |
| 1975 | const ObjCObjectPointerType *OPT = |
| 1976 | CatchDecl->getType()->getAs<ObjCObjectPointerType>(); |
| 1977 | assert(OPT && "Invalid @catch type."); |
| 1978 | const ObjCInterfaceDecl *IDecl = |
| 1979 | OPT->getObjectType()->getInterface(); |
| 1980 | assert(IDecl && "Invalid @catch type."); |
| 1981 | Handler.TypeInfo = MakeConstantString(IDecl->getNameAsString()); |
| 1982 | } |
| 1983 | |
| 1984 | EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size()); |
| 1985 | for (unsigned I = 0, E = Handlers.size(); I != E; ++I) |
| 1986 | Catch->setHandler(I, Handlers[I].TypeInfo, Handlers[I].Block); |
| 1987 | } |
| 1988 | |
| 1989 | // Emit the try body. |
| 1990 | CGF.EmitStmt(S.getTryBody()); |
| 1991 | |
| 1992 | // Leave the try. |
| 1993 | if (S.getNumCatchStmts()) |
| 1994 | CGF.EHStack.popCatch(); |
| 1995 | |
| 1996 | // Remember where we were. |
| 1997 | CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP(); |
| 1998 | |
| 1999 | // Emit the handlers. |
| 2000 | for (unsigned I = 0, E = Handlers.size(); I != E; ++I) { |
| 2001 | CatchHandler &Handler = Handlers[I]; |
| 2002 | CGF.EmitBlock(Handler.Block); |
| 2003 | |
| 2004 | llvm::Value *Exn = CGF.Builder.CreateLoad(CGF.getExceptionSlot()); |
| 2005 | |
| 2006 | // Bind the catch parameter if it exists. |
| 2007 | if (const VarDecl *CatchParam = Handler.Variable) { |
| 2008 | const llvm::Type *CatchType = CGF.ConvertType(CatchParam->getType()); |
| 2009 | Exn = CGF.Builder.CreateBitCast(Exn, CatchType); |
| 2010 | |
| 2011 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
| 2012 | CGF.Builder.CreateStore(Exn, CGF.GetAddrOfLocalVar(CatchParam)); |
| 2013 | } |
| 2014 | |
| 2015 | CGF.ObjCEHValueStack.push_back(Exn); |
| 2016 | CGF.EmitStmt(Handler.Body); |
| 2017 | CGF.ObjCEHValueStack.pop_back(); |
| 2018 | |
| 2019 | CGF.EmitBranchThroughCleanup(Cont); |
| 2020 | } |
| 2021 | |
| 2022 | // Go back to the try-statement fallthrough. |
| 2023 | CGF.Builder.restoreIP(SavedIP); |
| 2024 | |
| 2025 | // Pop out of the finally. |
| 2026 | if (S.getFinallyStmt()) |
| 2027 | CGF.ExitFinallyBlock(FinallyInfo); |
| 2028 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 2029 | if (Cont.isValid()) { |
| 2030 | if (Cont.getBlock()->use_empty()) |
| 2031 | delete Cont.getBlock(); |
| 2032 | else |
| 2033 | CGF.EmitBlock(Cont.getBlock()); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2034 | } |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2035 | } |
| 2036 | |
| 2037 | void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2038 | const ObjCAtThrowStmt &S) { |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 2039 | llvm::Value *ExceptionAsObject; |
| 2040 | |
| 2041 | std::vector<const llvm::Type*> Args(1, IdTy); |
| 2042 | llvm::FunctionType *FTy = |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2043 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2044 | llvm::Value *ThrowFn = |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 2045 | CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2046 | |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 2047 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 2048 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
Fariborz Jahanian | 1e64a95 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 2049 | ExceptionAsObject = Exception; |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 2050 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2051 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 2052 | "Unexpected rethrow outside @catch block."); |
| 2053 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
| 2054 | } |
Fariborz Jahanian | 1e64a95 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 2055 | ExceptionAsObject = |
| 2056 | CGF.Builder.CreateBitCast(ExceptionAsObject, IdTy, "tmp"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2057 | |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 2058 | // Note: This may have to be an invoke, if we want to support constructs like: |
| 2059 | // @try { |
| 2060 | // @throw(obj); |
| 2061 | // } |
| 2062 | // @catch(id) ... |
| 2063 | // |
| 2064 | // This is effectively turning @throw into an incredibly-expensive goto, but |
| 2065 | // it may happen as a result of inlining followed by missed optimizations, or |
| 2066 | // as a result of stupidity. |
| 2067 | llvm::BasicBlock *UnwindBB = CGF.getInvokeDest(); |
| 2068 | if (!UnwindBB) { |
| 2069 | CGF.Builder.CreateCall(ThrowFn, ExceptionAsObject); |
| 2070 | CGF.Builder.CreateUnreachable(); |
| 2071 | } else { |
| 2072 | CGF.Builder.CreateInvoke(ThrowFn, UnwindBB, UnwindBB, &ExceptionAsObject, |
| 2073 | &ExceptionAsObject+1); |
| 2074 | } |
| 2075 | // Clear the insertion point to indicate we are in unreachable code. |
| 2076 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2079 | llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2080 | llvm::Value *AddrWeakObj) { |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2081 | CGBuilderTy B = CGF.Builder; |
| 2082 | AddrWeakObj = EnforceType(B, AddrWeakObj, IdTy); |
| 2083 | return B.CreateCall(WeakReadFn, AddrWeakObj); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2086 | void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2087 | llvm::Value *src, llvm::Value *dst) { |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2088 | CGBuilderTy B = CGF.Builder; |
| 2089 | src = EnforceType(B, src, IdTy); |
| 2090 | dst = EnforceType(B, dst, PtrToIdTy); |
| 2091 | B.CreateCall2(WeakAssignFn, src, dst); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2094 | void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 2095 | llvm::Value *src, llvm::Value *dst, |
| 2096 | bool threadlocal) { |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2097 | CGBuilderTy B = CGF.Builder; |
| 2098 | src = EnforceType(B, src, IdTy); |
| 2099 | dst = EnforceType(B, dst, PtrToIdTy); |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 2100 | if (!threadlocal) |
| 2101 | B.CreateCall2(GlobalAssignFn, src, dst); |
| 2102 | else |
| 2103 | // FIXME. Add threadloca assign API |
| 2104 | assert(false && "EmitObjCGlobalAssign - Threal Local API NYI"); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2105 | } |
| 2106 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2107 | void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 2108 | llvm::Value *src, llvm::Value *dst, |
| 2109 | llvm::Value *ivarOffset) { |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2110 | CGBuilderTy B = CGF.Builder; |
| 2111 | src = EnforceType(B, src, IdTy); |
| 2112 | dst = EnforceType(B, dst, PtrToIdTy); |
| 2113 | B.CreateCall3(IvarAssignFn, src, dst, ivarOffset); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2116 | void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2117 | llvm::Value *src, llvm::Value *dst) { |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2118 | CGBuilderTy B = CGF.Builder; |
| 2119 | src = EnforceType(B, src, IdTy); |
| 2120 | dst = EnforceType(B, dst, PtrToIdTy); |
| 2121 | B.CreateCall2(StrongCastAssignFn, src, dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2122 | } |
| 2123 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2124 | void CGObjCGNU::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2125 | llvm::Value *DestPtr, |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2126 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 2127 | llvm::Value *Size) { |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2128 | CGBuilderTy B = CGF.Builder; |
| 2129 | DestPtr = EnforceType(B, DestPtr, IdTy); |
| 2130 | SrcPtr = EnforceType(B, SrcPtr, PtrToIdTy); |
| 2131 | |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 2132 | B.CreateCall3(MemMoveFn, DestPtr, SrcPtr, Size); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2135 | llvm::GlobalVariable *CGObjCGNU::ObjCIvarOffsetVariable( |
| 2136 | const ObjCInterfaceDecl *ID, |
| 2137 | const ObjCIvarDecl *Ivar) { |
| 2138 | const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString() |
| 2139 | + '.' + Ivar->getNameAsString(); |
| 2140 | // Emit the variable and initialize it with what we think the correct value |
| 2141 | // is. This allows code compiled with non-fragile ivars to work correctly |
| 2142 | // when linked against code which isn't (most of the time). |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2143 | llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name); |
| 2144 | if (!IvarOffsetPointer) { |
David Chisnall | d901da5 | 2010-04-19 01:37:25 +0000 | [diff] [blame] | 2145 | uint64_t Offset; |
| 2146 | if (ObjCImplementationDecl *OID = |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 2147 | CGM.getContext().getObjCImplementation( |
| 2148 | const_cast<ObjCInterfaceDecl *>(ID))) |
David Chisnall | d901da5 | 2010-04-19 01:37:25 +0000 | [diff] [blame] | 2149 | Offset = ComputeIvarBaseOffset(CGM, OID, Ivar); |
| 2150 | else |
| 2151 | Offset = ComputeIvarBaseOffset(CGM, ID, Ivar); |
| 2152 | |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2153 | llvm::ConstantInt *OffsetGuess = |
David Chisnall | f950837 | 2010-01-11 19:02:35 +0000 | [diff] [blame] | 2154 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Offset, "ivar"); |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2155 | // Don't emit the guess in non-PIC code because the linker will not be able |
| 2156 | // to replace it with the real version for a library. In non-PIC code you |
| 2157 | // must compile with the fragile ABI if you want to use ivars from a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2158 | // GCC-compiled class. |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2159 | if (CGM.getLangOptions().PICLevel) { |
| 2160 | llvm::GlobalVariable *IvarOffsetGV = new llvm::GlobalVariable(TheModule, |
| 2161 | llvm::Type::getInt32Ty(VMContext), false, |
| 2162 | llvm::GlobalValue::PrivateLinkage, OffsetGuess, Name+".guess"); |
| 2163 | IvarOffsetPointer = new llvm::GlobalVariable(TheModule, |
| 2164 | IvarOffsetGV->getType(), false, llvm::GlobalValue::LinkOnceAnyLinkage, |
| 2165 | IvarOffsetGV, Name); |
| 2166 | } else { |
| 2167 | IvarOffsetPointer = new llvm::GlobalVariable(TheModule, |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 2168 | llvm::Type::getInt32PtrTy(VMContext), false, |
| 2169 | llvm::GlobalValue::ExternalLinkage, 0, Name); |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2170 | } |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2171 | } |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2172 | return IvarOffsetPointer; |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2173 | } |
| 2174 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2175 | LValue CGObjCGNU::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 2176 | QualType ObjectTy, |
| 2177 | llvm::Value *BaseValue, |
| 2178 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2179 | unsigned CVRQualifiers) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2180 | const ObjCInterfaceDecl *ID = |
| 2181 | ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2182 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 2183 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2184 | } |
Mike Stump | bb1c860 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 2185 | |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2186 | static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context, |
| 2187 | const ObjCInterfaceDecl *OID, |
| 2188 | const ObjCIvarDecl *OIVD) { |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2189 | llvm::SmallVector<ObjCIvarDecl*, 16> Ivars; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2190 | Context.ShallowCollectObjCIvars(OID, Ivars); |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2191 | for (unsigned k = 0, e = Ivars.size(); k != e; ++k) { |
| 2192 | if (OIVD == Ivars[k]) |
| 2193 | return OID; |
| 2194 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2195 | |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2196 | // Otherwise check in the super class. |
| 2197 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
| 2198 | return FindIvarInterface(Context, Super, OIVD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2199 | |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2200 | return 0; |
| 2201 | } |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2202 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2203 | llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 2204 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2205 | const ObjCIvarDecl *Ivar) { |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2206 | if (CGM.getLangOptions().ObjCNonFragileABI) { |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2207 | Interface = FindIvarInterface(CGM.getContext(), Interface, Ivar); |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2208 | return CGF.Builder.CreateLoad(CGF.Builder.CreateLoad( |
| 2209 | ObjCIvarOffsetVariable(Interface, Ivar), false, "ivar")); |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2210 | } |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2211 | uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2212 | return llvm::ConstantInt::get(LongTy, Offset, "ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2213 | } |
| 2214 | |
Mike Stump | bb1c860 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 2215 | CodeGen::CGObjCRuntime * |
| 2216 | CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Chris Lattner | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 2217 | return new CGObjCGNU(CGM); |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 2218 | } |