Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1 | //===------- CGObjCGNU.cpp - Emit LLVM Code from ASTs for a Module --------===// |
Chris Lattner | b7256cd | 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 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 10 | // This provides Objective-C code generation targeting the GNU runtime. The |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 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 | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "CGObjCRuntime.h" |
Chris Lattner | 87ab27d | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 18 | #include "CodeGenModule.h" |
Daniel Dunbar | 97db84c | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 19 | #include "CodeGenFunction.h" |
John McCall | ed1ae86 | 2011-01-28 11:13:47 +0000 | [diff] [blame] | 20 | #include "CGCleanup.h" |
Chris Lattner | b6e9eb6 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 21 | |
Chris Lattner | 87ab27d | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 22 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 23 | #include "clang/AST/Decl.h" |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 25 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | f0b64d7 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 26 | #include "clang/AST/StmtObjC.h" |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 27 | #include "clang/Basic/SourceManager.h" |
| 28 | #include "clang/Basic/FileManager.h" |
Chris Lattner | b6e9eb6 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 29 | |
| 30 | #include "llvm/Intrinsics.h" |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 31 | #include "llvm/Module.h" |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 32 | #include "llvm/LLVMContext.h" |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallVector.h" |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/StringMap.h" |
David Chisnall | e1d2584d | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 35 | #include "llvm/Support/CallSite.h" |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Compiler.h" |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetData.h" |
Chris Lattner | b6e9eb6 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 38 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 39 | #include <stdarg.h> |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 40 | |
| 41 | |
Chris Lattner | 87ab27d | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 42 | using namespace clang; |
Daniel Dunbar | 41cf9de | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 43 | using namespace CodeGen; |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 44 | using llvm::dyn_cast; |
| 45 | |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 46 | |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 47 | namespace { |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 48 | /// Class that lazily initialises the runtime function. Avoids inserting the |
| 49 | /// types and the function declaration into a module if they're not used, and |
| 50 | /// avoids constructing the type more than once if it's used more than once. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 51 | class LazyRuntimeFunction { |
| 52 | CodeGenModule *CGM; |
| 53 | std::vector<const llvm::Type*> ArgTys; |
| 54 | const char *FunctionName; |
David Chisnall | 3fe8956 | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 55 | llvm::Constant *Function; |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 56 | public: |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 57 | /// Constructor leaves this class uninitialized, because it is intended to |
| 58 | /// be used as a field in another class and not all of the types that are |
| 59 | /// used as arguments will necessarily be available at construction time. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 60 | LazyRuntimeFunction() : CGM(0), FunctionName(0), Function(0) {} |
| 61 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 62 | /// Initialises the lazy function with the name, return type, and the types |
| 63 | /// of the arguments. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 64 | END_WITH_NULL |
| 65 | void init(CodeGenModule *Mod, const char *name, |
| 66 | const llvm::Type *RetTy, ...) { |
| 67 | CGM =Mod; |
| 68 | FunctionName = name; |
| 69 | Function = 0; |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 70 | ArgTys.clear(); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 71 | va_list Args; |
| 72 | va_start(Args, RetTy); |
| 73 | while (const llvm::Type *ArgTy = va_arg(Args, const llvm::Type*)) |
| 74 | ArgTys.push_back(ArgTy); |
| 75 | va_end(Args); |
| 76 | // Push the return type on at the end so we can pop it off easily |
| 77 | ArgTys.push_back(RetTy); |
| 78 | } |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 79 | /// Overloaded cast operator, allows the class to be implicitly cast to an |
| 80 | /// LLVM constant. |
David Chisnall | 3fe8956 | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 81 | operator llvm::Constant*() { |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 82 | if (!Function) { |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 83 | if (0 == FunctionName) return 0; |
| 84 | // We put the return type on the end of the vector, so pop it back off |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 85 | const llvm::Type *RetTy = ArgTys.back(); |
| 86 | ArgTys.pop_back(); |
| 87 | llvm::FunctionType *FTy = llvm::FunctionType::get(RetTy, ArgTys, false); |
| 88 | Function = |
David Chisnall | 3fe8956 | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 89 | cast<llvm::Constant>(CGM->CreateRuntimeFunction(FTy, FunctionName)); |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 90 | // We won't need to use the types again, so we may as well clean up the |
| 91 | // vector now |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 92 | ArgTys.resize(0); |
| 93 | } |
| 94 | return Function; |
| 95 | } |
David Chisnall | 3fe8956 | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 96 | operator llvm::Function*() { |
David Chisnall | b85775c | 2011-05-23 23:15:11 +0000 | [diff] [blame] | 97 | return cast<llvm::Function>((llvm::Constant*)*this); |
David Chisnall | 3fe8956 | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 98 | } |
David Chisnall | b85775c | 2011-05-23 23:15:11 +0000 | [diff] [blame] | 99 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 103 | /// GNU Objective-C runtime code generation. This class implements the parts of |
| 104 | /// Objective-C support that are specific to the GNU family of runtimes (GCC and |
| 105 | /// GNUstep). |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 106 | class CGObjCGNU : public CGObjCRuntime { |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 107 | protected: |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 108 | /// The module that is using this class |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 109 | CodeGenModule &CGM; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 110 | /// The LLVM module into which output is inserted |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 111 | llvm::Module &TheModule; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 112 | /// strut objc_super. Used for sending messages to super. This structure |
| 113 | /// contains the receiver (object) and the expected class. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 114 | const llvm::StructType *ObjCSuperTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 115 | /// struct objc_super*. The type of the argument to the superclass message |
| 116 | /// lookup functions. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 117 | const llvm::PointerType *PtrToObjCSuperTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 118 | /// LLVM type for selectors. Opaque pointer (i8*) unless a header declaring |
| 119 | /// SEL is included in a header somewhere, in which case it will be whatever |
| 120 | /// type is declared in that header, most likely {i8*, i8*}. |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 121 | const llvm::PointerType *SelectorTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 122 | /// LLVM i8 type. Cached here to avoid repeatedly getting it in all of the |
| 123 | /// places where it's used |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 124 | const llvm::IntegerType *Int8Ty; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 125 | /// Pointer to i8 - LLVM type of char*, for all of the places where the |
| 126 | /// runtime needs to deal with C strings. |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 127 | const llvm::PointerType *PtrToInt8Ty; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 128 | /// Instance Method Pointer type. This is a pointer to a function that takes, |
| 129 | /// at a minimum, an object and a selector, and is the generic type for |
| 130 | /// Objective-C methods. Due to differences between variadic / non-variadic |
| 131 | /// calling conventions, it must always be cast to the correct type before |
| 132 | /// actually being used. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 133 | const llvm::PointerType *IMPTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 134 | /// Type of an untyped Objective-C object. Clang treats id as a built-in type |
| 135 | /// when compiling Objective-C code, so this may be an opaque pointer (i8*), |
| 136 | /// but if the runtime header declaring it is included then it may be a |
| 137 | /// pointer to a structure. |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 138 | const llvm::PointerType *IdTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 139 | /// Pointer to a pointer to an Objective-C object. Used in the new ABI |
| 140 | /// message lookup function and some GC-related functions. |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 141 | const llvm::PointerType *PtrToIdTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 142 | /// The clang type of id. Used when using the clang CGCall infrastructure to |
| 143 | /// call Objective-C methods. |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 144 | CanQualType ASTIdTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 145 | /// LLVM type for C int type. |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 146 | const llvm::IntegerType *IntTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 147 | /// LLVM type for an opaque pointer. This is identical to PtrToInt8Ty, but is |
| 148 | /// used in the code to document the difference between i8* meaning a pointer |
| 149 | /// to a C string and i8* meaning a pointer to some opaque type. |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 150 | const llvm::PointerType *PtrTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 151 | /// LLVM type for C long type. The runtime uses this in a lot of places where |
| 152 | /// it should be using intptr_t, but we can't fix this without breaking |
| 153 | /// compatibility with GCC... |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 154 | const llvm::IntegerType *LongTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 155 | /// LLVM type for C size_t. Used in various runtime data structures. |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 156 | const llvm::IntegerType *SizeTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 157 | /// LLVM type for C ptrdiff_t. Mainly used in property accessor functions. |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 158 | const llvm::IntegerType *PtrDiffTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 159 | /// LLVM type for C int*. Used for GCC-ABI-compatible non-fragile instance |
| 160 | /// variables. |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 161 | const llvm::PointerType *PtrToIntTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 162 | /// LLVM type for Objective-C BOOL type. |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 163 | const llvm::Type *BoolTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 164 | /// Metadata kind used to tie method lookups to message sends. The GNUstep |
| 165 | /// runtime provides some LLVM passes that can use this to do things like |
| 166 | /// automatic IMP caching and speculative inlining. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 167 | unsigned msgSendMDKind; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 168 | /// Helper function that generates a constant string and returns a pointer to |
| 169 | /// the start of the string. The result of this function can be used anywhere |
| 170 | /// where the C code specifies const char*. |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 171 | llvm::Constant *MakeConstantString(const std::string &Str, |
| 172 | const std::string &Name="") { |
| 173 | llvm::Constant *ConstStr = CGM.GetAddrOfConstantCString(Str, Name.c_str()); |
| 174 | return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2); |
| 175 | } |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 176 | /// Emits a linkonce_odr string, whose name is the prefix followed by the |
| 177 | /// string value. This allows the linker to combine the strings between |
| 178 | /// different modules. Used for EH typeinfo names, selector strings, and a |
| 179 | /// few other things. |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 180 | llvm::Constant *ExportUniqueString(const std::string &Str, |
| 181 | const std::string prefix) { |
| 182 | std::string name = prefix + Str; |
| 183 | llvm::Constant *ConstStr = TheModule.getGlobalVariable(name); |
| 184 | if (!ConstStr) { |
| 185 | llvm::Constant *value = llvm::ConstantArray::get(VMContext, Str, true); |
| 186 | ConstStr = new llvm::GlobalVariable(TheModule, value->getType(), true, |
| 187 | llvm::GlobalValue::LinkOnceODRLinkage, value, prefix + Str); |
| 188 | } |
| 189 | return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2); |
| 190 | } |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 191 | /// Generates a global structure, initialized by the elements in the vector. |
| 192 | /// The element types must match the types of the structure elements in the |
| 193 | /// first argument. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 194 | llvm::GlobalVariable *MakeGlobal(const llvm::StructType *Ty, |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 195 | std::vector<llvm::Constant*> &V, |
| 196 | llvm::StringRef Name="", |
| 197 | llvm::GlobalValue::LinkageTypes linkage |
| 198 | =llvm::GlobalValue::InternalLinkage) { |
| 199 | llvm::Constant *C = llvm::ConstantStruct::get(Ty, V); |
| 200 | return new llvm::GlobalVariable(TheModule, Ty, false, |
| 201 | linkage, C, Name); |
| 202 | } |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 203 | /// Generates a global array. The vector must contain the same number of |
| 204 | /// elements that the array type declares, of the type specified as the array |
| 205 | /// element type. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 206 | llvm::GlobalVariable *MakeGlobal(const llvm::ArrayType *Ty, |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 207 | std::vector<llvm::Constant*> &V, |
| 208 | llvm::StringRef Name="", |
| 209 | llvm::GlobalValue::LinkageTypes linkage |
| 210 | =llvm::GlobalValue::InternalLinkage) { |
| 211 | llvm::Constant *C = llvm::ConstantArray::get(Ty, V); |
| 212 | return new llvm::GlobalVariable(TheModule, Ty, false, |
| 213 | linkage, C, Name); |
| 214 | } |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 215 | /// Generates a global array, inferring the array type from the specified |
| 216 | /// element type and the size of the initialiser. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 217 | llvm::GlobalVariable *MakeGlobalArray(const llvm::Type *Ty, |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 218 | std::vector<llvm::Constant*> &V, |
| 219 | llvm::StringRef Name="", |
| 220 | llvm::GlobalValue::LinkageTypes linkage |
| 221 | =llvm::GlobalValue::InternalLinkage) { |
| 222 | llvm::ArrayType *ArrayTy = llvm::ArrayType::get(Ty, V.size()); |
| 223 | return MakeGlobal(ArrayTy, V, Name, linkage); |
| 224 | } |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 225 | /// Ensures that the value has the required type, by inserting a bitcast if |
| 226 | /// required. This function lets us avoid inserting bitcasts that are |
| 227 | /// redundant. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 228 | llvm::Value* EnforceType(CGBuilderTy B, llvm::Value *V, const llvm::Type *Ty){ |
| 229 | if (V->getType() == Ty) return V; |
| 230 | return B.CreateBitCast(V, Ty); |
| 231 | } |
| 232 | // Some zeros used for GEPs in lots of places. |
| 233 | llvm::Constant *Zeros[2]; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 234 | /// Null pointer value. Mainly used as a terminator in various arrays. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 235 | llvm::Constant *NULLPtr; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 236 | /// LLVM context. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 237 | llvm::LLVMContext &VMContext; |
| 238 | private: |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 239 | /// Placeholder for the class. Lots of things refer to the class before we've |
| 240 | /// actually emitted it. We use this alias as a placeholder, and then replace |
| 241 | /// it with a pointer to the class structure before finally emitting the |
| 242 | /// module. |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 243 | llvm::GlobalAlias *ClassPtrAlias; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 244 | /// Placeholder for the metaclass. Lots of things refer to the class before |
| 245 | /// we've / actually emitted it. We use this alias as a placeholder, and then |
| 246 | /// replace / it with a pointer to the metaclass structure before finally |
| 247 | /// emitting the / module. |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 248 | llvm::GlobalAlias *MetaClassPtrAlias; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 249 | /// All of the classes that have been generated for this compilation units. |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 250 | std::vector<llvm::Constant*> Classes; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 251 | /// All of the categories that have been generated for this compilation units. |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 252 | std::vector<llvm::Constant*> Categories; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 253 | /// All of the Objective-C constant strings that have been generated for this |
| 254 | /// compilation units. |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 255 | std::vector<llvm::Constant*> ConstantStrings; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 256 | /// Map from string values to Objective-C constant strings in the output. |
| 257 | /// Used to prevent emitting Objective-C strings more than once. This should |
| 258 | /// not be required at all - CodeGenModule should manage this list. |
David Chisnall | 358e751 | 2010-01-27 12:49:23 +0000 | [diff] [blame] | 259 | llvm::StringMap<llvm::Constant*> ObjCStrings; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 260 | /// All of the protocols that have been declared. |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 261 | llvm::StringMap<llvm::Constant*> ExistingProtocols; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 262 | /// For each variant of a selector, we store the type encoding and a |
| 263 | /// placeholder value. For an untyped selector, the type will be the empty |
| 264 | /// string. Selector references are all done via the module's selector table, |
| 265 | /// so we create an alias as a placeholder and then replace it with the real |
| 266 | /// value later. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 267 | typedef std::pair<std::string, llvm::GlobalAlias*> TypedSelector; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 268 | /// Type of the selector map. This is roughly equivalent to the structure |
| 269 | /// used in the GNUstep runtime, which maintains a list of all of the valid |
| 270 | /// types for a selector in a table. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 271 | typedef llvm::DenseMap<Selector, llvm::SmallVector<TypedSelector, 2> > |
| 272 | SelectorMap; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 273 | /// A map from selectors to selector types. This allows us to emit all |
| 274 | /// selectors of the same name and type together. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 275 | SelectorMap SelectorTable; |
| 276 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 277 | /// Selectors related to memory management. When compiling in GC mode, we |
| 278 | /// omit these. |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 279 | Selector RetainSel, ReleaseSel, AutoreleaseSel; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 280 | /// Runtime functions used for memory management in GC mode. Note that clang |
| 281 | /// supports code generation for calling these functions, but neither GNU |
| 282 | /// runtime actually supports this API properly yet. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 283 | LazyRuntimeFunction IvarAssignFn, StrongCastAssignFn, MemMoveFn, WeakReadFn, |
| 284 | WeakAssignFn, GlobalAssignFn; |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 285 | |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 286 | protected: |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 287 | /// Function used for throwing Objective-C exceptions. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 288 | LazyRuntimeFunction ExceptionThrowFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 289 | /// Function used for rethrowing exceptions, used at the end of @finally or |
| 290 | /// @synchronize blocks. |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 291 | LazyRuntimeFunction ExceptionReThrowFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 292 | /// Function called when entering a catch function. This is required for |
| 293 | /// differentiating Objective-C exceptions and foreign exceptions. |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 294 | LazyRuntimeFunction EnterCatchFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 295 | /// Function called when exiting from a catch block. Used to do exception |
| 296 | /// cleanup. |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 297 | LazyRuntimeFunction ExitCatchFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 298 | /// Function called when entering an @synchronize block. Acquires the lock. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 299 | LazyRuntimeFunction SyncEnterFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 300 | /// Function called when exiting an @synchronize block. Releases the lock. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 301 | LazyRuntimeFunction SyncExitFn; |
| 302 | |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 303 | private: |
| 304 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 305 | /// Function called if fast enumeration detects that the collection is |
| 306 | /// modified during the update. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 307 | LazyRuntimeFunction EnumerationMutationFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 308 | /// Function for implementing synthesized property getters that return an |
| 309 | /// object. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 310 | LazyRuntimeFunction GetPropertyFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 311 | /// Function for implementing synthesized property setters that return an |
| 312 | /// object. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 313 | LazyRuntimeFunction SetPropertyFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 314 | /// Function used for non-object declared property getters. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 315 | LazyRuntimeFunction GetStructPropertyFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 316 | /// Function used for non-object declared property setters. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 317 | LazyRuntimeFunction SetStructPropertyFn; |
| 318 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 319 | /// The version of the runtime that this class targets. Must match the |
| 320 | /// version in the runtime. |
David Chisnall | 5c51177 | 2011-05-22 22:37:08 +0000 | [diff] [blame] | 321 | int RuntimeVersion; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 322 | /// The version of the protocol class. Used to differentiate between ObjC1 |
| 323 | /// and ObjC2 protocols. Objective-C 1 protocols can not contain optional |
| 324 | /// components and can not contain declared properties. We always emit |
| 325 | /// Objective-C 2 property structures, but we have to pretend that they're |
| 326 | /// Objective-C 1 property structures when targeting the GCC runtime or it |
| 327 | /// will abort. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 328 | const int ProtocolVersion; |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 329 | private: |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 330 | /// Generates an instance variable list structure. This is a structure |
| 331 | /// containing a size and an array of structures containing instance variable |
| 332 | /// metadata. This is used purely for introspection in the fragile ABI. In |
| 333 | /// the non-fragile ABI, it's used for instance variable fixup. |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 334 | llvm::Constant *GenerateIvarList( |
| 335 | const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames, |
| 336 | const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes, |
| 337 | const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets); |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 338 | /// Generates a method list structure. This is a structure containing a size |
| 339 | /// and an array of structures containing method metadata. |
| 340 | /// |
| 341 | /// This structure is used by both classes and categories, and contains a next |
| 342 | /// pointer allowing them to be chained together in a linked list. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 343 | llvm::Constant *GenerateMethodList(const llvm::StringRef &ClassName, |
| 344 | const llvm::StringRef &CategoryName, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 345 | const llvm::SmallVectorImpl<Selector> &MethodSels, |
| 346 | const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 347 | bool isClassMethodList); |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 348 | /// Emits an empty protocol. This is used for @protocol() where no protocol |
| 349 | /// is found. The runtime will (hopefully) fix up the pointer to refer to the |
| 350 | /// real protocol. |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 351 | llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName); |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 352 | /// Generates a list of property metadata structures. This follows the same |
| 353 | /// pattern as method and instance variable metadata lists. |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 354 | llvm::Constant *GeneratePropertyList(const ObjCImplementationDecl *OID, |
| 355 | llvm::SmallVectorImpl<Selector> &InstanceMethodSels, |
| 356 | llvm::SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes); |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 357 | /// Generates a list of referenced protocols. Classes, categories, and |
| 358 | /// protocols all use this structure. |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 359 | llvm::Constant *GenerateProtocolList( |
| 360 | const llvm::SmallVectorImpl<std::string> &Protocols); |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 361 | /// To ensure that all protocols are seen by the runtime, we add a category on |
| 362 | /// a class defined in the runtime, declaring no methods, but adopting the |
| 363 | /// protocols. This is a horribly ugly hack, but it allows us to collect all |
| 364 | /// of the protocols without changing the ABI. |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 365 | void GenerateProtocolHolderCategory(void); |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 366 | /// Generates a class structure. |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 367 | llvm::Constant *GenerateClassStructure( |
| 368 | llvm::Constant *MetaClass, |
| 369 | llvm::Constant *SuperClass, |
| 370 | unsigned info, |
Chris Lattner | da35bc8 | 2008-06-26 04:47:04 +0000 | [diff] [blame] | 371 | const char *Name, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 372 | llvm::Constant *Version, |
| 373 | llvm::Constant *InstanceSize, |
| 374 | llvm::Constant *IVars, |
| 375 | llvm::Constant *Methods, |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 376 | llvm::Constant *Protocols, |
| 377 | llvm::Constant *IvarOffsets, |
David Chisnall | d472c85 | 2010-04-28 14:29:56 +0000 | [diff] [blame] | 378 | llvm::Constant *Properties, |
| 379 | bool isMeta=false); |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 380 | /// Generates a method list. This is used by protocols to define the required |
| 381 | /// and optional methods. |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 382 | llvm::Constant *GenerateProtocolMethodList( |
| 383 | const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames, |
| 384 | const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes); |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 385 | /// Returns a selector with the specified type encoding. An empty string is |
| 386 | /// used to return an untyped selector (with the types field set to NULL). |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 387 | llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 388 | const std::string &TypeEncoding, bool lval); |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 389 | /// Returns the variable used to store the offset of an instance variable. |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 390 | llvm::GlobalVariable *ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID, |
| 391 | const ObjCIvarDecl *Ivar); |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 392 | /// Emits a reference to a class. This allows the linker to object if there |
| 393 | /// is no class of the matching name. |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 394 | void EmitClassRef(const std::string &className); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 395 | protected: |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 396 | /// Looks up the method for sending a message to the specified object. This |
| 397 | /// mechanism differs between the GCC and GNU runtimes, so this method must be |
| 398 | /// overridden in subclasses. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 399 | virtual llvm::Value *LookupIMP(CodeGenFunction &CGF, |
| 400 | llvm::Value *&Receiver, |
| 401 | llvm::Value *cmd, |
| 402 | llvm::MDNode *node) = 0; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 403 | /// Looks up the method for sending a message to a superclass. This mechanism |
| 404 | /// differs between the GCC and GNU runtimes, so this method must be |
| 405 | /// overridden in subclasses. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 406 | virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, |
| 407 | llvm::Value *ObjCSuper, |
| 408 | llvm::Value *cmd) = 0; |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 409 | public: |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 410 | CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion, |
| 411 | unsigned protocolClassVersion); |
| 412 | |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 413 | virtual llvm::Constant *GenerateConstantString(const StringLiteral *); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 414 | |
| 415 | virtual RValue |
| 416 | GenerateMessageSend(CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 417 | ReturnValueSlot Return, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 418 | QualType ResultType, |
| 419 | Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 420 | llvm::Value *Receiver, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 421 | const CallArgList &CallArgs, |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 422 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 423 | const ObjCMethodDecl *Method); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 424 | virtual RValue |
| 425 | GenerateMessageSendSuper(CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 426 | ReturnValueSlot Return, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 427 | QualType ResultType, |
| 428 | Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 429 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 430 | bool isCategoryImpl, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 431 | llvm::Value *Receiver, |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 432 | bool IsClassMessage, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 433 | const CallArgList &CallArgs, |
| 434 | const ObjCMethodDecl *Method); |
Daniel Dunbar | cb46385 | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 435 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 7c6d3a7 | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 436 | const ObjCInterfaceDecl *OID); |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 437 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 438 | bool lval = false); |
Daniel Dunbar | 45858d2 | 2010-02-03 20:11:42 +0000 | [diff] [blame] | 439 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl |
| 440 | *Method); |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 441 | virtual llvm::Constant *GetEHType(QualType T); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 442 | |
| 443 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
Fariborz Jahanian | 0196a1c | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 444 | const ObjCContainerDecl *CD); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 445 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
| 446 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | cb46385 | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 447 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 448 | const ObjCProtocolDecl *PD); |
| 449 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 450 | virtual llvm::Function *ModuleInitFunction(); |
David Chisnall | 3fe8956 | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 451 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 452 | virtual llvm::Constant *GetPropertySetFunction(); |
| 453 | virtual llvm::Constant *GetSetStructFunction(); |
| 454 | virtual llvm::Constant *GetGetStructFunction(); |
Daniel Dunbar | c46a079 | 2009-07-24 07:40:24 +0000 | [diff] [blame] | 455 | virtual llvm::Constant *EnumerationMutationFunction(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 457 | virtual void EmitTryStmt(CodeGenFunction &CGF, |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 458 | const ObjCAtTryStmt &S); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 459 | virtual void EmitSynchronizedStmt(CodeGenFunction &CGF, |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 460 | const ObjCAtSynchronizedStmt &S); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 461 | virtual void EmitThrowStmt(CodeGenFunction &CGF, |
Anders Carlsson | 1963b0c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 462 | const ObjCAtThrowStmt &S); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 463 | virtual llvm::Value * EmitObjCWeakRead(CodeGenFunction &CGF, |
Fariborz Jahanian | f5125d1 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 464 | llvm::Value *AddrWeakObj); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 465 | virtual void EmitObjCWeakAssign(CodeGenFunction &CGF, |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 466 | llvm::Value *src, llvm::Value *dst); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 467 | virtual void EmitObjCGlobalAssign(CodeGenFunction &CGF, |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 468 | llvm::Value *src, llvm::Value *dest, |
| 469 | bool threadlocal=false); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 470 | virtual void EmitObjCIvarAssign(CodeGenFunction &CGF, |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 471 | llvm::Value *src, llvm::Value *dest, |
| 472 | llvm::Value *ivarOffset); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 473 | virtual void EmitObjCStrongCastAssign(CodeGenFunction &CGF, |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 474 | llvm::Value *src, llvm::Value *dest); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 475 | virtual void EmitGCMemmoveCollectable(CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 476 | llvm::Value *DestPtr, |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 477 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 478 | llvm::Value *Size); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 479 | virtual LValue EmitObjCValueForIvar(CodeGenFunction &CGF, |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 480 | QualType ObjectTy, |
| 481 | llvm::Value *BaseValue, |
| 482 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 483 | unsigned CVRQualifiers); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 484 | virtual llvm::Value *EmitIvarOffset(CodeGenFunction &CGF, |
Daniel Dunbar | 722f424 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 485 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 486 | const ObjCIvarDecl *Ivar); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 487 | virtual llvm::Constant *BuildGCBlockLayout(CodeGenModule &CGM, |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 488 | const CGBlockInfo &blockInfo) { |
Fariborz Jahanian | c05349e | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 489 | return NULLPtr; |
| 490 | } |
Fariborz Jahanian | 7bd3d1c | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 491 | |
| 492 | virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) { |
| 493 | return 0; |
| 494 | } |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 495 | }; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 496 | /// Class representing the legacy GCC Objective-C ABI. This is the default when |
| 497 | /// -fobjc-nonfragile-abi is not specified. |
| 498 | /// |
| 499 | /// The GCC ABI target actually generates code that is approximately compatible |
| 500 | /// with the new GNUstep runtime ABI, but refrains from using any features that |
| 501 | /// would not work with the GCC runtime. For example, clang always generates |
| 502 | /// the extended form of the class structure, and the extra fields are simply |
| 503 | /// ignored by GCC libobjc. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 504 | class CGObjCGCC : public CGObjCGNU { |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 505 | /// The GCC ABI message lookup function. Returns an IMP pointing to the |
| 506 | /// method implementation for this message. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 507 | LazyRuntimeFunction MsgLookupFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 508 | /// The GCC ABI superclass message lookup function. Takes a pointer to a |
| 509 | /// structure describing the receiver and the class, and a selector as |
| 510 | /// arguments. Returns the IMP for the corresponding method. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 511 | LazyRuntimeFunction MsgLookupSuperFn; |
| 512 | protected: |
| 513 | virtual llvm::Value *LookupIMP(CodeGenFunction &CGF, |
| 514 | llvm::Value *&Receiver, |
| 515 | llvm::Value *cmd, |
| 516 | llvm::MDNode *node) { |
| 517 | CGBuilderTy &Builder = CGF.Builder; |
| 518 | llvm::Value *imp = Builder.CreateCall2(MsgLookupFn, |
| 519 | EnforceType(Builder, Receiver, IdTy), |
| 520 | EnforceType(Builder, cmd, SelectorTy)); |
| 521 | cast<llvm::CallInst>(imp)->setMetadata(msgSendMDKind, node); |
| 522 | return imp; |
| 523 | } |
| 524 | virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, |
| 525 | llvm::Value *ObjCSuper, |
| 526 | llvm::Value *cmd) { |
| 527 | CGBuilderTy &Builder = CGF.Builder; |
| 528 | llvm::Value *lookupArgs[] = {EnforceType(Builder, ObjCSuper, |
| 529 | PtrToObjCSuperTy), cmd}; |
| 530 | return Builder.CreateCall(MsgLookupSuperFn, lookupArgs, lookupArgs+2); |
| 531 | } |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 532 | public: |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 533 | CGObjCGCC(CodeGenModule &Mod) : CGObjCGNU(Mod, 8, 2) { |
| 534 | // IMP objc_msg_lookup(id, SEL); |
| 535 | MsgLookupFn.init(&CGM, "objc_msg_lookup", IMPTy, IdTy, SelectorTy, NULL); |
| 536 | // IMP objc_msg_lookup_super(struct objc_super*, SEL); |
| 537 | MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy, |
| 538 | PtrToObjCSuperTy, SelectorTy, NULL); |
| 539 | } |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 540 | }; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 541 | /// Class used when targeting the new GNUstep runtime ABI. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 542 | class CGObjCGNUstep : public CGObjCGNU { |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 543 | /// The slot lookup function. Returns a pointer to a cacheable structure |
| 544 | /// that contains (among other things) the IMP. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 545 | LazyRuntimeFunction SlotLookupFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 546 | /// The GNUstep ABI superclass message lookup function. Takes a pointer to |
| 547 | /// a structure describing the receiver and the class, and a selector as |
| 548 | /// arguments. Returns the slot for the corresponding method. Superclass |
| 549 | /// message lookup rarely changes, so this is a good caching opportunity. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 550 | LazyRuntimeFunction SlotLookupSuperFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 551 | /// Type of an slot structure pointer. This is returned by the various |
| 552 | /// lookup functions. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 553 | llvm::Type *SlotTy; |
| 554 | protected: |
| 555 | virtual llvm::Value *LookupIMP(CodeGenFunction &CGF, |
| 556 | llvm::Value *&Receiver, |
| 557 | llvm::Value *cmd, |
| 558 | llvm::MDNode *node) { |
| 559 | CGBuilderTy &Builder = CGF.Builder; |
| 560 | llvm::Function *LookupFn = SlotLookupFn; |
| 561 | |
| 562 | // Store the receiver on the stack so that we can reload it later |
| 563 | llvm::Value *ReceiverPtr = CGF.CreateTempAlloca(Receiver->getType()); |
| 564 | Builder.CreateStore(Receiver, ReceiverPtr); |
| 565 | |
| 566 | llvm::Value *self; |
| 567 | |
| 568 | if (isa<ObjCMethodDecl>(CGF.CurCodeDecl)) { |
| 569 | self = CGF.LoadObjCSelf(); |
| 570 | } else { |
| 571 | self = llvm::ConstantPointerNull::get(IdTy); |
| 572 | } |
| 573 | |
| 574 | // The lookup function is guaranteed not to capture the receiver pointer. |
| 575 | LookupFn->setDoesNotCapture(1); |
| 576 | |
| 577 | llvm::CallInst *slot = |
| 578 | Builder.CreateCall3(LookupFn, |
| 579 | EnforceType(Builder, ReceiverPtr, PtrToIdTy), |
| 580 | EnforceType(Builder, cmd, SelectorTy), |
| 581 | EnforceType(Builder, self, IdTy)); |
| 582 | slot->setOnlyReadsMemory(); |
| 583 | slot->setMetadata(msgSendMDKind, node); |
| 584 | |
| 585 | // Load the imp from the slot |
| 586 | llvm::Value *imp = Builder.CreateLoad(Builder.CreateStructGEP(slot, 4)); |
| 587 | |
| 588 | // The lookup function may have changed the receiver, so make sure we use |
| 589 | // the new one. |
| 590 | Receiver = Builder.CreateLoad(ReceiverPtr, true); |
| 591 | return imp; |
| 592 | } |
| 593 | virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, |
| 594 | llvm::Value *ObjCSuper, |
| 595 | llvm::Value *cmd) { |
| 596 | CGBuilderTy &Builder = CGF.Builder; |
| 597 | llvm::Value *lookupArgs[] = {ObjCSuper, cmd}; |
| 598 | |
| 599 | llvm::CallInst *slot = Builder.CreateCall(SlotLookupSuperFn, lookupArgs, |
| 600 | lookupArgs+2); |
| 601 | slot->setOnlyReadsMemory(); |
| 602 | |
| 603 | return Builder.CreateLoad(Builder.CreateStructGEP(slot, 4)); |
| 604 | } |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 605 | public: |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 606 | CGObjCGNUstep(CodeGenModule &Mod) : CGObjCGNU(Mod, 9, 3) { |
| 607 | llvm::StructType *SlotStructTy = llvm::StructType::get(VMContext, PtrTy, |
| 608 | PtrTy, PtrTy, IntTy, IMPTy, NULL); |
| 609 | SlotTy = llvm::PointerType::getUnqual(SlotStructTy); |
| 610 | // Slot_t objc_msg_lookup_sender(id *receiver, SEL selector, id sender); |
| 611 | SlotLookupFn.init(&CGM, "objc_msg_lookup_sender", SlotTy, PtrToIdTy, |
| 612 | SelectorTy, IdTy, NULL); |
| 613 | // Slot_t objc_msg_lookup_super(struct objc_super*, SEL); |
| 614 | SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy, |
| 615 | PtrToObjCSuperTy, SelectorTy, NULL); |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 616 | // If we're in ObjC++ mode, then we want to make |
| 617 | if (CGM.getLangOptions().CPlusPlus) { |
| 618 | const llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext); |
| 619 | // void *__cxa_begin_catch(void *e) |
| 620 | EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy, NULL); |
| 621 | // void __cxa_end_catch(void) |
| 622 | EnterCatchFn.init(&CGM, "__cxa_end_catch", VoidTy, NULL); |
| 623 | // void _Unwind_Resume_or_Rethrow(void*) |
David Chisnall | ec343e8 | 2011-04-05 17:15:18 +0000 | [diff] [blame] | 624 | ExceptionReThrowFn.init(&CGM, "_Unwind_Resume_or_Rethrow", VoidTy, PtrTy, NULL); |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 625 | } |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 626 | } |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 627 | }; |
| 628 | |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 629 | } // end anonymous namespace |
| 630 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 631 | |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 632 | /// Emits a reference to a dummy variable which is emitted with each class. |
| 633 | /// This ensures that a linker error will be generated when trying to link |
| 634 | /// together modules where a referenced class is not defined. |
Mike Stump | dd93a19 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 635 | void CGObjCGNU::EmitClassRef(const std::string &className) { |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 636 | std::string symbolRef = "__objc_class_ref_" + className; |
| 637 | // Don't emit two copies of the same symbol |
Mike Stump | dd93a19 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 638 | if (TheModule.getGlobalVariable(symbolRef)) |
| 639 | return; |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 640 | std::string symbolName = "__objc_class_name_" + className; |
| 641 | llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(symbolName); |
| 642 | if (!ClassSymbol) { |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 643 | ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false, |
| 644 | llvm::GlobalValue::ExternalLinkage, 0, symbolName); |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 645 | } |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 646 | new llvm::GlobalVariable(TheModule, ClassSymbol->getType(), true, |
Chris Lattner | c58e569 | 2009-08-05 05:25:18 +0000 | [diff] [blame] | 647 | llvm::GlobalValue::WeakAnyLinkage, ClassSymbol, symbolRef); |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 648 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 649 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 650 | static std::string SymbolNameForMethod(const llvm::StringRef &ClassName, |
| 651 | const llvm::StringRef &CategoryName, const Selector MethodName, |
| 652 | bool isClassMethod) { |
| 653 | std::string MethodNameColonStripped = MethodName.getAsString(); |
David Chisnall | 035ead2 | 2010-01-14 14:08:19 +0000 | [diff] [blame] | 654 | std::replace(MethodNameColonStripped.begin(), MethodNameColonStripped.end(), |
| 655 | ':', '_'); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 656 | return (llvm::Twine(isClassMethod ? "_c_" : "_i_") + ClassName + "_" + |
| 657 | CategoryName + "_" + MethodNameColonStripped).str(); |
David Chisnall | 0a24fd3 | 2010-05-08 20:58:05 +0000 | [diff] [blame] | 658 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 659 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 660 | CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion, |
| 661 | unsigned protocolClassVersion) |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 662 | : CGM(cgm), TheModule(CGM.getModule()), VMContext(cgm.getLLVMContext()), |
| 663 | ClassPtrAlias(0), MetaClassPtrAlias(0), RuntimeVersion(runtimeABIVersion), |
| 664 | ProtocolVersion(protocolClassVersion) { |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 665 | |
| 666 | msgSendMDKind = VMContext.getMDKindID("GNUObjCMessageSend"); |
| 667 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 668 | CodeGenTypes &Types = CGM.getTypes(); |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 669 | IntTy = cast<llvm::IntegerType>( |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 670 | Types.ConvertType(CGM.getContext().IntTy)); |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 671 | LongTy = cast<llvm::IntegerType>( |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 672 | Types.ConvertType(CGM.getContext().LongTy)); |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 673 | SizeTy = cast<llvm::IntegerType>( |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 674 | Types.ConvertType(CGM.getContext().getSizeType())); |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 675 | PtrDiffTy = cast<llvm::IntegerType>( |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 676 | Types.ConvertType(CGM.getContext().getPointerDiffType())); |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 677 | BoolTy = CGM.getTypes().ConvertType(CGM.getContext().BoolTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 678 | |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 679 | Int8Ty = llvm::Type::getInt8Ty(VMContext); |
| 680 | // C string type. Used in lots of places. |
| 681 | PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty); |
| 682 | |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 683 | Zeros[0] = llvm::ConstantInt::get(LongTy, 0); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 684 | Zeros[1] = Zeros[0]; |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 685 | NULLPtr = llvm::ConstantPointerNull::get(PtrToInt8Ty); |
Chris Lattner | 4bd5596 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 686 | // Get the selector Type. |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 687 | QualType selTy = CGM.getContext().getObjCSelType(); |
| 688 | if (QualType() == selTy) { |
| 689 | SelectorTy = PtrToInt8Ty; |
| 690 | } else { |
| 691 | SelectorTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(selTy)); |
| 692 | } |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 693 | |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 694 | PtrToIntTy = llvm::PointerType::getUnqual(IntTy); |
Chris Lattner | 4bd5596 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 695 | PtrTy = PtrToInt8Ty; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 696 | |
Chris Lattner | 4bd5596 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 697 | // Object type |
David Chisnall | 10d2ded | 2011-04-29 14:10:35 +0000 | [diff] [blame] | 698 | QualType UnqualIdTy = CGM.getContext().getObjCIdType(); |
| 699 | ASTIdTy = CanQualType(); |
| 700 | if (UnqualIdTy != QualType()) { |
| 701 | ASTIdTy = CGM.getContext().getCanonicalType(UnqualIdTy); |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 702 | IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy)); |
David Chisnall | 10d2ded | 2011-04-29 14:10:35 +0000 | [diff] [blame] | 703 | } else { |
| 704 | IdTy = PtrToInt8Ty; |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 705 | } |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 706 | PtrToIdTy = llvm::PointerType::getUnqual(IdTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 707 | |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 708 | ObjCSuperTy = llvm::StructType::get(VMContext, IdTy, IdTy, NULL); |
| 709 | PtrToObjCSuperTy = llvm::PointerType::getUnqual(ObjCSuperTy); |
| 710 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 711 | const llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext); |
| 712 | |
| 713 | // void objc_exception_throw(id); |
| 714 | ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy, NULL); |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 715 | ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy, NULL); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 716 | // int objc_sync_enter(id); |
| 717 | SyncEnterFn.init(&CGM, "objc_sync_enter", IntTy, IdTy, NULL); |
| 718 | // int objc_sync_exit(id); |
| 719 | SyncExitFn.init(&CGM, "objc_sync_exit", IntTy, IdTy, NULL); |
| 720 | |
| 721 | // void objc_enumerationMutation (id) |
| 722 | EnumerationMutationFn.init(&CGM, "objc_enumerationMutation", VoidTy, |
| 723 | IdTy, NULL); |
| 724 | |
| 725 | // id objc_getProperty(id, SEL, ptrdiff_t, BOOL) |
| 726 | GetPropertyFn.init(&CGM, "objc_getProperty", IdTy, IdTy, SelectorTy, |
| 727 | PtrDiffTy, BoolTy, NULL); |
| 728 | // void objc_setProperty(id, SEL, ptrdiff_t, id, BOOL, BOOL) |
| 729 | SetPropertyFn.init(&CGM, "objc_setProperty", VoidTy, IdTy, SelectorTy, |
| 730 | PtrDiffTy, IdTy, BoolTy, BoolTy, NULL); |
| 731 | // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL) |
| 732 | GetStructPropertyFn.init(&CGM, "objc_getPropertyStruct", VoidTy, PtrTy, PtrTy, |
| 733 | PtrDiffTy, BoolTy, BoolTy, NULL); |
| 734 | // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL) |
| 735 | SetStructPropertyFn.init(&CGM, "objc_setPropertyStruct", VoidTy, PtrTy, PtrTy, |
| 736 | PtrDiffTy, BoolTy, BoolTy, NULL); |
| 737 | |
Chris Lattner | 4bd5596 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 738 | // IMP type |
| 739 | std::vector<const llvm::Type*> IMPArgs; |
| 740 | IMPArgs.push_back(IdTy); |
| 741 | IMPArgs.push_back(SelectorTy); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 742 | IMPTy = llvm::PointerType::getUnqual(llvm::FunctionType::get(IdTy, IMPArgs, |
| 743 | true)); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 744 | |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 745 | // Don't bother initialising the GC stuff unless we're compiling in GC mode |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 746 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) { |
David Chisnall | 5c51177 | 2011-05-22 22:37:08 +0000 | [diff] [blame] | 747 | // This is a bit of an hack. We should sort this out by having a proper |
| 748 | // CGObjCGNUstep subclass for GC, but we may want to really support the old |
| 749 | // ABI and GC added in ObjectiveC2.framework, so we fudge it a bit for now |
| 750 | RuntimeVersion = 10; |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 751 | // Get selectors needed in GC mode |
| 752 | RetainSel = GetNullarySelector("retain", CGM.getContext()); |
| 753 | ReleaseSel = GetNullarySelector("release", CGM.getContext()); |
| 754 | AutoreleaseSel = GetNullarySelector("autorelease", CGM.getContext()); |
| 755 | |
| 756 | // Get functions needed in GC mode |
| 757 | |
| 758 | // id objc_assign_ivar(id, id, ptrdiff_t); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 759 | IvarAssignFn.init(&CGM, "objc_assign_ivar", IdTy, IdTy, IdTy, PtrDiffTy, |
| 760 | NULL); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 761 | // id objc_assign_strongCast (id, id*) |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 762 | StrongCastAssignFn.init(&CGM, "objc_assign_strongCast", IdTy, IdTy, |
| 763 | PtrToIdTy, NULL); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 764 | // id objc_assign_global(id, id*); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 765 | GlobalAssignFn.init(&CGM, "objc_assign_global", IdTy, IdTy, PtrToIdTy, |
| 766 | NULL); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 767 | // id objc_assign_weak(id, id*); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 768 | WeakAssignFn.init(&CGM, "objc_assign_weak", IdTy, IdTy, PtrToIdTy, NULL); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 769 | // id objc_read_weak(id*); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 770 | WeakReadFn.init(&CGM, "objc_read_weak", IdTy, PtrToIdTy, NULL); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 771 | // void *objc_memmove_collectable(void*, void *, size_t); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 772 | MemMoveFn.init(&CGM, "objc_memmove_collectable", PtrTy, PtrTy, PtrTy, |
| 773 | SizeTy, NULL); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 774 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 775 | } |
Mike Stump | dd93a19 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 776 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 777 | // This has to perform the lookup every time, since posing and related |
| 778 | // techniques can modify the name -> class mapping. |
Daniel Dunbar | cb46385 | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 779 | llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 7c6d3a7 | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 780 | const ObjCInterfaceDecl *OID) { |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 781 | llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString()); |
David Chisnall | df34917 | 2010-01-08 00:14:31 +0000 | [diff] [blame] | 782 | // With the incompatible ABI, this will need to be replaced with a direct |
| 783 | // reference to the class symbol. For the compatible nonfragile ABI we are |
| 784 | // still performing this lookup at run time but emitting the symbol for the |
| 785 | // class externally so that we can make the switch later. |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 786 | EmitClassRef(OID->getNameAsString()); |
Daniel Dunbar | 7c6d3a7 | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 787 | ClassName = Builder.CreateStructGEP(ClassName, 0); |
| 788 | |
Fariborz Jahanian | 3b636c1 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 789 | std::vector<const llvm::Type*> Params(1, PtrToInt8Ty); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 790 | llvm::Constant *ClassLookupFn = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 791 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, |
Fariborz Jahanian | 3b636c1 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 792 | Params, |
| 793 | true), |
| 794 | "objc_lookup_class"); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 795 | return Builder.CreateCall(ClassLookupFn, ClassName); |
Chris Lattner | 4bd5596 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 796 | } |
| 797 | |
Fariborz Jahanian | 9240f3d | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 798 | llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel, |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 799 | const std::string &TypeEncoding, bool lval) { |
| 800 | |
| 801 | llvm::SmallVector<TypedSelector, 2> &Types = SelectorTable[Sel]; |
| 802 | llvm::GlobalAlias *SelValue = 0; |
| 803 | |
| 804 | |
| 805 | for (llvm::SmallVectorImpl<TypedSelector>::iterator i = Types.begin(), |
| 806 | e = Types.end() ; i!=e ; i++) { |
| 807 | if (i->first == TypeEncoding) { |
| 808 | SelValue = i->second; |
| 809 | break; |
| 810 | } |
| 811 | } |
| 812 | if (0 == SelValue) { |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 813 | SelValue = new llvm::GlobalAlias(SelectorTy, |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 814 | llvm::GlobalValue::PrivateLinkage, |
| 815 | ".objc_selector_"+Sel.getAsString(), NULL, |
| 816 | &TheModule); |
| 817 | Types.push_back(TypedSelector(TypeEncoding, SelValue)); |
| 818 | } |
| 819 | |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 820 | if (lval) { |
| 821 | llvm::Value *tmp = Builder.CreateAlloca(SelValue->getType()); |
| 822 | Builder.CreateStore(SelValue, tmp); |
| 823 | return tmp; |
| 824 | } |
| 825 | return SelValue; |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 829 | bool lval) { |
| 830 | return GetSelector(Builder, Sel, std::string(), lval); |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Daniel Dunbar | 45858d2 | 2010-02-03 20:11:42 +0000 | [diff] [blame] | 833 | llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 834 | *Method) { |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 835 | std::string SelTypes; |
| 836 | CGM.getContext().getObjCEncodingForMethodDecl(Method, SelTypes); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 837 | return GetSelector(Builder, Method->getSelector(), SelTypes, false); |
Chris Lattner | 6d522c0 | 2008-06-26 04:37:12 +0000 | [diff] [blame] | 838 | } |
| 839 | |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 840 | llvm::Constant *CGObjCGNU::GetEHType(QualType T) { |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 841 | if (!CGM.getLangOptions().CPlusPlus) { |
| 842 | if (T->isObjCIdType() |
| 843 | || T->isObjCQualifiedIdType()) { |
| 844 | // With the old ABI, there was only one kind of catchall, which broke |
| 845 | // foreign exceptions. With the new ABI, we use __objc_id_typeinfo as |
| 846 | // a pointer indicating object catchalls, and NULL to indicate real |
| 847 | // catchalls |
| 848 | if (CGM.getLangOptions().ObjCNonFragileABI) { |
| 849 | return MakeConstantString("@id"); |
| 850 | } else { |
| 851 | return 0; |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | // All other types should be Objective-C interface pointer types. |
| 856 | const ObjCObjectPointerType *OPT = |
| 857 | T->getAs<ObjCObjectPointerType>(); |
| 858 | assert(OPT && "Invalid @catch type."); |
| 859 | const ObjCInterfaceDecl *IDecl = |
| 860 | OPT->getObjectType()->getInterface(); |
| 861 | assert(IDecl && "Invalid @catch type."); |
| 862 | return MakeConstantString(IDecl->getIdentifier()->getName()); |
| 863 | } |
David Chisnall | e1d2584d | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 864 | // For Objective-C++, we want to provide the ability to catch both C++ and |
| 865 | // Objective-C objects in the same function. |
| 866 | |
| 867 | // There's a particular fixed type info for 'id'. |
| 868 | if (T->isObjCIdType() || |
| 869 | T->isObjCQualifiedIdType()) { |
| 870 | llvm::Constant *IDEHType = |
| 871 | CGM.getModule().getGlobalVariable("__objc_id_type_info"); |
| 872 | if (!IDEHType) |
| 873 | IDEHType = |
| 874 | new llvm::GlobalVariable(CGM.getModule(), PtrToInt8Ty, |
| 875 | false, |
| 876 | llvm::GlobalValue::ExternalLinkage, |
| 877 | 0, "__objc_id_type_info"); |
| 878 | return llvm::ConstantExpr::getBitCast(IDEHType, PtrToInt8Ty); |
| 879 | } |
| 880 | |
| 881 | const ObjCObjectPointerType *PT = |
| 882 | T->getAs<ObjCObjectPointerType>(); |
| 883 | assert(PT && "Invalid @catch type."); |
| 884 | const ObjCInterfaceType *IT = PT->getInterfaceType(); |
| 885 | assert(IT && "Invalid @catch type."); |
| 886 | std::string className = IT->getDecl()->getIdentifier()->getName(); |
| 887 | |
| 888 | std::string typeinfoName = "__objc_eh_typeinfo_" + className; |
| 889 | |
| 890 | // Return the existing typeinfo if it exists |
| 891 | llvm::Constant *typeinfo = TheModule.getGlobalVariable(typeinfoName); |
| 892 | if (typeinfo) return typeinfo; |
| 893 | |
| 894 | // Otherwise create it. |
| 895 | |
| 896 | // vtable for gnustep::libobjc::__objc_class_type_info |
| 897 | // It's quite ugly hard-coding this. Ideally we'd generate it using the host |
| 898 | // platform's name mangling. |
| 899 | const char *vtableName = "_ZTVN7gnustep7libobjc22__objc_class_type_infoE"; |
| 900 | llvm::Constant *Vtable = TheModule.getGlobalVariable(vtableName); |
| 901 | if (!Vtable) { |
| 902 | Vtable = new llvm::GlobalVariable(TheModule, PtrToInt8Ty, true, |
| 903 | llvm::GlobalValue::ExternalLinkage, 0, vtableName); |
| 904 | } |
| 905 | llvm::Constant *Two = llvm::ConstantInt::get(IntTy, 2); |
| 906 | Vtable = llvm::ConstantExpr::getGetElementPtr(Vtable, &Two, 1); |
| 907 | Vtable = llvm::ConstantExpr::getBitCast(Vtable, PtrToInt8Ty); |
| 908 | |
| 909 | llvm::Constant *typeName = |
| 910 | ExportUniqueString(className, "__objc_eh_typename_"); |
| 911 | |
| 912 | std::vector<llvm::Constant*> fields; |
| 913 | fields.push_back(Vtable); |
| 914 | fields.push_back(typeName); |
| 915 | llvm::Constant *TI = |
| 916 | MakeGlobal(llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty, |
| 917 | NULL), fields, "__objc_eh_typeinfo_" + className, |
| 918 | llvm::GlobalValue::LinkOnceODRLinkage); |
| 919 | return llvm::ConstantExpr::getBitCast(TI, PtrToInt8Ty); |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 922 | /// Generate an NSConstantString object. |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 923 | llvm::Constant *CGObjCGNU::GenerateConstantString(const StringLiteral *SL) { |
David Chisnall | 358e751 | 2010-01-27 12:49:23 +0000 | [diff] [blame] | 924 | |
Benjamin Kramer | 35b077e | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 925 | std::string Str = SL->getString().str(); |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 926 | |
David Chisnall | 358e751 | 2010-01-27 12:49:23 +0000 | [diff] [blame] | 927 | // Look for an existing one |
| 928 | llvm::StringMap<llvm::Constant*>::iterator old = ObjCStrings.find(Str); |
| 929 | if (old != ObjCStrings.end()) |
| 930 | return old->getValue(); |
| 931 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 932 | std::vector<llvm::Constant*> Ivars; |
| 933 | Ivars.push_back(NULLPtr); |
Chris Lattner | 091f698 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 934 | Ivars.push_back(MakeConstantString(Str)); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 935 | Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size())); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 936 | llvm::Constant *ObjCStr = MakeGlobal( |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 937 | llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL), |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 938 | Ivars, ".objc_str"); |
David Chisnall | 358e751 | 2010-01-27 12:49:23 +0000 | [diff] [blame] | 939 | ObjCStr = llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty); |
| 940 | ObjCStrings[Str] = ObjCStr; |
| 941 | ConstantStrings.push_back(ObjCStr); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 942 | return ObjCStr; |
| 943 | } |
| 944 | |
| 945 | ///Generates a message send where the super is the receiver. This is a message |
| 946 | ///send to self with special delivery semantics indicating which class's method |
| 947 | ///should be called. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 948 | RValue |
| 949 | CGObjCGNU::GenerateMessageSendSuper(CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 950 | ReturnValueSlot Return, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 951 | QualType ResultType, |
| 952 | Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 953 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 954 | bool isCategoryImpl, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 955 | llvm::Value *Receiver, |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 956 | bool IsClassMessage, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 957 | const CallArgList &CallArgs, |
| 958 | const ObjCMethodDecl *Method) { |
David Chisnall | 8a42d19 | 2011-05-28 14:09:01 +0000 | [diff] [blame^] | 959 | CGBuilderTy &Builder = CGF.Builder; |
David Chisnall | 1254f6e | 2011-05-23 23:13:25 +0000 | [diff] [blame] | 960 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) { |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 961 | if (Sel == RetainSel || Sel == AutoreleaseSel) { |
David Chisnall | 8a42d19 | 2011-05-28 14:09:01 +0000 | [diff] [blame^] | 962 | return RValue::get(EnforceType(Builder, Receiver, |
| 963 | CGM.getTypes().ConvertType(ResultType))); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 964 | } |
| 965 | if (Sel == ReleaseSel) { |
| 966 | return RValue::get(0); |
| 967 | } |
| 968 | } |
David Chisnall | ea529a4 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 969 | |
David Chisnall | ea529a4 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 970 | llvm::Value *cmd = GetSelector(Builder, Sel); |
| 971 | |
Fariborz Jahanian | b73a23e | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 972 | |
| 973 | CallArgList ActualArgs; |
| 974 | |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 975 | ActualArgs.add(RValue::get(EnforceType(Builder, Receiver, IdTy)), ASTIdTy); |
| 976 | ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType()); |
Fariborz Jahanian | b73a23e | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 977 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
| 978 | |
| 979 | CodeGenTypes &Types = CGM.getTypes(); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 980 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 981 | FunctionType::ExtInfo()); |
Fariborz Jahanian | b73a23e | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 982 | |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 983 | llvm::Value *ReceiverClass = 0; |
Chris Lattner | a02cb80 | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 984 | if (isCategoryImpl) { |
| 985 | llvm::Constant *classLookupFunction = 0; |
| 986 | std::vector<const llvm::Type*> Params; |
| 987 | Params.push_back(PtrTy); |
| 988 | if (IsClassMessage) { |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 989 | classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get( |
Chris Lattner | a02cb80 | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 990 | IdTy, Params, true), "objc_get_meta_class"); |
| 991 | } else { |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 992 | classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get( |
Chris Lattner | a02cb80 | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 993 | IdTy, Params, true), "objc_get_class"); |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 994 | } |
David Chisnall | ea529a4 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 995 | ReceiverClass = Builder.CreateCall(classLookupFunction, |
Chris Lattner | a02cb80 | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 996 | MakeConstantString(Class->getNameAsString())); |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 997 | } else { |
Chris Lattner | a02cb80 | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 998 | // Set up global aliases for the metaclass or class pointer if they do not |
| 999 | // already exist. These will are forward-references which will be set to |
Mike Stump | dd93a19 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 1000 | // pointers to the class and metaclass structure created for the runtime |
| 1001 | // load function. To send a message to super, we look up the value of the |
Chris Lattner | a02cb80 | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 1002 | // super_class pointer from either the class or metaclass structure. |
| 1003 | if (IsClassMessage) { |
| 1004 | if (!MetaClassPtrAlias) { |
| 1005 | MetaClassPtrAlias = new llvm::GlobalAlias(IdTy, |
| 1006 | llvm::GlobalValue::InternalLinkage, ".objc_metaclass_ref" + |
| 1007 | Class->getNameAsString(), NULL, &TheModule); |
| 1008 | } |
| 1009 | ReceiverClass = MetaClassPtrAlias; |
| 1010 | } else { |
| 1011 | if (!ClassPtrAlias) { |
| 1012 | ClassPtrAlias = new llvm::GlobalAlias(IdTy, |
| 1013 | llvm::GlobalValue::InternalLinkage, ".objc_class_ref" + |
| 1014 | Class->getNameAsString(), NULL, &TheModule); |
| 1015 | } |
| 1016 | ReceiverClass = ClassPtrAlias; |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1017 | } |
Chris Lattner | c06ce0f | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 1018 | } |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1019 | // Cast the pointer to a simplified version of the class structure |
David Chisnall | ea529a4 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 1020 | ReceiverClass = Builder.CreateBitCast(ReceiverClass, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1021 | llvm::PointerType::getUnqual( |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1022 | llvm::StructType::get(VMContext, IdTy, IdTy, NULL))); |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1023 | // Get the superclass pointer |
David Chisnall | ea529a4 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 1024 | ReceiverClass = Builder.CreateStructGEP(ReceiverClass, 1); |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1025 | // Load the superclass pointer |
David Chisnall | ea529a4 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 1026 | ReceiverClass = Builder.CreateLoad(ReceiverClass); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1027 | // Construct the structure used to look up the IMP |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1028 | llvm::StructType *ObjCSuperTy = llvm::StructType::get(VMContext, |
| 1029 | Receiver->getType(), IdTy, NULL); |
David Chisnall | ea529a4 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 1030 | llvm::Value *ObjCSuper = Builder.CreateAlloca(ObjCSuperTy); |
Fariborz Jahanian | b73a23e | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 1031 | |
David Chisnall | ea529a4 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 1032 | Builder.CreateStore(Receiver, Builder.CreateStructGEP(ObjCSuper, 0)); |
| 1033 | Builder.CreateStore(ReceiverClass, Builder.CreateStructGEP(ObjCSuper, 1)); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1034 | |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1035 | ObjCSuper = EnforceType(Builder, ObjCSuper, PtrToObjCSuperTy); |
| 1036 | const llvm::FunctionType *impType = |
| 1037 | Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false); |
| 1038 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1039 | // Get the IMP |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1040 | llvm::Value *imp = LookupIMPSuper(CGF, ObjCSuper, cmd); |
| 1041 | imp = EnforceType(Builder, imp, llvm::PointerType::getUnqual(impType)); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1042 | |
David Chisnall | 9eecafa | 2010-05-01 11:15:56 +0000 | [diff] [blame] | 1043 | llvm::Value *impMD[] = { |
| 1044 | llvm::MDString::get(VMContext, Sel.getAsString()), |
| 1045 | llvm::MDString::get(VMContext, Class->getSuperClass()->getNameAsString()), |
| 1046 | llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsClassMessage) |
| 1047 | }; |
Jay Foad | ea324f1 | 2011-04-21 19:59:12 +0000 | [diff] [blame] | 1048 | llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD); |
David Chisnall | 9eecafa | 2010-05-01 11:15:56 +0000 | [diff] [blame] | 1049 | |
David Chisnall | ff5f88c | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 1050 | llvm::Instruction *call; |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1051 | RValue msgRet = CGF.EmitCall(FnInfo, imp, Return, ActualArgs, |
David Chisnall | ff5f88c | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 1052 | 0, &call); |
| 1053 | call->setMetadata(msgSendMDKind, node); |
| 1054 | return msgRet; |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1057 | /// Generate code for a message send expression. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1058 | RValue |
| 1059 | CGObjCGNU::GenerateMessageSend(CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1060 | ReturnValueSlot Return, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1061 | QualType ResultType, |
| 1062 | Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1063 | llvm::Value *Receiver, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1064 | const CallArgList &CallArgs, |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1065 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1066 | const ObjCMethodDecl *Method) { |
David Chisnall | 8a42d19 | 2011-05-28 14:09:01 +0000 | [diff] [blame^] | 1067 | CGBuilderTy &Builder = CGF.Builder; |
| 1068 | |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1069 | // Strip out message sends to retain / release in GC mode |
David Chisnall | 1254f6e | 2011-05-23 23:13:25 +0000 | [diff] [blame] | 1070 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) { |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 1071 | if (Sel == RetainSel || Sel == AutoreleaseSel) { |
David Chisnall | 8a42d19 | 2011-05-28 14:09:01 +0000 | [diff] [blame^] | 1072 | return RValue::get(EnforceType(Builder, Receiver, |
| 1073 | CGM.getTypes().ConvertType(ResultType))); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 1074 | } |
| 1075 | if (Sel == ReleaseSel) { |
| 1076 | return RValue::get(0); |
| 1077 | } |
| 1078 | } |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1079 | |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1080 | // If the return type is something that goes in an integer register, the |
| 1081 | // runtime will handle 0 returns. For other cases, we fill in the 0 value |
| 1082 | // ourselves. |
| 1083 | // |
| 1084 | // The language spec says the result of this kind of message send is |
| 1085 | // undefined, but lots of people seem to have forgotten to read that |
| 1086 | // paragraph and insist on sending messages to nil that have structure |
| 1087 | // returns. With GCC, this generates a random return value (whatever happens |
| 1088 | // to be on the stack / in those registers at the time) on most platforms, |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1089 | // and generates an illegal instruction trap on SPARC. With LLVM it corrupts |
| 1090 | // the stack. |
| 1091 | bool isPointerSizedReturn = (ResultType->isAnyPointerType() || |
| 1092 | ResultType->isIntegralOrEnumerationType() || ResultType->isVoidType()); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1093 | |
| 1094 | llvm::BasicBlock *startBB = 0; |
| 1095 | llvm::BasicBlock *messageBB = 0; |
David Chisnall | 29cefd1 | 2010-05-20 13:45:48 +0000 | [diff] [blame] | 1096 | llvm::BasicBlock *continueBB = 0; |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1097 | |
| 1098 | if (!isPointerSizedReturn) { |
| 1099 | startBB = Builder.GetInsertBlock(); |
| 1100 | messageBB = CGF.createBasicBlock("msgSend"); |
David Chisnall | 29cefd1 | 2010-05-20 13:45:48 +0000 | [diff] [blame] | 1101 | continueBB = CGF.createBasicBlock("continue"); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1102 | |
| 1103 | llvm::Value *isNil = Builder.CreateICmpEQ(Receiver, |
| 1104 | llvm::Constant::getNullValue(Receiver->getType())); |
David Chisnall | 29cefd1 | 2010-05-20 13:45:48 +0000 | [diff] [blame] | 1105 | Builder.CreateCondBr(isNil, continueBB, messageBB); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1106 | CGF.EmitBlock(messageBB); |
| 1107 | } |
| 1108 | |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 1109 | IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy)); |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1110 | llvm::Value *cmd; |
| 1111 | if (Method) |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1112 | cmd = GetSelector(Builder, Method); |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1113 | else |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1114 | cmd = GetSelector(Builder, Sel); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1115 | cmd = EnforceType(Builder, cmd, SelectorTy); |
| 1116 | Receiver = EnforceType(Builder, Receiver, IdTy); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1117 | |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1118 | llvm::Value *impMD[] = { |
| 1119 | llvm::MDString::get(VMContext, Sel.getAsString()), |
| 1120 | llvm::MDString::get(VMContext, Class ? Class->getNameAsString() :""), |
| 1121 | llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), Class!=0) |
| 1122 | }; |
Jay Foad | ea324f1 | 2011-04-21 19:59:12 +0000 | [diff] [blame] | 1123 | llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1124 | |
| 1125 | // Get the IMP to call |
| 1126 | llvm::Value *imp = LookupIMP(CGF, Receiver, cmd, node); |
| 1127 | |
| 1128 | CallArgList ActualArgs; |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1129 | ActualArgs.add(RValue::get(Receiver), ASTIdTy); |
| 1130 | ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType()); |
Fariborz Jahanian | b73a23e | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 1131 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
| 1132 | |
| 1133 | CodeGenTypes &Types = CGM.getTypes(); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1134 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs, |
Rafael Espindola | c50c27c | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1135 | FunctionType::ExtInfo()); |
Daniel Dunbar | df0e62d | 2009-09-17 04:01:40 +0000 | [diff] [blame] | 1136 | const llvm::FunctionType *impType = |
| 1137 | Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1138 | imp = EnforceType(Builder, imp, llvm::PointerType::getUnqual(impType)); |
David Chisnall | c0cf422 | 2010-05-01 12:56:56 +0000 | [diff] [blame] | 1139 | |
| 1140 | |
Fariborz Jahanian | a4404f2 | 2009-05-22 20:17:16 +0000 | [diff] [blame] | 1141 | // For sender-aware dispatch, we pass the sender as the third argument to a |
| 1142 | // lookup function. When sending messages from C code, the sender is nil. |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1143 | // objc_msg_lookup_sender(id *receiver, SEL selector, id sender); |
David Chisnall | ff5f88c | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 1144 | llvm::Instruction *call; |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1145 | RValue msgRet = CGF.EmitCall(FnInfo, imp, Return, ActualArgs, |
David Chisnall | ff5f88c | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 1146 | 0, &call); |
| 1147 | call->setMetadata(msgSendMDKind, node); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1148 | |
David Chisnall | 29cefd1 | 2010-05-20 13:45:48 +0000 | [diff] [blame] | 1149 | |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1150 | if (!isPointerSizedReturn) { |
David Chisnall | 29cefd1 | 2010-05-20 13:45:48 +0000 | [diff] [blame] | 1151 | messageBB = CGF.Builder.GetInsertBlock(); |
| 1152 | CGF.Builder.CreateBr(continueBB); |
| 1153 | CGF.EmitBlock(continueBB); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1154 | if (msgRet.isScalar()) { |
| 1155 | llvm::Value *v = msgRet.getScalarVal(); |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 1156 | llvm::PHINode *phi = Builder.CreatePHI(v->getType(), 2); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1157 | phi->addIncoming(v, messageBB); |
| 1158 | phi->addIncoming(llvm::Constant::getNullValue(v->getType()), startBB); |
| 1159 | msgRet = RValue::get(phi); |
| 1160 | } else if (msgRet.isAggregate()) { |
| 1161 | llvm::Value *v = msgRet.getAggregateAddr(); |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 1162 | llvm::PHINode *phi = Builder.CreatePHI(v->getType(), 2); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1163 | const llvm::PointerType *RetTy = cast<llvm::PointerType>(v->getType()); |
David Chisnall | d6a6af6 | 2010-04-30 13:36:12 +0000 | [diff] [blame] | 1164 | llvm::AllocaInst *NullVal = |
| 1165 | CGF.CreateTempAlloca(RetTy->getElementType(), "null"); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1166 | CGF.InitTempAlloca(NullVal, |
| 1167 | llvm::Constant::getNullValue(RetTy->getElementType())); |
| 1168 | phi->addIncoming(v, messageBB); |
| 1169 | phi->addIncoming(NullVal, startBB); |
| 1170 | msgRet = RValue::getAggregate(phi); |
| 1171 | } else /* isComplex() */ { |
| 1172 | std::pair<llvm::Value*,llvm::Value*> v = msgRet.getComplexVal(); |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 1173 | llvm::PHINode *phi = Builder.CreatePHI(v.first->getType(), 2); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1174 | phi->addIncoming(v.first, messageBB); |
| 1175 | phi->addIncoming(llvm::Constant::getNullValue(v.first->getType()), |
| 1176 | startBB); |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 1177 | llvm::PHINode *phi2 = Builder.CreatePHI(v.second->getType(), 2); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1178 | phi2->addIncoming(v.second, messageBB); |
| 1179 | phi2->addIncoming(llvm::Constant::getNullValue(v.second->getType()), |
| 1180 | startBB); |
| 1181 | msgRet = RValue::getComplex(phi, phi2); |
| 1182 | } |
| 1183 | } |
| 1184 | return msgRet; |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1187 | /// Generates a MethodList. Used in construction of a objc_class and |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1188 | /// objc_category structures. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1189 | llvm::Constant *CGObjCGNU::GenerateMethodList(const llvm::StringRef &ClassName, |
| 1190 | const llvm::StringRef &CategoryName, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1191 | const llvm::SmallVectorImpl<Selector> &MethodSels, |
| 1192 | const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1193 | bool isClassMethodList) { |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 1194 | if (MethodSels.empty()) |
| 1195 | return NULLPtr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1196 | // Get the method structure type. |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1197 | llvm::StructType *ObjCMethodTy = llvm::StructType::get(VMContext, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1198 | PtrToInt8Ty, // Really a selector, but the runtime creates it us. |
| 1199 | PtrToInt8Ty, // Method types |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1200 | IMPTy, //Method pointer |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1201 | NULL); |
| 1202 | std::vector<llvm::Constant*> Methods; |
| 1203 | std::vector<llvm::Constant*> Elements; |
| 1204 | for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) { |
| 1205 | Elements.clear(); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1206 | llvm::Constant *Method = |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1207 | TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName, |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1208 | MethodSels[i], |
| 1209 | isClassMethodList)); |
| 1210 | assert(Method && "Can't generate metadata for method that doesn't exist"); |
| 1211 | llvm::Constant *C = MakeConstantString(MethodSels[i].getAsString()); |
| 1212 | Elements.push_back(C); |
| 1213 | Elements.push_back(MethodTypes[i]); |
| 1214 | Method = llvm::ConstantExpr::getBitCast(Method, |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1215 | IMPTy); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1216 | Elements.push_back(Method); |
| 1217 | Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements)); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | // Array of method structures |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1221 | llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy, |
Fariborz Jahanian | 078cd52 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 1222 | Methods.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 1223 | llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy, |
Chris Lattner | 882034d | 2008-06-26 04:52:29 +0000 | [diff] [blame] | 1224 | Methods); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1225 | |
| 1226 | // Structure containing list pointer, array and array count |
| 1227 | llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields; |
Owen Anderson | c36edfe | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 1228 | llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get(VMContext); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1229 | llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy); |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1230 | llvm::StructType *ObjCMethodListTy = llvm::StructType::get(VMContext, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1231 | NextPtrTy, |
| 1232 | IntTy, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1233 | ObjCMethodArrayTy, |
| 1234 | NULL); |
| 1235 | // Refine next pointer type to concrete type |
| 1236 | llvm::cast<llvm::OpaqueType>( |
| 1237 | OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy); |
| 1238 | ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get()); |
| 1239 | |
| 1240 | Methods.clear(); |
Owen Anderson | 7ec07a5 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 1241 | Methods.push_back(llvm::ConstantPointerNull::get( |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1242 | llvm::PointerType::getUnqual(ObjCMethodListTy))); |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1243 | Methods.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1244 | MethodTypes.size())); |
| 1245 | Methods.push_back(MethodArray); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1246 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1247 | // Create an instance of the structure |
| 1248 | return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list"); |
| 1249 | } |
| 1250 | |
| 1251 | /// Generates an IvarList. Used in construction of a objc_class. |
| 1252 | llvm::Constant *CGObjCGNU::GenerateIvarList( |
| 1253 | const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames, |
| 1254 | const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes, |
| 1255 | const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) { |
David Chisnall | b3b44ce | 2009-11-16 19:05:54 +0000 | [diff] [blame] | 1256 | if (IvarNames.size() == 0) |
| 1257 | return NULLPtr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1258 | // Get the method structure type. |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1259 | llvm::StructType *ObjCIvarTy = llvm::StructType::get(VMContext, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1260 | PtrToInt8Ty, |
| 1261 | PtrToInt8Ty, |
| 1262 | IntTy, |
| 1263 | NULL); |
| 1264 | std::vector<llvm::Constant*> Ivars; |
| 1265 | std::vector<llvm::Constant*> Elements; |
| 1266 | for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) { |
| 1267 | Elements.clear(); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1268 | Elements.push_back(IvarNames[i]); |
| 1269 | Elements.push_back(IvarTypes[i]); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1270 | Elements.push_back(IvarOffsets[i]); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1271 | Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements)); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | // Array of method structures |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1275 | llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1276 | IvarNames.size()); |
| 1277 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1278 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1279 | Elements.clear(); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1280 | Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size())); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 1281 | Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars)); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1282 | // Structure containing array and array count |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1283 | llvm::StructType *ObjCIvarListTy = llvm::StructType::get(VMContext, IntTy, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1284 | ObjCIvarArrayTy, |
| 1285 | NULL); |
| 1286 | |
| 1287 | // Create an instance of the structure |
| 1288 | return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list"); |
| 1289 | } |
| 1290 | |
| 1291 | /// Generate a class structure |
| 1292 | llvm::Constant *CGObjCGNU::GenerateClassStructure( |
| 1293 | llvm::Constant *MetaClass, |
| 1294 | llvm::Constant *SuperClass, |
| 1295 | unsigned info, |
Chris Lattner | da35bc8 | 2008-06-26 04:47:04 +0000 | [diff] [blame] | 1296 | const char *Name, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1297 | llvm::Constant *Version, |
| 1298 | llvm::Constant *InstanceSize, |
| 1299 | llvm::Constant *IVars, |
| 1300 | llvm::Constant *Methods, |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1301 | llvm::Constant *Protocols, |
| 1302 | llvm::Constant *IvarOffsets, |
David Chisnall | d472c85 | 2010-04-28 14:29:56 +0000 | [diff] [blame] | 1303 | llvm::Constant *Properties, |
| 1304 | bool isMeta) { |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1305 | // Set up the class structure |
| 1306 | // Note: Several of these are char*s when they should be ids. This is |
| 1307 | // because the runtime performs this translation on load. |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1308 | // |
| 1309 | // Fields marked New ABI are part of the GNUstep runtime. We emit them |
| 1310 | // anyway; the classes will still work with the GNU runtime, they will just |
| 1311 | // be ignored. |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1312 | llvm::StructType *ClassTy = llvm::StructType::get(VMContext, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1313 | PtrToInt8Ty, // class_pointer |
| 1314 | PtrToInt8Ty, // super_class |
| 1315 | PtrToInt8Ty, // name |
| 1316 | LongTy, // version |
| 1317 | LongTy, // info |
| 1318 | LongTy, // instance_size |
| 1319 | IVars->getType(), // ivars |
| 1320 | Methods->getType(), // methods |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1321 | // These are all filled in by the runtime, so we pretend |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1322 | PtrTy, // dtable |
| 1323 | PtrTy, // subclass_list |
| 1324 | PtrTy, // sibling_class |
| 1325 | PtrTy, // protocols |
| 1326 | PtrTy, // gc_object_type |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1327 | // New ABI: |
| 1328 | LongTy, // abi_version |
| 1329 | IvarOffsets->getType(), // ivar_offsets |
| 1330 | Properties->getType(), // properties |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1331 | NULL); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1332 | llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1333 | // Fill in the structure |
| 1334 | std::vector<llvm::Constant*> Elements; |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1335 | Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty)); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1336 | Elements.push_back(SuperClass); |
Chris Lattner | da35bc8 | 2008-06-26 04:47:04 +0000 | [diff] [blame] | 1337 | Elements.push_back(MakeConstantString(Name, ".class_name")); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1338 | Elements.push_back(Zero); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1339 | Elements.push_back(llvm::ConstantInt::get(LongTy, info)); |
David Chisnall | 055f064 | 2011-02-21 23:47:40 +0000 | [diff] [blame] | 1340 | if (isMeta) { |
| 1341 | llvm::TargetData td(&TheModule); |
Ken Dyck | 0fed10e | 2011-04-22 17:59:22 +0000 | [diff] [blame] | 1342 | Elements.push_back( |
| 1343 | llvm::ConstantInt::get(LongTy, |
| 1344 | td.getTypeSizeInBits(ClassTy) / |
| 1345 | CGM.getContext().getCharWidth())); |
David Chisnall | 055f064 | 2011-02-21 23:47:40 +0000 | [diff] [blame] | 1346 | } else |
| 1347 | Elements.push_back(InstanceSize); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1348 | Elements.push_back(IVars); |
| 1349 | Elements.push_back(Methods); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1350 | Elements.push_back(NULLPtr); |
| 1351 | Elements.push_back(NULLPtr); |
| 1352 | Elements.push_back(NULLPtr); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1353 | Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy)); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1354 | Elements.push_back(NULLPtr); |
| 1355 | Elements.push_back(Zero); |
| 1356 | Elements.push_back(IvarOffsets); |
| 1357 | Elements.push_back(Properties); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1358 | // Create an instance of the structure |
David Chisnall | df34917 | 2010-01-08 00:14:31 +0000 | [diff] [blame] | 1359 | // This is now an externally visible symbol, so that we can speed up class |
| 1360 | // messages in the next ABI. |
David Chisnall | d472c85 | 2010-04-28 14:29:56 +0000 | [diff] [blame] | 1361 | return MakeGlobal(ClassTy, Elements, (isMeta ? "_OBJC_METACLASS_": |
| 1362 | "_OBJC_CLASS_") + std::string(Name), llvm::GlobalValue::ExternalLinkage); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | llvm::Constant *CGObjCGNU::GenerateProtocolMethodList( |
| 1366 | const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames, |
| 1367 | const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1368 | // Get the method structure type. |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1369 | llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(VMContext, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1370 | PtrToInt8Ty, // Really a selector, but the runtime does the casting for us. |
| 1371 | PtrToInt8Ty, |
| 1372 | NULL); |
| 1373 | std::vector<llvm::Constant*> Methods; |
| 1374 | std::vector<llvm::Constant*> Elements; |
| 1375 | for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) { |
| 1376 | Elements.clear(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 | Elements.push_back(MethodNames[i]); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1378 | Elements.push_back(MethodTypes[i]); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1379 | Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements)); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1380 | } |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1381 | llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1382 | MethodNames.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 1383 | llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, |
Mike Stump | dd93a19 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 1384 | Methods); |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1385 | llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(VMContext, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1386 | IntTy, ObjCMethodArrayTy, NULL); |
| 1387 | Methods.clear(); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1388 | Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size())); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1389 | Methods.push_back(Array); |
| 1390 | return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list"); |
| 1391 | } |
Mike Stump | dd93a19 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 1392 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1393 | // Create the protocol list structure used in classes, categories and so on |
| 1394 | llvm::Constant *CGObjCGNU::GenerateProtocolList( |
| 1395 | const llvm::SmallVectorImpl<std::string> &Protocols) { |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1396 | llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1397 | Protocols.size()); |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1398 | llvm::StructType *ProtocolListTy = llvm::StructType::get(VMContext, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1399 | PtrTy, //Should be a recurisve pointer, but it's always NULL here. |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1400 | SizeTy, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1401 | ProtocolArrayTy, |
| 1402 | NULL); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1403 | std::vector<llvm::Constant*> Elements; |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1404 | for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end(); |
| 1405 | iter != endIter ; iter++) { |
David Chisnall | bc8bdea | 2009-11-20 14:50:59 +0000 | [diff] [blame] | 1406 | llvm::Constant *protocol = 0; |
| 1407 | llvm::StringMap<llvm::Constant*>::iterator value = |
| 1408 | ExistingProtocols.find(*iter); |
| 1409 | if (value == ExistingProtocols.end()) { |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1410 | protocol = GenerateEmptyProtocol(*iter); |
David Chisnall | bc8bdea | 2009-11-20 14:50:59 +0000 | [diff] [blame] | 1411 | } else { |
| 1412 | protocol = value->getValue(); |
| 1413 | } |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1414 | llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(protocol, |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1415 | PtrToInt8Ty); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1416 | Elements.push_back(Ptr); |
| 1417 | } |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 1418 | llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1419 | Elements); |
| 1420 | Elements.clear(); |
| 1421 | Elements.push_back(NULLPtr); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1422 | Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size())); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1423 | Elements.push_back(ProtocolArray); |
| 1424 | return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list"); |
| 1425 | } |
| 1426 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1427 | llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1428 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1429 | llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1430 | const llvm::Type *T = |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1431 | CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType()); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1432 | return Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T)); |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | llvm::Constant *CGObjCGNU::GenerateEmptyProtocol( |
| 1436 | const std::string &ProtocolName) { |
| 1437 | llvm::SmallVector<std::string, 0> EmptyStringVector; |
| 1438 | llvm::SmallVector<llvm::Constant*, 0> EmptyConstantVector; |
| 1439 | |
| 1440 | llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1441 | llvm::Constant *MethodList = |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1442 | GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector); |
| 1443 | // Protocols are objects containing lists of the methods implemented and |
| 1444 | // protocols adopted. |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1445 | llvm::StructType *ProtocolTy = llvm::StructType::get(VMContext, IdTy, |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1446 | PtrToInt8Ty, |
| 1447 | ProtocolList->getType(), |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1448 | MethodList->getType(), |
| 1449 | MethodList->getType(), |
| 1450 | MethodList->getType(), |
| 1451 | MethodList->getType(), |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1452 | NULL); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1453 | std::vector<llvm::Constant*> Elements; |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1454 | // The isa pointer must be set to a magic number so the runtime knows it's |
| 1455 | // the correct layout. |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1456 | Elements.push_back(llvm::ConstantExpr::getIntToPtr( |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1457 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
| 1458 | ProtocolVersion), IdTy)); |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1459 | Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name")); |
| 1460 | Elements.push_back(ProtocolList); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1461 | Elements.push_back(MethodList); |
| 1462 | Elements.push_back(MethodList); |
| 1463 | Elements.push_back(MethodList); |
| 1464 | Elements.push_back(MethodList); |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1465 | return MakeGlobal(ProtocolTy, Elements, ".objc_protocol"); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1468 | void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) { |
| 1469 | ASTContext &Context = CGM.getContext(); |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1470 | std::string ProtocolName = PD->getNameAsString(); |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1471 | llvm::SmallVector<std::string, 16> Protocols; |
| 1472 | for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(), |
| 1473 | E = PD->protocol_end(); PI != E; ++PI) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1474 | Protocols.push_back((*PI)->getNameAsString()); |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1475 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames; |
| 1476 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes; |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1477 | llvm::SmallVector<llvm::Constant*, 16> OptionalInstanceMethodNames; |
| 1478 | llvm::SmallVector<llvm::Constant*, 16> OptionalInstanceMethodTypes; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1479 | for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(), |
| 1480 | E = PD->instmeth_end(); iter != E; iter++) { |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1481 | std::string TypeStr; |
| 1482 | Context.getObjCEncodingForMethodDecl(*iter, TypeStr); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1483 | if ((*iter)->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1484 | InstanceMethodNames.push_back( |
| 1485 | MakeConstantString((*iter)->getSelector().getAsString())); |
| 1486 | InstanceMethodTypes.push_back(MakeConstantString(TypeStr)); |
| 1487 | } else { |
| 1488 | OptionalInstanceMethodNames.push_back( |
| 1489 | MakeConstantString((*iter)->getSelector().getAsString())); |
| 1490 | OptionalInstanceMethodTypes.push_back(MakeConstantString(TypeStr)); |
| 1491 | } |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1492 | } |
| 1493 | // Collect information about class methods: |
| 1494 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames; |
| 1495 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes; |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1496 | llvm::SmallVector<llvm::Constant*, 16> OptionalClassMethodNames; |
| 1497 | llvm::SmallVector<llvm::Constant*, 16> OptionalClassMethodTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1498 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1499 | iter = PD->classmeth_begin(), endIter = PD->classmeth_end(); |
| 1500 | iter != endIter ; iter++) { |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1501 | std::string TypeStr; |
| 1502 | Context.getObjCEncodingForMethodDecl((*iter),TypeStr); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1503 | if ((*iter)->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1504 | ClassMethodNames.push_back( |
| 1505 | MakeConstantString((*iter)->getSelector().getAsString())); |
| 1506 | ClassMethodTypes.push_back(MakeConstantString(TypeStr)); |
| 1507 | } else { |
| 1508 | OptionalClassMethodNames.push_back( |
| 1509 | MakeConstantString((*iter)->getSelector().getAsString())); |
| 1510 | OptionalClassMethodTypes.push_back(MakeConstantString(TypeStr)); |
| 1511 | } |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1512 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1513 | |
| 1514 | llvm::Constant *ProtocolList = GenerateProtocolList(Protocols); |
| 1515 | llvm::Constant *InstanceMethodList = |
| 1516 | GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes); |
| 1517 | llvm::Constant *ClassMethodList = |
| 1518 | GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1519 | llvm::Constant *OptionalInstanceMethodList = |
| 1520 | GenerateProtocolMethodList(OptionalInstanceMethodNames, |
| 1521 | OptionalInstanceMethodTypes); |
| 1522 | llvm::Constant *OptionalClassMethodList = |
| 1523 | GenerateProtocolMethodList(OptionalClassMethodNames, |
| 1524 | OptionalClassMethodTypes); |
| 1525 | |
| 1526 | // Property metadata: name, attributes, isSynthesized, setter name, setter |
| 1527 | // types, getter name, getter types. |
| 1528 | // The isSynthesized value is always set to 0 in a protocol. It exists to |
| 1529 | // simplify the runtime library by allowing it to use the same data |
| 1530 | // structures for protocol metadata everywhere. |
| 1531 | llvm::StructType *PropertyMetadataTy = llvm::StructType::get(VMContext, |
| 1532 | PtrToInt8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty, |
| 1533 | PtrToInt8Ty, NULL); |
| 1534 | std::vector<llvm::Constant*> Properties; |
| 1535 | std::vector<llvm::Constant*> OptionalProperties; |
| 1536 | |
| 1537 | // Add all of the property methods need adding to the method list and to the |
| 1538 | // property metadata list. |
| 1539 | for (ObjCContainerDecl::prop_iterator |
| 1540 | iter = PD->prop_begin(), endIter = PD->prop_end(); |
| 1541 | iter != endIter ; iter++) { |
| 1542 | std::vector<llvm::Constant*> Fields; |
| 1543 | ObjCPropertyDecl *property = (*iter); |
| 1544 | |
| 1545 | Fields.push_back(MakeConstantString(property->getNameAsString())); |
| 1546 | Fields.push_back(llvm::ConstantInt::get(Int8Ty, |
| 1547 | property->getPropertyAttributes())); |
| 1548 | Fields.push_back(llvm::ConstantInt::get(Int8Ty, 0)); |
| 1549 | if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) { |
| 1550 | std::string TypeStr; |
| 1551 | Context.getObjCEncodingForMethodDecl(getter,TypeStr); |
| 1552 | llvm::Constant *TypeEncoding = MakeConstantString(TypeStr); |
| 1553 | InstanceMethodTypes.push_back(TypeEncoding); |
| 1554 | Fields.push_back(MakeConstantString(getter->getSelector().getAsString())); |
| 1555 | Fields.push_back(TypeEncoding); |
| 1556 | } else { |
| 1557 | Fields.push_back(NULLPtr); |
| 1558 | Fields.push_back(NULLPtr); |
| 1559 | } |
| 1560 | if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) { |
| 1561 | std::string TypeStr; |
| 1562 | Context.getObjCEncodingForMethodDecl(setter,TypeStr); |
| 1563 | llvm::Constant *TypeEncoding = MakeConstantString(TypeStr); |
| 1564 | InstanceMethodTypes.push_back(TypeEncoding); |
| 1565 | Fields.push_back(MakeConstantString(setter->getSelector().getAsString())); |
| 1566 | Fields.push_back(TypeEncoding); |
| 1567 | } else { |
| 1568 | Fields.push_back(NULLPtr); |
| 1569 | Fields.push_back(NULLPtr); |
| 1570 | } |
| 1571 | if (property->getPropertyImplementation() == ObjCPropertyDecl::Optional) { |
| 1572 | OptionalProperties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields)); |
| 1573 | } else { |
| 1574 | Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields)); |
| 1575 | } |
| 1576 | } |
| 1577 | llvm::Constant *PropertyArray = llvm::ConstantArray::get( |
| 1578 | llvm::ArrayType::get(PropertyMetadataTy, Properties.size()), Properties); |
| 1579 | llvm::Constant* PropertyListInitFields[] = |
| 1580 | {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray}; |
| 1581 | |
| 1582 | llvm::Constant *PropertyListInit = |
Nick Lewycky | 41eaf0a | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 1583 | llvm::ConstantStruct::get(VMContext, PropertyListInitFields, 3, false); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1584 | llvm::Constant *PropertyList = new llvm::GlobalVariable(TheModule, |
| 1585 | PropertyListInit->getType(), false, llvm::GlobalValue::InternalLinkage, |
| 1586 | PropertyListInit, ".objc_property_list"); |
| 1587 | |
| 1588 | llvm::Constant *OptionalPropertyArray = |
| 1589 | llvm::ConstantArray::get(llvm::ArrayType::get(PropertyMetadataTy, |
| 1590 | OptionalProperties.size()) , OptionalProperties); |
| 1591 | llvm::Constant* OptionalPropertyListInitFields[] = { |
| 1592 | llvm::ConstantInt::get(IntTy, OptionalProperties.size()), NULLPtr, |
| 1593 | OptionalPropertyArray }; |
| 1594 | |
| 1595 | llvm::Constant *OptionalPropertyListInit = |
Nick Lewycky | 41eaf0a | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 1596 | llvm::ConstantStruct::get(VMContext, OptionalPropertyListInitFields, 3, false); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1597 | llvm::Constant *OptionalPropertyList = new llvm::GlobalVariable(TheModule, |
| 1598 | OptionalPropertyListInit->getType(), false, |
| 1599 | llvm::GlobalValue::InternalLinkage, OptionalPropertyListInit, |
| 1600 | ".objc_property_list"); |
| 1601 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1602 | // Protocols are objects containing lists of the methods implemented and |
| 1603 | // protocols adopted. |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1604 | llvm::StructType *ProtocolTy = llvm::StructType::get(VMContext, IdTy, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1605 | PtrToInt8Ty, |
| 1606 | ProtocolList->getType(), |
| 1607 | InstanceMethodList->getType(), |
| 1608 | ClassMethodList->getType(), |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1609 | OptionalInstanceMethodList->getType(), |
| 1610 | OptionalClassMethodList->getType(), |
| 1611 | PropertyList->getType(), |
| 1612 | OptionalPropertyList->getType(), |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1613 | NULL); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1614 | std::vector<llvm::Constant*> Elements; |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1615 | // The isa pointer must be set to a magic number so the runtime knows it's |
| 1616 | // the correct layout. |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1617 | Elements.push_back(llvm::ConstantExpr::getIntToPtr( |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1618 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
| 1619 | ProtocolVersion), IdTy)); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1620 | Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name")); |
| 1621 | Elements.push_back(ProtocolList); |
| 1622 | Elements.push_back(InstanceMethodList); |
| 1623 | Elements.push_back(ClassMethodList); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1624 | Elements.push_back(OptionalInstanceMethodList); |
| 1625 | Elements.push_back(OptionalClassMethodList); |
| 1626 | Elements.push_back(PropertyList); |
| 1627 | Elements.push_back(OptionalPropertyList); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1628 | ExistingProtocols[ProtocolName] = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1629 | llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1630 | ".objc_protocol"), IdTy); |
| 1631 | } |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1632 | void CGObjCGNU::GenerateProtocolHolderCategory(void) { |
| 1633 | // Collect information about instance methods |
| 1634 | llvm::SmallVector<Selector, 1> MethodSels; |
| 1635 | llvm::SmallVector<llvm::Constant*, 1> MethodTypes; |
| 1636 | |
| 1637 | std::vector<llvm::Constant*> Elements; |
| 1638 | const std::string ClassName = "__ObjC_Protocol_Holder_Ugly_Hack"; |
| 1639 | const std::string CategoryName = "AnotherHack"; |
| 1640 | Elements.push_back(MakeConstantString(CategoryName)); |
| 1641 | Elements.push_back(MakeConstantString(ClassName)); |
| 1642 | // Instance method list |
| 1643 | Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList( |
| 1644 | ClassName, CategoryName, MethodSels, MethodTypes, false), PtrTy)); |
| 1645 | // Class method list |
| 1646 | Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList( |
| 1647 | ClassName, CategoryName, MethodSels, MethodTypes, true), PtrTy)); |
| 1648 | // Protocol list |
| 1649 | llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrTy, |
| 1650 | ExistingProtocols.size()); |
| 1651 | llvm::StructType *ProtocolListTy = llvm::StructType::get(VMContext, |
| 1652 | PtrTy, //Should be a recurisve pointer, but it's always NULL here. |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1653 | SizeTy, |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1654 | ProtocolArrayTy, |
| 1655 | NULL); |
| 1656 | std::vector<llvm::Constant*> ProtocolElements; |
| 1657 | for (llvm::StringMapIterator<llvm::Constant*> iter = |
| 1658 | ExistingProtocols.begin(), endIter = ExistingProtocols.end(); |
| 1659 | iter != endIter ; iter++) { |
| 1660 | llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(iter->getValue(), |
| 1661 | PtrTy); |
| 1662 | ProtocolElements.push_back(Ptr); |
| 1663 | } |
| 1664 | llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy, |
| 1665 | ProtocolElements); |
| 1666 | ProtocolElements.clear(); |
| 1667 | ProtocolElements.push_back(NULLPtr); |
| 1668 | ProtocolElements.push_back(llvm::ConstantInt::get(LongTy, |
| 1669 | ExistingProtocols.size())); |
| 1670 | ProtocolElements.push_back(ProtocolArray); |
| 1671 | Elements.push_back(llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolListTy, |
| 1672 | ProtocolElements, ".objc_protocol_list"), PtrTy)); |
| 1673 | Categories.push_back(llvm::ConstantExpr::getBitCast( |
| 1674 | MakeGlobal(llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty, |
| 1675 | PtrTy, PtrTy, PtrTy, NULL), Elements), PtrTy)); |
| 1676 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1677 | |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1678 | void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1679 | std::string ClassName = OCD->getClassInterface()->getNameAsString(); |
| 1680 | std::string CategoryName = OCD->getNameAsString(); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1681 | // Collect information about instance methods |
| 1682 | llvm::SmallVector<Selector, 16> InstanceMethodSels; |
| 1683 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes; |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1684 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1685 | iter = OCD->instmeth_begin(), endIter = OCD->instmeth_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1686 | iter != endIter ; iter++) { |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1687 | InstanceMethodSels.push_back((*iter)->getSelector()); |
| 1688 | std::string TypeStr; |
| 1689 | CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1690 | InstanceMethodTypes.push_back(MakeConstantString(TypeStr)); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | // Collect information about class methods |
| 1694 | llvm::SmallVector<Selector, 16> ClassMethodSels; |
| 1695 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1696 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1697 | iter = OCD->classmeth_begin(), endIter = OCD->classmeth_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1698 | iter != endIter ; iter++) { |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1699 | ClassMethodSels.push_back((*iter)->getSelector()); |
| 1700 | std::string TypeStr; |
| 1701 | CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1702 | ClassMethodTypes.push_back(MakeConstantString(TypeStr)); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | // Collect the names of referenced protocols |
| 1706 | llvm::SmallVector<std::string, 16> Protocols; |
David Chisnall | 2bfc50b | 2010-03-13 22:20:45 +0000 | [diff] [blame] | 1707 | const ObjCCategoryDecl *CatDecl = OCD->getCategoryDecl(); |
| 1708 | const ObjCList<ObjCProtocolDecl> &Protos = CatDecl->getReferencedProtocols(); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1709 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(), |
| 1710 | E = Protos.end(); I != E; ++I) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1711 | Protocols.push_back((*I)->getNameAsString()); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1712 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1713 | std::vector<llvm::Constant*> Elements; |
| 1714 | Elements.push_back(MakeConstantString(CategoryName)); |
| 1715 | Elements.push_back(MakeConstantString(ClassName)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1716 | // Instance method list |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1717 | Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList( |
Chris Lattner | bf231a6 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 1718 | ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1719 | false), PtrTy)); |
| 1720 | // Class method list |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1721 | Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList( |
Chris Lattner | bf231a6 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 1722 | ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true), |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1723 | PtrTy)); |
| 1724 | // Protocol list |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1725 | Elements.push_back(llvm::ConstantExpr::getBitCast( |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1726 | GenerateProtocolList(Protocols), PtrTy)); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1727 | Categories.push_back(llvm::ConstantExpr::getBitCast( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1728 | MakeGlobal(llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty, |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1729 | PtrTy, PtrTy, PtrTy, NULL), Elements), PtrTy)); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1730 | } |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1731 | |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1732 | llvm::Constant *CGObjCGNU::GeneratePropertyList(const ObjCImplementationDecl *OID, |
| 1733 | llvm::SmallVectorImpl<Selector> &InstanceMethodSels, |
| 1734 | llvm::SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes) { |
| 1735 | ASTContext &Context = CGM.getContext(); |
| 1736 | // |
| 1737 | // Property metadata: name, attributes, isSynthesized, setter name, setter |
| 1738 | // types, getter name, getter types. |
| 1739 | llvm::StructType *PropertyMetadataTy = llvm::StructType::get(VMContext, |
| 1740 | PtrToInt8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty, |
| 1741 | PtrToInt8Ty, NULL); |
| 1742 | std::vector<llvm::Constant*> Properties; |
| 1743 | |
| 1744 | |
| 1745 | // Add all of the property methods need adding to the method list and to the |
| 1746 | // property metadata list. |
| 1747 | for (ObjCImplDecl::propimpl_iterator |
| 1748 | iter = OID->propimpl_begin(), endIter = OID->propimpl_end(); |
| 1749 | iter != endIter ; iter++) { |
| 1750 | std::vector<llvm::Constant*> Fields; |
| 1751 | ObjCPropertyDecl *property = (*iter)->getPropertyDecl(); |
David Chisnall | 36c6320 | 2010-02-26 01:11:38 +0000 | [diff] [blame] | 1752 | ObjCPropertyImplDecl *propertyImpl = *iter; |
| 1753 | bool isSynthesized = (propertyImpl->getPropertyImplementation() == |
| 1754 | ObjCPropertyImplDecl::Synthesize); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1755 | |
| 1756 | Fields.push_back(MakeConstantString(property->getNameAsString())); |
| 1757 | Fields.push_back(llvm::ConstantInt::get(Int8Ty, |
| 1758 | property->getPropertyAttributes())); |
David Chisnall | 36c6320 | 2010-02-26 01:11:38 +0000 | [diff] [blame] | 1759 | Fields.push_back(llvm::ConstantInt::get(Int8Ty, isSynthesized)); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1760 | if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) { |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1761 | std::string TypeStr; |
| 1762 | Context.getObjCEncodingForMethodDecl(getter,TypeStr); |
| 1763 | llvm::Constant *TypeEncoding = MakeConstantString(TypeStr); |
David Chisnall | 36c6320 | 2010-02-26 01:11:38 +0000 | [diff] [blame] | 1764 | if (isSynthesized) { |
| 1765 | InstanceMethodTypes.push_back(TypeEncoding); |
| 1766 | InstanceMethodSels.push_back(getter->getSelector()); |
| 1767 | } |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1768 | Fields.push_back(MakeConstantString(getter->getSelector().getAsString())); |
| 1769 | Fields.push_back(TypeEncoding); |
| 1770 | } else { |
| 1771 | Fields.push_back(NULLPtr); |
| 1772 | Fields.push_back(NULLPtr); |
| 1773 | } |
| 1774 | if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) { |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1775 | std::string TypeStr; |
| 1776 | Context.getObjCEncodingForMethodDecl(setter,TypeStr); |
| 1777 | llvm::Constant *TypeEncoding = MakeConstantString(TypeStr); |
David Chisnall | 36c6320 | 2010-02-26 01:11:38 +0000 | [diff] [blame] | 1778 | if (isSynthesized) { |
| 1779 | InstanceMethodTypes.push_back(TypeEncoding); |
| 1780 | InstanceMethodSels.push_back(setter->getSelector()); |
| 1781 | } |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1782 | Fields.push_back(MakeConstantString(setter->getSelector().getAsString())); |
| 1783 | Fields.push_back(TypeEncoding); |
| 1784 | } else { |
| 1785 | Fields.push_back(NULLPtr); |
| 1786 | Fields.push_back(NULLPtr); |
| 1787 | } |
| 1788 | Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields)); |
| 1789 | } |
| 1790 | llvm::ArrayType *PropertyArrayTy = |
| 1791 | llvm::ArrayType::get(PropertyMetadataTy, Properties.size()); |
| 1792 | llvm::Constant *PropertyArray = llvm::ConstantArray::get(PropertyArrayTy, |
| 1793 | Properties); |
| 1794 | llvm::Constant* PropertyListInitFields[] = |
| 1795 | {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray}; |
| 1796 | |
| 1797 | llvm::Constant *PropertyListInit = |
Nick Lewycky | 41eaf0a | 2009-09-19 20:00:52 +0000 | [diff] [blame] | 1798 | llvm::ConstantStruct::get(VMContext, PropertyListInitFields, 3, false); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1799 | return new llvm::GlobalVariable(TheModule, PropertyListInit->getType(), false, |
| 1800 | llvm::GlobalValue::InternalLinkage, PropertyListInit, |
| 1801 | ".objc_property_list"); |
| 1802 | } |
| 1803 | |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1804 | void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) { |
| 1805 | ASTContext &Context = CGM.getContext(); |
| 1806 | |
| 1807 | // Get the superclass name. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1808 | const ObjCInterfaceDecl * SuperClassDecl = |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1809 | OID->getClassInterface()->getSuperClass(); |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1810 | std::string SuperClassName; |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 1811 | if (SuperClassDecl) { |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1812 | SuperClassName = SuperClassDecl->getNameAsString(); |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 1813 | EmitClassRef(SuperClassName); |
| 1814 | } |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1815 | |
| 1816 | // Get the class name |
Chris Lattner | 87bc387 | 2009-04-01 02:00:48 +0000 | [diff] [blame] | 1817 | ObjCInterfaceDecl *ClassDecl = |
| 1818 | const_cast<ObjCInterfaceDecl *>(OID->getClassInterface()); |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1819 | std::string ClassName = ClassDecl->getNameAsString(); |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 1820 | // Emit the symbol that is used to generate linker errors if this class is |
| 1821 | // referenced in other modules but not declared. |
Fariborz Jahanian | bacbed9 | 2009-07-03 15:10:14 +0000 | [diff] [blame] | 1822 | std::string classSymbolName = "__objc_class_name_" + ClassName; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | if (llvm::GlobalVariable *symbol = |
Fariborz Jahanian | bacbed9 | 2009-07-03 15:10:14 +0000 | [diff] [blame] | 1824 | TheModule.getGlobalVariable(classSymbolName)) { |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1825 | symbol->setInitializer(llvm::ConstantInt::get(LongTy, 0)); |
Fariborz Jahanian | bacbed9 | 2009-07-03 15:10:14 +0000 | [diff] [blame] | 1826 | } else { |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1827 | new llvm::GlobalVariable(TheModule, LongTy, false, |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1828 | llvm::GlobalValue::ExternalLinkage, llvm::ConstantInt::get(LongTy, 0), |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1829 | classSymbolName); |
Fariborz Jahanian | bacbed9 | 2009-07-03 15:10:14 +0000 | [diff] [blame] | 1830 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1831 | |
Daniel Dunbar | 12119b9 | 2009-05-03 10:46:44 +0000 | [diff] [blame] | 1832 | // Get the size of instances. |
Ken Dyck | c8ae550 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 1833 | int instanceSize = |
| 1834 | Context.getASTObjCImplementationLayout(OID).getSize().getQuantity(); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1835 | |
| 1836 | // Collect information about instance variables. |
| 1837 | llvm::SmallVector<llvm::Constant*, 16> IvarNames; |
| 1838 | llvm::SmallVector<llvm::Constant*, 16> IvarTypes; |
| 1839 | llvm::SmallVector<llvm::Constant*, 16> IvarOffsets; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1840 | |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1841 | std::vector<llvm::Constant*> IvarOffsetValues; |
| 1842 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1843 | int superInstanceSize = !SuperClassDecl ? 0 : |
Ken Dyck | c8ae550 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 1844 | Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize().getQuantity(); |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 1845 | // For non-fragile ivars, set the instance size to 0 - {the size of just this |
| 1846 | // class}. The runtime will then set this to the correct value on load. |
| 1847 | if (CGM.getContext().getLangOptions().ObjCNonFragileABI) { |
| 1848 | instanceSize = 0 - (instanceSize - superInstanceSize); |
| 1849 | } |
David Chisnall | 18cf737 | 2010-04-19 00:45:34 +0000 | [diff] [blame] | 1850 | |
| 1851 | // Collect declared and synthesized ivars. |
| 1852 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
| 1853 | CGM.getContext().ShallowCollectObjCIvars(ClassDecl, OIvars); |
| 1854 | |
| 1855 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 1856 | ObjCIvarDecl *IVD = OIvars[i]; |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1857 | // Store the name |
David Chisnall | 18cf737 | 2010-04-19 00:45:34 +0000 | [diff] [blame] | 1858 | IvarNames.push_back(MakeConstantString(IVD->getNameAsString())); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1859 | // Get the type encoding for this ivar |
| 1860 | std::string TypeStr; |
David Chisnall | 18cf737 | 2010-04-19 00:45:34 +0000 | [diff] [blame] | 1861 | Context.getObjCEncodingForType(IVD->getType(), TypeStr); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1862 | IvarTypes.push_back(MakeConstantString(TypeStr)); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1863 | // Get the offset |
David Chisnall | 44ec555 | 2010-04-19 01:37:25 +0000 | [diff] [blame] | 1864 | uint64_t BaseOffset = ComputeIvarBaseOffset(CGM, OID, IVD); |
David Chisnall | cb1b7bf | 2009-11-17 19:32:15 +0000 | [diff] [blame] | 1865 | uint64_t Offset = BaseOffset; |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 1866 | if (CGM.getContext().getLangOptions().ObjCNonFragileABI) { |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1867 | Offset = BaseOffset - superInstanceSize; |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 1868 | } |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1869 | IvarOffsets.push_back( |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1870 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Offset)); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1871 | IvarOffsetValues.push_back(new llvm::GlobalVariable(TheModule, IntTy, |
| 1872 | false, llvm::GlobalValue::ExternalLinkage, |
David Chisnall | e8431a7 | 2010-11-03 16:12:44 +0000 | [diff] [blame] | 1873 | llvm::ConstantInt::get(IntTy, Offset), |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1874 | "__objc_ivar_offset_value_" + ClassName +"." + |
David Chisnall | 18cf737 | 2010-04-19 00:45:34 +0000 | [diff] [blame] | 1875 | IVD->getNameAsString())); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1876 | } |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1877 | llvm::GlobalVariable *IvarOffsetArray = |
| 1878 | MakeGlobalArray(PtrToIntTy, IvarOffsetValues, ".ivar.offsets"); |
| 1879 | |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1880 | |
| 1881 | // Collect information about instance methods |
| 1882 | llvm::SmallVector<Selector, 16> InstanceMethodSels; |
| 1883 | llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1884 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1885 | iter = OID->instmeth_begin(), endIter = OID->instmeth_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1886 | iter != endIter ; iter++) { |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1887 | InstanceMethodSels.push_back((*iter)->getSelector()); |
| 1888 | std::string TypeStr; |
| 1889 | Context.getObjCEncodingForMethodDecl((*iter),TypeStr); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1890 | InstanceMethodTypes.push_back(MakeConstantString(TypeStr)); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1891 | } |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1892 | |
| 1893 | llvm::Constant *Properties = GeneratePropertyList(OID, InstanceMethodSels, |
| 1894 | InstanceMethodTypes); |
| 1895 | |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1896 | |
| 1897 | // Collect information about class methods |
| 1898 | llvm::SmallVector<Selector, 16> ClassMethodSels; |
| 1899 | llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes; |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1900 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1901 | iter = OID->classmeth_begin(), endIter = OID->classmeth_end(); |
Douglas Gregor | 29bd76f | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1902 | iter != endIter ; iter++) { |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1903 | ClassMethodSels.push_back((*iter)->getSelector()); |
| 1904 | std::string TypeStr; |
| 1905 | Context.getObjCEncodingForMethodDecl((*iter),TypeStr); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1906 | ClassMethodTypes.push_back(MakeConstantString(TypeStr)); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1907 | } |
| 1908 | // Collect the names of referenced protocols |
| 1909 | llvm::SmallVector<std::string, 16> Protocols; |
| 1910 | const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols(); |
| 1911 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(), |
| 1912 | E = Protos.end(); I != E; ++I) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1913 | Protocols.push_back((*I)->getNameAsString()); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1914 | |
| 1915 | |
| 1916 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1917 | // Get the superclass pointer. |
| 1918 | llvm::Constant *SuperClass; |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1919 | if (!SuperClassName.empty()) { |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1920 | SuperClass = MakeConstantString(SuperClassName, ".super_class_name"); |
| 1921 | } else { |
Owen Anderson | 7ec07a5 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 1922 | SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1923 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1924 | // Empty vector used to construct empty method lists |
| 1925 | llvm::SmallVector<llvm::Constant*, 1> empty; |
| 1926 | // Generate the method and instance variable lists |
| 1927 | llvm::Constant *MethodList = GenerateMethodList(ClassName, "", |
Chris Lattner | bf231a6 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 1928 | InstanceMethodSels, InstanceMethodTypes, false); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1929 | llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "", |
Chris Lattner | bf231a6 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 1930 | ClassMethodSels, ClassMethodTypes, true); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1931 | llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes, |
| 1932 | IvarOffsets); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1933 | // Irrespective of whether we are compiling for a fragile or non-fragile ABI, |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1934 | // we emit a symbol containing the offset for each ivar in the class. This |
| 1935 | // allows code compiled for the non-Fragile ABI to inherit from code compiled |
| 1936 | // for the legacy ABI, without causing problems. The converse is also |
| 1937 | // possible, but causes all ivar accesses to be fragile. |
David Chisnall | e8431a7 | 2010-11-03 16:12:44 +0000 | [diff] [blame] | 1938 | |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1939 | // Offset pointer for getting at the correct field in the ivar list when |
| 1940 | // setting up the alias. These are: The base address for the global, the |
| 1941 | // ivar array (second field), the ivar in this list (set for each ivar), and |
| 1942 | // the offset (third field in ivar structure) |
| 1943 | const llvm::Type *IndexTy = llvm::Type::getInt32Ty(VMContext); |
| 1944 | llvm::Constant *offsetPointerIndexes[] = {Zeros[0], |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1945 | llvm::ConstantInt::get(IndexTy, 1), 0, |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1946 | llvm::ConstantInt::get(IndexTy, 2) }; |
| 1947 | |
David Chisnall | e8431a7 | 2010-11-03 16:12:44 +0000 | [diff] [blame] | 1948 | |
| 1949 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 1950 | ObjCIvarDecl *IVD = OIvars[i]; |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1951 | const std::string Name = "__objc_ivar_offset_" + ClassName + '.' |
David Chisnall | e8431a7 | 2010-11-03 16:12:44 +0000 | [diff] [blame] | 1952 | + IVD->getNameAsString(); |
| 1953 | offsetPointerIndexes[2] = llvm::ConstantInt::get(IndexTy, i); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1954 | // Get the correct ivar field |
| 1955 | llvm::Constant *offsetValue = llvm::ConstantExpr::getGetElementPtr( |
| 1956 | IvarList, offsetPointerIndexes, 4); |
David Chisnall | e8431a7 | 2010-11-03 16:12:44 +0000 | [diff] [blame] | 1957 | // Get the existing variable, if one exists. |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1958 | llvm::GlobalVariable *offset = TheModule.getNamedGlobal(Name); |
| 1959 | if (offset) { |
| 1960 | offset->setInitializer(offsetValue); |
| 1961 | // If this is the real definition, change its linkage type so that |
| 1962 | // different modules will use this one, rather than their private |
| 1963 | // copy. |
| 1964 | offset->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 1965 | } else { |
| 1966 | // Add a new alias if there isn't one already. |
| 1967 | offset = new llvm::GlobalVariable(TheModule, offsetValue->getType(), |
| 1968 | false, llvm::GlobalValue::ExternalLinkage, offsetValue, Name); |
| 1969 | } |
| 1970 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1971 | //Generate metaclass for class methods |
| 1972 | llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr, |
David Chisnall | b3b44ce | 2009-11-16 19:05:54 +0000 | [diff] [blame] | 1973 | NULLPtr, 0x12L, ClassName.c_str(), 0, Zeros[0], GenerateIvarList( |
David Chisnall | d472c85 | 2010-04-28 14:29:56 +0000 | [diff] [blame] | 1974 | empty, empty, empty), ClassMethodList, NULLPtr, NULLPtr, NULLPtr, true); |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1975 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1976 | // Generate the class structure |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1977 | llvm::Constant *ClassStruct = |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1978 | GenerateClassStructure(MetaClassStruct, SuperClass, 0x11L, |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1979 | ClassName.c_str(), 0, |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1980 | llvm::ConstantInt::get(LongTy, instanceSize), IvarList, |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1981 | MethodList, GenerateProtocolList(Protocols), IvarOffsetArray, |
| 1982 | Properties); |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1983 | |
| 1984 | // Resolve the class aliases, if they exist. |
| 1985 | if (ClassPtrAlias) { |
David Chisnall | 82f755c | 2010-11-09 11:21:43 +0000 | [diff] [blame] | 1986 | ClassPtrAlias->replaceAllUsesWith( |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1987 | llvm::ConstantExpr::getBitCast(ClassStruct, IdTy)); |
David Chisnall | 82f755c | 2010-11-09 11:21:43 +0000 | [diff] [blame] | 1988 | ClassPtrAlias->eraseFromParent(); |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1989 | ClassPtrAlias = 0; |
| 1990 | } |
| 1991 | if (MetaClassPtrAlias) { |
David Chisnall | 82f755c | 2010-11-09 11:21:43 +0000 | [diff] [blame] | 1992 | MetaClassPtrAlias->replaceAllUsesWith( |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1993 | llvm::ConstantExpr::getBitCast(MetaClassStruct, IdTy)); |
David Chisnall | 82f755c | 2010-11-09 11:21:43 +0000 | [diff] [blame] | 1994 | MetaClassPtrAlias->eraseFromParent(); |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1995 | MetaClassPtrAlias = 0; |
| 1996 | } |
| 1997 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1998 | // Add class structure to list to be added to the symtab later |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1999 | ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2000 | Classes.push_back(ClassStruct); |
| 2001 | } |
| 2002 | |
Fariborz Jahanian | 248c719 | 2009-06-23 21:47:46 +0000 | [diff] [blame] | 2003 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2004 | llvm::Function *CGObjCGNU::ModuleInitFunction() { |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2005 | // Only emit an ObjC load function if no Objective-C stuff has been called |
| 2006 | if (Classes.empty() && Categories.empty() && ConstantStrings.empty() && |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2007 | ExistingProtocols.empty() && SelectorTable.empty()) |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2008 | return NULL; |
Eli Friedman | 412c668 | 2008-06-01 16:00:02 +0000 | [diff] [blame] | 2009 | |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2010 | // Add all referenced protocols to a category. |
| 2011 | GenerateProtocolHolderCategory(); |
| 2012 | |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 2013 | const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>( |
| 2014 | SelectorTy->getElementType()); |
| 2015 | const llvm::Type *SelStructPtrTy = SelectorTy; |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 2016 | if (SelStructTy == 0) { |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 2017 | SelStructTy = llvm::StructType::get(VMContext, PtrToInt8Ty, |
| 2018 | PtrToInt8Ty, NULL); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2019 | SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy); |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
Eli Friedman | 412c668 | 2008-06-01 16:00:02 +0000 | [diff] [blame] | 2022 | // Name the ObjC types to make the IR a bit easier to read |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 2023 | TheModule.addTypeName(".objc_selector", SelStructPtrTy); |
Eli Friedman | 412c668 | 2008-06-01 16:00:02 +0000 | [diff] [blame] | 2024 | TheModule.addTypeName(".objc_id", IdTy); |
| 2025 | TheModule.addTypeName(".objc_imp", IMPTy); |
| 2026 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2027 | std::vector<llvm::Constant*> Elements; |
Chris Lattner | c06ce0f | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 2028 | llvm::Constant *Statics = NULLPtr; |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2029 | // Generate statics list: |
Chris Lattner | c06ce0f | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 2030 | if (ConstantStrings.size()) { |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2031 | llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty, |
Chris Lattner | c06ce0f | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 2032 | ConstantStrings.size() + 1); |
| 2033 | ConstantStrings.push_back(NULLPtr); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2034 | |
Daniel Dunbar | 75fa84e | 2009-11-29 02:38:47 +0000 | [diff] [blame] | 2035 | llvm::StringRef StringClass = CGM.getLangOptions().ObjCConstantStringClass; |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2036 | |
Daniel Dunbar | 75fa84e | 2009-11-29 02:38:47 +0000 | [diff] [blame] | 2037 | if (StringClass.empty()) StringClass = "NXConstantString"; |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2038 | |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2039 | Elements.push_back(MakeConstantString(StringClass, |
| 2040 | ".objc_static_class_name")); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2041 | Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, |
Chris Lattner | c06ce0f | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 2042 | ConstantStrings)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2043 | llvm::StructType *StaticsListTy = |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 2044 | llvm::StructType::get(VMContext, PtrToInt8Ty, StaticsArrayTy, NULL); |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2045 | llvm::Type *StaticsListPtrTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2046 | llvm::PointerType::getUnqual(StaticsListTy); |
Chris Lattner | c06ce0f | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 2047 | Statics = MakeGlobal(StaticsListTy, Elements, ".objc_statics"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2048 | llvm::ArrayType *StaticsListArrayTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2049 | llvm::ArrayType::get(StaticsListPtrTy, 2); |
Chris Lattner | c06ce0f | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 2050 | Elements.clear(); |
| 2051 | Elements.push_back(Statics); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2052 | Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy)); |
Chris Lattner | c06ce0f | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 2053 | Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr"); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2054 | Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy); |
Chris Lattner | c06ce0f | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 2055 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2056 | // Array of classes, categories, and constant objects |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2057 | llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2058 | Classes.size() + Categories.size() + 2); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2059 | llvm::StructType *SymTabTy = llvm::StructType::get(VMContext, |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 2060 | LongTy, SelStructPtrTy, |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2061 | llvm::Type::getInt16Ty(VMContext), |
| 2062 | llvm::Type::getInt16Ty(VMContext), |
Chris Lattner | 63dd337 | 2008-06-26 04:10:42 +0000 | [diff] [blame] | 2063 | ClassListTy, NULL); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2064 | |
| 2065 | Elements.clear(); |
| 2066 | // Pointer to an array of selectors used in this module. |
| 2067 | std::vector<llvm::Constant*> Selectors; |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2068 | std::vector<llvm::GlobalAlias*> SelectorAliases; |
| 2069 | for (SelectorMap::iterator iter = SelectorTable.begin(), |
| 2070 | iterEnd = SelectorTable.end(); iter != iterEnd ; ++iter) { |
| 2071 | |
| 2072 | std::string SelNameStr = iter->first.getAsString(); |
| 2073 | llvm::Constant *SelName = ExportUniqueString(SelNameStr, ".objc_sel_name"); |
| 2074 | |
| 2075 | llvm::SmallVectorImpl<TypedSelector> &Types = iter->second; |
| 2076 | for (llvm::SmallVectorImpl<TypedSelector>::iterator i = Types.begin(), |
| 2077 | e = Types.end() ; i!=e ; i++) { |
| 2078 | |
| 2079 | llvm::Constant *SelectorTypeEncoding = NULLPtr; |
| 2080 | if (!i->first.empty()) |
| 2081 | SelectorTypeEncoding = MakeConstantString(i->first, ".objc_sel_types"); |
| 2082 | |
| 2083 | Elements.push_back(SelName); |
| 2084 | Elements.push_back(SelectorTypeEncoding); |
| 2085 | Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements)); |
| 2086 | Elements.clear(); |
| 2087 | |
| 2088 | // Store the selector alias for later replacement |
| 2089 | SelectorAliases.push_back(i->second); |
| 2090 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2091 | } |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2092 | unsigned SelectorCount = Selectors.size(); |
| 2093 | // NULL-terminate the selector list. This should not actually be required, |
| 2094 | // because the selector list has a length field. Unfortunately, the GCC |
| 2095 | // runtime decides to ignore the length field and expects a NULL terminator, |
| 2096 | // and GCC cooperates with this by always setting the length to 0. |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2097 | Elements.push_back(NULLPtr); |
| 2098 | Elements.push_back(NULLPtr); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2099 | Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements)); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2100 | Elements.clear(); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2101 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2102 | // Number of static selectors |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2103 | Elements.push_back(llvm::ConstantInt::get(LongTy, SelectorCount)); |
| 2104 | llvm::Constant *SelectorList = MakeGlobalArray(SelStructTy, Selectors, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2105 | ".objc_selector_list"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2106 | Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList, |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 2107 | SelStructPtrTy)); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2108 | |
| 2109 | // Now that all of the static selectors exist, create pointers to them. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2110 | for (unsigned int i=0 ; i<SelectorCount ; i++) { |
| 2111 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2112 | llvm::Constant *Idxs[] = {Zeros[0], |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2113 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), i), Zeros[0]}; |
| 2114 | // FIXME: We're generating redundant loads and stores here! |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 2115 | llvm::Constant *SelPtr = llvm::ConstantExpr::getGetElementPtr(SelectorList, |
| 2116 | Idxs, 2); |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 2117 | // If selectors are defined as an opaque type, cast the pointer to this |
| 2118 | // type. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 2119 | SelPtr = llvm::ConstantExpr::getBitCast(SelPtr, SelectorTy); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2120 | SelectorAliases[i]->replaceAllUsesWith(SelPtr); |
| 2121 | SelectorAliases[i]->eraseFromParent(); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2122 | } |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2123 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2124 | // Number of classes defined. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2125 | Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext), |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2126 | Classes.size())); |
| 2127 | // Number of categories defined |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2128 | Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext), |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2129 | Categories.size())); |
| 2130 | // Create an array of classes, then categories, then static object instances |
| 2131 | Classes.insert(Classes.end(), Categories.begin(), Categories.end()); |
| 2132 | // NULL-terminated list of static object instances (mainly constant strings) |
| 2133 | Classes.push_back(Statics); |
| 2134 | Classes.push_back(NULLPtr); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2135 | llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2136 | Elements.push_back(ClassList); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2137 | // Construct the symbol table |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2138 | llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements); |
| 2139 | |
| 2140 | // The symbol table is contained in a module which has some version-checking |
| 2141 | // constants |
Owen Anderson | 758428f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 2142 | llvm::StructType * ModuleTy = llvm::StructType::get(VMContext, LongTy, LongTy, |
David Chisnall | 5c51177 | 2011-05-22 22:37:08 +0000 | [diff] [blame] | 2143 | PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), |
| 2144 | (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) ? NULL : IntTy, |
| 2145 | NULL); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2146 | Elements.clear(); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2147 | // Runtime version, used for ABI compatibility checking. |
| 2148 | Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion)); |
Fariborz Jahanian | c2d5618 | 2009-04-01 19:49:42 +0000 | [diff] [blame] | 2149 | // sizeof(ModuleTy) |
Benjamin Kramer | f3a499a | 2010-02-09 19:31:24 +0000 | [diff] [blame] | 2150 | llvm::TargetData td(&TheModule); |
Ken Dyck | 0fed10e | 2011-04-22 17:59:22 +0000 | [diff] [blame] | 2151 | Elements.push_back( |
| 2152 | llvm::ConstantInt::get(LongTy, |
| 2153 | td.getTypeSizeInBits(ModuleTy) / |
| 2154 | CGM.getContext().getCharWidth())); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2155 | |
| 2156 | // The path to the source file where this module was declared |
| 2157 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 2158 | const FileEntry *mainFile = SM.getFileEntryForID(SM.getMainFileID()); |
| 2159 | std::string path = |
| 2160 | std::string(mainFile->getDir()->getName()) + '/' + mainFile->getName(); |
| 2161 | Elements.push_back(MakeConstantString(path, ".objc_source_file_name")); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2162 | Elements.push_back(SymTab); |
David Chisnall | 5c51177 | 2011-05-22 22:37:08 +0000 | [diff] [blame] | 2163 | |
| 2164 | switch (CGM.getLangOptions().getGCMode()) { |
| 2165 | case LangOptions::GCOnly: |
| 2166 | Elements.push_back(llvm::ConstantInt::get(IntTy, 2)); |
| 2167 | case LangOptions::NonGC: |
| 2168 | break; |
| 2169 | case LangOptions::HybridGC: |
| 2170 | Elements.push_back(llvm::ConstantInt::get(IntTy, 1)); |
| 2171 | } |
| 2172 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2173 | llvm::Value *Module = MakeGlobal(ModuleTy, Elements); |
| 2174 | |
| 2175 | // Create the load function calling the runtime entry point with the module |
| 2176 | // structure |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2177 | llvm::Function * LoadFunction = llvm::Function::Create( |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2178 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false), |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2179 | llvm::GlobalValue::InternalLinkage, ".objc_load_function", |
| 2180 | &TheModule); |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2181 | llvm::BasicBlock *EntryBB = |
| 2182 | llvm::BasicBlock::Create(VMContext, "entry", LoadFunction); |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2183 | CGBuilderTy Builder(VMContext); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2184 | Builder.SetInsertPoint(EntryBB); |
Fariborz Jahanian | 3b636c1 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 2185 | |
| 2186 | std::vector<const llvm::Type*> Params(1, |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2187 | llvm::PointerType::getUnqual(ModuleTy)); |
| 2188 | llvm::Value *Register = CGM.CreateRuntimeFunction(llvm::FunctionType::get( |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2189 | llvm::Type::getVoidTy(VMContext), Params, true), "__objc_exec_class"); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2190 | Builder.CreateCall(Register, Module); |
| 2191 | Builder.CreateRetVoid(); |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2192 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2193 | return LoadFunction; |
| 2194 | } |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2195 | |
Fariborz Jahanian | 0196a1c | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2196 | llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2197 | const ObjCContainerDecl *CD) { |
| 2198 | const ObjCCategoryImplDecl *OCD = |
Steve Naroff | 11b387f | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 2199 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext()); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2200 | llvm::StringRef CategoryName = OCD ? OCD->getName() : ""; |
| 2201 | llvm::StringRef ClassName = CD->getName(); |
| 2202 | Selector MethodName = OMD->getSelector(); |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 2203 | bool isClassMethod = !OMD->isInstanceMethod(); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2204 | |
Daniel Dunbar | bf8c24a | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2205 | CodeGenTypes &Types = CGM.getTypes(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2206 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | bf8c24a | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2207 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2208 | std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName, |
| 2209 | MethodName, isClassMethod); |
| 2210 | |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 2211 | llvm::Function *Method |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2212 | = llvm::Function::Create(MethodTy, |
| 2213 | llvm::GlobalValue::InternalLinkage, |
| 2214 | FunctionName, |
| 2215 | &TheModule); |
Chris Lattner | 4bd5596 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 2216 | return Method; |
| 2217 | } |
| 2218 | |
David Chisnall | 3fe8956 | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 2219 | llvm::Constant *CGObjCGNU::GetPropertyGetFunction() { |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2220 | return GetPropertyFn; |
Daniel Dunbar | a91c3e0 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
David Chisnall | 3fe8956 | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 2223 | llvm::Constant *CGObjCGNU::GetPropertySetFunction() { |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2224 | return SetPropertyFn; |
Daniel Dunbar | a91c3e0 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2225 | } |
| 2226 | |
David Chisnall | 3fe8956 | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 2227 | llvm::Constant *CGObjCGNU::GetGetStructFunction() { |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2228 | return GetStructPropertyFn; |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 2229 | } |
David Chisnall | 3fe8956 | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 2230 | llvm::Constant *CGObjCGNU::GetSetStructFunction() { |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2231 | return SetStructPropertyFn; |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 2232 | } |
| 2233 | |
Daniel Dunbar | c46a079 | 2009-07-24 07:40:24 +0000 | [diff] [blame] | 2234 | llvm::Constant *CGObjCGNU::EnumerationMutationFunction() { |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2235 | return EnumerationMutationFn; |
Anders Carlsson | 3f35a26 | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2236 | } |
| 2237 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2238 | void CGObjCGNU::EmitSynchronizedStmt(CodeGenFunction &CGF, |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2239 | const ObjCAtSynchronizedStmt &S) { |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 2240 | EmitAtSynchronizedStmt(CGF, S, SyncEnterFn, SyncExitFn); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2241 | } |
Chris Lattner | b6e9eb6 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 2242 | |
David Chisnall | 3a509cd | 2009-12-24 02:26:34 +0000 | [diff] [blame] | 2243 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2244 | void CGObjCGNU::EmitTryStmt(CodeGenFunction &CGF, |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2245 | const ObjCAtTryStmt &S) { |
| 2246 | // Unlike the Apple non-fragile runtimes, which also uses |
| 2247 | // unwind-based zero cost exceptions, the GNU Objective C runtime's |
| 2248 | // EH support isn't a veneer over C++ EH. Instead, exception |
| 2249 | // objects are created by __objc_exception_throw and destroyed by |
| 2250 | // the personality function; this avoids the need for bracketing |
| 2251 | // catch handlers with calls to __blah_begin_catch/__blah_end_catch |
| 2252 | // (or even _Unwind_DeleteException), but probably doesn't |
| 2253 | // interoperate very well with foreign exceptions. |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 2254 | // |
David Chisnall | e1d2584d | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 2255 | // In Objective-C++ mode, we actually emit something equivalent to the C++ |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 2256 | // exception handler. |
| 2257 | EmitTryCatchStmt(CGF, S, EnterCatchFn, ExitCatchFn, ExceptionReThrowFn); |
| 2258 | return ; |
Anders Carlsson | 1963b0c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2259 | } |
| 2260 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2261 | void CGObjCGNU::EmitThrowStmt(CodeGenFunction &CGF, |
Daniel Dunbar | a91c3e0 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2262 | const ObjCAtThrowStmt &S) { |
Chris Lattner | b6e9eb6 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 2263 | llvm::Value *ExceptionAsObject; |
| 2264 | |
Chris Lattner | b6e9eb6 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 2265 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 2266 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
Fariborz Jahanian | 078cd52 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 2267 | ExceptionAsObject = Exception; |
Chris Lattner | b6e9eb6 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 2268 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2269 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Chris Lattner | b6e9eb6 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 2270 | "Unexpected rethrow outside @catch block."); |
| 2271 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
| 2272 | } |
Fariborz Jahanian | 078cd52 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 2273 | ExceptionAsObject = |
| 2274 | CGF.Builder.CreateBitCast(ExceptionAsObject, IdTy, "tmp"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2275 | |
Chris Lattner | b6e9eb6 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 2276 | // Note: This may have to be an invoke, if we want to support constructs like: |
| 2277 | // @try { |
| 2278 | // @throw(obj); |
| 2279 | // } |
| 2280 | // @catch(id) ... |
| 2281 | // |
| 2282 | // This is effectively turning @throw into an incredibly-expensive goto, but |
| 2283 | // it may happen as a result of inlining followed by missed optimizations, or |
| 2284 | // as a result of stupidity. |
| 2285 | llvm::BasicBlock *UnwindBB = CGF.getInvokeDest(); |
| 2286 | if (!UnwindBB) { |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2287 | CGF.Builder.CreateCall(ExceptionThrowFn, ExceptionAsObject); |
Chris Lattner | b6e9eb6 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 2288 | CGF.Builder.CreateUnreachable(); |
| 2289 | } else { |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2290 | CGF.Builder.CreateInvoke(ExceptionThrowFn, UnwindBB, UnwindBB, &ExceptionAsObject, |
Chris Lattner | b6e9eb6 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 2291 | &ExceptionAsObject+1); |
| 2292 | } |
| 2293 | // Clear the insertion point to indicate we are in unreachable code. |
| 2294 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 1963b0c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2297 | llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2298 | llvm::Value *AddrWeakObj) { |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2299 | CGBuilderTy B = CGF.Builder; |
| 2300 | AddrWeakObj = EnforceType(B, AddrWeakObj, IdTy); |
| 2301 | return B.CreateCall(WeakReadFn, AddrWeakObj); |
Fariborz Jahanian | f5125d1 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2304 | void CGObjCGNU::EmitObjCWeakAssign(CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2305 | llvm::Value *src, llvm::Value *dst) { |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2306 | CGBuilderTy B = CGF.Builder; |
| 2307 | src = EnforceType(B, src, IdTy); |
| 2308 | dst = EnforceType(B, dst, PtrToIdTy); |
| 2309 | B.CreateCall2(WeakAssignFn, src, dst); |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2310 | } |
| 2311 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2312 | void CGObjCGNU::EmitObjCGlobalAssign(CodeGenFunction &CGF, |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 2313 | llvm::Value *src, llvm::Value *dst, |
| 2314 | bool threadlocal) { |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2315 | CGBuilderTy B = CGF.Builder; |
| 2316 | src = EnforceType(B, src, IdTy); |
| 2317 | dst = EnforceType(B, dst, PtrToIdTy); |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 2318 | if (!threadlocal) |
| 2319 | B.CreateCall2(GlobalAssignFn, src, dst); |
| 2320 | else |
| 2321 | // FIXME. Add threadloca assign API |
| 2322 | assert(false && "EmitObjCGlobalAssign - Threal Local API NYI"); |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2325 | void CGObjCGNU::EmitObjCIvarAssign(CodeGenFunction &CGF, |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 2326 | llvm::Value *src, llvm::Value *dst, |
| 2327 | llvm::Value *ivarOffset) { |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2328 | CGBuilderTy B = CGF.Builder; |
| 2329 | src = EnforceType(B, src, IdTy); |
David Chisnall | e4e5c0f | 2011-05-25 20:33:17 +0000 | [diff] [blame] | 2330 | dst = EnforceType(B, dst, IdTy); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2331 | B.CreateCall3(IvarAssignFn, src, dst, ivarOffset); |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2334 | void CGObjCGNU::EmitObjCStrongCastAssign(CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2335 | llvm::Value *src, llvm::Value *dst) { |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2336 | CGBuilderTy B = CGF.Builder; |
| 2337 | src = EnforceType(B, src, IdTy); |
| 2338 | dst = EnforceType(B, dst, PtrToIdTy); |
| 2339 | B.CreateCall2(StrongCastAssignFn, src, dst); |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2342 | void CGObjCGNU::EmitGCMemmoveCollectable(CodeGenFunction &CGF, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2343 | llvm::Value *DestPtr, |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2344 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 2345 | llvm::Value *Size) { |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2346 | CGBuilderTy B = CGF.Builder; |
| 2347 | DestPtr = EnforceType(B, DestPtr, IdTy); |
| 2348 | SrcPtr = EnforceType(B, SrcPtr, PtrToIdTy); |
| 2349 | |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 2350 | B.CreateCall3(MemMoveFn, DestPtr, SrcPtr, Size); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2351 | } |
| 2352 | |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2353 | llvm::GlobalVariable *CGObjCGNU::ObjCIvarOffsetVariable( |
| 2354 | const ObjCInterfaceDecl *ID, |
| 2355 | const ObjCIvarDecl *Ivar) { |
| 2356 | const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString() |
| 2357 | + '.' + Ivar->getNameAsString(); |
| 2358 | // Emit the variable and initialize it with what we think the correct value |
| 2359 | // is. This allows code compiled with non-fragile ivars to work correctly |
| 2360 | // when linked against code which isn't (most of the time). |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2361 | llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name); |
| 2362 | if (!IvarOffsetPointer) { |
David Chisnall | e8431a7 | 2010-11-03 16:12:44 +0000 | [diff] [blame] | 2363 | // This will cause a run-time crash if we accidentally use it. A value of |
| 2364 | // 0 would seem more sensible, but will silently overwrite the isa pointer |
| 2365 | // causing a great deal of confusion. |
| 2366 | uint64_t Offset = -1; |
| 2367 | // We can't call ComputeIvarBaseOffset() here if we have the |
| 2368 | // implementation, because it will create an invalid ASTRecordLayout object |
| 2369 | // that we are then stuck with forever, so we only initialize the ivar |
| 2370 | // offset variable with a guess if we only have the interface. The |
| 2371 | // initializer will be reset later anyway, when we are generating the class |
| 2372 | // description. |
| 2373 | if (!CGM.getContext().getObjCImplementation( |
Dan Gohman | 145f3f1 | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 2374 | const_cast<ObjCInterfaceDecl *>(ID))) |
David Chisnall | 44ec555 | 2010-04-19 01:37:25 +0000 | [diff] [blame] | 2375 | Offset = ComputeIvarBaseOffset(CGM, ID, Ivar); |
| 2376 | |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2377 | llvm::ConstantInt *OffsetGuess = |
David Chisnall | c8fc573 | 2010-01-11 19:02:35 +0000 | [diff] [blame] | 2378 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Offset, "ivar"); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2379 | // Don't emit the guess in non-PIC code because the linker will not be able |
| 2380 | // to replace it with the real version for a library. In non-PIC code you |
| 2381 | // must compile with the fragile ABI if you want to use ivars from a |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2382 | // GCC-compiled class. |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2383 | if (CGM.getLangOptions().PICLevel) { |
| 2384 | llvm::GlobalVariable *IvarOffsetGV = new llvm::GlobalVariable(TheModule, |
| 2385 | llvm::Type::getInt32Ty(VMContext), false, |
| 2386 | llvm::GlobalValue::PrivateLinkage, OffsetGuess, Name+".guess"); |
| 2387 | IvarOffsetPointer = new llvm::GlobalVariable(TheModule, |
| 2388 | IvarOffsetGV->getType(), false, llvm::GlobalValue::LinkOnceAnyLinkage, |
| 2389 | IvarOffsetGV, Name); |
| 2390 | } else { |
| 2391 | IvarOffsetPointer = new llvm::GlobalVariable(TheModule, |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 2392 | llvm::Type::getInt32PtrTy(VMContext), false, |
| 2393 | llvm::GlobalValue::ExternalLinkage, 0, Name); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2394 | } |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2395 | } |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2396 | return IvarOffsetPointer; |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2397 | } |
| 2398 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2399 | LValue CGObjCGNU::EmitObjCValueForIvar(CodeGenFunction &CGF, |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2400 | QualType ObjectTy, |
| 2401 | llvm::Value *BaseValue, |
| 2402 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2403 | unsigned CVRQualifiers) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2404 | const ObjCInterfaceDecl *ID = |
| 2405 | ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Daniel Dunbar | 9fd114d | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2406 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 2407 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 9f84b78 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2408 | } |
Mike Stump | dd93a19 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 2409 | |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2410 | static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context, |
| 2411 | const ObjCInterfaceDecl *OID, |
| 2412 | const ObjCIvarDecl *OIVD) { |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2413 | llvm::SmallVector<ObjCIvarDecl*, 16> Ivars; |
Fariborz Jahanian | 7c80959 | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2414 | Context.ShallowCollectObjCIvars(OID, Ivars); |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2415 | for (unsigned k = 0, e = Ivars.size(); k != e; ++k) { |
| 2416 | if (OIVD == Ivars[k]) |
| 2417 | return OID; |
| 2418 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2419 | |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2420 | // Otherwise check in the super class. |
| 2421 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
| 2422 | return FindIvarInterface(Context, Super, OIVD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2423 | |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2424 | return 0; |
| 2425 | } |
Fariborz Jahanian | 9f84b78 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2426 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2427 | llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGenFunction &CGF, |
Daniel Dunbar | 722f424 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 2428 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2429 | const ObjCIvarDecl *Ivar) { |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2430 | if (CGM.getLangOptions().ObjCNonFragileABI) { |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2431 | Interface = FindIvarInterface(CGM.getContext(), Interface, Ivar); |
David Chisnall | 6a566d2 | 2011-03-22 19:57:51 +0000 | [diff] [blame] | 2432 | return CGF.Builder.CreateZExtOrBitCast( |
| 2433 | CGF.Builder.CreateLoad(CGF.Builder.CreateLoad( |
| 2434 | ObjCIvarOffsetVariable(Interface, Ivar), false, "ivar")), |
| 2435 | PtrDiffTy); |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2436 | } |
Daniel Dunbar | 9fd114d | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2437 | uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar); |
David Chisnall | 6a566d2 | 2011-03-22 19:57:51 +0000 | [diff] [blame] | 2438 | return llvm::ConstantInt::get(PtrDiffTy, Offset, "ivar"); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2439 | } |
| 2440 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2441 | CGObjCRuntime * |
| 2442 | clang::CodeGen::CreateGNUObjCRuntime(CodeGenModule &CGM) { |
| 2443 | if (CGM.getLangOptions().ObjCNonFragileABI) |
| 2444 | return new CGObjCGNUstep(CGM); |
| 2445 | return new CGObjCGCC(CGM); |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 2446 | } |