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