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