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