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