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