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" |
Anders Carlsson | 3d598a5 | 2009-07-14 17:29:11 +0000 | [diff] [blame] | 22 | #include "clang/AST/ASTRecordLayout.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 23 | #include "clang/AST/Decl.h" |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclObjC.h" |
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; |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 47 | namespace { |
Chris Lattner | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 48 | class CGObjCGNU : public CodeGen::CGObjCRuntime { |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 49 | private: |
Chris Lattner | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 50 | CodeGen::CodeGenModule &CGM; |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 51 | llvm::Module &TheModule; |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 52 | const llvm::PointerType *SelectorTy; |
| 53 | const llvm::PointerType *PtrToInt8Ty; |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 54 | const llvm::FunctionType *IMPTy; |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 55 | const llvm::PointerType *IdTy; |
| 56 | const llvm::IntegerType *IntTy; |
| 57 | const llvm::PointerType *PtrTy; |
| 58 | const llvm::IntegerType *LongTy; |
| 59 | const llvm::PointerType *PtrToIntTy; |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 60 | llvm::GlobalAlias *ClassPtrAlias; |
| 61 | llvm::GlobalAlias *MetaClassPtrAlias; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 62 | std::vector<llvm::Constant*> Classes; |
| 63 | std::vector<llvm::Constant*> Categories; |
| 64 | std::vector<llvm::Constant*> ConstantStrings; |
| 65 | llvm::Function *LoadFunction; |
| 66 | llvm::StringMap<llvm::Constant*> ExistingProtocols; |
| 67 | typedef std::pair<std::string, std::string> TypedSelector; |
| 68 | std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors; |
| 69 | llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors; |
| 70 | // Some zeros used for GEPs in lots of places. |
| 71 | llvm::Constant *Zeros[2]; |
| 72 | llvm::Constant *NULLPtr; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 73 | llvm::LLVMContext &VMContext; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 74 | private: |
| 75 | llvm::Constant *GenerateIvarList( |
| 76 | const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames, |
| 77 | const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes, |
| 78 | const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets); |
| 79 | llvm::Constant *GenerateMethodList(const std::string &ClassName, |
| 80 | const std::string &CategoryName, |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 81 | const llvm::SmallVectorImpl<Selector> &MethodSels, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 82 | const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes, |
| 83 | bool isClassMethodList); |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 84 | llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 85 | llvm::Constant *GenerateProtocolList( |
| 86 | const llvm::SmallVectorImpl<std::string> &Protocols); |
| 87 | llvm::Constant *GenerateClassStructure( |
| 88 | llvm::Constant *MetaClass, |
| 89 | llvm::Constant *SuperClass, |
| 90 | unsigned info, |
Chris Lattner | d002cc6 | 2008-06-26 04:47:04 +0000 | [diff] [blame] | 91 | const char *Name, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 92 | llvm::Constant *Version, |
| 93 | llvm::Constant *InstanceSize, |
| 94 | llvm::Constant *IVars, |
| 95 | llvm::Constant *Methods, |
| 96 | llvm::Constant *Protocols); |
| 97 | llvm::Constant *GenerateProtocolMethodList( |
| 98 | const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames, |
| 99 | const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes); |
| 100 | llvm::Constant *MakeConstantString(const std::string &Str, const std::string |
| 101 | &Name=""); |
| 102 | llvm::Constant *MakeGlobal(const llvm::StructType *Ty, |
| 103 | std::vector<llvm::Constant*> &V, const std::string &Name=""); |
| 104 | llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty, |
| 105 | std::vector<llvm::Constant*> &V, const std::string &Name=""); |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 106 | llvm::GlobalVariable *ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID, |
| 107 | const ObjCIvarDecl *Ivar); |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 108 | void EmitClassRef(const std::string &className); |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 109 | public: |
Chris Lattner | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 110 | CGObjCGNU(CodeGen::CodeGenModule &cgm); |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 111 | virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *); |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 112 | virtual CodeGen::RValue |
| 113 | GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 114 | QualType ResultType, |
| 115 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 116 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 117 | bool IsClassMessage, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 118 | const CallArgList &CallArgs, |
| 119 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 120 | virtual CodeGen::RValue |
| 121 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 122 | QualType ResultType, |
| 123 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 124 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 125 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 126 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 127 | bool IsClassMessage, |
| 128 | const CallArgList &CallArgs); |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 129 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 130 | const ObjCInterfaceDecl *OID); |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 131 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 132 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl |
| 133 | *Method); |
Chris Lattner | 8e67b63 | 2008-06-26 04:37:12 +0000 | [diff] [blame] | 134 | |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 135 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 136 | const ObjCContainerDecl *CD); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 137 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
| 138 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 139 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 140 | const ObjCProtocolDecl *PD); |
| 141 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 142 | virtual llvm::Function *ModuleInitFunction(); |
Fariborz Jahanian | c38e9af | 2009-06-23 21:47:46 +0000 | [diff] [blame] | 143 | virtual void MergeMetadataGlobals(std::vector<llvm::Constant*> &UsedArray); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 144 | virtual llvm::Function *GetPropertyGetFunction(); |
| 145 | virtual llvm::Function *GetPropertySetFunction(); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 146 | virtual llvm::Function *EnumerationMutationFunction(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 147 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 148 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 149 | const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 150 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 151 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 152 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 153 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 154 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 155 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 156 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 157 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 158 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 159 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 160 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 161 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 162 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 163 | llvm::Value *DestPtr, |
| 164 | llvm::Value *SrcPtr, |
| 165 | unsigned long size); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 166 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 167 | QualType ObjectTy, |
| 168 | llvm::Value *BaseValue, |
| 169 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 170 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 171 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 172 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 173 | const ObjCIvarDecl *Ivar); |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 174 | }; |
| 175 | } // end anonymous namespace |
| 176 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 177 | |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 178 | /// Emits a reference to a dummy variable which is emitted with each class. |
| 179 | /// This ensures that a linker error will be generated when trying to link |
| 180 | /// together modules where a referenced class is not defined. |
| 181 | void CGObjCGNU::EmitClassRef(const std::string &className){ |
| 182 | std::string symbolRef = "__objc_class_ref_" + className; |
| 183 | // Don't emit two copies of the same symbol |
| 184 | if (TheModule.getGlobalVariable(symbolRef)) return; |
| 185 | std::string symbolName = "__objc_class_name_" + className; |
| 186 | llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(symbolName); |
| 187 | if (!ClassSymbol) { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 188 | ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false, |
| 189 | llvm::GlobalValue::ExternalLinkage, 0, symbolName); |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 190 | } |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 191 | new llvm::GlobalVariable(TheModule, ClassSymbol->getType(), true, |
| 192 | llvm::GlobalValue::CommonLinkage, ClassSymbol, symbolRef); |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 193 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 194 | |
| 195 | static std::string SymbolNameForClass(const std::string &ClassName) { |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 196 | return "_OBJC_CLASS_" + ClassName; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | static std::string SymbolNameForMethod(const std::string &ClassName, const |
| 200 | std::string &CategoryName, const std::string &MethodName, bool isClassMethod) |
| 201 | { |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 202 | return "_OBJC_METHOD_" + ClassName + "("+CategoryName+")"+ |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 203 | (isClassMethod ? "+" : "-") + MethodName; |
| 204 | } |
| 205 | |
Chris Lattner | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 206 | CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm) |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 207 | : CGM(cgm), TheModule(CGM.getModule()), ClassPtrAlias(0), |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 208 | MetaClassPtrAlias(0), VMContext(cgm.getLLVMContext()) { |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 209 | IntTy = cast<llvm::IntegerType>( |
| 210 | CGM.getTypes().ConvertType(CGM.getContext().IntTy)); |
| 211 | LongTy = cast<llvm::IntegerType>( |
| 212 | CGM.getTypes().ConvertType(CGM.getContext().LongTy)); |
Chris Lattner | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 213 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 214 | Zeros[0] = VMContext.getConstantInt(LongTy, 0); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 215 | Zeros[1] = Zeros[0]; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 216 | NULLPtr = VMContext.getConstantPointerNull( |
| 217 | VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty)); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 218 | // C string type. Used in lots of places. |
| 219 | PtrToInt8Ty = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 220 | VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 221 | // Get the selector Type. |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 222 | SelectorTy = cast<llvm::PointerType>( |
| 223 | CGM.getTypes().ConvertType(CGM.getContext().getObjCSelType())); |
| 224 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 225 | PtrToIntTy = VMContext.getPointerTypeUnqual(IntTy); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 226 | PtrTy = PtrToInt8Ty; |
| 227 | |
| 228 | // Object type |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 229 | IdTy = cast<llvm::PointerType>( |
| 230 | CGM.getTypes().ConvertType(CGM.getContext().getObjCIdType())); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 231 | |
| 232 | // IMP type |
| 233 | std::vector<const llvm::Type*> IMPArgs; |
| 234 | IMPArgs.push_back(IdTy); |
| 235 | IMPArgs.push_back(SelectorTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 236 | IMPTy = VMContext.getFunctionType(IdTy, IMPArgs, true); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 237 | } |
| 238 | // This has to perform the lookup every time, since posing and related |
| 239 | // techniques can modify the name -> class mapping. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 240 | llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 241 | const ObjCInterfaceDecl *OID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 242 | llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString()); |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 243 | EmitClassRef(OID->getNameAsString()); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 244 | ClassName = Builder.CreateStructGEP(ClassName, 0); |
| 245 | |
Fariborz Jahanian | 26c8294 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 246 | std::vector<const llvm::Type*> Params(1, PtrToInt8Ty); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 247 | llvm::Constant *ClassLookupFn = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 248 | CGM.CreateRuntimeFunction(VMContext.getFunctionType(IdTy, |
Fariborz Jahanian | 26c8294 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 249 | Params, |
| 250 | true), |
| 251 | "objc_lookup_class"); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 252 | return Builder.CreateCall(ClassLookupFn, ClassName); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 255 | llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) { |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 256 | llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()]; |
Chris Lattner | 8e67b63 | 2008-06-26 04:37:12 +0000 | [diff] [blame] | 257 | if (US == 0) |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 258 | US = new llvm::GlobalAlias(VMContext.getPointerTypeUnqual(SelectorTy), |
Chris Lattner | 8e67b63 | 2008-06-26 04:37:12 +0000 | [diff] [blame] | 259 | llvm::GlobalValue::InternalLinkage, |
| 260 | ".objc_untyped_selector_alias", |
| 261 | NULL, &TheModule); |
| 262 | |
| 263 | return Builder.CreateLoad(US); |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl |
| 267 | *Method) { |
| 268 | |
| 269 | std::string SelName = Method->getSelector().getAsString(); |
| 270 | std::string SelTypes; |
| 271 | CGM.getContext().getObjCEncodingForMethodDecl(Method, SelTypes); |
| 272 | // Typed selectors |
| 273 | TypedSelector Selector = TypedSelector(SelName, |
| 274 | SelTypes); |
| 275 | |
| 276 | // If it's already cached, return it. |
| 277 | if (TypedSelectors[Selector]) |
| 278 | { |
| 279 | return Builder.CreateLoad(TypedSelectors[Selector]); |
| 280 | } |
| 281 | |
| 282 | // If it isn't, cache it. |
| 283 | llvm::GlobalAlias *Sel = new llvm::GlobalAlias( |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 284 | VMContext.getPointerTypeUnqual(SelectorTy), |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 285 | llvm::GlobalValue::InternalLinkage, SelName, |
| 286 | NULL, &TheModule); |
| 287 | TypedSelectors[Selector] = Sel; |
| 288 | |
| 289 | return Builder.CreateLoad(Sel); |
Chris Lattner | 8e67b63 | 2008-06-26 04:37:12 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Chris Lattner | 5e7dcc6 | 2008-06-26 04:44:19 +0000 | [diff] [blame] | 292 | llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str, |
| 293 | const std::string &Name) { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 294 | llvm::Constant * ConstStr = VMContext.getConstantArray(Str); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 295 | ConstStr = new llvm::GlobalVariable(TheModule, ConstStr->getType(), true, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 296 | llvm::GlobalValue::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 297 | ConstStr, Name); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 298 | return VMContext.getConstantExprGetElementPtr(ConstStr, Zeros, 2); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 299 | } |
| 300 | llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty, |
| 301 | std::vector<llvm::Constant*> &V, const std::string &Name) { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 302 | llvm::Constant *C = VMContext.getConstantStruct(Ty, V); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 303 | return new llvm::GlobalVariable(TheModule, Ty, false, |
| 304 | llvm::GlobalValue::InternalLinkage, C, Name); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 305 | } |
| 306 | llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty, |
| 307 | std::vector<llvm::Constant*> &V, const std::string &Name) { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 308 | llvm::Constant *C = VMContext.getConstantArray(Ty, V); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 309 | return new llvm::GlobalVariable(TheModule, Ty, false, |
| 310 | llvm::GlobalValue::InternalLinkage, C, Name); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /// Generate an NSConstantString object. |
| 314 | //TODO: In case there are any crazy people still using the GNU runtime without |
| 315 | //an OpenStep implementation, this should let them select their own class for |
| 316 | //constant strings. |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 317 | llvm::Constant *CGObjCGNU::GenerateConstantString(const ObjCStringLiteral *SL) { |
| 318 | std::string Str(SL->getString()->getStrData(), |
| 319 | SL->getString()->getByteLength()); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 320 | std::vector<llvm::Constant*> Ivars; |
| 321 | Ivars.push_back(NULLPtr); |
Chris Lattner | 13fd7e5 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 322 | Ivars.push_back(MakeConstantString(Str)); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 323 | Ivars.push_back(VMContext.getConstantInt(IntTy, Str.size())); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 324 | llvm::Constant *ObjCStr = MakeGlobal( |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 325 | VMContext.getStructType(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL), |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 326 | Ivars, ".objc_str"); |
| 327 | ConstantStrings.push_back( |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 328 | VMContext.getConstantExprBitCast(ObjCStr, PtrToInt8Ty)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 329 | return ObjCStr; |
| 330 | } |
| 331 | |
| 332 | ///Generates a message send where the super is the receiver. This is a message |
| 333 | ///send to self with special delivery semantics indicating which class's method |
| 334 | ///should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 335 | CodeGen::RValue |
| 336 | CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 337 | QualType ResultType, |
| 338 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 339 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 340 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 341 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 342 | bool IsClassMessage, |
| 343 | const CallArgList &CallArgs) { |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 344 | llvm::Value *cmd = GetSelector(CGF.Builder, Sel); |
| 345 | |
| 346 | CallArgList ActualArgs; |
| 347 | |
| 348 | ActualArgs.push_back( |
| 349 | std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)), |
| 350 | CGF.getContext().getObjCIdType())); |
| 351 | ActualArgs.push_back(std::make_pair(RValue::get(cmd), |
| 352 | CGF.getContext().getObjCSelType())); |
| 353 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
| 354 | |
| 355 | CodeGenTypes &Types = CGM.getTypes(); |
| 356 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); |
| 357 | const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false); |
| 358 | |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 359 | llvm::Value *ReceiverClass = 0; |
Chris Lattner | 48e6e7e | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 360 | if (isCategoryImpl) { |
| 361 | llvm::Constant *classLookupFunction = 0; |
| 362 | std::vector<const llvm::Type*> Params; |
| 363 | Params.push_back(PtrTy); |
| 364 | if (IsClassMessage) { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 365 | classLookupFunction = CGM.CreateRuntimeFunction(VMContext.getFunctionType( |
Chris Lattner | 48e6e7e | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 366 | IdTy, Params, true), "objc_get_meta_class"); |
| 367 | } else { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 368 | classLookupFunction = CGM.CreateRuntimeFunction(VMContext.getFunctionType( |
Chris Lattner | 48e6e7e | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 369 | IdTy, Params, true), "objc_get_class"); |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 370 | } |
Chris Lattner | 48e6e7e | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 371 | ReceiverClass = CGF.Builder.CreateCall(classLookupFunction, |
| 372 | MakeConstantString(Class->getNameAsString())); |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 373 | } else { |
Chris Lattner | 48e6e7e | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 374 | // Set up global aliases for the metaclass or class pointer if they do not |
| 375 | // already exist. These will are forward-references which will be set to |
| 376 | // pointers to the class and metaclass structure created for the runtime load |
| 377 | // function. To send a message to super, we look up the value of the |
| 378 | // super_class pointer from either the class or metaclass structure. |
| 379 | if (IsClassMessage) { |
| 380 | if (!MetaClassPtrAlias) { |
| 381 | MetaClassPtrAlias = new llvm::GlobalAlias(IdTy, |
| 382 | llvm::GlobalValue::InternalLinkage, ".objc_metaclass_ref" + |
| 383 | Class->getNameAsString(), NULL, &TheModule); |
| 384 | } |
| 385 | ReceiverClass = MetaClassPtrAlias; |
| 386 | } else { |
| 387 | if (!ClassPtrAlias) { |
| 388 | ClassPtrAlias = new llvm::GlobalAlias(IdTy, |
| 389 | llvm::GlobalValue::InternalLinkage, ".objc_class_ref" + |
| 390 | Class->getNameAsString(), NULL, &TheModule); |
| 391 | } |
| 392 | ReceiverClass = ClassPtrAlias; |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 393 | } |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 394 | } |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 395 | // Cast the pointer to a simplified version of the class structure |
| 396 | ReceiverClass = CGF.Builder.CreateBitCast(ReceiverClass, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 397 | VMContext.getPointerTypeUnqual( |
| 398 | VMContext.getStructType(IdTy, IdTy, NULL))); |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 399 | // Get the superclass pointer |
| 400 | ReceiverClass = CGF.Builder.CreateStructGEP(ReceiverClass, 1); |
| 401 | // Load the superclass pointer |
| 402 | ReceiverClass = CGF.Builder.CreateLoad(ReceiverClass); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 403 | // Construct the structure used to look up the IMP |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 404 | llvm::StructType *ObjCSuperTy = VMContext.getStructType(Receiver->getType(), |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 405 | IdTy, NULL); |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 406 | llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy); |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 407 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 408 | CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 409 | CGF.Builder.CreateStore(ReceiverClass, |
| 410 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 411 | |
| 412 | // Get the IMP |
Fariborz Jahanian | 26c8294 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 413 | std::vector<const llvm::Type*> Params; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 414 | Params.push_back(VMContext.getPointerTypeUnqual(ObjCSuperTy)); |
Fariborz Jahanian | 26c8294 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 415 | Params.push_back(SelectorTy); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 416 | llvm::Constant *lookupFunction = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 417 | CGM.CreateRuntimeFunction(VMContext.getFunctionType( |
| 418 | VMContext.getPointerTypeUnqual(impType), Params, true), |
Fariborz Jahanian | 26c8294 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 419 | "objc_msg_lookup_super"); |
| 420 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 421 | llvm::Value *lookupArgs[] = {ObjCSuper, cmd}; |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 422 | llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 423 | lookupArgs+2); |
| 424 | |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 425 | return CGF.EmitCall(FnInfo, imp, ActualArgs); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 429 | CodeGen::RValue |
| 430 | CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 431 | QualType ResultType, |
| 432 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 433 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 434 | bool IsClassMessage, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 435 | const CallArgList &CallArgs, |
| 436 | const ObjCMethodDecl *Method) { |
| 437 | llvm::Value *cmd; |
| 438 | if (Method) |
| 439 | cmd = GetSelector(CGF.Builder, Method); |
| 440 | else |
| 441 | cmd = GetSelector(CGF.Builder, Sel); |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 442 | CallArgList ActualArgs; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 443 | |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 444 | ActualArgs.push_back( |
Fariborz Jahanian | 34e6577 | 2009-05-22 20:17:16 +0000 | [diff] [blame] | 445 | std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)), |
| 446 | CGF.getContext().getObjCIdType())); |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 447 | ActualArgs.push_back(std::make_pair(RValue::get(cmd), |
| 448 | CGF.getContext().getObjCSelType())); |
| 449 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
| 450 | |
| 451 | CodeGenTypes &Types = CGM.getTypes(); |
| 452 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); |
| 453 | const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false); |
| 454 | |
Fariborz Jahanian | 34e6577 | 2009-05-22 20:17:16 +0000 | [diff] [blame] | 455 | llvm::Value *imp; |
Fariborz Jahanian | 26c8294 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 456 | std::vector<const llvm::Type*> Params; |
| 457 | Params.push_back(Receiver->getType()); |
| 458 | Params.push_back(SelectorTy); |
Fariborz Jahanian | 34e6577 | 2009-05-22 20:17:16 +0000 | [diff] [blame] | 459 | // For sender-aware dispatch, we pass the sender as the third argument to a |
| 460 | // lookup function. When sending messages from C code, the sender is nil. |
| 461 | // objc_msg_lookup_sender(id receiver, SEL selector, id sender); |
| 462 | if (CGM.getContext().getLangOptions().ObjCSenderDispatch) { |
| 463 | llvm::Value *self; |
Fariborz Jahanian | 26c8294 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 464 | |
Fariborz Jahanian | 34e6577 | 2009-05-22 20:17:16 +0000 | [diff] [blame] | 465 | if (isa<ObjCMethodDecl>(CGF.CurFuncDecl)) { |
| 466 | self = CGF.LoadObjCSelf(); |
| 467 | } else { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 468 | self = VMContext.getConstantPointerNull(IdTy); |
Fariborz Jahanian | 34e6577 | 2009-05-22 20:17:16 +0000 | [diff] [blame] | 469 | } |
| 470 | Params.push_back(self->getType()); |
| 471 | llvm::Constant *lookupFunction = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 472 | CGM.CreateRuntimeFunction(VMContext.getFunctionType( |
| 473 | VMContext.getPointerTypeUnqual(impType), Params, true), |
Fariborz Jahanian | 34e6577 | 2009-05-22 20:17:16 +0000 | [diff] [blame] | 474 | "objc_msg_lookup_sender"); |
| 475 | |
| 476 | imp = CGF.Builder.CreateCall3(lookupFunction, Receiver, cmd, self); |
| 477 | } else { |
| 478 | llvm::Constant *lookupFunction = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 479 | CGM.CreateRuntimeFunction(VMContext.getFunctionType( |
| 480 | VMContext.getPointerTypeUnqual(impType), Params, true), |
Fariborz Jahanian | 34e6577 | 2009-05-22 20:17:16 +0000 | [diff] [blame] | 481 | "objc_msg_lookup"); |
| 482 | |
| 483 | imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd); |
| 484 | } |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 485 | |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 486 | return CGF.EmitCall(FnInfo, imp, ActualArgs); |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 489 | /// Generates a MethodList. Used in construction of a objc_class and |
| 490 | /// objc_category structures. |
| 491 | llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName, |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 492 | const std::string &CategoryName, |
| 493 | const llvm::SmallVectorImpl<Selector> &MethodSels, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 494 | const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes, |
| 495 | bool isClassMethodList) { |
| 496 | // Get the method structure type. |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 497 | llvm::StructType *ObjCMethodTy = VMContext.getStructType( |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 498 | PtrToInt8Ty, // Really a selector, but the runtime creates it us. |
| 499 | PtrToInt8Ty, // Method types |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 500 | VMContext.getPointerTypeUnqual(IMPTy), //Method pointer |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 501 | NULL); |
| 502 | std::vector<llvm::Constant*> Methods; |
| 503 | std::vector<llvm::Constant*> Elements; |
| 504 | for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) { |
| 505 | Elements.clear(); |
Fariborz Jahanian | 1e64a95 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 506 | if (llvm::Constant *Method = |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 507 | TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName, |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 508 | MethodSels[i].getAsString(), |
Fariborz Jahanian | 1e64a95 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 509 | isClassMethodList))) { |
| 510 | llvm::Constant *C = |
| 511 | CGM.GetAddrOfConstantCString(MethodSels[i].getAsString()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 512 | Elements.push_back(VMContext.getConstantExprGetElementPtr(C, Zeros, 2)); |
Fariborz Jahanian | 1e64a95 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 513 | Elements.push_back( |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 514 | VMContext.getConstantExprGetElementPtr(MethodTypes[i], Zeros, 2)); |
| 515 | Method = VMContext.getConstantExprBitCast(Method, |
| 516 | VMContext.getPointerTypeUnqual(IMPTy)); |
Fariborz Jahanian | 1e64a95 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 517 | Elements.push_back(Method); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 518 | Methods.push_back(VMContext.getConstantStruct(ObjCMethodTy, Elements)); |
Fariborz Jahanian | 1e64a95 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 519 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | // Array of method structures |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 523 | llvm::ArrayType *ObjCMethodArrayTy = VMContext.getArrayType(ObjCMethodTy, |
Fariborz Jahanian | 1e64a95 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 524 | Methods.size()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 525 | llvm::Constant *MethodArray = VMContext.getConstantArray(ObjCMethodArrayTy, |
Chris Lattner | fba6763 | 2008-06-26 04:52:29 +0000 | [diff] [blame] | 526 | Methods); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 527 | |
| 528 | // Structure containing list pointer, array and array count |
| 529 | llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 530 | llvm::PATypeHolder OpaqueNextTy = VMContext.getOpaqueType(); |
| 531 | llvm::Type *NextPtrTy = VMContext.getPointerTypeUnqual(OpaqueNextTy); |
| 532 | llvm::StructType *ObjCMethodListTy = VMContext.getStructType(NextPtrTy, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 533 | IntTy, |
| 534 | ObjCMethodArrayTy, |
| 535 | NULL); |
| 536 | // Refine next pointer type to concrete type |
| 537 | llvm::cast<llvm::OpaqueType>( |
| 538 | OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy); |
| 539 | ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get()); |
| 540 | |
| 541 | Methods.clear(); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 542 | Methods.push_back(VMContext.getConstantPointerNull( |
| 543 | VMContext.getPointerTypeUnqual(ObjCMethodListTy))); |
| 544 | Methods.push_back(VMContext.getConstantInt(llvm::Type::Int32Ty, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 545 | MethodTypes.size())); |
| 546 | Methods.push_back(MethodArray); |
| 547 | |
| 548 | // Create an instance of the structure |
| 549 | return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list"); |
| 550 | } |
| 551 | |
| 552 | /// Generates an IvarList. Used in construction of a objc_class. |
| 553 | llvm::Constant *CGObjCGNU::GenerateIvarList( |
| 554 | const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames, |
| 555 | const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes, |
| 556 | const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) { |
| 557 | // Get the method structure type. |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 558 | llvm::StructType *ObjCIvarTy = VMContext.getStructType( |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 559 | PtrToInt8Ty, |
| 560 | PtrToInt8Ty, |
| 561 | IntTy, |
| 562 | NULL); |
| 563 | std::vector<llvm::Constant*> Ivars; |
| 564 | std::vector<llvm::Constant*> Elements; |
| 565 | for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) { |
| 566 | Elements.clear(); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 567 | Elements.push_back( VMContext.getConstantExprGetElementPtr(IvarNames[i], |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 568 | Zeros, 2)); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 569 | Elements.push_back( VMContext.getConstantExprGetElementPtr(IvarTypes[i], |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 570 | Zeros, 2)); |
| 571 | Elements.push_back(IvarOffsets[i]); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 572 | Ivars.push_back(VMContext.getConstantStruct(ObjCIvarTy, Elements)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | // Array of method structures |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 576 | llvm::ArrayType *ObjCIvarArrayTy = VMContext.getArrayType(ObjCIvarTy, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 577 | IvarNames.size()); |
| 578 | |
| 579 | |
| 580 | Elements.clear(); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 581 | Elements.push_back(VMContext.getConstantInt(IntTy, (int)IvarNames.size())); |
| 582 | Elements.push_back(VMContext.getConstantArray(ObjCIvarArrayTy, Ivars)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 583 | // Structure containing array and array count |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 584 | llvm::StructType *ObjCIvarListTy = VMContext.getStructType(IntTy, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 585 | ObjCIvarArrayTy, |
| 586 | NULL); |
| 587 | |
| 588 | // Create an instance of the structure |
| 589 | return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list"); |
| 590 | } |
| 591 | |
| 592 | /// Generate a class structure |
| 593 | llvm::Constant *CGObjCGNU::GenerateClassStructure( |
| 594 | llvm::Constant *MetaClass, |
| 595 | llvm::Constant *SuperClass, |
| 596 | unsigned info, |
Chris Lattner | d002cc6 | 2008-06-26 04:47:04 +0000 | [diff] [blame] | 597 | const char *Name, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 598 | llvm::Constant *Version, |
| 599 | llvm::Constant *InstanceSize, |
| 600 | llvm::Constant *IVars, |
| 601 | llvm::Constant *Methods, |
| 602 | llvm::Constant *Protocols) { |
| 603 | // Set up the class structure |
| 604 | // Note: Several of these are char*s when they should be ids. This is |
| 605 | // because the runtime performs this translation on load. |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 606 | llvm::StructType *ClassTy = VMContext.getStructType( |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 607 | PtrToInt8Ty, // class_pointer |
| 608 | PtrToInt8Ty, // super_class |
| 609 | PtrToInt8Ty, // name |
| 610 | LongTy, // version |
| 611 | LongTy, // info |
| 612 | LongTy, // instance_size |
| 613 | IVars->getType(), // ivars |
| 614 | Methods->getType(), // methods |
| 615 | // These are all filled in by the runtime, so we pretend |
| 616 | PtrTy, // dtable |
| 617 | PtrTy, // subclass_list |
| 618 | PtrTy, // sibling_class |
| 619 | PtrTy, // protocols |
| 620 | PtrTy, // gc_object_type |
| 621 | NULL); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 622 | llvm::Constant *Zero = VMContext.getConstantInt(LongTy, 0); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 623 | llvm::Constant *NullP = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 624 | VMContext.getConstantPointerNull(PtrTy); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 625 | // Fill in the structure |
| 626 | std::vector<llvm::Constant*> Elements; |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 627 | Elements.push_back(VMContext.getConstantExprBitCast(MetaClass, PtrToInt8Ty)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 628 | Elements.push_back(SuperClass); |
Chris Lattner | d002cc6 | 2008-06-26 04:47:04 +0000 | [diff] [blame] | 629 | Elements.push_back(MakeConstantString(Name, ".class_name")); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 630 | Elements.push_back(Zero); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 631 | Elements.push_back(VMContext.getConstantInt(LongTy, info)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 632 | Elements.push_back(InstanceSize); |
| 633 | Elements.push_back(IVars); |
| 634 | Elements.push_back(Methods); |
| 635 | Elements.push_back(NullP); |
| 636 | Elements.push_back(NullP); |
| 637 | Elements.push_back(NullP); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 638 | Elements.push_back(VMContext.getConstantExprBitCast(Protocols, PtrTy)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 639 | Elements.push_back(NullP); |
| 640 | // Create an instance of the structure |
Chris Lattner | 1565e03 | 2008-07-21 06:31:05 +0000 | [diff] [blame] | 641 | return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | llvm::Constant *CGObjCGNU::GenerateProtocolMethodList( |
| 645 | const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames, |
| 646 | const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) { |
| 647 | // Get the method structure type. |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 648 | llvm::StructType *ObjCMethodDescTy = VMContext.getStructType( |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 649 | PtrToInt8Ty, // Really a selector, but the runtime does the casting for us. |
| 650 | PtrToInt8Ty, |
| 651 | NULL); |
| 652 | std::vector<llvm::Constant*> Methods; |
| 653 | std::vector<llvm::Constant*> Elements; |
| 654 | for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) { |
| 655 | Elements.clear(); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 656 | Elements.push_back(VMContext.getConstantExprGetElementPtr(MethodNames[i], |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 657 | Zeros, 2)); |
| 658 | Elements.push_back( |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 659 | VMContext.getConstantExprGetElementPtr(MethodTypes[i], Zeros, 2)); |
| 660 | Methods.push_back(VMContext.getConstantStruct(ObjCMethodDescTy, Elements)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 661 | } |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 662 | llvm::ArrayType *ObjCMethodArrayTy = VMContext.getArrayType(ObjCMethodDescTy, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 663 | MethodNames.size()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 664 | llvm::Constant *Array = VMContext.getConstantArray(ObjCMethodArrayTy, |
| 665 | Methods); |
| 666 | llvm::StructType *ObjCMethodDescListTy = VMContext.getStructType( |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 667 | IntTy, ObjCMethodArrayTy, NULL); |
| 668 | Methods.clear(); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 669 | Methods.push_back(VMContext.getConstantInt(IntTy, MethodNames.size())); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 670 | Methods.push_back(Array); |
| 671 | return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list"); |
| 672 | } |
| 673 | // Create the protocol list structure used in classes, categories and so on |
| 674 | llvm::Constant *CGObjCGNU::GenerateProtocolList( |
| 675 | const llvm::SmallVectorImpl<std::string> &Protocols) { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 676 | llvm::ArrayType *ProtocolArrayTy = VMContext.getArrayType(PtrToInt8Ty, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 677 | Protocols.size()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 678 | llvm::StructType *ProtocolListTy = VMContext.getStructType( |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 679 | PtrTy, //Should be a recurisve pointer, but it's always NULL here. |
| 680 | LongTy,//FIXME: Should be size_t |
| 681 | ProtocolArrayTy, |
| 682 | NULL); |
| 683 | std::vector<llvm::Constant*> Elements; |
| 684 | for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end(); |
| 685 | iter != endIter ; iter++) { |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 686 | llvm::Constant *protocol = ExistingProtocols[*iter]; |
| 687 | if (!protocol) |
| 688 | protocol = GenerateEmptyProtocol(*iter); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 689 | llvm::Constant *Ptr = VMContext.getConstantExprBitCast(protocol, |
| 690 | PtrToInt8Ty); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 691 | Elements.push_back(Ptr); |
| 692 | } |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 693 | llvm::Constant * ProtocolArray = VMContext.getConstantArray(ProtocolArrayTy, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 694 | Elements); |
| 695 | Elements.clear(); |
| 696 | Elements.push_back(NULLPtr); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 697 | Elements.push_back(VMContext.getConstantInt(LongTy, Protocols.size())); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 698 | Elements.push_back(ProtocolArray); |
| 699 | return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list"); |
| 700 | } |
| 701 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 702 | llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 703 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 704 | llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()]; |
| 705 | const llvm::Type *T = |
| 706 | CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 707 | return Builder.CreateBitCast(protocol, VMContext.getPointerTypeUnqual(T)); |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | llvm::Constant *CGObjCGNU::GenerateEmptyProtocol( |
| 711 | const std::string &ProtocolName) { |
| 712 | llvm::SmallVector<std::string, 0> EmptyStringVector; |
| 713 | llvm::SmallVector<llvm::Constant*, 0> EmptyConstantVector; |
| 714 | |
| 715 | llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector); |
| 716 | llvm::Constant *InstanceMethodList = |
| 717 | GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector); |
| 718 | llvm::Constant *ClassMethodList = |
| 719 | GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector); |
| 720 | // Protocols are objects containing lists of the methods implemented and |
| 721 | // protocols adopted. |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 722 | llvm::StructType *ProtocolTy = VMContext.getStructType(IdTy, |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 723 | PtrToInt8Ty, |
| 724 | ProtocolList->getType(), |
| 725 | InstanceMethodList->getType(), |
| 726 | ClassMethodList->getType(), |
| 727 | NULL); |
| 728 | std::vector<llvm::Constant*> Elements; |
| 729 | // The isa pointer must be set to a magic number so the runtime knows it's |
| 730 | // the correct layout. |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 731 | Elements.push_back(VMContext.getConstantExprIntToPtr( |
| 732 | VMContext.getConstantInt(llvm::Type::Int32Ty, ProtocolVersion), IdTy)); |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 733 | Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name")); |
| 734 | Elements.push_back(ProtocolList); |
| 735 | Elements.push_back(InstanceMethodList); |
| 736 | Elements.push_back(ClassMethodList); |
| 737 | return MakeGlobal(ProtocolTy, Elements, ".objc_protocol"); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 740 | void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) { |
| 741 | ASTContext &Context = CGM.getContext(); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 742 | std::string ProtocolName = PD->getNameAsString(); |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 743 | llvm::SmallVector<std::string, 16> Protocols; |
| 744 | for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(), |
| 745 | E = PD->protocol_end(); PI != E; ++PI) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 746 | Protocols.push_back((*PI)->getNameAsString()); |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 747 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames; |
| 748 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 749 | for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(), |
| 750 | E = PD->instmeth_end(); iter != E; iter++) { |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 751 | std::string TypeStr; |
| 752 | Context.getObjCEncodingForMethodDecl(*iter, TypeStr); |
| 753 | InstanceMethodNames.push_back( |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 754 | CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString())); |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 755 | InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 756 | } |
| 757 | // Collect information about class methods: |
| 758 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames; |
| 759 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes; |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 760 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 761 | iter = PD->classmeth_begin(), endIter = PD->classmeth_end(); |
| 762 | iter != endIter ; iter++) { |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 763 | std::string TypeStr; |
| 764 | Context.getObjCEncodingForMethodDecl((*iter),TypeStr); |
| 765 | ClassMethodNames.push_back( |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 766 | CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString())); |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 767 | ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 768 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 769 | |
| 770 | llvm::Constant *ProtocolList = GenerateProtocolList(Protocols); |
| 771 | llvm::Constant *InstanceMethodList = |
| 772 | GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes); |
| 773 | llvm::Constant *ClassMethodList = |
| 774 | GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes); |
| 775 | // Protocols are objects containing lists of the methods implemented and |
| 776 | // protocols adopted. |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 777 | llvm::StructType *ProtocolTy = VMContext.getStructType(IdTy, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 778 | PtrToInt8Ty, |
| 779 | ProtocolList->getType(), |
| 780 | InstanceMethodList->getType(), |
| 781 | ClassMethodList->getType(), |
| 782 | NULL); |
| 783 | std::vector<llvm::Constant*> Elements; |
| 784 | // The isa pointer must be set to a magic number so the runtime knows it's |
| 785 | // the correct layout. |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 786 | Elements.push_back(VMContext.getConstantExprIntToPtr( |
| 787 | VMContext.getConstantInt(llvm::Type::Int32Ty, ProtocolVersion), IdTy)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 788 | Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name")); |
| 789 | Elements.push_back(ProtocolList); |
| 790 | Elements.push_back(InstanceMethodList); |
| 791 | Elements.push_back(ClassMethodList); |
| 792 | ExistingProtocols[ProtocolName] = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 793 | VMContext.getConstantExprBitCast(MakeGlobal(ProtocolTy, Elements, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 794 | ".objc_protocol"), IdTy); |
| 795 | } |
| 796 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 797 | void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 798 | std::string ClassName = OCD->getClassInterface()->getNameAsString(); |
| 799 | std::string CategoryName = OCD->getNameAsString(); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 800 | // Collect information about instance methods |
| 801 | llvm::SmallVector<Selector, 16> InstanceMethodSels; |
| 802 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 803 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 804 | iter = OCD->instmeth_begin(), endIter = OCD->instmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 805 | iter != endIter ; iter++) { |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 806 | InstanceMethodSels.push_back((*iter)->getSelector()); |
| 807 | std::string TypeStr; |
| 808 | CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr); |
| 809 | InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); |
| 810 | } |
| 811 | |
| 812 | // Collect information about class methods |
| 813 | llvm::SmallVector<Selector, 16> ClassMethodSels; |
| 814 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 815 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 816 | iter = OCD->classmeth_begin(), endIter = OCD->classmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 817 | iter != endIter ; iter++) { |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 818 | ClassMethodSels.push_back((*iter)->getSelector()); |
| 819 | std::string TypeStr; |
| 820 | CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr); |
| 821 | ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); |
| 822 | } |
| 823 | |
| 824 | // Collect the names of referenced protocols |
| 825 | llvm::SmallVector<std::string, 16> Protocols; |
| 826 | const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface(); |
| 827 | const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols(); |
| 828 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(), |
| 829 | E = Protos.end(); I != E; ++I) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 830 | Protocols.push_back((*I)->getNameAsString()); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 831 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 832 | std::vector<llvm::Constant*> Elements; |
| 833 | Elements.push_back(MakeConstantString(CategoryName)); |
| 834 | Elements.push_back(MakeConstantString(ClassName)); |
| 835 | // Instance method list |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 836 | Elements.push_back(VMContext.getConstantExprBitCast(GenerateMethodList( |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 837 | ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 838 | false), PtrTy)); |
| 839 | // Class method list |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 840 | Elements.push_back(VMContext.getConstantExprBitCast(GenerateMethodList( |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 841 | ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true), |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 842 | PtrTy)); |
| 843 | // Protocol list |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 844 | Elements.push_back(VMContext.getConstantExprBitCast( |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 845 | GenerateProtocolList(Protocols), PtrTy)); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 846 | Categories.push_back(VMContext.getConstantExprBitCast( |
| 847 | MakeGlobal(VMContext.getStructType(PtrToInt8Ty, PtrToInt8Ty, PtrTy, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 848 | PtrTy, PtrTy, NULL), Elements), PtrTy)); |
| 849 | } |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 850 | |
| 851 | void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) { |
| 852 | ASTContext &Context = CGM.getContext(); |
| 853 | |
| 854 | // Get the superclass name. |
| 855 | const ObjCInterfaceDecl * SuperClassDecl = |
| 856 | OID->getClassInterface()->getSuperClass(); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 857 | std::string SuperClassName; |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 858 | if (SuperClassDecl) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 859 | SuperClassName = SuperClassDecl->getNameAsString(); |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 860 | EmitClassRef(SuperClassName); |
| 861 | } |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 862 | |
| 863 | // Get the class name |
Chris Lattner | 09dc666 | 2009-04-01 02:00:48 +0000 | [diff] [blame] | 864 | ObjCInterfaceDecl *ClassDecl = |
| 865 | const_cast<ObjCInterfaceDecl *>(OID->getClassInterface()); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 866 | std::string ClassName = ClassDecl->getNameAsString(); |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 867 | // Emit the symbol that is used to generate linker errors if this class is |
| 868 | // referenced in other modules but not declared. |
Fariborz Jahanian | c51db23 | 2009-07-03 15:10:14 +0000 | [diff] [blame] | 869 | std::string classSymbolName = "__objc_class_name_" + ClassName; |
| 870 | if (llvm::GlobalVariable *symbol = |
| 871 | TheModule.getGlobalVariable(classSymbolName)) { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 872 | symbol->setInitializer(VMContext.getConstantInt(LongTy, 0)); |
Fariborz Jahanian | c51db23 | 2009-07-03 15:10:14 +0000 | [diff] [blame] | 873 | } else { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 874 | new llvm::GlobalVariable(TheModule, LongTy, false, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 875 | llvm::GlobalValue::ExternalLinkage, VMContext.getConstantInt(LongTy, 0), |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 876 | classSymbolName); |
Fariborz Jahanian | c51db23 | 2009-07-03 15:10:14 +0000 | [diff] [blame] | 877 | } |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 878 | |
Daniel Dunbar | 2bebbf0 | 2009-05-03 10:46:44 +0000 | [diff] [blame] | 879 | // Get the size of instances. |
| 880 | int instanceSize = Context.getASTObjCImplementationLayout(OID).getSize() / 8; |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 881 | |
| 882 | // Collect information about instance variables. |
| 883 | llvm::SmallVector<llvm::Constant*, 16> IvarNames; |
| 884 | llvm::SmallVector<llvm::Constant*, 16> IvarTypes; |
| 885 | llvm::SmallVector<llvm::Constant*, 16> IvarOffsets; |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 886 | |
| 887 | int superInstanceSize = !SuperClassDecl ? 0 : |
| 888 | Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize() / 8; |
| 889 | // For non-fragile ivars, set the instance size to 0 - {the size of just this |
| 890 | // class}. The runtime will then set this to the correct value on load. |
| 891 | if (CGM.getContext().getLangOptions().ObjCNonFragileABI) { |
| 892 | instanceSize = 0 - (instanceSize - superInstanceSize); |
| 893 | } |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 894 | for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(), |
| 895 | endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) { |
| 896 | // Store the name |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 897 | IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter) |
| 898 | ->getNameAsString())); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 899 | // Get the type encoding for this ivar |
| 900 | std::string TypeStr; |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 901 | Context.getObjCEncodingForType((*iter)->getType(), TypeStr); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 902 | IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); |
| 903 | // Get the offset |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 904 | uint64_t Offset; |
| 905 | if (CGM.getContext().getLangOptions().ObjCNonFragileABI) { |
| 906 | Offset = ComputeIvarBaseOffset(CGM, ClassDecl, *iter) - |
| 907 | superInstanceSize; |
| 908 | ObjCIvarOffsetVariable(ClassDecl, *iter); |
| 909 | } else { |
| 910 | Offset = ComputeIvarBaseOffset(CGM, ClassDecl, *iter); |
| 911 | } |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 912 | IvarOffsets.push_back( |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 913 | VMContext.getConstantInt(llvm::Type::Int32Ty, Offset)); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | // Collect information about instance methods |
| 917 | llvm::SmallVector<Selector, 16> InstanceMethodSels; |
| 918 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 919 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 920 | iter = OID->instmeth_begin(), endIter = OID->instmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 921 | iter != endIter ; iter++) { |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 922 | InstanceMethodSels.push_back((*iter)->getSelector()); |
| 923 | std::string TypeStr; |
| 924 | Context.getObjCEncodingForMethodDecl((*iter),TypeStr); |
| 925 | InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); |
| 926 | } |
Fariborz Jahanian | 1e64a95 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 927 | for (ObjCImplDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 928 | iter = OID->propimpl_begin(), endIter = OID->propimpl_end(); |
Fariborz Jahanian | 1e64a95 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 929 | iter != endIter ; iter++) { |
| 930 | ObjCPropertyDecl *property = (*iter)->getPropertyDecl(); |
| 931 | if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) { |
| 932 | InstanceMethodSels.push_back(getter->getSelector()); |
| 933 | std::string TypeStr; |
| 934 | Context.getObjCEncodingForMethodDecl(getter,TypeStr); |
| 935 | InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); |
| 936 | } |
| 937 | if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) { |
| 938 | InstanceMethodSels.push_back(setter->getSelector()); |
| 939 | std::string TypeStr; |
| 940 | Context.getObjCEncodingForMethodDecl(setter,TypeStr); |
| 941 | InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); |
| 942 | } |
| 943 | } |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 944 | |
| 945 | // Collect information about class methods |
| 946 | llvm::SmallVector<Selector, 16> ClassMethodSels; |
| 947 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 948 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 949 | iter = OID->classmeth_begin(), endIter = OID->classmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 950 | iter != endIter ; iter++) { |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 951 | ClassMethodSels.push_back((*iter)->getSelector()); |
| 952 | std::string TypeStr; |
| 953 | Context.getObjCEncodingForMethodDecl((*iter),TypeStr); |
| 954 | ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); |
| 955 | } |
| 956 | // Collect the names of referenced protocols |
| 957 | llvm::SmallVector<std::string, 16> Protocols; |
| 958 | const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols(); |
| 959 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(), |
| 960 | E = Protos.end(); I != E; ++I) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 961 | Protocols.push_back((*I)->getNameAsString()); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 962 | |
| 963 | |
| 964 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 965 | // Get the superclass pointer. |
| 966 | llvm::Constant *SuperClass; |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 967 | if (!SuperClassName.empty()) { |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 968 | SuperClass = MakeConstantString(SuperClassName, ".super_class_name"); |
| 969 | } else { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 970 | SuperClass = VMContext.getConstantPointerNull(PtrToInt8Ty); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 971 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 972 | // Empty vector used to construct empty method lists |
| 973 | llvm::SmallVector<llvm::Constant*, 1> empty; |
| 974 | // Generate the method and instance variable lists |
| 975 | llvm::Constant *MethodList = GenerateMethodList(ClassName, "", |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 976 | InstanceMethodSels, InstanceMethodTypes, false); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 977 | llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "", |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 978 | ClassMethodSels, ClassMethodTypes, true); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 979 | llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes, |
| 980 | IvarOffsets); |
| 981 | //Generate metaclass for class methods |
| 982 | llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr, |
Chris Lattner | 1565e03 | 2008-07-21 06:31:05 +0000 | [diff] [blame] | 983 | NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList( |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 984 | empty, empty, empty), ClassMethodList, NULLPtr); |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 985 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 986 | // Generate the class structure |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 987 | llvm::Constant *ClassStruct = |
| 988 | GenerateClassStructure(MetaClassStruct, SuperClass, 0x1L, |
| 989 | ClassName.c_str(), 0, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 990 | VMContext.getConstantInt(LongTy, instanceSize), IvarList, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 991 | MethodList, GenerateProtocolList(Protocols)); |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 992 | |
| 993 | // Resolve the class aliases, if they exist. |
| 994 | if (ClassPtrAlias) { |
| 995 | ClassPtrAlias->setAliasee( |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 996 | VMContext.getConstantExprBitCast(ClassStruct, IdTy)); |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 997 | ClassPtrAlias = 0; |
| 998 | } |
| 999 | if (MetaClassPtrAlias) { |
| 1000 | MetaClassPtrAlias->setAliasee( |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1001 | VMContext.getConstantExprBitCast(MetaClassStruct, IdTy)); |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1002 | MetaClassPtrAlias = 0; |
| 1003 | } |
| 1004 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1005 | // Add class structure to list to be added to the symtab later |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1006 | ClassStruct = VMContext.getConstantExprBitCast(ClassStruct, PtrToInt8Ty); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1007 | Classes.push_back(ClassStruct); |
| 1008 | } |
| 1009 | |
Fariborz Jahanian | c38e9af | 2009-06-23 21:47:46 +0000 | [diff] [blame] | 1010 | void CGObjCGNU::MergeMetadataGlobals( |
| 1011 | std::vector<llvm::Constant*> &UsedArray) { |
| 1012 | } |
| 1013 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1014 | llvm::Function *CGObjCGNU::ModuleInitFunction() { |
| 1015 | // Only emit an ObjC load function if no Objective-C stuff has been called |
| 1016 | if (Classes.empty() && Categories.empty() && ConstantStrings.empty() && |
| 1017 | ExistingProtocols.empty() && TypedSelectors.empty() && |
Anton Korobeynikov | 5f58b91 | 2008-06-01 15:14:46 +0000 | [diff] [blame] | 1018 | UntypedSelectors.empty()) |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1019 | return NULL; |
Eli Friedman | 1b8956e | 2008-06-01 16:00:02 +0000 | [diff] [blame] | 1020 | |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 1021 | const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>( |
| 1022 | SelectorTy->getElementType()); |
| 1023 | const llvm::Type *SelStructPtrTy = SelectorTy; |
| 1024 | bool isSelOpaque = false; |
| 1025 | if (SelStructTy == 0) { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1026 | SelStructTy = VMContext.getStructType(PtrToInt8Ty, PtrToInt8Ty, NULL); |
| 1027 | SelStructPtrTy = VMContext.getPointerTypeUnqual(SelStructTy); |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 1028 | isSelOpaque = true; |
| 1029 | } |
| 1030 | |
Eli Friedman | 1b8956e | 2008-06-01 16:00:02 +0000 | [diff] [blame] | 1031 | // 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] | 1032 | TheModule.addTypeName(".objc_selector", SelStructPtrTy); |
Eli Friedman | 1b8956e | 2008-06-01 16:00:02 +0000 | [diff] [blame] | 1033 | TheModule.addTypeName(".objc_id", IdTy); |
| 1034 | TheModule.addTypeName(".objc_imp", IMPTy); |
| 1035 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1036 | std::vector<llvm::Constant*> Elements; |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 1037 | llvm::Constant *Statics = NULLPtr; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1038 | // Generate statics list: |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 1039 | if (ConstantStrings.size()) { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1040 | llvm::ArrayType *StaticsArrayTy = VMContext.getArrayType(PtrToInt8Ty, |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 1041 | ConstantStrings.size() + 1); |
| 1042 | ConstantStrings.push_back(NULLPtr); |
| 1043 | Elements.push_back(MakeConstantString("NSConstantString", |
| 1044 | ".objc_static_class_name")); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1045 | Elements.push_back(VMContext.getConstantArray(StaticsArrayTy, |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 1046 | ConstantStrings)); |
| 1047 | llvm::StructType *StaticsListTy = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1048 | VMContext.getStructType(PtrToInt8Ty, StaticsArrayTy, NULL); |
| 1049 | llvm::Type *StaticsListPtrTy = |
| 1050 | VMContext.getPointerTypeUnqual(StaticsListTy); |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 1051 | Statics = MakeGlobal(StaticsListTy, Elements, ".objc_statics"); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1052 | llvm::ArrayType *StaticsListArrayTy = |
| 1053 | VMContext.getArrayType(StaticsListPtrTy, 2); |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 1054 | Elements.clear(); |
| 1055 | Elements.push_back(Statics); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1056 | Elements.push_back(VMContext.getNullValue(StaticsListPtrTy)); |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 1057 | Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr"); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1058 | Statics = VMContext.getConstantExprBitCast(Statics, PtrTy); |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 1059 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1060 | // Array of classes, categories, and constant objects |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1061 | llvm::ArrayType *ClassListTy = VMContext.getArrayType(PtrToInt8Ty, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1062 | Classes.size() + Categories.size() + 2); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1063 | llvm::StructType *SymTabTy = VMContext.getStructType(LongTy, SelStructPtrTy, |
Chris Lattner | 630404b | 2008-06-26 04:10:42 +0000 | [diff] [blame] | 1064 | llvm::Type::Int16Ty, |
| 1065 | llvm::Type::Int16Ty, |
| 1066 | ClassListTy, NULL); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1067 | |
| 1068 | Elements.clear(); |
| 1069 | // Pointer to an array of selectors used in this module. |
| 1070 | std::vector<llvm::Constant*> Selectors; |
| 1071 | for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator |
| 1072 | iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end(); |
| 1073 | iter != iterEnd ; ++iter) { |
Chris Lattner | 630404b | 2008-06-26 04:10:42 +0000 | [diff] [blame] | 1074 | Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name")); |
| 1075 | Elements.push_back(MakeConstantString(iter->first.second, |
| 1076 | ".objc_sel_types")); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1077 | Selectors.push_back(VMContext.getConstantStruct(SelStructTy, Elements)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1078 | Elements.clear(); |
| 1079 | } |
| 1080 | for (llvm::StringMap<llvm::GlobalAlias*>::iterator |
| 1081 | iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end(); |
Chris Lattner | 630404b | 2008-06-26 04:10:42 +0000 | [diff] [blame] | 1082 | iter != iterEnd; ++iter) { |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1083 | Elements.push_back( |
Chris Lattner | 630404b | 2008-06-26 04:10:42 +0000 | [diff] [blame] | 1084 | MakeConstantString(iter->getKeyData(), ".objc_sel_name")); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1085 | Elements.push_back(NULLPtr); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1086 | Selectors.push_back(VMContext.getConstantStruct(SelStructTy, Elements)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1087 | Elements.clear(); |
| 1088 | } |
| 1089 | Elements.push_back(NULLPtr); |
| 1090 | Elements.push_back(NULLPtr); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1091 | Selectors.push_back(VMContext.getConstantStruct(SelStructTy, Elements)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1092 | Elements.clear(); |
| 1093 | // Number of static selectors |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1094 | Elements.push_back(VMContext.getConstantInt(LongTy, Selectors.size() )); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1095 | llvm::Constant *SelectorList = MakeGlobal( |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1096 | VMContext.getArrayType(SelStructTy, Selectors.size()), Selectors, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1097 | ".objc_selector_list"); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1098 | Elements.push_back(VMContext.getConstantExprBitCast(SelectorList, |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 1099 | SelStructPtrTy)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1100 | |
| 1101 | // Now that all of the static selectors exist, create pointers to them. |
| 1102 | int index = 0; |
| 1103 | for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator |
| 1104 | iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end(); |
| 1105 | iter != iterEnd; ++iter) { |
| 1106 | llvm::Constant *Idxs[] = {Zeros[0], |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1107 | VMContext.getConstantInt(llvm::Type::Int32Ty, index++), Zeros[0]}; |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1108 | llvm::Constant *SelPtr = new llvm::GlobalVariable(TheModule, SelStructPtrTy, |
| 1109 | true, llvm::GlobalValue::InternalLinkage, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1110 | VMContext.getConstantExprGetElementPtr(SelectorList, Idxs, 2), |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1111 | ".objc_sel_ptr"); |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 1112 | // If selectors are defined as an opaque type, cast the pointer to this |
| 1113 | // type. |
| 1114 | if (isSelOpaque) { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1115 | SelPtr = VMContext.getConstantExprBitCast(SelPtr, |
| 1116 | VMContext.getPointerTypeUnqual(SelectorTy)); |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 1117 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1118 | (*iter).second->setAliasee(SelPtr); |
| 1119 | } |
| 1120 | for (llvm::StringMap<llvm::GlobalAlias*>::iterator |
| 1121 | iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end(); |
| 1122 | iter != iterEnd; iter++) { |
| 1123 | llvm::Constant *Idxs[] = {Zeros[0], |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1124 | VMContext.getConstantInt(llvm::Type::Int32Ty, index++), Zeros[0]}; |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1125 | llvm::Constant *SelPtr = new llvm::GlobalVariable(TheModule, SelStructPtrTy, |
| 1126 | true, llvm::GlobalValue::InternalLinkage, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1127 | VMContext.getConstantExprGetElementPtr(SelectorList, Idxs, 2), |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1128 | ".objc_sel_ptr"); |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 1129 | // If selectors are defined as an opaque type, cast the pointer to this |
| 1130 | // type. |
| 1131 | if (isSelOpaque) { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1132 | SelPtr = VMContext.getConstantExprBitCast(SelPtr, |
| 1133 | VMContext.getPointerTypeUnqual(SelectorTy)); |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 1134 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1135 | (*iter).second->setAliasee(SelPtr); |
| 1136 | } |
| 1137 | // Number of classes defined. |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1138 | Elements.push_back(VMContext.getConstantInt(llvm::Type::Int16Ty, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1139 | Classes.size())); |
| 1140 | // Number of categories defined |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1141 | Elements.push_back(VMContext.getConstantInt(llvm::Type::Int16Ty, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1142 | Categories.size())); |
| 1143 | // Create an array of classes, then categories, then static object instances |
| 1144 | Classes.insert(Classes.end(), Categories.begin(), Categories.end()); |
| 1145 | // NULL-terminated list of static object instances (mainly constant strings) |
| 1146 | Classes.push_back(Statics); |
| 1147 | Classes.push_back(NULLPtr); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1148 | llvm::Constant *ClassList = VMContext.getConstantArray(ClassListTy, Classes); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1149 | Elements.push_back(ClassList); |
| 1150 | // Construct the symbol table |
| 1151 | llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements); |
| 1152 | |
| 1153 | // The symbol table is contained in a module which has some version-checking |
| 1154 | // constants |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1155 | llvm::StructType * ModuleTy = VMContext.getStructType(LongTy, LongTy, |
| 1156 | PtrToInt8Ty, VMContext.getPointerTypeUnqual(SymTabTy), NULL); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1157 | Elements.clear(); |
| 1158 | // Runtime version used for compatibility checking. |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 1159 | if (CGM.getContext().getLangOptions().ObjCNonFragileABI) { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1160 | Elements.push_back(VMContext.getConstantInt(LongTy, |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 1161 | NonFragileRuntimeVersion)); |
| 1162 | } else { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1163 | Elements.push_back(VMContext.getConstantInt(LongTy, RuntimeVersion)); |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 1164 | } |
Fariborz Jahanian | 91a0b51 | 2009-04-01 19:49:42 +0000 | [diff] [blame] | 1165 | // sizeof(ModuleTy) |
| 1166 | llvm::TargetData td = llvm::TargetData::TargetData(&TheModule); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1167 | Elements.push_back(VMContext.getConstantInt(LongTy, |
| 1168 | td.getTypeSizeInBits(ModuleTy)/8)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1169 | //FIXME: Should be the path to the file where this module was declared |
| 1170 | Elements.push_back(NULLPtr); |
| 1171 | Elements.push_back(SymTab); |
| 1172 | llvm::Value *Module = MakeGlobal(ModuleTy, Elements); |
| 1173 | |
| 1174 | // Create the load function calling the runtime entry point with the module |
| 1175 | // structure |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1176 | llvm::Function * LoadFunction = llvm::Function::Create( |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1177 | VMContext.getFunctionType(llvm::Type::VoidTy, false), |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1178 | llvm::GlobalValue::InternalLinkage, ".objc_load_function", |
| 1179 | &TheModule); |
| 1180 | llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1181 | CGBuilderTy Builder(VMContext); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1182 | Builder.SetInsertPoint(EntryBB); |
Fariborz Jahanian | 26c8294 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 1183 | |
| 1184 | std::vector<const llvm::Type*> Params(1, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1185 | VMContext.getPointerTypeUnqual(ModuleTy)); |
| 1186 | llvm::Value *Register = CGM.CreateRuntimeFunction(VMContext.getFunctionType( |
Fariborz Jahanian | 26c8294 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 1187 | llvm::Type::VoidTy, Params, true), "__objc_exec_class"); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1188 | Builder.CreateCall(Register, Module); |
| 1189 | Builder.CreateRetVoid(); |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 1190 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1191 | return LoadFunction; |
| 1192 | } |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1193 | |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 1194 | llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD, |
| 1195 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1196 | const ObjCCategoryImplDecl *OCD = |
Steve Naroff | 3e0a540 | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 1197 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext()); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1198 | std::string CategoryName = OCD ? OCD->getNameAsString() : ""; |
| 1199 | std::string ClassName = OMD->getClassInterface()->getNameAsString(); |
| 1200 | std::string MethodName = OMD->getSelector().getAsString(); |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 1201 | bool isClassMethod = !OMD->isInstanceMethod(); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1202 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1203 | CodeGenTypes &Types = CGM.getTypes(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 1204 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1205 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1206 | std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName, |
| 1207 | MethodName, isClassMethod); |
| 1208 | |
Gabor Greif | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 1209 | llvm::Function *Method = llvm::Function::Create(MethodTy, |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 1210 | llvm::GlobalValue::InternalLinkage, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1211 | FunctionName, |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 1212 | &TheModule); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 1213 | return Method; |
| 1214 | } |
| 1215 | |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 1216 | llvm::Function *CGObjCGNU::GetPropertyGetFunction() { |
Fariborz Jahanian | cb9dad0 | 2009-05-19 00:28:43 +0000 | [diff] [blame] | 1217 | std::vector<const llvm::Type*> Params; |
| 1218 | const llvm::Type *BoolTy = |
| 1219 | CGM.getTypes().ConvertType(CGM.getContext().BoolTy); |
| 1220 | Params.push_back(IdTy); |
| 1221 | Params.push_back(SelectorTy); |
| 1222 | // FIXME: Using LongTy for ptrdiff_t is probably broken on Win64 |
| 1223 | Params.push_back(LongTy); |
| 1224 | Params.push_back(BoolTy); |
| 1225 | // void objc_getProperty (id, SEL, ptrdiff_t, bool) |
| 1226 | const llvm::FunctionType *FTy = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1227 | VMContext.getFunctionType(IdTy, Params, false); |
Fariborz Jahanian | cb9dad0 | 2009-05-19 00:28:43 +0000 | [diff] [blame] | 1228 | return cast<llvm::Function>(CGM.CreateRuntimeFunction(FTy, |
| 1229 | "objc_getProperty")); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | llvm::Function *CGObjCGNU::GetPropertySetFunction() { |
Fariborz Jahanian | cb9dad0 | 2009-05-19 00:28:43 +0000 | [diff] [blame] | 1233 | std::vector<const llvm::Type*> Params; |
| 1234 | const llvm::Type *BoolTy = |
| 1235 | CGM.getTypes().ConvertType(CGM.getContext().BoolTy); |
| 1236 | Params.push_back(IdTy); |
| 1237 | Params.push_back(SelectorTy); |
| 1238 | // FIXME: Using LongTy for ptrdiff_t is probably broken on Win64 |
| 1239 | Params.push_back(LongTy); |
| 1240 | Params.push_back(IdTy); |
| 1241 | Params.push_back(BoolTy); |
| 1242 | Params.push_back(BoolTy); |
| 1243 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
| 1244 | const llvm::FunctionType *FTy = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1245 | VMContext.getFunctionType(llvm::Type::VoidTy, Params, false); |
Fariborz Jahanian | cb9dad0 | 2009-05-19 00:28:43 +0000 | [diff] [blame] | 1246 | return cast<llvm::Function>(CGM.CreateRuntimeFunction(FTy, |
| 1247 | "objc_setProperty")); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | llvm::Function *CGObjCGNU::EnumerationMutationFunction() { |
Fariborz Jahanian | 26c8294 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 1251 | std::vector<const llvm::Type*> Params(1, IdTy); |
| 1252 | return cast<llvm::Function>(CGM.CreateRuntimeFunction( |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1253 | VMContext.getFunctionType(llvm::Type::VoidTy, Params, true), |
Fariborz Jahanian | 26c8294 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 1254 | "objc_enumerationMutation")); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 1257 | void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1258 | const Stmt &S) { |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1259 | // Pointer to the personality function |
| 1260 | llvm::Constant *Personality = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1261 | CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::Int32Ty, |
Chris Lattner | b59761b | 2009-07-01 04:13:52 +0000 | [diff] [blame] | 1262 | true), |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1263 | "__gnu_objc_personality_v0"); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1264 | Personality = VMContext.getConstantExprBitCast(Personality, PtrTy); |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1265 | std::vector<const llvm::Type*> Params; |
| 1266 | Params.push_back(PtrTy); |
| 1267 | llvm::Value *RethrowFn = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1268 | CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::VoidTy, |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1269 | Params, false), "_Unwind_Resume_or_Rethrow"); |
| 1270 | |
| 1271 | bool isTry = isa<ObjCAtTryStmt>(S); |
| 1272 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 1273 | llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest(); |
| 1274 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Chris Lattner | d6a9907 | 2009-05-11 18:16:28 +0000 | [diff] [blame] | 1275 | llvm::BasicBlock *CatchInCatch = CGF.createBasicBlock("catch.rethrow"); |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1276 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
| 1277 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
| 1278 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
| 1279 | |
| 1280 | // GNU runtime does not currently support @synchronized() |
| 1281 | if (!isTry) { |
Chris Lattner | d6a9907 | 2009-05-11 18:16:28 +0000 | [diff] [blame] | 1282 | std::vector<const llvm::Type*> Args(1, IdTy); |
| 1283 | llvm::FunctionType *FTy = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1284 | VMContext.getFunctionType(llvm::Type::VoidTy, Args, false); |
Chris Lattner | d6a9907 | 2009-05-11 18:16:28 +0000 | [diff] [blame] | 1285 | llvm::Value *SyncEnter = CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 1286 | llvm::Value *SyncArg = |
| 1287 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 1288 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, IdTy); |
| 1289 | CGF.Builder.CreateCall(SyncEnter, SyncArg); |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
Chris Lattner | d6a9907 | 2009-05-11 18:16:28 +0000 | [diff] [blame] | 1292 | |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1293 | // Push an EH context entry, used for handling rethrows and jumps |
| 1294 | // through finally. |
| 1295 | CGF.PushCleanupBlock(FinallyBlock); |
| 1296 | |
| 1297 | // Emit the statements in the @try {} block |
| 1298 | CGF.setInvokeDest(TryHandler); |
| 1299 | |
| 1300 | CGF.EmitBlock(TryBlock); |
| 1301 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 1302 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
| 1303 | |
| 1304 | // Jump to @finally if there is no exception |
| 1305 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 1306 | |
| 1307 | // Emit the handlers |
| 1308 | CGF.EmitBlock(TryHandler); |
| 1309 | |
Chris Lattner | bb422ad | 2009-05-08 17:36:08 +0000 | [diff] [blame] | 1310 | // Get the correct versions of the exception handling intrinsics |
| 1311 | llvm::TargetData td = llvm::TargetData::TargetData(&TheModule); |
| 1312 | int PointerWidth = td.getTypeSizeInBits(PtrTy); |
| 1313 | assert((PointerWidth == 32 || PointerWidth == 64) && |
| 1314 | "Can't yet handle exceptions if pointers are not 32 or 64 bits"); |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1315 | llvm::Value *llvm_eh_exception = |
| 1316 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
Chris Lattner | bb422ad | 2009-05-08 17:36:08 +0000 | [diff] [blame] | 1317 | llvm::Value *llvm_eh_selector = PointerWidth == 32 ? |
| 1318 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i32) : |
| 1319 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64); |
| 1320 | llvm::Value *llvm_eh_typeid_for = PointerWidth == 32 ? |
| 1321 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i32) : |
| 1322 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64); |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1323 | |
| 1324 | // Exception object |
| 1325 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 1326 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow"); |
| 1327 | |
| 1328 | llvm::SmallVector<llvm::Value*, 8> ESelArgs; |
| 1329 | llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers; |
| 1330 | |
| 1331 | ESelArgs.push_back(Exc); |
| 1332 | ESelArgs.push_back(Personality); |
| 1333 | |
| 1334 | bool HasCatchAll = false; |
| 1335 | // Only @try blocks are allowed @catch blocks, but both can have @finally |
| 1336 | if (isTry) { |
| 1337 | if (const ObjCAtCatchStmt* CatchStmt = |
| 1338 | cast<ObjCAtTryStmt>(S).getCatchStmts()) { |
Chris Lattner | d6a9907 | 2009-05-11 18:16:28 +0000 | [diff] [blame] | 1339 | CGF.setInvokeDest(CatchInCatch); |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1340 | |
| 1341 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
| 1342 | const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl(); |
| 1343 | Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody())); |
| 1344 | |
| 1345 | // @catch() and @catch(id) both catch any ObjC exception |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1346 | if (!CatchDecl || CatchDecl->getType()->isObjCIdType() |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1347 | || CatchDecl->getType()->isObjCQualifiedIdType()) { |
| 1348 | // Use i8* null here to signal this is a catch all, not a cleanup. |
| 1349 | ESelArgs.push_back(NULLPtr); |
| 1350 | HasCatchAll = true; |
| 1351 | // No further catches after this one will ever by reached |
| 1352 | break; |
| 1353 | } |
| 1354 | |
| 1355 | // All other types should be Objective-C interface pointer types. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1356 | const ObjCObjectPointerType *OPT = |
| 1357 | CatchDecl->getType()->getAsObjCObjectPointerType(); |
| 1358 | assert(OPT && "Invalid @catch type."); |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1359 | const ObjCInterfaceType *IT = |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1360 | OPT->getPointeeType()->getAsObjCInterfaceType(); |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1361 | assert(IT && "Invalid @catch type."); |
Chris Lattner | bb422ad | 2009-05-08 17:36:08 +0000 | [diff] [blame] | 1362 | llvm::Value *EHType = |
| 1363 | MakeConstantString(IT->getDecl()->getNameAsString()); |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1364 | ESelArgs.push_back(EHType); |
| 1365 | } |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | // We use a cleanup unless there was already a catch all. |
| 1370 | if (!HasCatchAll) { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1371 | ESelArgs.push_back(VMContext.getConstantInt(llvm::Type::Int32Ty, 0)); |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1372 | Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0)); |
| 1373 | } |
| 1374 | |
| 1375 | // Find which handler was matched. |
Chris Lattner | bb422ad | 2009-05-08 17:36:08 +0000 | [diff] [blame] | 1376 | llvm::Value *ESelector = CGF.Builder.CreateCall(llvm_eh_selector, |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1377 | ESelArgs.begin(), ESelArgs.end(), "selector"); |
| 1378 | |
| 1379 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
| 1380 | const ParmVarDecl *CatchParam = Handlers[i].first; |
| 1381 | const Stmt *CatchBody = Handlers[i].second; |
| 1382 | |
| 1383 | llvm::BasicBlock *Next = 0; |
| 1384 | |
| 1385 | // The last handler always matches. |
| 1386 | if (i + 1 != e) { |
| 1387 | assert(CatchParam && "Only last handler can be a catch all."); |
| 1388 | |
| 1389 | // Test whether this block matches the type for the selector and branch |
| 1390 | // to Match if it does, or to the next BB if it doesn't. |
| 1391 | llvm::BasicBlock *Match = CGF.createBasicBlock("match"); |
| 1392 | Next = CGF.createBasicBlock("catch.next"); |
Chris Lattner | bb422ad | 2009-05-08 17:36:08 +0000 | [diff] [blame] | 1393 | llvm::Value *Id = CGF.Builder.CreateCall(llvm_eh_typeid_for, |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1394 | CGF.Builder.CreateBitCast(ESelArgs[i+2], PtrTy)); |
| 1395 | CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(ESelector, Id), Match, |
| 1396 | Next); |
| 1397 | |
| 1398 | CGF.EmitBlock(Match); |
| 1399 | } |
| 1400 | |
| 1401 | if (CatchBody) { |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1402 | llvm::Value *ExcObject = CGF.Builder.CreateBitCast(Exc, |
| 1403 | CGF.ConvertType(CatchParam->getType())); |
| 1404 | |
| 1405 | // Bind the catch parameter if it exists. |
| 1406 | if (CatchParam) { |
| 1407 | // CatchParam is a ParmVarDecl because of the grammar |
| 1408 | // construction used to handle this, but for codegen purposes |
| 1409 | // we treat this as a local decl. |
| 1410 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
| 1411 | CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam)); |
| 1412 | } |
| 1413 | |
| 1414 | CGF.ObjCEHValueStack.push_back(ExcObject); |
| 1415 | CGF.EmitStmt(CatchBody); |
| 1416 | CGF.ObjCEHValueStack.pop_back(); |
| 1417 | |
| 1418 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 1419 | |
| 1420 | if (Next) |
| 1421 | CGF.EmitBlock(Next); |
| 1422 | } else { |
| 1423 | assert(!Next && "catchup should be last handler."); |
| 1424 | |
| 1425 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 1426 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 1427 | } |
| 1428 | } |
Chris Lattner | d6a9907 | 2009-05-11 18:16:28 +0000 | [diff] [blame] | 1429 | // The @finally block is a secondary landing pad for any exceptions thrown in |
| 1430 | // @catch() blocks |
| 1431 | CGF.EmitBlock(CatchInCatch); |
| 1432 | Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 1433 | ESelArgs.clear(); |
| 1434 | ESelArgs.push_back(Exc); |
| 1435 | ESelArgs.push_back(Personality); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1436 | ESelArgs.push_back(VMContext.getConstantInt(llvm::Type::Int32Ty, 0)); |
Chris Lattner | d6a9907 | 2009-05-11 18:16:28 +0000 | [diff] [blame] | 1437 | CGF.Builder.CreateCall(llvm_eh_selector, ESelArgs.begin(), ESelArgs.end(), |
| 1438 | "selector"); |
| 1439 | CGF.Builder.CreateCall(llvm_eh_typeid_for, |
| 1440 | CGF.Builder.CreateIntToPtr(ESelArgs[2], PtrTy)); |
| 1441 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 1442 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 1443 | |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1444 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 1445 | |
| 1446 | CGF.setInvokeDest(PrevLandingPad); |
| 1447 | |
| 1448 | CGF.EmitBlock(FinallyBlock); |
| 1449 | |
Chris Lattner | d6a9907 | 2009-05-11 18:16:28 +0000 | [diff] [blame] | 1450 | |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1451 | if (isTry) { |
| 1452 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 1453 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 1454 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 1455 | } else { |
Chris Lattner | d6a9907 | 2009-05-11 18:16:28 +0000 | [diff] [blame] | 1456 | // Emit 'objc_sync_exit(expr)' as finally's sole statement for |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1457 | // @synchronized. |
Chris Lattner | d6a9907 | 2009-05-11 18:16:28 +0000 | [diff] [blame] | 1458 | std::vector<const llvm::Type*> Args(1, IdTy); |
| 1459 | llvm::FunctionType *FTy = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1460 | VMContext.getFunctionType(llvm::Type::VoidTy, Args, false); |
Chris Lattner | d6a9907 | 2009-05-11 18:16:28 +0000 | [diff] [blame] | 1461 | llvm::Value *SyncExit = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
| 1462 | llvm::Value *SyncArg = |
| 1463 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 1464 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, IdTy); |
| 1465 | CGF.Builder.CreateCall(SyncExit, SyncArg); |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | if (Info.SwitchBlock) |
| 1469 | CGF.EmitBlock(Info.SwitchBlock); |
| 1470 | if (Info.EndBlock) |
| 1471 | CGF.EmitBlock(Info.EndBlock); |
| 1472 | |
| 1473 | // Branch around the rethrow code. |
| 1474 | CGF.EmitBranch(FinallyEnd); |
| 1475 | |
| 1476 | CGF.EmitBlock(FinallyRethrow); |
| 1477 | CGF.Builder.CreateCall(RethrowFn, CGF.Builder.CreateLoad(RethrowPtr)); |
| 1478 | CGF.Builder.CreateUnreachable(); |
| 1479 | |
| 1480 | CGF.EmitBlock(FinallyEnd); |
| 1481 | |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 1485 | const ObjCAtThrowStmt &S) { |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1486 | llvm::Value *ExceptionAsObject; |
| 1487 | |
| 1488 | std::vector<const llvm::Type*> Args(1, IdTy); |
| 1489 | llvm::FunctionType *FTy = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1490 | VMContext.getFunctionType(llvm::Type::VoidTy, Args, false); |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1491 | llvm::Value *ThrowFn = |
| 1492 | CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
| 1493 | |
| 1494 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 1495 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
Fariborz Jahanian | 1e64a95 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 1496 | ExceptionAsObject = Exception; |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1497 | } else { |
| 1498 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
| 1499 | "Unexpected rethrow outside @catch block."); |
| 1500 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
| 1501 | } |
Fariborz Jahanian | 1e64a95 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 1502 | ExceptionAsObject = |
| 1503 | CGF.Builder.CreateBitCast(ExceptionAsObject, IdTy, "tmp"); |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 1504 | |
| 1505 | // Note: This may have to be an invoke, if we want to support constructs like: |
| 1506 | // @try { |
| 1507 | // @throw(obj); |
| 1508 | // } |
| 1509 | // @catch(id) ... |
| 1510 | // |
| 1511 | // This is effectively turning @throw into an incredibly-expensive goto, but |
| 1512 | // it may happen as a result of inlining followed by missed optimizations, or |
| 1513 | // as a result of stupidity. |
| 1514 | llvm::BasicBlock *UnwindBB = CGF.getInvokeDest(); |
| 1515 | if (!UnwindBB) { |
| 1516 | CGF.Builder.CreateCall(ThrowFn, ExceptionAsObject); |
| 1517 | CGF.Builder.CreateUnreachable(); |
| 1518 | } else { |
| 1519 | CGF.Builder.CreateInvoke(ThrowFn, UnwindBB, UnwindBB, &ExceptionAsObject, |
| 1520 | &ExceptionAsObject+1); |
| 1521 | } |
| 1522 | // Clear the insertion point to indicate we are in unreachable code. |
| 1523 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1524 | } |
| 1525 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1526 | llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 1527 | llvm::Value *AddrWeakObj) |
| 1528 | { |
| 1529 | return 0; |
| 1530 | } |
| 1531 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1532 | void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 1533 | llvm::Value *src, llvm::Value *dst) |
| 1534 | { |
| 1535 | return; |
| 1536 | } |
| 1537 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1538 | void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 1539 | llvm::Value *src, llvm::Value *dst) |
| 1540 | { |
| 1541 | return; |
| 1542 | } |
| 1543 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 1544 | void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 1545 | llvm::Value *src, llvm::Value *dst) |
| 1546 | { |
| 1547 | return; |
| 1548 | } |
| 1549 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1550 | void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 1551 | llvm::Value *src, llvm::Value *dst) |
| 1552 | { |
| 1553 | return; |
| 1554 | } |
| 1555 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1556 | void CGObjCGNU::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1557 | llvm::Value *DestPtr, |
| 1558 | llvm::Value *SrcPtr, |
| 1559 | unsigned long size) { |
| 1560 | return; |
| 1561 | } |
| 1562 | |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 1563 | llvm::GlobalVariable *CGObjCGNU::ObjCIvarOffsetVariable( |
| 1564 | const ObjCInterfaceDecl *ID, |
| 1565 | const ObjCIvarDecl *Ivar) { |
| 1566 | const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString() |
| 1567 | + '.' + Ivar->getNameAsString(); |
| 1568 | // Emit the variable and initialize it with what we think the correct value |
| 1569 | // is. This allows code compiled with non-fragile ivars to work correctly |
| 1570 | // when linked against code which isn't (most of the time). |
| 1571 | llvm::GlobalVariable *IvarOffsetGV = CGM.getModule().getGlobalVariable(Name); |
| 1572 | if (!IvarOffsetGV) { |
| 1573 | uint64_t Offset = ComputeIvarBaseOffset(CGM, ID, Ivar); |
| 1574 | llvm::ConstantInt *OffsetGuess = |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1575 | VMContext.getConstantInt(LongTy, Offset, "ivar"); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1576 | IvarOffsetGV = new llvm::GlobalVariable(TheModule, LongTy, false, |
| 1577 | llvm::GlobalValue::CommonLinkage, OffsetGuess, Name); |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 1578 | } |
| 1579 | return IvarOffsetGV; |
| 1580 | } |
| 1581 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1582 | LValue CGObjCGNU::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1583 | QualType ObjectTy, |
| 1584 | llvm::Value *BaseValue, |
| 1585 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1586 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 1587 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 1588 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 1589 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 1590 | } |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 1591 | static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context, |
| 1592 | const ObjCInterfaceDecl *OID, |
| 1593 | const ObjCIvarDecl *OIVD) { |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 1594 | llvm::SmallVector<ObjCIvarDecl*, 16> Ivars; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 1595 | Context.ShallowCollectObjCIvars(OID, Ivars); |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 1596 | for (unsigned k = 0, e = Ivars.size(); k != e; ++k) { |
| 1597 | if (OIVD == Ivars[k]) |
| 1598 | return OID; |
| 1599 | } |
| 1600 | |
| 1601 | // Otherwise check in the super class. |
| 1602 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
| 1603 | return FindIvarInterface(Context, Super, OIVD); |
| 1604 | |
| 1605 | return 0; |
| 1606 | } |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 1607 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1608 | llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1609 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1610 | const ObjCIvarDecl *Ivar) { |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 1611 | if (CGF.getContext().getLangOptions().ObjCNonFragileABI) |
| 1612 | { |
| 1613 | Interface = FindIvarInterface(CGM.getContext(), Interface, Ivar); |
| 1614 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar), |
| 1615 | false, "ivar"); |
| 1616 | } |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 1617 | uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame^] | 1618 | return VMContext.getConstantInt(LongTy, Offset, "ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1621 | CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){ |
Chris Lattner | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 1622 | return new CGObjCGNU(CGM); |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 1623 | } |