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