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