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