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