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 | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 21 | #include "clang/AST/Decl.h" |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 23 | #include "llvm/Module.h" |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallVector.h" |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringMap.h" |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Compiler.h" |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetData.h" |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 28 | #include <map> |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 29 | |
| 30 | |
Chris Lattner | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 31 | using namespace clang; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 32 | using namespace CodeGen; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 33 | using llvm::dyn_cast; |
| 34 | |
| 35 | // The version of the runtime that this class targets. Must match the version |
| 36 | // in the runtime. |
| 37 | static const int RuntimeVersion = 8; |
| 38 | static const int ProtocolVersion = 2; |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 40 | namespace { |
Chris Lattner | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 41 | class CGObjCGNU : public CodeGen::CGObjCRuntime { |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 42 | private: |
Chris Lattner | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 43 | CodeGen::CodeGenModule &CGM; |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 44 | llvm::Module &TheModule; |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 45 | const llvm::PointerType *SelectorTy; |
| 46 | const llvm::PointerType *PtrToInt8Ty; |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 47 | const llvm::FunctionType *IMPTy; |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 48 | const llvm::PointerType *IdTy; |
| 49 | const llvm::IntegerType *IntTy; |
| 50 | const llvm::PointerType *PtrTy; |
| 51 | const llvm::IntegerType *LongTy; |
| 52 | const llvm::PointerType *PtrToIntTy; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 53 | std::vector<llvm::Constant*> Classes; |
| 54 | std::vector<llvm::Constant*> Categories; |
| 55 | std::vector<llvm::Constant*> ConstantStrings; |
| 56 | llvm::Function *LoadFunction; |
| 57 | llvm::StringMap<llvm::Constant*> ExistingProtocols; |
| 58 | typedef std::pair<std::string, std::string> TypedSelector; |
| 59 | std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors; |
| 60 | llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors; |
| 61 | // Some zeros used for GEPs in lots of places. |
| 62 | llvm::Constant *Zeros[2]; |
| 63 | llvm::Constant *NULLPtr; |
| 64 | private: |
| 65 | llvm::Constant *GenerateIvarList( |
| 66 | const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames, |
| 67 | const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes, |
| 68 | const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets); |
| 69 | llvm::Constant *GenerateMethodList(const std::string &ClassName, |
| 70 | const std::string &CategoryName, |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 71 | const llvm::SmallVectorImpl<Selector> &MethodSels, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 72 | const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes, |
| 73 | bool isClassMethodList); |
| 74 | llvm::Constant *GenerateProtocolList( |
| 75 | const llvm::SmallVectorImpl<std::string> &Protocols); |
| 76 | llvm::Constant *GenerateClassStructure( |
| 77 | llvm::Constant *MetaClass, |
| 78 | llvm::Constant *SuperClass, |
| 79 | unsigned info, |
Chris Lattner | d002cc6 | 2008-06-26 04:47:04 +0000 | [diff] [blame] | 80 | const char *Name, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 81 | llvm::Constant *Version, |
| 82 | llvm::Constant *InstanceSize, |
| 83 | llvm::Constant *IVars, |
| 84 | llvm::Constant *Methods, |
| 85 | llvm::Constant *Protocols); |
| 86 | llvm::Constant *GenerateProtocolMethodList( |
| 87 | const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames, |
| 88 | const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes); |
| 89 | llvm::Constant *MakeConstantString(const std::string &Str, const std::string |
| 90 | &Name=""); |
| 91 | llvm::Constant *MakeGlobal(const llvm::StructType *Ty, |
| 92 | std::vector<llvm::Constant*> &V, const std::string &Name=""); |
| 93 | llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty, |
| 94 | std::vector<llvm::Constant*> &V, const std::string &Name=""); |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 95 | public: |
Chris Lattner | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 96 | CGObjCGNU(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 97 | virtual llvm::Constant *GenerateConstantString(const std::string &String); |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 98 | virtual CodeGen::RValue |
| 99 | GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 100 | QualType ResultType, |
| 101 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 102 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 103 | bool IsClassMessage, |
| 104 | const CallArgList &CallArgs); |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 105 | virtual CodeGen::RValue |
| 106 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 107 | QualType ResultType, |
| 108 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 109 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 110 | bool isCategoryImpl, |
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, |
| 113 | const CallArgList &CallArgs); |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 114 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 115 | const ObjCInterfaceDecl *OID); |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 116 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); |
Chris Lattner | 8e67b63 | 2008-06-26 04:37:12 +0000 | [diff] [blame] | 117 | |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 118 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 119 | const ObjCContainerDecl *CD); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 120 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
| 121 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 122 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 123 | const ObjCProtocolDecl *PD); |
| 124 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 125 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 126 | virtual llvm::Function *GetPropertyGetFunction(); |
| 127 | virtual llvm::Function *GetPropertySetFunction(); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 128 | virtual llvm::Function *EnumerationMutationFunction(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 129 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 130 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 131 | const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 132 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 133 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 134 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 135 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 136 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 137 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 138 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 139 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 140 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 141 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 142 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 143 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 144 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 145 | QualType ObjectTy, |
| 146 | llvm::Value *BaseValue, |
| 147 | const ObjCIvarDecl *Ivar, |
| 148 | const FieldDecl *Field, |
| 149 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 150 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 151 | ObjCInterfaceDecl *Interface, |
| 152 | const ObjCIvarDecl *Ivar); |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 153 | }; |
| 154 | } // end anonymous namespace |
| 155 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 156 | |
| 157 | |
| 158 | static std::string SymbolNameForClass(const std::string &ClassName) { |
| 159 | return ".objc_class_" + ClassName; |
| 160 | } |
| 161 | |
| 162 | static std::string SymbolNameForMethod(const std::string &ClassName, const |
| 163 | std::string &CategoryName, const std::string &MethodName, bool isClassMethod) |
| 164 | { |
| 165 | return "._objc_method_" + ClassName +"("+CategoryName+")"+ |
| 166 | (isClassMethod ? "+" : "-") + MethodName; |
| 167 | } |
| 168 | |
Chris Lattner | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 169 | CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm) |
| 170 | : CGM(cgm), TheModule(CGM.getModule()) { |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 171 | IntTy = cast<llvm::IntegerType>( |
| 172 | CGM.getTypes().ConvertType(CGM.getContext().IntTy)); |
| 173 | LongTy = cast<llvm::IntegerType>( |
| 174 | CGM.getTypes().ConvertType(CGM.getContext().LongTy)); |
Chris Lattner | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 175 | |
Sebastian Redl | 3a5013c | 2008-12-04 00:10:55 +0000 | [diff] [blame] | 176 | Zeros[0] = llvm::ConstantInt::get(LongTy, 0); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 177 | Zeros[1] = Zeros[0]; |
| 178 | NULLPtr = llvm::ConstantPointerNull::get( |
| 179 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty)); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 180 | // C string type. Used in lots of places. |
| 181 | PtrToInt8Ty = |
| 182 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 183 | // Get the selector Type. |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 184 | SelectorTy = cast<llvm::PointerType>( |
| 185 | CGM.getTypes().ConvertType(CGM.getContext().getObjCSelType())); |
| 186 | |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 187 | PtrToIntTy = llvm::PointerType::getUnqual(IntTy); |
| 188 | PtrTy = PtrToInt8Ty; |
| 189 | |
| 190 | // Object type |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 191 | IdTy = cast<llvm::PointerType>( |
| 192 | CGM.getTypes().ConvertType(CGM.getContext().getObjCIdType())); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 193 | |
| 194 | // IMP type |
| 195 | std::vector<const llvm::Type*> IMPArgs; |
| 196 | IMPArgs.push_back(IdTy); |
| 197 | IMPArgs.push_back(SelectorTy); |
| 198 | IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 199 | } |
| 200 | // This has to perform the lookup every time, since posing and related |
| 201 | // techniques can modify the name -> class mapping. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 202 | llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 203 | const ObjCInterfaceDecl *OID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 204 | llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString()); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 205 | ClassName = Builder.CreateStructGEP(ClassName, 0); |
| 206 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 207 | llvm::Constant *ClassLookupFn = |
| 208 | TheModule.getOrInsertFunction("objc_lookup_class", IdTy, PtrToInt8Ty, |
| 209 | NULL); |
| 210 | return Builder.CreateCall(ClassLookupFn, ClassName); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Chris Lattner | 8e67b63 | 2008-06-26 04:37:12 +0000 | [diff] [blame] | 213 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 214 | llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) { |
Chris Lattner | 8e67b63 | 2008-06-26 04:37:12 +0000 | [diff] [blame] | 215 | // FIXME: uniquing on the string is wasteful, unique on Sel instead! |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 216 | llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()]; |
Chris Lattner | 8e67b63 | 2008-06-26 04:37:12 +0000 | [diff] [blame] | 217 | if (US == 0) |
| 218 | US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy), |
| 219 | llvm::GlobalValue::InternalLinkage, |
| 220 | ".objc_untyped_selector_alias", |
| 221 | NULL, &TheModule); |
| 222 | |
| 223 | return Builder.CreateLoad(US); |
| 224 | |
| 225 | } |
| 226 | |
Chris Lattner | 5e7dcc6 | 2008-06-26 04:44:19 +0000 | [diff] [blame] | 227 | llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str, |
| 228 | const std::string &Name) { |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 229 | llvm::Constant * ConstStr = llvm::ConstantArray::get(Str); |
| 230 | ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true, |
| 231 | llvm::GlobalValue::InternalLinkage, |
| 232 | ConstStr, Name, &TheModule); |
| 233 | return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2); |
| 234 | } |
| 235 | llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty, |
| 236 | std::vector<llvm::Constant*> &V, const std::string &Name) { |
| 237 | llvm::Constant *C = llvm::ConstantStruct::get(Ty, V); |
| 238 | return new llvm::GlobalVariable(Ty, false, |
| 239 | llvm::GlobalValue::InternalLinkage, C, Name, &TheModule); |
| 240 | } |
| 241 | llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty, |
| 242 | std::vector<llvm::Constant*> &V, const std::string &Name) { |
| 243 | llvm::Constant *C = llvm::ConstantArray::get(Ty, V); |
| 244 | return new llvm::GlobalVariable(Ty, false, |
| 245 | llvm::GlobalValue::InternalLinkage, C, Name, &TheModule); |
| 246 | } |
| 247 | |
| 248 | /// Generate an NSConstantString object. |
| 249 | //TODO: In case there are any crazy people still using the GNU runtime without |
| 250 | //an OpenStep implementation, this should let them select their own class for |
| 251 | //constant strings. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 252 | llvm::Constant *CGObjCGNU::GenerateConstantString(const std::string &Str) { |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 253 | std::vector<llvm::Constant*> Ivars; |
| 254 | Ivars.push_back(NULLPtr); |
Chris Lattner | 13fd7e5 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 255 | Ivars.push_back(MakeConstantString(Str)); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 256 | Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size())); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 257 | llvm::Constant *ObjCStr = MakeGlobal( |
| 258 | llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL), |
| 259 | Ivars, ".objc_str"); |
| 260 | ConstantStrings.push_back( |
| 261 | llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty)); |
| 262 | return ObjCStr; |
| 263 | } |
| 264 | |
| 265 | ///Generates a message send where the super is the receiver. This is a message |
| 266 | ///send to self with special delivery semantics indicating which class's method |
| 267 | ///should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 268 | CodeGen::RValue |
| 269 | CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 270 | QualType ResultType, |
| 271 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 272 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 273 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 274 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 275 | bool IsClassMessage, |
| 276 | const CallArgList &CallArgs) { |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 277 | llvm::Value *cmd = GetSelector(CGF.Builder, Sel); |
| 278 | |
| 279 | CallArgList ActualArgs; |
| 280 | |
| 281 | ActualArgs.push_back( |
| 282 | std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)), |
| 283 | CGF.getContext().getObjCIdType())); |
| 284 | ActualArgs.push_back(std::make_pair(RValue::get(cmd), |
| 285 | CGF.getContext().getObjCSelType())); |
| 286 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
| 287 | |
| 288 | CodeGenTypes &Types = CGM.getTypes(); |
| 289 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); |
| 290 | const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false); |
| 291 | |
| 292 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 293 | const ObjCInterfaceDecl *SuperClass = Class->getSuperClass(); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 294 | // TODO: This should be cached, not looked up every time. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 295 | llvm::Value *ReceiverClass = GetClass(CGF.Builder, SuperClass); |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 296 | |
| 297 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 298 | // Construct the structure used to look up the IMP |
| 299 | llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(), |
| 300 | IdTy, NULL); |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 301 | llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy); |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 302 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 303 | CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
| 304 | CGF.Builder.CreateStore(ReceiverClass, CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 305 | |
| 306 | // Get the IMP |
| 307 | llvm::Constant *lookupFunction = |
| 308 | TheModule.getOrInsertFunction("objc_msg_lookup_super", |
| 309 | llvm::PointerType::getUnqual(impType), |
| 310 | llvm::PointerType::getUnqual(ObjCSuperTy), |
| 311 | SelectorTy, NULL); |
| 312 | llvm::Value *lookupArgs[] = {ObjCSuper, cmd}; |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 313 | llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 314 | lookupArgs+2); |
| 315 | |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 316 | return CGF.EmitCall(FnInfo, imp, ActualArgs); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 320 | CodeGen::RValue |
| 321 | CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 322 | QualType ResultType, |
| 323 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 324 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 325 | bool IsClassMessage, |
| 326 | const CallArgList &CallArgs) { |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 327 | llvm::Value *cmd = GetSelector(CGF.Builder, Sel); |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 328 | CallArgList ActualArgs; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 329 | |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 330 | ActualArgs.push_back( |
| 331 | std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)), |
| 332 | CGF.getContext().getObjCIdType())); |
| 333 | ActualArgs.push_back(std::make_pair(RValue::get(cmd), |
| 334 | CGF.getContext().getObjCSelType())); |
| 335 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
| 336 | |
| 337 | CodeGenTypes &Types = CGM.getTypes(); |
| 338 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); |
| 339 | const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false); |
| 340 | |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 341 | llvm::Constant *lookupFunction = |
| 342 | TheModule.getOrInsertFunction("objc_msg_lookup", |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 343 | llvm::PointerType::getUnqual(impType), |
| 344 | Receiver->getType(), SelectorTy, NULL); |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 345 | llvm::Value *imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd); |
Chris Lattner | 3eae03e | 2008-05-06 00:56:42 +0000 | [diff] [blame] | 346 | |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 347 | return CGF.EmitCall(FnInfo, imp, ActualArgs); |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 350 | /// Generates a MethodList. Used in construction of a objc_class and |
| 351 | /// objc_category structures. |
| 352 | llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName, |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 353 | const std::string &CategoryName, |
| 354 | const llvm::SmallVectorImpl<Selector> &MethodSels, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 355 | const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes, |
| 356 | bool isClassMethodList) { |
| 357 | // Get the method structure type. |
| 358 | llvm::StructType *ObjCMethodTy = llvm::StructType::get( |
| 359 | PtrToInt8Ty, // Really a selector, but the runtime creates it us. |
| 360 | PtrToInt8Ty, // Method types |
| 361 | llvm::PointerType::getUnqual(IMPTy), //Method pointer |
| 362 | NULL); |
| 363 | std::vector<llvm::Constant*> Methods; |
| 364 | std::vector<llvm::Constant*> Elements; |
| 365 | for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) { |
| 366 | Elements.clear(); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 367 | llvm::Constant *C = |
| 368 | CGM.GetAddrOfConstantCString(MethodSels[i].getAsString()); |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 369 | Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 370 | Elements.push_back( |
| 371 | llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2)); |
| 372 | llvm::Constant *Method = |
| 373 | TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName, |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 374 | MethodSels[i].getAsString(), |
Chris Lattner | 550b8db | 2008-06-26 04:05:20 +0000 | [diff] [blame] | 375 | isClassMethodList)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 376 | Method = llvm::ConstantExpr::getBitCast(Method, |
| 377 | llvm::PointerType::getUnqual(IMPTy)); |
| 378 | Elements.push_back(Method); |
| 379 | Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements)); |
| 380 | } |
| 381 | |
| 382 | // Array of method structures |
| 383 | llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy, |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 384 | MethodSels.size()); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 385 | llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy, |
Chris Lattner | fba6763 | 2008-06-26 04:52:29 +0000 | [diff] [blame] | 386 | Methods); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 387 | |
| 388 | // Structure containing list pointer, array and array count |
| 389 | llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields; |
| 390 | llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get(); |
| 391 | llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy); |
| 392 | llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy, |
| 393 | IntTy, |
| 394 | ObjCMethodArrayTy, |
| 395 | NULL); |
| 396 | // Refine next pointer type to concrete type |
| 397 | llvm::cast<llvm::OpaqueType>( |
| 398 | OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy); |
| 399 | ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get()); |
| 400 | |
| 401 | Methods.clear(); |
| 402 | Methods.push_back(llvm::ConstantPointerNull::get( |
| 403 | llvm::PointerType::getUnqual(ObjCMethodListTy))); |
| 404 | Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, |
| 405 | MethodTypes.size())); |
| 406 | Methods.push_back(MethodArray); |
| 407 | |
| 408 | // Create an instance of the structure |
| 409 | return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list"); |
| 410 | } |
| 411 | |
| 412 | /// Generates an IvarList. Used in construction of a objc_class. |
| 413 | llvm::Constant *CGObjCGNU::GenerateIvarList( |
| 414 | const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames, |
| 415 | const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes, |
| 416 | const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) { |
| 417 | // Get the method structure type. |
| 418 | llvm::StructType *ObjCIvarTy = llvm::StructType::get( |
| 419 | PtrToInt8Ty, |
| 420 | PtrToInt8Ty, |
| 421 | IntTy, |
| 422 | NULL); |
| 423 | std::vector<llvm::Constant*> Ivars; |
| 424 | std::vector<llvm::Constant*> Elements; |
| 425 | for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) { |
| 426 | Elements.clear(); |
| 427 | Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i], |
| 428 | Zeros, 2)); |
| 429 | Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i], |
| 430 | Zeros, 2)); |
| 431 | Elements.push_back(IvarOffsets[i]); |
| 432 | Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements)); |
| 433 | } |
| 434 | |
| 435 | // Array of method structures |
| 436 | llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy, |
| 437 | IvarNames.size()); |
| 438 | |
| 439 | |
| 440 | Elements.clear(); |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 441 | Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size())); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 442 | Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars)); |
| 443 | // Structure containing array and array count |
| 444 | llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy, |
| 445 | ObjCIvarArrayTy, |
| 446 | NULL); |
| 447 | |
| 448 | // Create an instance of the structure |
| 449 | return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list"); |
| 450 | } |
| 451 | |
| 452 | /// Generate a class structure |
| 453 | llvm::Constant *CGObjCGNU::GenerateClassStructure( |
| 454 | llvm::Constant *MetaClass, |
| 455 | llvm::Constant *SuperClass, |
| 456 | unsigned info, |
Chris Lattner | d002cc6 | 2008-06-26 04:47:04 +0000 | [diff] [blame] | 457 | const char *Name, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 458 | llvm::Constant *Version, |
| 459 | llvm::Constant *InstanceSize, |
| 460 | llvm::Constant *IVars, |
| 461 | llvm::Constant *Methods, |
| 462 | llvm::Constant *Protocols) { |
| 463 | // Set up the class structure |
| 464 | // Note: Several of these are char*s when they should be ids. This is |
| 465 | // because the runtime performs this translation on load. |
| 466 | llvm::StructType *ClassTy = llvm::StructType::get( |
| 467 | PtrToInt8Ty, // class_pointer |
| 468 | PtrToInt8Ty, // super_class |
| 469 | PtrToInt8Ty, // name |
| 470 | LongTy, // version |
| 471 | LongTy, // info |
| 472 | LongTy, // instance_size |
| 473 | IVars->getType(), // ivars |
| 474 | Methods->getType(), // methods |
| 475 | // These are all filled in by the runtime, so we pretend |
| 476 | PtrTy, // dtable |
| 477 | PtrTy, // subclass_list |
| 478 | PtrTy, // sibling_class |
| 479 | PtrTy, // protocols |
| 480 | PtrTy, // gc_object_type |
| 481 | NULL); |
| 482 | llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0); |
| 483 | llvm::Constant *NullP = |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 484 | llvm::ConstantPointerNull::get(PtrTy); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 485 | // Fill in the structure |
| 486 | std::vector<llvm::Constant*> Elements; |
| 487 | Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty)); |
| 488 | Elements.push_back(SuperClass); |
Chris Lattner | d002cc6 | 2008-06-26 04:47:04 +0000 | [diff] [blame] | 489 | Elements.push_back(MakeConstantString(Name, ".class_name")); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 490 | Elements.push_back(Zero); |
| 491 | Elements.push_back(llvm::ConstantInt::get(LongTy, info)); |
| 492 | Elements.push_back(InstanceSize); |
| 493 | Elements.push_back(IVars); |
| 494 | Elements.push_back(Methods); |
| 495 | Elements.push_back(NullP); |
| 496 | Elements.push_back(NullP); |
| 497 | Elements.push_back(NullP); |
| 498 | Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy)); |
| 499 | Elements.push_back(NullP); |
| 500 | // Create an instance of the structure |
Chris Lattner | 1565e03 | 2008-07-21 06:31:05 +0000 | [diff] [blame] | 501 | return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | llvm::Constant *CGObjCGNU::GenerateProtocolMethodList( |
| 505 | const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames, |
| 506 | const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) { |
| 507 | // Get the method structure type. |
| 508 | llvm::StructType *ObjCMethodDescTy = llvm::StructType::get( |
| 509 | PtrToInt8Ty, // Really a selector, but the runtime does the casting for us. |
| 510 | PtrToInt8Ty, |
| 511 | NULL); |
| 512 | std::vector<llvm::Constant*> Methods; |
| 513 | std::vector<llvm::Constant*> Elements; |
| 514 | for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) { |
| 515 | Elements.clear(); |
| 516 | Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i], |
| 517 | Zeros, 2)); |
| 518 | Elements.push_back( |
| 519 | llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2)); |
| 520 | Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements)); |
| 521 | } |
| 522 | llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy, |
| 523 | MethodNames.size()); |
| 524 | llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods); |
| 525 | llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get( |
| 526 | IntTy, ObjCMethodArrayTy, NULL); |
| 527 | Methods.clear(); |
| 528 | Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size())); |
| 529 | Methods.push_back(Array); |
| 530 | return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list"); |
| 531 | } |
| 532 | // Create the protocol list structure used in classes, categories and so on |
| 533 | llvm::Constant *CGObjCGNU::GenerateProtocolList( |
| 534 | const llvm::SmallVectorImpl<std::string> &Protocols) { |
| 535 | llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty, |
| 536 | Protocols.size()); |
| 537 | llvm::StructType *ProtocolListTy = llvm::StructType::get( |
| 538 | PtrTy, //Should be a recurisve pointer, but it's always NULL here. |
| 539 | LongTy,//FIXME: Should be size_t |
| 540 | ProtocolArrayTy, |
| 541 | NULL); |
| 542 | std::vector<llvm::Constant*> Elements; |
| 543 | for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end(); |
| 544 | iter != endIter ; iter++) { |
| 545 | llvm::Constant *Ptr = |
| 546 | llvm::ConstantExpr::getBitCast(ExistingProtocols[*iter], PtrToInt8Ty); |
| 547 | Elements.push_back(Ptr); |
| 548 | } |
| 549 | llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy, |
| 550 | Elements); |
| 551 | Elements.clear(); |
| 552 | Elements.push_back(NULLPtr); |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 553 | Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size())); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 554 | Elements.push_back(ProtocolArray); |
| 555 | return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list"); |
| 556 | } |
| 557 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 558 | llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 559 | const ObjCProtocolDecl *PD) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 560 | return ExistingProtocols[PD->getNameAsString()]; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 563 | void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) { |
| 564 | ASTContext &Context = CGM.getContext(); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 565 | std::string ProtocolName = PD->getNameAsString(); |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 566 | llvm::SmallVector<std::string, 16> Protocols; |
| 567 | for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(), |
| 568 | E = PD->protocol_end(); PI != E; ++PI) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 569 | Protocols.push_back((*PI)->getNameAsString()); |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 570 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames; |
| 571 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes; |
| 572 | for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(), |
| 573 | E = PD->instmeth_end(); iter != E; iter++) { |
| 574 | std::string TypeStr; |
| 575 | Context.getObjCEncodingForMethodDecl(*iter, TypeStr); |
| 576 | InstanceMethodNames.push_back( |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 577 | CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString())); |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 578 | InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 579 | } |
| 580 | // Collect information about class methods: |
| 581 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames; |
| 582 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes; |
| 583 | for (ObjCProtocolDecl::classmeth_iterator iter = PD->classmeth_begin(), |
| 584 | endIter = PD->classmeth_end() ; iter != endIter ; iter++) { |
| 585 | std::string TypeStr; |
| 586 | Context.getObjCEncodingForMethodDecl((*iter),TypeStr); |
| 587 | ClassMethodNames.push_back( |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 588 | CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString())); |
Daniel Dunbar | 6143293 | 2008-08-13 23:20:05 +0000 | [diff] [blame] | 589 | ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 590 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 591 | |
| 592 | llvm::Constant *ProtocolList = GenerateProtocolList(Protocols); |
| 593 | llvm::Constant *InstanceMethodList = |
| 594 | GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes); |
| 595 | llvm::Constant *ClassMethodList = |
| 596 | GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes); |
| 597 | // Protocols are objects containing lists of the methods implemented and |
| 598 | // protocols adopted. |
| 599 | llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy, |
| 600 | PtrToInt8Ty, |
| 601 | ProtocolList->getType(), |
| 602 | InstanceMethodList->getType(), |
| 603 | ClassMethodList->getType(), |
| 604 | NULL); |
| 605 | std::vector<llvm::Constant*> Elements; |
| 606 | // The isa pointer must be set to a magic number so the runtime knows it's |
| 607 | // the correct layout. |
| 608 | Elements.push_back(llvm::ConstantExpr::getIntToPtr( |
| 609 | llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy)); |
| 610 | Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name")); |
| 611 | Elements.push_back(ProtocolList); |
| 612 | Elements.push_back(InstanceMethodList); |
| 613 | Elements.push_back(ClassMethodList); |
| 614 | ExistingProtocols[ProtocolName] = |
| 615 | llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements, |
| 616 | ".objc_protocol"), IdTy); |
| 617 | } |
| 618 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 619 | void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 620 | std::string ClassName = OCD->getClassInterface()->getNameAsString(); |
| 621 | std::string CategoryName = OCD->getNameAsString(); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 622 | // Collect information about instance methods |
| 623 | llvm::SmallVector<Selector, 16> InstanceMethodSels; |
| 624 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes; |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 625 | for (ObjCCategoryImplDecl::instmeth_iterator iter = OCD->instmeth_begin(), |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 626 | endIter = OCD->instmeth_end() ; iter != endIter ; iter++) { |
| 627 | InstanceMethodSels.push_back((*iter)->getSelector()); |
| 628 | std::string TypeStr; |
| 629 | CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr); |
| 630 | InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); |
| 631 | } |
| 632 | |
| 633 | // Collect information about class methods |
| 634 | llvm::SmallVector<Selector, 16> ClassMethodSels; |
| 635 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes; |
Steve Naroff | 0701bbb | 2009-01-08 17:28:14 +0000 | [diff] [blame] | 636 | for (ObjCCategoryImplDecl::classmeth_iterator iter = OCD->classmeth_begin(), |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 637 | endIter = OCD->classmeth_end() ; iter != endIter ; iter++) { |
| 638 | ClassMethodSels.push_back((*iter)->getSelector()); |
| 639 | std::string TypeStr; |
| 640 | CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr); |
| 641 | ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); |
| 642 | } |
| 643 | |
| 644 | // Collect the names of referenced protocols |
| 645 | llvm::SmallVector<std::string, 16> Protocols; |
| 646 | const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface(); |
| 647 | const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols(); |
| 648 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(), |
| 649 | E = Protos.end(); I != E; ++I) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 650 | Protocols.push_back((*I)->getNameAsString()); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 651 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 652 | std::vector<llvm::Constant*> Elements; |
| 653 | Elements.push_back(MakeConstantString(CategoryName)); |
| 654 | Elements.push_back(MakeConstantString(ClassName)); |
| 655 | // Instance method list |
| 656 | Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList( |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 657 | ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 658 | false), PtrTy)); |
| 659 | // Class method list |
| 660 | Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList( |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 661 | ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true), |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 662 | PtrTy)); |
| 663 | // Protocol list |
| 664 | Elements.push_back(llvm::ConstantExpr::getBitCast( |
| 665 | GenerateProtocolList(Protocols), PtrTy)); |
| 666 | Categories.push_back(llvm::ConstantExpr::getBitCast( |
| 667 | MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy, |
| 668 | PtrTy, PtrTy, NULL), Elements), PtrTy)); |
| 669 | } |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 670 | |
| 671 | void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) { |
| 672 | ASTContext &Context = CGM.getContext(); |
| 673 | |
| 674 | // Get the superclass name. |
| 675 | const ObjCInterfaceDecl * SuperClassDecl = |
| 676 | OID->getClassInterface()->getSuperClass(); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 677 | std::string SuperClassName; |
| 678 | if (SuperClassDecl) |
| 679 | SuperClassName = SuperClassDecl->getNameAsString(); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 680 | |
| 681 | // Get the class name |
| 682 | ObjCInterfaceDecl * ClassDecl = (ObjCInterfaceDecl*)OID->getClassInterface(); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 683 | std::string ClassName = ClassDecl->getNameAsString(); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 684 | |
| 685 | // Get the size of instances. For runtimes that support late-bound instances |
| 686 | // this should probably be something different (size just of instance |
| 687 | // varaibles in this class, not superclasses?). |
| 688 | int instanceSize = 0; |
| 689 | const llvm::Type *ObjTy = 0; |
| 690 | if (!LateBoundIVars()) { |
| 691 | ObjTy = CGM.getTypes().ConvertType(Context.getObjCInterfaceType(ClassDecl)); |
Daniel Dunbar | 491c7b7 | 2009-01-12 21:08:18 +0000 | [diff] [blame] | 692 | instanceSize = CGM.getTargetData().getTypePaddedSize(ObjTy); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 693 | } else { |
| 694 | // This is required by newer ObjC runtimes. |
| 695 | assert(0 && "Late-bound instance variables not yet supported"); |
| 696 | } |
| 697 | |
| 698 | // Collect information about instance variables. |
| 699 | llvm::SmallVector<llvm::Constant*, 16> IvarNames; |
| 700 | llvm::SmallVector<llvm::Constant*, 16> IvarTypes; |
| 701 | llvm::SmallVector<llvm::Constant*, 16> IvarOffsets; |
| 702 | const llvm::StructLayout *Layout = |
| 703 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(ObjTy)); |
| 704 | ObjTy = llvm::PointerType::getUnqual(ObjTy); |
| 705 | for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(), |
| 706 | endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) { |
| 707 | // Store the name |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 708 | IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter) |
| 709 | ->getNameAsString())); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 710 | // Get the type encoding for this ivar |
| 711 | std::string TypeStr; |
Daniel Dunbar | 0d504c1 | 2008-10-17 20:21:44 +0000 | [diff] [blame] | 712 | Context.getObjCEncodingForType((*iter)->getType(), TypeStr); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 713 | IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); |
| 714 | // Get the offset |
Fariborz Jahanian | fd64bb6 | 2008-12-15 20:35:07 +0000 | [diff] [blame] | 715 | FieldDecl *Field = ClassDecl->lookupFieldDeclForIvar(Context, (*iter)); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 716 | int offset = |
Fariborz Jahanian | fd64bb6 | 2008-12-15 20:35:07 +0000 | [diff] [blame] | 717 | (int)Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 718 | IvarOffsets.push_back( |
| 719 | llvm::ConstantInt::get(llvm::Type::Int32Ty, offset)); |
| 720 | } |
| 721 | |
| 722 | // Collect information about instance methods |
| 723 | llvm::SmallVector<Selector, 16> InstanceMethodSels; |
| 724 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes; |
| 725 | for (ObjCImplementationDecl::instmeth_iterator iter = OID->instmeth_begin(), |
| 726 | endIter = OID->instmeth_end() ; iter != endIter ; iter++) { |
| 727 | InstanceMethodSels.push_back((*iter)->getSelector()); |
| 728 | std::string TypeStr; |
| 729 | Context.getObjCEncodingForMethodDecl((*iter),TypeStr); |
| 730 | InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); |
| 731 | } |
| 732 | |
| 733 | // Collect information about class methods |
| 734 | llvm::SmallVector<Selector, 16> ClassMethodSels; |
| 735 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes; |
| 736 | for (ObjCImplementationDecl::classmeth_iterator iter = OID->classmeth_begin(), |
| 737 | endIter = OID->classmeth_end() ; iter != endIter ; iter++) { |
| 738 | ClassMethodSels.push_back((*iter)->getSelector()); |
| 739 | std::string TypeStr; |
| 740 | Context.getObjCEncodingForMethodDecl((*iter),TypeStr); |
| 741 | ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr)); |
| 742 | } |
| 743 | // Collect the names of referenced protocols |
| 744 | llvm::SmallVector<std::string, 16> Protocols; |
| 745 | const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols(); |
| 746 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(), |
| 747 | E = Protos.end(); I != E; ++I) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 748 | Protocols.push_back((*I)->getNameAsString()); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 749 | |
| 750 | |
| 751 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 752 | // Get the superclass pointer. |
| 753 | llvm::Constant *SuperClass; |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 754 | if (!SuperClassName.empty()) { |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 755 | SuperClass = MakeConstantString(SuperClassName, ".super_class_name"); |
| 756 | } else { |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 757 | SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 758 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 759 | // Empty vector used to construct empty method lists |
| 760 | llvm::SmallVector<llvm::Constant*, 1> empty; |
| 761 | // Generate the method and instance variable lists |
| 762 | llvm::Constant *MethodList = GenerateMethodList(ClassName, "", |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 763 | InstanceMethodSels, InstanceMethodTypes, false); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 764 | llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "", |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 765 | ClassMethodSels, ClassMethodTypes, true); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 766 | llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes, |
| 767 | IvarOffsets); |
| 768 | //Generate metaclass for class methods |
| 769 | llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr, |
Chris Lattner | 1565e03 | 2008-07-21 06:31:05 +0000 | [diff] [blame] | 770 | NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList( |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 771 | empty, empty, empty), ClassMethodList, NULLPtr); |
| 772 | // Generate the class structure |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 773 | llvm::Constant *ClassStruct = |
| 774 | GenerateClassStructure(MetaClassStruct, SuperClass, 0x1L, |
| 775 | ClassName.c_str(), 0, |
Sebastian Redl | 3a5013c | 2008-12-04 00:10:55 +0000 | [diff] [blame] | 776 | llvm::ConstantInt::get(LongTy, instanceSize), IvarList, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 777 | MethodList, GenerateProtocolList(Protocols)); |
| 778 | // Add class structure to list to be added to the symtab later |
| 779 | ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty); |
| 780 | Classes.push_back(ClassStruct); |
| 781 | } |
| 782 | |
| 783 | llvm::Function *CGObjCGNU::ModuleInitFunction() { |
| 784 | // Only emit an ObjC load function if no Objective-C stuff has been called |
| 785 | if (Classes.empty() && Categories.empty() && ConstantStrings.empty() && |
| 786 | ExistingProtocols.empty() && TypedSelectors.empty() && |
Anton Korobeynikov | 5f58b91 | 2008-06-01 15:14:46 +0000 | [diff] [blame] | 787 | UntypedSelectors.empty()) |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 788 | return NULL; |
Eli Friedman | 1b8956e | 2008-06-01 16:00:02 +0000 | [diff] [blame] | 789 | |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 790 | const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>( |
| 791 | SelectorTy->getElementType()); |
| 792 | const llvm::Type *SelStructPtrTy = SelectorTy; |
| 793 | bool isSelOpaque = false; |
| 794 | if (SelStructTy == 0) { |
| 795 | SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, NULL); |
| 796 | SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy); |
| 797 | isSelOpaque = true; |
| 798 | } |
| 799 | |
Eli Friedman | 1b8956e | 2008-06-01 16:00:02 +0000 | [diff] [blame] | 800 | // 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] | 801 | TheModule.addTypeName(".objc_selector", SelStructPtrTy); |
Eli Friedman | 1b8956e | 2008-06-01 16:00:02 +0000 | [diff] [blame] | 802 | TheModule.addTypeName(".objc_id", IdTy); |
| 803 | TheModule.addTypeName(".objc_imp", IMPTy); |
| 804 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 805 | std::vector<llvm::Constant*> Elements; |
| 806 | // Generate statics list: |
| 807 | llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty, |
| 808 | ConstantStrings.size() + 1); |
| 809 | ConstantStrings.push_back(NULLPtr); |
| 810 | Elements.push_back(MakeConstantString("NSConstantString", |
| 811 | ".objc_static_class_name")); |
| 812 | Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings)); |
| 813 | llvm::StructType *StaticsListTy = |
| 814 | llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL); |
Chris Lattner | 630404b | 2008-06-26 04:10:42 +0000 | [diff] [blame] | 815 | llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 816 | llvm::Constant *Statics = |
| 817 | MakeGlobal(StaticsListTy, Elements, ".objc_statics"); |
Chris Lattner | 4e0b264 | 2008-06-24 17:01:28 +0000 | [diff] [blame] | 818 | llvm::ArrayType *StaticsListArrayTy = |
Chris Lattner | 630404b | 2008-06-26 04:10:42 +0000 | [diff] [blame] | 819 | llvm::ArrayType::get(StaticsListPtrTy, 2); |
Chris Lattner | 4e0b264 | 2008-06-24 17:01:28 +0000 | [diff] [blame] | 820 | Elements.clear(); |
| 821 | Elements.push_back(Statics); |
Chris Lattner | 630404b | 2008-06-26 04:10:42 +0000 | [diff] [blame] | 822 | Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy)); |
Chris Lattner | 4e0b264 | 2008-06-24 17:01:28 +0000 | [diff] [blame] | 823 | Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr"); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 824 | Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy); |
| 825 | // Array of classes, categories, and constant objects |
| 826 | llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty, |
| 827 | Classes.size() + Categories.size() + 2); |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 828 | llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy, |
Chris Lattner | 630404b | 2008-06-26 04:10:42 +0000 | [diff] [blame] | 829 | llvm::Type::Int16Ty, |
| 830 | llvm::Type::Int16Ty, |
| 831 | ClassListTy, NULL); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 832 | |
| 833 | Elements.clear(); |
| 834 | // Pointer to an array of selectors used in this module. |
| 835 | std::vector<llvm::Constant*> Selectors; |
| 836 | for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator |
| 837 | iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end(); |
| 838 | iter != iterEnd ; ++iter) { |
Chris Lattner | 630404b | 2008-06-26 04:10:42 +0000 | [diff] [blame] | 839 | Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name")); |
| 840 | Elements.push_back(MakeConstantString(iter->first.second, |
| 841 | ".objc_sel_types")); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 842 | Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements)); |
| 843 | Elements.clear(); |
| 844 | } |
| 845 | for (llvm::StringMap<llvm::GlobalAlias*>::iterator |
| 846 | iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end(); |
Chris Lattner | 630404b | 2008-06-26 04:10:42 +0000 | [diff] [blame] | 847 | iter != iterEnd; ++iter) { |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 848 | Elements.push_back( |
Chris Lattner | 630404b | 2008-06-26 04:10:42 +0000 | [diff] [blame] | 849 | MakeConstantString(iter->getKeyData(), ".objc_sel_name")); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 850 | Elements.push_back(NULLPtr); |
| 851 | Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements)); |
| 852 | Elements.clear(); |
| 853 | } |
| 854 | Elements.push_back(NULLPtr); |
| 855 | Elements.push_back(NULLPtr); |
| 856 | Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements)); |
| 857 | Elements.clear(); |
| 858 | // Number of static selectors |
| 859 | Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() )); |
| 860 | llvm::Constant *SelectorList = MakeGlobal( |
| 861 | llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors, |
| 862 | ".objc_selector_list"); |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 863 | Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList, |
| 864 | SelStructPtrTy)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 865 | |
| 866 | // Now that all of the static selectors exist, create pointers to them. |
| 867 | int index = 0; |
| 868 | for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator |
| 869 | iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end(); |
| 870 | iter != iterEnd; ++iter) { |
| 871 | llvm::Constant *Idxs[] = {Zeros[0], |
| 872 | llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]}; |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 873 | llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy, |
| 874 | true, llvm::GlobalValue::InternalLinkage, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 875 | llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2), |
| 876 | ".objc_sel_ptr", &TheModule); |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 877 | // If selectors are defined as an opaque type, cast the pointer to this |
| 878 | // type. |
| 879 | if (isSelOpaque) { |
| 880 | SelPtr = llvm::ConstantExpr::getBitCast(SelPtr, |
| 881 | llvm::PointerType::getUnqual(SelectorTy)); |
| 882 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 883 | (*iter).second->setAliasee(SelPtr); |
| 884 | } |
| 885 | for (llvm::StringMap<llvm::GlobalAlias*>::iterator |
| 886 | iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end(); |
| 887 | iter != iterEnd; iter++) { |
| 888 | llvm::Constant *Idxs[] = {Zeros[0], |
| 889 | llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]}; |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 890 | llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy, true, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 891 | llvm::GlobalValue::InternalLinkage, |
| 892 | llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2), |
| 893 | ".objc_sel_ptr", &TheModule); |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 894 | // If selectors are defined as an opaque type, cast the pointer to this |
| 895 | // type. |
| 896 | if (isSelOpaque) { |
| 897 | SelPtr = llvm::ConstantExpr::getBitCast(SelPtr, |
| 898 | llvm::PointerType::getUnqual(SelectorTy)); |
| 899 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 900 | (*iter).second->setAliasee(SelPtr); |
| 901 | } |
| 902 | // Number of classes defined. |
| 903 | Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty, |
| 904 | Classes.size())); |
| 905 | // Number of categories defined |
| 906 | Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty, |
| 907 | Categories.size())); |
| 908 | // Create an array of classes, then categories, then static object instances |
| 909 | Classes.insert(Classes.end(), Categories.begin(), Categories.end()); |
| 910 | // NULL-terminated list of static object instances (mainly constant strings) |
| 911 | Classes.push_back(Statics); |
| 912 | Classes.push_back(NULLPtr); |
| 913 | llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes); |
| 914 | Elements.push_back(ClassList); |
| 915 | // Construct the symbol table |
| 916 | llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements); |
| 917 | |
| 918 | // The symbol table is contained in a module which has some version-checking |
| 919 | // constants |
| 920 | llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy, |
| 921 | PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL); |
| 922 | Elements.clear(); |
| 923 | // Runtime version used for compatibility checking. |
| 924 | Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion)); |
| 925 | //FIXME: Should be sizeof(ModuleTy) |
| 926 | Elements.push_back(llvm::ConstantInt::get(LongTy, 16)); |
| 927 | //FIXME: Should be the path to the file where this module was declared |
| 928 | Elements.push_back(NULLPtr); |
| 929 | Elements.push_back(SymTab); |
| 930 | llvm::Value *Module = MakeGlobal(ModuleTy, Elements); |
| 931 | |
| 932 | // Create the load function calling the runtime entry point with the module |
| 933 | // structure |
| 934 | std::vector<const llvm::Type*> VoidArgs; |
| 935 | llvm::Function * LoadFunction = llvm::Function::Create( |
| 936 | llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false), |
| 937 | llvm::GlobalValue::InternalLinkage, ".objc_load_function", |
| 938 | &TheModule); |
| 939 | llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction); |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 940 | CGBuilderTy Builder; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 941 | Builder.SetInsertPoint(EntryBB); |
| 942 | llvm::Value *Register = TheModule.getOrInsertFunction("__objc_exec_class", |
| 943 | llvm::Type::VoidTy, llvm::PointerType::getUnqual(ModuleTy), NULL); |
| 944 | Builder.CreateCall(Register, Module); |
| 945 | Builder.CreateRetVoid(); |
| 946 | return LoadFunction; |
| 947 | } |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 948 | |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 949 | llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD, |
| 950 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 951 | const ObjCCategoryImplDecl *OCD = |
Steve Naroff | 3e0a540 | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 952 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext()); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 953 | std::string CategoryName = OCD ? OCD->getNameAsString() : ""; |
| 954 | std::string ClassName = OMD->getClassInterface()->getNameAsString(); |
| 955 | std::string MethodName = OMD->getSelector().getAsString(); |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 956 | bool isClassMethod = !OMD->isInstanceMethod(); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 957 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 958 | CodeGenTypes &Types = CGM.getTypes(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 959 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 960 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 961 | std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName, |
| 962 | MethodName, isClassMethod); |
| 963 | |
Gabor Greif | 984d0b4 | 2008-04-06 20:42:52 +0000 | [diff] [blame] | 964 | llvm::Function *Method = llvm::Function::Create(MethodTy, |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 965 | llvm::GlobalValue::InternalLinkage, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 966 | FunctionName, |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 967 | &TheModule); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 968 | return Method; |
| 969 | } |
| 970 | |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 971 | llvm::Function *CGObjCGNU::GetPropertyGetFunction() { |
| 972 | return 0; |
| 973 | } |
| 974 | |
| 975 | llvm::Function *CGObjCGNU::GetPropertySetFunction() { |
| 976 | return 0; |
| 977 | } |
| 978 | |
| 979 | llvm::Function *CGObjCGNU::EnumerationMutationFunction() { |
Fariborz Jahanian | 660af74 | 2009-02-03 21:52:35 +0000 | [diff] [blame] | 980 | return |
| 981 | (llvm::Function*)TheModule.getOrInsertFunction( |
| 982 | "objc_enumerationMutation", llvm::Type::VoidTy, IdTy, NULL); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 983 | } |
| 984 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 985 | void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 986 | const Stmt &S) { |
| 987 | CGF.ErrorUnsupported(&S, "@try/@synchronized statement"); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 991 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 992 | CGF.ErrorUnsupported(&S, "@throw statement"); |
| 993 | } |
| 994 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 995 | llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 996 | llvm::Value *AddrWeakObj) |
| 997 | { |
| 998 | return 0; |
| 999 | } |
| 1000 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1001 | void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 1002 | llvm::Value *src, llvm::Value *dst) |
| 1003 | { |
| 1004 | return; |
| 1005 | } |
| 1006 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1007 | void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 1008 | llvm::Value *src, llvm::Value *dst) |
| 1009 | { |
| 1010 | return; |
| 1011 | } |
| 1012 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 1013 | void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 1014 | llvm::Value *src, llvm::Value *dst) |
| 1015 | { |
| 1016 | return; |
| 1017 | } |
| 1018 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1019 | void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 1020 | llvm::Value *src, llvm::Value *dst) |
| 1021 | { |
| 1022 | return; |
| 1023 | } |
| 1024 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1025 | LValue CGObjCGNU::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1026 | QualType ObjectTy, |
| 1027 | llvm::Value *BaseValue, |
| 1028 | const ObjCIvarDecl *Ivar, |
| 1029 | const FieldDecl *Field, |
| 1030 | unsigned CVRQualifiers) { |
| 1031 | if (Ivar->isBitField()) |
| 1032 | return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field), |
| 1033 | CVRQualifiers); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 1034 | // TODO: Add a special case for isa (index 0) |
| 1035 | unsigned Index = CGM.getTypes().getLLVMFieldNo(Field); |
| 1036 | llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp"); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1037 | LValue LV = LValue::MakeAddr(V, |
| 1038 | Ivar->getType().getCVRQualifiers()|CVRQualifiers); |
| 1039 | LValue::SetObjCIvar(LV, true); |
| 1040 | return LV; |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1043 | llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
| 1044 | ObjCInterfaceDecl *Interface, |
| 1045 | const ObjCIvarDecl *Ivar) { |
| 1046 | const llvm::Type *InterfaceLTy = |
| 1047 | CGM.getTypes().ConvertType( |
| 1048 | CGM.getContext().getObjCInterfaceType(Interface)); |
| 1049 | const llvm::StructLayout *Layout = |
| 1050 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(InterfaceLTy)); |
| 1051 | FieldDecl *Field = Interface->lookupFieldDeclForIvar(CGM.getContext(), Ivar); |
| 1052 | uint64_t Offset = |
| 1053 | Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); |
| 1054 | |
| 1055 | return llvm::ConstantInt::get( |
| 1056 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 1057 | Offset); |
| 1058 | } |
| 1059 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1060 | CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){ |
Chris Lattner | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 1061 | return new CGObjCGNU(CGM); |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 1062 | } |