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