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