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