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