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" |
John McCall | ed1ae86 | 2011-01-28 11:13:47 +0000 | [diff] [blame] | 18 | #include "CGCleanup.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "CodeGenFunction.h" |
| 20 | #include "CodeGenModule.h" |
Chris Lattner | 87ab27d | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 22 | #include "clang/AST/Decl.h" |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | f0b64d7 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 25 | #include "clang/AST/StmtObjC.h" |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 26 | #include "clang/Basic/FileManager.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 27 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallVector.h" |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/StringMap.h" |
Chandler Carruth | c80ceea | 2014-03-04 11:02:08 +0000 | [diff] [blame] | 30 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 31 | #include "llvm/IR/DataLayout.h" |
| 32 | #include "llvm/IR/Intrinsics.h" |
| 33 | #include "llvm/IR/LLVMContext.h" |
| 34 | #include "llvm/IR/Module.h" |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 36 | #include <cstdarg> |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 87ab27d | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 38 | using namespace clang; |
Daniel Dunbar | 41cf9de | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 39 | using namespace CodeGen; |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 40 | |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 41 | namespace { |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 42 | /// Class that lazily initialises the runtime function. Avoids inserting the |
| 43 | /// types and the function declaration into a module if they're not used, and |
| 44 | /// 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] | 45 | class LazyRuntimeFunction { |
| 46 | CodeGenModule *CGM; |
David Blaikie | bf178d3 | 2015-05-19 21:31:34 +0000 | [diff] [blame] | 47 | llvm::FunctionType *FTy; |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 48 | const char *FunctionName; |
David Chisnall | 3fe8956 | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 49 | llvm::Constant *Function; |
David Blaikie | 7d9e792 | 2015-05-18 22:51:39 +0000 | [diff] [blame] | 50 | |
| 51 | public: |
| 52 | /// Constructor leaves this class uninitialized, because it is intended to |
| 53 | /// be used as a field in another class and not all of the types that are |
| 54 | /// used as arguments will necessarily be available at construction time. |
| 55 | LazyRuntimeFunction() |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 56 | : CGM(nullptr), FunctionName(nullptr), Function(nullptr) {} |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 57 | |
David Blaikie | 7d9e792 | 2015-05-18 22:51:39 +0000 | [diff] [blame] | 58 | /// Initialises the lazy function with the name, return type, and the types |
| 59 | /// of the arguments. |
| 60 | LLVM_END_WITH_NULL |
| 61 | void init(CodeGenModule *Mod, const char *name, llvm::Type *RetTy, ...) { |
| 62 | CGM = Mod; |
| 63 | FunctionName = name; |
| 64 | Function = nullptr; |
David Blaikie | bf178d3 | 2015-05-19 21:31:34 +0000 | [diff] [blame] | 65 | std::vector<llvm::Type *> ArgTys; |
David Blaikie | 7d9e792 | 2015-05-18 22:51:39 +0000 | [diff] [blame] | 66 | va_list Args; |
| 67 | va_start(Args, RetTy); |
| 68 | while (llvm::Type *ArgTy = va_arg(Args, llvm::Type *)) |
| 69 | ArgTys.push_back(ArgTy); |
| 70 | va_end(Args); |
David Blaikie | bf178d3 | 2015-05-19 21:31:34 +0000 | [diff] [blame] | 71 | FTy = llvm::FunctionType::get(RetTy, ArgTys, false); |
David Blaikie | 7d9e792 | 2015-05-18 22:51:39 +0000 | [diff] [blame] | 72 | } |
David Blaikie | bf178d3 | 2015-05-19 21:31:34 +0000 | [diff] [blame] | 73 | |
| 74 | llvm::FunctionType *getType() { return FTy; } |
| 75 | |
David Blaikie | 7d9e792 | 2015-05-18 22:51:39 +0000 | [diff] [blame] | 76 | /// Overloaded cast operator, allows the class to be implicitly cast to an |
| 77 | /// LLVM constant. |
| 78 | operator llvm::Constant *() { |
| 79 | if (!Function) { |
| 80 | if (!FunctionName) |
| 81 | return nullptr; |
David Blaikie | 7d9e792 | 2015-05-18 22:51:39 +0000 | [diff] [blame] | 82 | Function = |
| 83 | cast<llvm::Constant>(CGM->CreateRuntimeFunction(FTy, FunctionName)); |
David Blaikie | 7d9e792 | 2015-05-18 22:51:39 +0000 | [diff] [blame] | 84 | } |
| 85 | return Function; |
| 86 | } |
| 87 | operator llvm::Function *() { |
| 88 | return cast<llvm::Function>((llvm::Constant *)*this); |
| 89 | } |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 93 | /// GNU Objective-C runtime code generation. This class implements the parts of |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 94 | /// Objective-C support that are specific to the GNU family of runtimes (GCC, |
| 95 | /// GNUstep and ObjFW). |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 96 | class CGObjCGNU : public CGObjCRuntime { |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 97 | protected: |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 98 | /// The LLVM module into which output is inserted |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 99 | llvm::Module &TheModule; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 100 | /// strut objc_super. Used for sending messages to super. This structure |
| 101 | /// contains the receiver (object) and the expected class. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 102 | llvm::StructType *ObjCSuperTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 103 | /// struct objc_super*. The type of the argument to the superclass message |
| 104 | /// lookup functions. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 105 | llvm::PointerType *PtrToObjCSuperTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 106 | /// LLVM type for selectors. Opaque pointer (i8*) unless a header declaring |
| 107 | /// SEL is included in a header somewhere, in which case it will be whatever |
| 108 | /// type is declared in that header, most likely {i8*, i8*}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 109 | llvm::PointerType *SelectorTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 110 | /// LLVM i8 type. Cached here to avoid repeatedly getting it in all of the |
| 111 | /// places where it's used |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 112 | llvm::IntegerType *Int8Ty; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 113 | /// Pointer to i8 - LLVM type of char*, for all of the places where the |
| 114 | /// runtime needs to deal with C strings. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 115 | llvm::PointerType *PtrToInt8Ty; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 116 | /// Instance Method Pointer type. This is a pointer to a function that takes, |
| 117 | /// at a minimum, an object and a selector, and is the generic type for |
| 118 | /// Objective-C methods. Due to differences between variadic / non-variadic |
| 119 | /// calling conventions, it must always be cast to the correct type before |
| 120 | /// actually being used. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 121 | llvm::PointerType *IMPTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 122 | /// Type of an untyped Objective-C object. Clang treats id as a built-in type |
| 123 | /// when compiling Objective-C code, so this may be an opaque pointer (i8*), |
| 124 | /// but if the runtime header declaring it is included then it may be a |
| 125 | /// pointer to a structure. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 126 | llvm::PointerType *IdTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 127 | /// Pointer to a pointer to an Objective-C object. Used in the new ABI |
| 128 | /// message lookup function and some GC-related functions. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 129 | llvm::PointerType *PtrToIdTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 130 | /// The clang type of id. Used when using the clang CGCall infrastructure to |
| 131 | /// call Objective-C methods. |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 132 | CanQualType ASTIdTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 133 | /// LLVM type for C int type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 134 | llvm::IntegerType *IntTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 135 | /// LLVM type for an opaque pointer. This is identical to PtrToInt8Ty, but is |
| 136 | /// used in the code to document the difference between i8* meaning a pointer |
| 137 | /// to a C string and i8* meaning a pointer to some opaque type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 138 | llvm::PointerType *PtrTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 139 | /// LLVM type for C long type. The runtime uses this in a lot of places where |
| 140 | /// it should be using intptr_t, but we can't fix this without breaking |
| 141 | /// compatibility with GCC... |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 142 | llvm::IntegerType *LongTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 143 | /// LLVM type for C size_t. Used in various runtime data structures. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 144 | llvm::IntegerType *SizeTy; |
David Chisnall | e0dc7cb | 2011-10-08 08:54:36 +0000 | [diff] [blame] | 145 | /// LLVM type for C intptr_t. |
| 146 | llvm::IntegerType *IntPtrTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 147 | /// LLVM type for C ptrdiff_t. Mainly used in property accessor functions. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 148 | llvm::IntegerType *PtrDiffTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 149 | /// LLVM type for C int*. Used for GCC-ABI-compatible non-fragile instance |
| 150 | /// variables. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 151 | llvm::PointerType *PtrToIntTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 152 | /// LLVM type for Objective-C BOOL type. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 153 | llvm::Type *BoolTy; |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 154 | /// 32-bit integer type, to save us needing to look it up every time it's used. |
| 155 | llvm::IntegerType *Int32Ty; |
| 156 | /// 64-bit integer type, to save us needing to look it up every time it's used. |
| 157 | llvm::IntegerType *Int64Ty; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 158 | /// Metadata kind used to tie method lookups to message sends. The GNUstep |
| 159 | /// runtime provides some LLVM passes that can use this to do things like |
| 160 | /// automatic IMP caching and speculative inlining. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 161 | unsigned msgSendMDKind; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 162 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 163 | /// Helper function that generates a constant string and returns a pointer to |
| 164 | /// the start of the string. The result of this function can be used anywhere |
| 165 | /// where the C code specifies const char*. |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 166 | llvm::Constant *MakeConstantString(const std::string &Str, |
| 167 | const std::string &Name="") { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 168 | ConstantAddress Array = CGM.GetAddrOfConstantCString(Str, Name.c_str()); |
| 169 | return llvm::ConstantExpr::getGetElementPtr(Array.getElementType(), |
| 170 | Array.getPointer(), Zeros); |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 171 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 172 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 173 | /// Emits a linkonce_odr string, whose name is the prefix followed by the |
| 174 | /// string value. This allows the linker to combine the strings between |
| 175 | /// different modules. Used for EH typeinfo names, selector strings, and a |
| 176 | /// few other things. |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 177 | llvm::Constant *ExportUniqueString(const std::string &Str, |
| 178 | const std::string prefix) { |
| 179 | std::string name = prefix + Str; |
David Blaikie | e3b172a | 2015-04-02 18:55:21 +0000 | [diff] [blame] | 180 | auto *ConstStr = TheModule.getGlobalVariable(name); |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 181 | if (!ConstStr) { |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 182 | llvm::Constant *value = llvm::ConstantDataArray::getString(VMContext,Str); |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 183 | ConstStr = new llvm::GlobalVariable(TheModule, value->getType(), true, |
| 184 | llvm::GlobalValue::LinkOnceODRLinkage, value, prefix + Str); |
| 185 | } |
David Blaikie | e3b172a | 2015-04-02 18:55:21 +0000 | [diff] [blame] | 186 | return llvm::ConstantExpr::getGetElementPtr(ConstStr->getValueType(), |
| 187 | ConstStr, Zeros); |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 188 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 189 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 190 | /// Generates a global structure, initialized by the elements in the vector. |
| 191 | /// The element types must match the types of the structure elements in the |
| 192 | /// first argument. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 193 | llvm::GlobalVariable *MakeGlobal(llvm::StructType *Ty, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 194 | ArrayRef<llvm::Constant *> V, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 195 | CharUnits Align, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 196 | StringRef Name="", |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 197 | llvm::GlobalValue::LinkageTypes linkage |
| 198 | =llvm::GlobalValue::InternalLinkage) { |
| 199 | llvm::Constant *C = llvm::ConstantStruct::get(Ty, V); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 200 | auto GV = new llvm::GlobalVariable(TheModule, Ty, false, |
| 201 | linkage, C, Name); |
| 202 | GV->setAlignment(Align.getQuantity()); |
| 203 | return GV; |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 204 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 205 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 206 | /// Generates a global array. The vector must contain the same number of |
| 207 | /// elements that the array type declares, of the type specified as the array |
| 208 | /// element type. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 209 | llvm::GlobalVariable *MakeGlobal(llvm::ArrayType *Ty, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 210 | ArrayRef<llvm::Constant *> V, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 211 | CharUnits Align, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 212 | StringRef Name="", |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 213 | llvm::GlobalValue::LinkageTypes linkage |
| 214 | =llvm::GlobalValue::InternalLinkage) { |
| 215 | llvm::Constant *C = llvm::ConstantArray::get(Ty, V); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 216 | auto GV = new llvm::GlobalVariable(TheModule, Ty, false, |
| 217 | linkage, C, Name); |
| 218 | GV->setAlignment(Align.getQuantity()); |
| 219 | return GV; |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 220 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 221 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 222 | /// Generates a global array, inferring the array type from the specified |
| 223 | /// element type and the size of the initialiser. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 224 | llvm::GlobalVariable *MakeGlobalArray(llvm::Type *Ty, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 225 | ArrayRef<llvm::Constant *> V, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 226 | CharUnits Align, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 227 | StringRef Name="", |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 228 | llvm::GlobalValue::LinkageTypes linkage |
| 229 | =llvm::GlobalValue::InternalLinkage) { |
| 230 | llvm::ArrayType *ArrayTy = llvm::ArrayType::get(Ty, V.size()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 231 | return MakeGlobal(ArrayTy, V, Align, Name, linkage); |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 232 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 233 | |
David Chisnall | a5f5941 | 2012-10-16 15:11:55 +0000 | [diff] [blame] | 234 | /// Returns a property name and encoding string. |
| 235 | llvm::Constant *MakePropertyEncodingString(const ObjCPropertyDecl *PD, |
| 236 | const Decl *Container) { |
David Chisnall | beb8013 | 2013-02-28 13:59:29 +0000 | [diff] [blame] | 237 | const ObjCRuntime &R = CGM.getLangOpts().ObjCRuntime; |
David Chisnall | a5f5941 | 2012-10-16 15:11:55 +0000 | [diff] [blame] | 238 | if ((R.getKind() == ObjCRuntime::GNUstep) && |
| 239 | (R.getVersion() >= VersionTuple(1, 6))) { |
| 240 | std::string NameAndAttributes; |
| 241 | std::string TypeStr; |
| 242 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
| 243 | NameAndAttributes += '\0'; |
| 244 | NameAndAttributes += TypeStr.length() + 3; |
| 245 | NameAndAttributes += TypeStr; |
| 246 | NameAndAttributes += '\0'; |
| 247 | NameAndAttributes += PD->getNameAsString(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 248 | return MakeConstantString(NameAndAttributes); |
David Chisnall | a5f5941 | 2012-10-16 15:11:55 +0000 | [diff] [blame] | 249 | } |
| 250 | return MakeConstantString(PD->getNameAsString()); |
| 251 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 252 | |
David Chisnall | beb8013 | 2013-02-28 13:59:29 +0000 | [diff] [blame] | 253 | /// Push the property attributes into two structure fields. |
| 254 | void PushPropertyAttributes(std::vector<llvm::Constant*> &Fields, |
| 255 | ObjCPropertyDecl *property, bool isSynthesized=true, bool |
| 256 | isDynamic=true) { |
| 257 | int attrs = property->getPropertyAttributes(); |
| 258 | // For read-only properties, clear the copy and retain flags |
| 259 | if (attrs & ObjCPropertyDecl::OBJC_PR_readonly) { |
| 260 | attrs &= ~ObjCPropertyDecl::OBJC_PR_copy; |
| 261 | attrs &= ~ObjCPropertyDecl::OBJC_PR_retain; |
| 262 | attrs &= ~ObjCPropertyDecl::OBJC_PR_weak; |
| 263 | attrs &= ~ObjCPropertyDecl::OBJC_PR_strong; |
| 264 | } |
| 265 | // The first flags field has the same attribute values as clang uses internally |
| 266 | Fields.push_back(llvm::ConstantInt::get(Int8Ty, attrs & 0xff)); |
| 267 | attrs >>= 8; |
| 268 | attrs <<= 2; |
| 269 | // For protocol properties, synthesized and dynamic have no meaning, so we |
| 270 | // reuse these flags to indicate that this is a protocol property (both set |
| 271 | // has no meaning, as a property can't be both synthesized and dynamic) |
| 272 | attrs |= isSynthesized ? (1<<0) : 0; |
| 273 | attrs |= isDynamic ? (1<<1) : 0; |
| 274 | // The second field is the next four fields left shifted by two, with the |
| 275 | // low bit set to indicate whether the field is synthesized or dynamic. |
| 276 | Fields.push_back(llvm::ConstantInt::get(Int8Ty, attrs & 0xff)); |
| 277 | // Two padding fields |
| 278 | Fields.push_back(llvm::ConstantInt::get(Int8Ty, 0)); |
| 279 | Fields.push_back(llvm::ConstantInt::get(Int8Ty, 0)); |
| 280 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 281 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 282 | /// Ensures that the value has the required type, by inserting a bitcast if |
| 283 | /// required. This function lets us avoid inserting bitcasts that are |
| 284 | /// redundant. |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 285 | llvm::Value* EnforceType(CGBuilderTy &B, llvm::Value *V, llvm::Type *Ty) { |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 286 | if (V->getType() == Ty) return V; |
| 287 | return B.CreateBitCast(V, Ty); |
| 288 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 289 | Address EnforceType(CGBuilderTy &B, Address V, llvm::Type *Ty) { |
| 290 | if (V.getType() == Ty) return V; |
| 291 | return B.CreateBitCast(V, Ty); |
| 292 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 293 | |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 294 | // Some zeros used for GEPs in lots of places. |
| 295 | llvm::Constant *Zeros[2]; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 296 | /// Null pointer value. Mainly used as a terminator in various arrays. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 297 | llvm::Constant *NULLPtr; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 298 | /// LLVM context. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 299 | llvm::LLVMContext &VMContext; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 300 | |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 301 | private: |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 302 | /// Placeholder for the class. Lots of things refer to the class before we've |
| 303 | /// actually emitted it. We use this alias as a placeholder, and then replace |
| 304 | /// it with a pointer to the class structure before finally emitting the |
| 305 | /// module. |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 306 | llvm::GlobalAlias *ClassPtrAlias; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 307 | /// Placeholder for the metaclass. Lots of things refer to the class before |
| 308 | /// we've / actually emitted it. We use this alias as a placeholder, and then |
| 309 | /// replace / it with a pointer to the metaclass structure before finally |
| 310 | /// emitting the / module. |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 311 | llvm::GlobalAlias *MetaClassPtrAlias; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 312 | /// All of the classes that have been generated for this compilation units. |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 313 | std::vector<llvm::Constant*> Classes; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 314 | /// All of the categories that have been generated for this compilation units. |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 315 | std::vector<llvm::Constant*> Categories; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 316 | /// All of the Objective-C constant strings that have been generated for this |
| 317 | /// compilation units. |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 318 | std::vector<llvm::Constant*> ConstantStrings; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 319 | /// Map from string values to Objective-C constant strings in the output. |
| 320 | /// Used to prevent emitting Objective-C strings more than once. This should |
| 321 | /// not be required at all - CodeGenModule should manage this list. |
David Chisnall | 358e751 | 2010-01-27 12:49:23 +0000 | [diff] [blame] | 322 | llvm::StringMap<llvm::Constant*> ObjCStrings; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 323 | /// All of the protocols that have been declared. |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 324 | llvm::StringMap<llvm::Constant*> ExistingProtocols; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 325 | /// For each variant of a selector, we store the type encoding and a |
| 326 | /// placeholder value. For an untyped selector, the type will be the empty |
| 327 | /// string. Selector references are all done via the module's selector table, |
| 328 | /// so we create an alias as a placeholder and then replace it with the real |
| 329 | /// value later. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 330 | typedef std::pair<std::string, llvm::GlobalAlias*> TypedSelector; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 331 | /// Type of the selector map. This is roughly equivalent to the structure |
| 332 | /// used in the GNUstep runtime, which maintains a list of all of the valid |
| 333 | /// types for a selector in a table. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 334 | typedef llvm::DenseMap<Selector, SmallVector<TypedSelector, 2> > |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 335 | SelectorMap; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 336 | /// A map from selectors to selector types. This allows us to emit all |
| 337 | /// selectors of the same name and type together. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 338 | SelectorMap SelectorTable; |
| 339 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 340 | /// Selectors related to memory management. When compiling in GC mode, we |
| 341 | /// omit these. |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 342 | Selector RetainSel, ReleaseSel, AutoreleaseSel; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 343 | /// Runtime functions used for memory management in GC mode. Note that clang |
| 344 | /// supports code generation for calling these functions, but neither GNU |
| 345 | /// runtime actually supports this API properly yet. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 346 | LazyRuntimeFunction IvarAssignFn, StrongCastAssignFn, MemMoveFn, WeakReadFn, |
| 347 | WeakAssignFn, GlobalAssignFn; |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 348 | |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 349 | typedef std::pair<std::string, std::string> ClassAliasPair; |
| 350 | /// All classes that have aliases set for them. |
| 351 | std::vector<ClassAliasPair> ClassAliases; |
| 352 | |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 353 | protected: |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 354 | /// Function used for throwing Objective-C exceptions. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 355 | LazyRuntimeFunction ExceptionThrowFn; |
James Dennett | b9199ee | 2012-06-13 22:07:09 +0000 | [diff] [blame] | 356 | /// Function used for rethrowing exceptions, used at the end of \@finally or |
| 357 | /// \@synchronize blocks. |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 358 | LazyRuntimeFunction ExceptionReThrowFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 359 | /// Function called when entering a catch function. This is required for |
| 360 | /// differentiating Objective-C exceptions and foreign exceptions. |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 361 | LazyRuntimeFunction EnterCatchFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 362 | /// Function called when exiting from a catch block. Used to do exception |
| 363 | /// cleanup. |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 364 | LazyRuntimeFunction ExitCatchFn; |
James Dennett | b9199ee | 2012-06-13 22:07:09 +0000 | [diff] [blame] | 365 | /// Function called when entering an \@synchronize block. Acquires the lock. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 366 | LazyRuntimeFunction SyncEnterFn; |
James Dennett | b9199ee | 2012-06-13 22:07:09 +0000 | [diff] [blame] | 367 | /// Function called when exiting an \@synchronize block. Releases the lock. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 368 | LazyRuntimeFunction SyncExitFn; |
| 369 | |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 370 | private: |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 371 | /// Function called if fast enumeration detects that the collection is |
| 372 | /// modified during the update. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 373 | LazyRuntimeFunction EnumerationMutationFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 374 | /// Function for implementing synthesized property getters that return an |
| 375 | /// object. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 376 | LazyRuntimeFunction GetPropertyFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 377 | /// Function for implementing synthesized property setters that return an |
| 378 | /// object. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 379 | LazyRuntimeFunction SetPropertyFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 380 | /// Function used for non-object declared property getters. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 381 | LazyRuntimeFunction GetStructPropertyFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 382 | /// Function used for non-object declared property setters. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 383 | LazyRuntimeFunction SetStructPropertyFn; |
| 384 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 385 | /// The version of the runtime that this class targets. Must match the |
| 386 | /// version in the runtime. |
David Chisnall | 5c51177 | 2011-05-22 22:37:08 +0000 | [diff] [blame] | 387 | int RuntimeVersion; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 388 | /// The version of the protocol class. Used to differentiate between ObjC1 |
| 389 | /// and ObjC2 protocols. Objective-C 1 protocols can not contain optional |
| 390 | /// components and can not contain declared properties. We always emit |
| 391 | /// Objective-C 2 property structures, but we have to pretend that they're |
| 392 | /// Objective-C 1 property structures when targeting the GCC runtime or it |
| 393 | /// will abort. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 394 | const int ProtocolVersion; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 395 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 396 | /// Generates an instance variable list structure. This is a structure |
| 397 | /// containing a size and an array of structures containing instance variable |
| 398 | /// metadata. This is used purely for introspection in the fragile ABI. In |
| 399 | /// the non-fragile ABI, it's used for instance variable fixup. |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 400 | llvm::Constant *GenerateIvarList(ArrayRef<llvm::Constant *> IvarNames, |
| 401 | ArrayRef<llvm::Constant *> IvarTypes, |
| 402 | ArrayRef<llvm::Constant *> IvarOffsets); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 403 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 404 | /// Generates a method list structure. This is a structure containing a size |
| 405 | /// and an array of structures containing method metadata. |
| 406 | /// |
| 407 | /// This structure is used by both classes and categories, and contains a next |
| 408 | /// pointer allowing them to be chained together in a linked list. |
Craig Topper | bf3e327 | 2014-08-30 16:55:52 +0000 | [diff] [blame] | 409 | llvm::Constant *GenerateMethodList(StringRef ClassName, |
| 410 | StringRef CategoryName, |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 411 | ArrayRef<Selector> MethodSels, |
| 412 | ArrayRef<llvm::Constant *> MethodTypes, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 413 | bool isClassMethodList); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 414 | |
James Dennett | b9199ee | 2012-06-13 22:07:09 +0000 | [diff] [blame] | 415 | /// Emits an empty protocol. This is used for \@protocol() where no protocol |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 416 | /// is found. The runtime will (hopefully) fix up the pointer to refer to the |
| 417 | /// real protocol. |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 418 | llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 419 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 420 | /// Generates a list of property metadata structures. This follows the same |
| 421 | /// pattern as method and instance variable metadata lists. |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 422 | llvm::Constant *GeneratePropertyList(const ObjCImplementationDecl *OID, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 423 | SmallVectorImpl<Selector> &InstanceMethodSels, |
| 424 | SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 425 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 426 | /// Generates a list of referenced protocols. Classes, categories, and |
| 427 | /// protocols all use this structure. |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 428 | llvm::Constant *GenerateProtocolList(ArrayRef<std::string> Protocols); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 429 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 430 | /// To ensure that all protocols are seen by the runtime, we add a category on |
| 431 | /// a class defined in the runtime, declaring no methods, but adopting the |
| 432 | /// protocols. This is a horribly ugly hack, but it allows us to collect all |
| 433 | /// of the protocols without changing the ABI. |
Dmitri Gribenko | b2aa923 | 2012-11-15 14:28:07 +0000 | [diff] [blame] | 434 | void GenerateProtocolHolderCategory(); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 435 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 436 | /// Generates a class structure. |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 437 | llvm::Constant *GenerateClassStructure( |
| 438 | llvm::Constant *MetaClass, |
| 439 | llvm::Constant *SuperClass, |
| 440 | unsigned info, |
Chris Lattner | da35bc8 | 2008-06-26 04:47:04 +0000 | [diff] [blame] | 441 | const char *Name, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 442 | llvm::Constant *Version, |
| 443 | llvm::Constant *InstanceSize, |
| 444 | llvm::Constant *IVars, |
| 445 | llvm::Constant *Methods, |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 446 | llvm::Constant *Protocols, |
| 447 | llvm::Constant *IvarOffsets, |
David Chisnall | d472c85 | 2010-04-28 14:29:56 +0000 | [diff] [blame] | 448 | llvm::Constant *Properties, |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 449 | llvm::Constant *StrongIvarBitmap, |
| 450 | llvm::Constant *WeakIvarBitmap, |
David Chisnall | d472c85 | 2010-04-28 14:29:56 +0000 | [diff] [blame] | 451 | bool isMeta=false); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 452 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 453 | /// Generates a method list. This is used by protocols to define the required |
| 454 | /// and optional methods. |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 455 | llvm::Constant *GenerateProtocolMethodList( |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 456 | ArrayRef<llvm::Constant *> MethodNames, |
| 457 | ArrayRef<llvm::Constant *> MethodTypes); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 458 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 459 | /// Returns a selector with the specified type encoding. An empty string is |
| 460 | /// used to return an untyped selector (with the types field set to NULL). |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 461 | llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 462 | const std::string &TypeEncoding); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 463 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 464 | /// Returns the variable used to store the offset of an instance variable. |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 465 | llvm::GlobalVariable *ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID, |
| 466 | const ObjCIvarDecl *Ivar); |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 467 | /// Emits a reference to a class. This allows the linker to object if there |
| 468 | /// is no class of the matching name. |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 469 | |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 470 | protected: |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 471 | void EmitClassRef(const std::string &className); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 472 | |
David Chisnall | 920e83b | 2011-06-29 13:16:41 +0000 | [diff] [blame] | 473 | /// Emits a pointer to the named class |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 474 | virtual llvm::Value *GetClassNamed(CodeGenFunction &CGF, |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 475 | const std::string &Name, bool isWeak); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 476 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 477 | /// Looks up the method for sending a message to the specified object. This |
| 478 | /// mechanism differs between the GCC and GNU runtimes, so this method must be |
| 479 | /// overridden in subclasses. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 480 | virtual llvm::Value *LookupIMP(CodeGenFunction &CGF, |
| 481 | llvm::Value *&Receiver, |
| 482 | llvm::Value *cmd, |
Eli Friedman | f24bd3b | 2013-07-26 00:53:29 +0000 | [diff] [blame] | 483 | llvm::MDNode *node, |
| 484 | MessageSendInfo &MSI) = 0; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 485 | |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 486 | /// Looks up the method for sending a message to a superclass. This |
| 487 | /// mechanism differs between the GCC and GNU runtimes, so this method must |
| 488 | /// be overridden in subclasses. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 489 | virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 490 | Address ObjCSuper, |
Eli Friedman | f24bd3b | 2013-07-26 00:53:29 +0000 | [diff] [blame] | 491 | llvm::Value *cmd, |
| 492 | MessageSendInfo &MSI) = 0; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 493 | |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 494 | /// Libobjc2 uses a bitfield representation where small(ish) bitfields are |
| 495 | /// stored in a 64-bit value with the low bit set to 1 and the remaining 63 |
| 496 | /// bits set to their values, LSB first, while larger ones are stored in a |
| 497 | /// structure of this / form: |
| 498 | /// |
| 499 | /// struct { int32_t length; int32_t values[length]; }; |
| 500 | /// |
| 501 | /// The values in the array are stored in host-endian format, with the least |
| 502 | /// significant bit being assumed to come first in the bitfield. Therefore, |
| 503 | /// a bitfield with the 64th bit set will be (int64_t)&{ 2, [0, 1<<31] }, |
| 504 | /// while a bitfield / with the 63rd bit set will be 1<<64. |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 505 | llvm::Constant *MakeBitField(ArrayRef<bool> bits); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 506 | |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 507 | public: |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 508 | CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion, |
| 509 | unsigned protocolClassVersion); |
| 510 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 511 | ConstantAddress GenerateConstantString(const StringLiteral *) override; |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 512 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 513 | RValue |
| 514 | GenerateMessageSend(CodeGenFunction &CGF, ReturnValueSlot Return, |
| 515 | QualType ResultType, Selector Sel, |
| 516 | llvm::Value *Receiver, const CallArgList &CallArgs, |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 517 | const ObjCInterfaceDecl *Class, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 518 | const ObjCMethodDecl *Method) override; |
| 519 | RValue |
| 520 | GenerateMessageSendSuper(CodeGenFunction &CGF, ReturnValueSlot Return, |
| 521 | QualType ResultType, Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 522 | const ObjCInterfaceDecl *Class, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 523 | bool isCategoryImpl, llvm::Value *Receiver, |
| 524 | bool IsClassMessage, const CallArgList &CallArgs, |
| 525 | const ObjCMethodDecl *Method) override; |
| 526 | llvm::Value *GetClass(CodeGenFunction &CGF, |
| 527 | const ObjCInterfaceDecl *OID) override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 528 | llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel) override; |
| 529 | Address GetAddrOfSelector(CodeGenFunction &CGF, Selector Sel) override; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 530 | llvm::Value *GetSelector(CodeGenFunction &CGF, |
| 531 | const ObjCMethodDecl *Method) override; |
| 532 | llvm::Constant *GetEHType(QualType T) override; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 533 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 534 | llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 535 | const ObjCContainerDecl *CD) override; |
| 536 | void GenerateCategory(const ObjCCategoryImplDecl *CMD) override; |
| 537 | void GenerateClass(const ObjCImplementationDecl *ClassDecl) override; |
| 538 | void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) override; |
| 539 | llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF, |
| 540 | const ObjCProtocolDecl *PD) override; |
| 541 | void GenerateProtocol(const ObjCProtocolDecl *PD) override; |
| 542 | llvm::Function *ModuleInitFunction() override; |
| 543 | llvm::Constant *GetPropertyGetFunction() override; |
| 544 | llvm::Constant *GetPropertySetFunction() override; |
| 545 | llvm::Constant *GetOptimizedPropertySetFunction(bool atomic, |
| 546 | bool copy) override; |
| 547 | llvm::Constant *GetSetStructFunction() override; |
| 548 | llvm::Constant *GetGetStructFunction() override; |
| 549 | llvm::Constant *GetCppAtomicObjectGetFunction() override; |
| 550 | llvm::Constant *GetCppAtomicObjectSetFunction() override; |
| 551 | llvm::Constant *EnumerationMutationFunction() override; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 552 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 553 | void EmitTryStmt(CodeGenFunction &CGF, |
| 554 | const ObjCAtTryStmt &S) override; |
| 555 | void EmitSynchronizedStmt(CodeGenFunction &CGF, |
| 556 | const ObjCAtSynchronizedStmt &S) override; |
| 557 | void EmitThrowStmt(CodeGenFunction &CGF, |
| 558 | const ObjCAtThrowStmt &S, |
| 559 | bool ClearInsertionPoint=true) override; |
| 560 | llvm::Value * EmitObjCWeakRead(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 561 | Address AddrWeakObj) override; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 562 | void EmitObjCWeakAssign(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 563 | llvm::Value *src, Address dst) override; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 564 | void EmitObjCGlobalAssign(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 565 | llvm::Value *src, Address dest, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 566 | bool threadlocal=false) override; |
| 567 | void EmitObjCIvarAssign(CodeGenFunction &CGF, llvm::Value *src, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 568 | Address dest, llvm::Value *ivarOffset) override; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 569 | void EmitObjCStrongCastAssign(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 570 | llvm::Value *src, Address dest) override; |
| 571 | void EmitGCMemmoveCollectable(CodeGenFunction &CGF, Address DestPtr, |
| 572 | Address SrcPtr, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 573 | llvm::Value *Size) override; |
| 574 | LValue EmitObjCValueForIvar(CodeGenFunction &CGF, QualType ObjectTy, |
| 575 | llvm::Value *BaseValue, const ObjCIvarDecl *Ivar, |
| 576 | unsigned CVRQualifiers) override; |
| 577 | llvm::Value *EmitIvarOffset(CodeGenFunction &CGF, |
| 578 | const ObjCInterfaceDecl *Interface, |
| 579 | const ObjCIvarDecl *Ivar) override; |
| 580 | llvm::Value *EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) override; |
| 581 | llvm::Constant *BuildGCBlockLayout(CodeGenModule &CGM, |
| 582 | const CGBlockInfo &blockInfo) override { |
Fariborz Jahanian | c05349e | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 583 | return NULLPtr; |
| 584 | } |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 585 | llvm::Constant *BuildRCBlockLayout(CodeGenModule &CGM, |
| 586 | const CGBlockInfo &blockInfo) override { |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 587 | return NULLPtr; |
| 588 | } |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 589 | |
| 590 | llvm::Constant *BuildByrefLayout(CodeGenModule &CGM, QualType T) override { |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 591 | return NULLPtr; |
| 592 | } |
Rafael Espindola | 554256c | 2014-02-26 22:25:45 +0000 | [diff] [blame] | 593 | |
Benjamin Kramer | 0772c42 | 2016-02-13 13:42:54 +0000 | [diff] [blame] | 594 | llvm::GlobalVariable *GetClassGlobal(StringRef Name, |
Craig Topper | a798a9d | 2014-03-02 09:32:10 +0000 | [diff] [blame] | 595 | bool Weak = false) override { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 596 | return nullptr; |
Fariborz Jahanian | 7bd3d1c | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 597 | } |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 598 | }; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 599 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 600 | /// Class representing the legacy GCC Objective-C ABI. This is the default when |
| 601 | /// -fobjc-nonfragile-abi is not specified. |
| 602 | /// |
| 603 | /// The GCC ABI target actually generates code that is approximately compatible |
| 604 | /// with the new GNUstep runtime ABI, but refrains from using any features that |
| 605 | /// would not work with the GCC runtime. For example, clang always generates |
| 606 | /// the extended form of the class structure, and the extra fields are simply |
| 607 | /// ignored by GCC libobjc. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 608 | class CGObjCGCC : public CGObjCGNU { |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 609 | /// The GCC ABI message lookup function. Returns an IMP pointing to the |
| 610 | /// method implementation for this message. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 611 | LazyRuntimeFunction MsgLookupFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 612 | /// The GCC ABI superclass message lookup function. Takes a pointer to a |
| 613 | /// structure describing the receiver and the class, and a selector as |
| 614 | /// arguments. Returns the IMP for the corresponding method. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 615 | LazyRuntimeFunction MsgLookupSuperFn; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 616 | |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 617 | protected: |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 618 | llvm::Value *LookupIMP(CodeGenFunction &CGF, llvm::Value *&Receiver, |
| 619 | llvm::Value *cmd, llvm::MDNode *node, |
| 620 | MessageSendInfo &MSI) override { |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 621 | CGBuilderTy &Builder = CGF.Builder; |
David Chisnall | 0cc83e7 | 2011-10-28 17:55:06 +0000 | [diff] [blame] | 622 | llvm::Value *args[] = { |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 623 | EnforceType(Builder, Receiver, IdTy), |
David Chisnall | 0cc83e7 | 2011-10-28 17:55:06 +0000 | [diff] [blame] | 624 | EnforceType(Builder, cmd, SelectorTy) }; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 625 | llvm::CallSite imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFn, args); |
David Chisnall | 0cc83e7 | 2011-10-28 17:55:06 +0000 | [diff] [blame] | 626 | imp->setMetadata(msgSendMDKind, node); |
| 627 | return imp.getInstruction(); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 628 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 629 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 630 | llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, Address ObjCSuper, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 631 | llvm::Value *cmd, MessageSendInfo &MSI) override { |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 632 | CGBuilderTy &Builder = CGF.Builder; |
| 633 | llvm::Value *lookupArgs[] = {EnforceType(Builder, ObjCSuper, |
| 634 | PtrToObjCSuperTy).getPointer(), cmd}; |
| 635 | return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFn, lookupArgs); |
| 636 | } |
| 637 | |
| 638 | public: |
| 639 | CGObjCGCC(CodeGenModule &Mod) : CGObjCGNU(Mod, 8, 2) { |
| 640 | // IMP objc_msg_lookup(id, SEL); |
| 641 | MsgLookupFn.init(&CGM, "objc_msg_lookup", IMPTy, IdTy, SelectorTy, |
| 642 | nullptr); |
| 643 | // IMP objc_msg_lookup_super(struct objc_super*, SEL); |
| 644 | MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy, |
| 645 | PtrToObjCSuperTy, SelectorTy, nullptr); |
| 646 | } |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 647 | }; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 648 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 649 | /// Class used when targeting the new GNUstep runtime ABI. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 650 | class CGObjCGNUstep : public CGObjCGNU { |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 651 | /// The slot lookup function. Returns a pointer to a cacheable structure |
| 652 | /// that contains (among other things) the IMP. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 653 | LazyRuntimeFunction SlotLookupFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 654 | /// The GNUstep ABI superclass message lookup function. Takes a pointer to |
| 655 | /// a structure describing the receiver and the class, and a selector as |
| 656 | /// arguments. Returns the slot for the corresponding method. Superclass |
| 657 | /// message lookup rarely changes, so this is a good caching opportunity. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 658 | LazyRuntimeFunction SlotLookupSuperFn; |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 659 | /// Specialised function for setting atomic retain properties |
| 660 | LazyRuntimeFunction SetPropertyAtomic; |
| 661 | /// Specialised function for setting atomic copy properties |
| 662 | LazyRuntimeFunction SetPropertyAtomicCopy; |
| 663 | /// Specialised function for setting nonatomic retain properties |
| 664 | LazyRuntimeFunction SetPropertyNonAtomic; |
| 665 | /// Specialised function for setting nonatomic copy properties |
| 666 | LazyRuntimeFunction SetPropertyNonAtomicCopy; |
| 667 | /// Function to perform atomic copies of C++ objects with nontrivial copy |
| 668 | /// constructors from Objective-C ivars. |
| 669 | LazyRuntimeFunction CxxAtomicObjectGetFn; |
| 670 | /// Function to perform atomic copies of C++ objects with nontrivial copy |
| 671 | /// constructors to Objective-C ivars. |
| 672 | LazyRuntimeFunction CxxAtomicObjectSetFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 673 | /// Type of an slot structure pointer. This is returned by the various |
| 674 | /// lookup functions. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 675 | llvm::Type *SlotTy; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 676 | |
John McCall | c31d893 | 2012-11-14 09:08:34 +0000 | [diff] [blame] | 677 | public: |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 678 | llvm::Constant *GetEHType(QualType T) override; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 679 | |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 680 | protected: |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 681 | llvm::Value *LookupIMP(CodeGenFunction &CGF, llvm::Value *&Receiver, |
| 682 | llvm::Value *cmd, llvm::MDNode *node, |
| 683 | MessageSendInfo &MSI) override { |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 684 | CGBuilderTy &Builder = CGF.Builder; |
| 685 | llvm::Function *LookupFn = SlotLookupFn; |
| 686 | |
| 687 | // Store the receiver on the stack so that we can reload it later |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 688 | Address ReceiverPtr = |
| 689 | CGF.CreateTempAlloca(Receiver->getType(), CGF.getPointerAlign()); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 690 | Builder.CreateStore(Receiver, ReceiverPtr); |
| 691 | |
| 692 | llvm::Value *self; |
| 693 | |
| 694 | if (isa<ObjCMethodDecl>(CGF.CurCodeDecl)) { |
| 695 | self = CGF.LoadObjCSelf(); |
| 696 | } else { |
| 697 | self = llvm::ConstantPointerNull::get(IdTy); |
| 698 | } |
| 699 | |
| 700 | // The lookup function is guaranteed not to capture the receiver pointer. |
| 701 | LookupFn->setDoesNotCapture(1); |
| 702 | |
David Chisnall | 0cc83e7 | 2011-10-28 17:55:06 +0000 | [diff] [blame] | 703 | llvm::Value *args[] = { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 704 | EnforceType(Builder, ReceiverPtr.getPointer(), PtrToIdTy), |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 705 | EnforceType(Builder, cmd, SelectorTy), |
David Chisnall | 0cc83e7 | 2011-10-28 17:55:06 +0000 | [diff] [blame] | 706 | EnforceType(Builder, self, IdTy) }; |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 707 | llvm::CallSite slot = CGF.EmitRuntimeCallOrInvoke(LookupFn, args); |
David Chisnall | 0cc83e7 | 2011-10-28 17:55:06 +0000 | [diff] [blame] | 708 | slot.setOnlyReadsMemory(); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 709 | slot->setMetadata(msgSendMDKind, node); |
| 710 | |
| 711 | // Load the imp from the slot |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 712 | llvm::Value *imp = Builder.CreateAlignedLoad( |
| 713 | Builder.CreateStructGEP(nullptr, slot.getInstruction(), 4), |
| 714 | CGF.getPointerAlign()); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 715 | |
| 716 | // The lookup function may have changed the receiver, so make sure we use |
| 717 | // the new one. |
| 718 | Receiver = Builder.CreateLoad(ReceiverPtr, true); |
| 719 | return imp; |
| 720 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 721 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 722 | llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, Address ObjCSuper, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 723 | llvm::Value *cmd, |
| 724 | MessageSendInfo &MSI) override { |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 725 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 726 | llvm::Value *lookupArgs[] = {ObjCSuper.getPointer(), cmd}; |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 727 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 728 | llvm::CallInst *slot = |
| 729 | CGF.EmitNounwindRuntimeCall(SlotLookupSuperFn, lookupArgs); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 730 | slot->setOnlyReadsMemory(); |
| 731 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 732 | return Builder.CreateAlignedLoad(Builder.CreateStructGEP(nullptr, slot, 4), |
| 733 | CGF.getPointerAlign()); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 734 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 735 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 736 | public: |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 737 | CGObjCGNUstep(CodeGenModule &Mod) : CGObjCGNU(Mod, 9, 3) { |
David Chisnall | beb8013 | 2013-02-28 13:59:29 +0000 | [diff] [blame] | 738 | const ObjCRuntime &R = CGM.getLangOpts().ObjCRuntime; |
David Chisnall | 2ec1b10d | 2013-01-11 15:33:01 +0000 | [diff] [blame] | 739 | |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 740 | llvm::StructType *SlotStructTy = llvm::StructType::get(PtrTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 741 | PtrTy, PtrTy, IntTy, IMPTy, nullptr); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 742 | SlotTy = llvm::PointerType::getUnqual(SlotStructTy); |
| 743 | // Slot_t objc_msg_lookup_sender(id *receiver, SEL selector, id sender); |
| 744 | SlotLookupFn.init(&CGM, "objc_msg_lookup_sender", SlotTy, PtrToIdTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 745 | SelectorTy, IdTy, nullptr); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 746 | // Slot_t objc_msg_lookup_super(struct objc_super*, SEL); |
| 747 | SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 748 | PtrToObjCSuperTy, SelectorTy, nullptr); |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 749 | // If we're in ObjC++ mode, then we want to make |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 750 | if (CGM.getLangOpts().CPlusPlus) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 751 | llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext); |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 752 | // void *__cxa_begin_catch(void *e) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 753 | EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy, nullptr); |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 754 | // void __cxa_end_catch(void) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 755 | ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy, nullptr); |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 756 | // void _Unwind_Resume_or_Rethrow(void*) |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 757 | ExceptionReThrowFn.init(&CGM, "_Unwind_Resume_or_Rethrow", VoidTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 758 | PtrTy, nullptr); |
David Chisnall | 2ec1b10d | 2013-01-11 15:33:01 +0000 | [diff] [blame] | 759 | } else if (R.getVersion() >= VersionTuple(1, 7)) { |
| 760 | llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext); |
| 761 | // id objc_begin_catch(void *e) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 762 | EnterCatchFn.init(&CGM, "objc_begin_catch", IdTy, PtrTy, nullptr); |
David Chisnall | 2ec1b10d | 2013-01-11 15:33:01 +0000 | [diff] [blame] | 763 | // void objc_end_catch(void) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 764 | ExitCatchFn.init(&CGM, "objc_end_catch", VoidTy, nullptr); |
David Chisnall | 2ec1b10d | 2013-01-11 15:33:01 +0000 | [diff] [blame] | 765 | // void _Unwind_Resume_or_Rethrow(void*) |
| 766 | ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 767 | PtrTy, nullptr); |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 768 | } |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 769 | llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext); |
| 770 | SetPropertyAtomic.init(&CGM, "objc_setProperty_atomic", VoidTy, IdTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 771 | SelectorTy, IdTy, PtrDiffTy, nullptr); |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 772 | SetPropertyAtomicCopy.init(&CGM, "objc_setProperty_atomic_copy", VoidTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 773 | IdTy, SelectorTy, IdTy, PtrDiffTy, nullptr); |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 774 | SetPropertyNonAtomic.init(&CGM, "objc_setProperty_nonatomic", VoidTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 775 | IdTy, SelectorTy, IdTy, PtrDiffTy, nullptr); |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 776 | SetPropertyNonAtomicCopy.init(&CGM, "objc_setProperty_nonatomic_copy", |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 777 | VoidTy, IdTy, SelectorTy, IdTy, PtrDiffTy, nullptr); |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 778 | // void objc_setCppObjectAtomic(void *dest, const void *src, void |
| 779 | // *helper); |
| 780 | CxxAtomicObjectSetFn.init(&CGM, "objc_setCppObjectAtomic", VoidTy, PtrTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 781 | PtrTy, PtrTy, nullptr); |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 782 | // void objc_getCppObjectAtomic(void *dest, const void *src, void |
| 783 | // *helper); |
| 784 | CxxAtomicObjectGetFn.init(&CGM, "objc_getCppObjectAtomic", VoidTy, PtrTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 785 | PtrTy, PtrTy, nullptr); |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 786 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 787 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 788 | llvm::Constant *GetCppAtomicObjectGetFunction() override { |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 789 | // The optimised functions were added in version 1.7 of the GNUstep |
| 790 | // runtime. |
| 791 | assert (CGM.getLangOpts().ObjCRuntime.getVersion() >= |
| 792 | VersionTuple(1, 7)); |
| 793 | return CxxAtomicObjectGetFn; |
| 794 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 795 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 796 | llvm::Constant *GetCppAtomicObjectSetFunction() override { |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 797 | // The optimised functions were added in version 1.7 of the GNUstep |
| 798 | // runtime. |
| 799 | assert (CGM.getLangOpts().ObjCRuntime.getVersion() >= |
| 800 | VersionTuple(1, 7)); |
| 801 | return CxxAtomicObjectSetFn; |
| 802 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 803 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 804 | llvm::Constant *GetOptimizedPropertySetFunction(bool atomic, |
| 805 | bool copy) override { |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 806 | // The optimised property functions omit the GC check, and so are not |
| 807 | // safe to use in GC mode. The standard functions are fast in GC mode, |
| 808 | // so there is less advantage in using them. |
| 809 | assert ((CGM.getLangOpts().getGC() == LangOptions::NonGC)); |
| 810 | // The optimised functions were added in version 1.7 of the GNUstep |
| 811 | // runtime. |
| 812 | assert (CGM.getLangOpts().ObjCRuntime.getVersion() >= |
| 813 | VersionTuple(1, 7)); |
| 814 | |
| 815 | if (atomic) { |
| 816 | if (copy) return SetPropertyAtomicCopy; |
| 817 | return SetPropertyAtomic; |
| 818 | } |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 819 | |
Ted Kremenek | 090a273 | 2014-03-07 18:53:05 +0000 | [diff] [blame] | 820 | return copy ? SetPropertyNonAtomicCopy : SetPropertyNonAtomic; |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 821 | } |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 822 | }; |
| 823 | |
Alp Toker | 272e9bc | 2013-11-25 00:40:53 +0000 | [diff] [blame] | 824 | /// Support for the ObjFW runtime. |
John McCall | 3deb1ad | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 825 | class CGObjCObjFW: public CGObjCGNU { |
| 826 | protected: |
| 827 | /// The GCC ABI message lookup function. Returns an IMP pointing to the |
| 828 | /// method implementation for this message. |
| 829 | LazyRuntimeFunction MsgLookupFn; |
Eli Friedman | f24bd3b | 2013-07-26 00:53:29 +0000 | [diff] [blame] | 830 | /// stret lookup function. While this does not seem to make sense at the |
| 831 | /// first look, this is required to call the correct forwarding function. |
| 832 | LazyRuntimeFunction MsgLookupFnSRet; |
John McCall | 3deb1ad | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 833 | /// The GCC ABI superclass message lookup function. Takes a pointer to a |
| 834 | /// structure describing the receiver and the class, and a selector as |
| 835 | /// arguments. Returns the IMP for the corresponding method. |
Eli Friedman | f24bd3b | 2013-07-26 00:53:29 +0000 | [diff] [blame] | 836 | LazyRuntimeFunction MsgLookupSuperFn, MsgLookupSuperFnSRet; |
John McCall | 3deb1ad | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 837 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 838 | llvm::Value *LookupIMP(CodeGenFunction &CGF, llvm::Value *&Receiver, |
| 839 | llvm::Value *cmd, llvm::MDNode *node, |
| 840 | MessageSendInfo &MSI) override { |
John McCall | 3deb1ad | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 841 | CGBuilderTy &Builder = CGF.Builder; |
| 842 | llvm::Value *args[] = { |
| 843 | EnforceType(Builder, Receiver, IdTy), |
| 844 | EnforceType(Builder, cmd, SelectorTy) }; |
Eli Friedman | f24bd3b | 2013-07-26 00:53:29 +0000 | [diff] [blame] | 845 | |
| 846 | llvm::CallSite imp; |
| 847 | if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) |
| 848 | imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFnSRet, args); |
| 849 | else |
| 850 | imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFn, args); |
| 851 | |
John McCall | 3deb1ad | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 852 | imp->setMetadata(msgSendMDKind, node); |
| 853 | return imp.getInstruction(); |
| 854 | } |
| 855 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 856 | llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, Address ObjCSuper, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 857 | llvm::Value *cmd, MessageSendInfo &MSI) override { |
Saleem Abdulrasool | a088ad9 | 2016-07-17 22:27:41 +0000 | [diff] [blame] | 858 | CGBuilderTy &Builder = CGF.Builder; |
| 859 | llvm::Value *lookupArgs[] = { |
| 860 | EnforceType(Builder, ObjCSuper.getPointer(), PtrToObjCSuperTy), cmd, |
| 861 | }; |
Eli Friedman | f24bd3b | 2013-07-26 00:53:29 +0000 | [diff] [blame] | 862 | |
Saleem Abdulrasool | a088ad9 | 2016-07-17 22:27:41 +0000 | [diff] [blame] | 863 | if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) |
| 864 | return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFnSRet, lookupArgs); |
| 865 | else |
| 866 | return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFn, lookupArgs); |
| 867 | } |
John McCall | 3deb1ad | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 868 | |
Saleem Abdulrasool | a088ad9 | 2016-07-17 22:27:41 +0000 | [diff] [blame] | 869 | llvm::Value *GetClassNamed(CodeGenFunction &CGF, const std::string &Name, |
| 870 | bool isWeak) override { |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 871 | if (isWeak) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 872 | return CGObjCGNU::GetClassNamed(CGF, Name, isWeak); |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 873 | |
| 874 | EmitClassRef(Name); |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 875 | std::string SymbolName = "_OBJC_CLASS_" + Name; |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 876 | llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(SymbolName); |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 877 | if (!ClassSymbol) |
| 878 | ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false, |
| 879 | llvm::GlobalValue::ExternalLinkage, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 880 | nullptr, SymbolName); |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 881 | return ClassSymbol; |
| 882 | } |
| 883 | |
| 884 | public: |
John McCall | 3deb1ad | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 885 | CGObjCObjFW(CodeGenModule &Mod): CGObjCGNU(Mod, 9, 3) { |
| 886 | // IMP objc_msg_lookup(id, SEL); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 887 | MsgLookupFn.init(&CGM, "objc_msg_lookup", IMPTy, IdTy, SelectorTy, nullptr); |
Eli Friedman | f24bd3b | 2013-07-26 00:53:29 +0000 | [diff] [blame] | 888 | MsgLookupFnSRet.init(&CGM, "objc_msg_lookup_stret", IMPTy, IdTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 889 | SelectorTy, nullptr); |
John McCall | 3deb1ad | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 890 | // IMP objc_msg_lookup_super(struct objc_super*, SEL); |
| 891 | MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 892 | PtrToObjCSuperTy, SelectorTy, nullptr); |
Eli Friedman | f24bd3b | 2013-07-26 00:53:29 +0000 | [diff] [blame] | 893 | MsgLookupSuperFnSRet.init(&CGM, "objc_msg_lookup_super_stret", IMPTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 894 | PtrToObjCSuperTy, SelectorTy, nullptr); |
John McCall | 3deb1ad | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 895 | } |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 896 | }; |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 897 | } // end anonymous namespace |
| 898 | |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 899 | /// Emits a reference to a dummy variable which is emitted with each class. |
| 900 | /// This ensures that a linker error will be generated when trying to link |
| 901 | /// together modules where a referenced class is not defined. |
Mike Stump | dd93a19 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 902 | void CGObjCGNU::EmitClassRef(const std::string &className) { |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 903 | std::string symbolRef = "__objc_class_ref_" + className; |
| 904 | // Don't emit two copies of the same symbol |
Mike Stump | dd93a19 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 905 | if (TheModule.getGlobalVariable(symbolRef)) |
| 906 | return; |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 907 | std::string symbolName = "__objc_class_name_" + className; |
| 908 | llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(symbolName); |
| 909 | if (!ClassSymbol) { |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 910 | ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 911 | llvm::GlobalValue::ExternalLinkage, |
| 912 | nullptr, symbolName); |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 913 | } |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 914 | new llvm::GlobalVariable(TheModule, ClassSymbol->getType(), true, |
Chris Lattner | c58e569 | 2009-08-05 05:25:18 +0000 | [diff] [blame] | 915 | llvm::GlobalValue::WeakAnyLinkage, ClassSymbol, symbolRef); |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 916 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 917 | |
Craig Topper | bf3e327 | 2014-08-30 16:55:52 +0000 | [diff] [blame] | 918 | static std::string SymbolNameForMethod( StringRef ClassName, |
| 919 | StringRef CategoryName, const Selector MethodName, |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 920 | bool isClassMethod) { |
| 921 | std::string MethodNameColonStripped = MethodName.getAsString(); |
David Chisnall | 035ead2 | 2010-01-14 14:08:19 +0000 | [diff] [blame] | 922 | std::replace(MethodNameColonStripped.begin(), MethodNameColonStripped.end(), |
| 923 | ':', '_'); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 924 | return (Twine(isClassMethod ? "_c_" : "_i_") + ClassName + "_" + |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 925 | CategoryName + "_" + MethodNameColonStripped).str(); |
David Chisnall | 0a24fd3 | 2010-05-08 20:58:05 +0000 | [diff] [blame] | 926 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 927 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 928 | CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 929 | unsigned protocolClassVersion) |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 930 | : CGObjCRuntime(cgm), TheModule(CGM.getModule()), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 931 | VMContext(cgm.getLLVMContext()), ClassPtrAlias(nullptr), |
| 932 | MetaClassPtrAlias(nullptr), RuntimeVersion(runtimeABIVersion), |
| 933 | ProtocolVersion(protocolClassVersion) { |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 934 | |
| 935 | msgSendMDKind = VMContext.getMDKindID("GNUObjCMessageSend"); |
| 936 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 937 | CodeGenTypes &Types = CGM.getTypes(); |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 938 | IntTy = cast<llvm::IntegerType>( |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 939 | Types.ConvertType(CGM.getContext().IntTy)); |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 940 | LongTy = cast<llvm::IntegerType>( |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 941 | Types.ConvertType(CGM.getContext().LongTy)); |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 942 | SizeTy = cast<llvm::IntegerType>( |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 943 | Types.ConvertType(CGM.getContext().getSizeType())); |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 944 | PtrDiffTy = cast<llvm::IntegerType>( |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 945 | Types.ConvertType(CGM.getContext().getPointerDiffType())); |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 946 | BoolTy = CGM.getTypes().ConvertType(CGM.getContext().BoolTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 947 | |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 948 | Int8Ty = llvm::Type::getInt8Ty(VMContext); |
| 949 | // C string type. Used in lots of places. |
| 950 | PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty); |
| 951 | |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 952 | Zeros[0] = llvm::ConstantInt::get(LongTy, 0); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 953 | Zeros[1] = Zeros[0]; |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 954 | NULLPtr = llvm::ConstantPointerNull::get(PtrToInt8Ty); |
Chris Lattner | 4bd5596 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 955 | // Get the selector Type. |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 956 | QualType selTy = CGM.getContext().getObjCSelType(); |
| 957 | if (QualType() == selTy) { |
| 958 | SelectorTy = PtrToInt8Ty; |
| 959 | } else { |
| 960 | SelectorTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(selTy)); |
| 961 | } |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 962 | |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 963 | PtrToIntTy = llvm::PointerType::getUnqual(IntTy); |
Chris Lattner | 4bd5596 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 964 | PtrTy = PtrToInt8Ty; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 965 | |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 966 | Int32Ty = llvm::Type::getInt32Ty(VMContext); |
| 967 | Int64Ty = llvm::Type::getInt64Ty(VMContext); |
| 968 | |
Rafael Espindola | 3cc5c2d | 2014-01-09 21:32:51 +0000 | [diff] [blame] | 969 | IntPtrTy = |
| 970 | CGM.getDataLayout().getPointerSizeInBits() == 32 ? Int32Ty : Int64Ty; |
David Chisnall | e0dc7cb | 2011-10-08 08:54:36 +0000 | [diff] [blame] | 971 | |
Chris Lattner | 4bd5596 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 972 | // Object type |
David Chisnall | 10d2ded | 2011-04-29 14:10:35 +0000 | [diff] [blame] | 973 | QualType UnqualIdTy = CGM.getContext().getObjCIdType(); |
| 974 | ASTIdTy = CanQualType(); |
| 975 | if (UnqualIdTy != QualType()) { |
| 976 | ASTIdTy = CGM.getContext().getCanonicalType(UnqualIdTy); |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 977 | IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy)); |
David Chisnall | 10d2ded | 2011-04-29 14:10:35 +0000 | [diff] [blame] | 978 | } else { |
| 979 | IdTy = PtrToInt8Ty; |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 980 | } |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 981 | PtrToIdTy = llvm::PointerType::getUnqual(IdTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 982 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 983 | ObjCSuperTy = llvm::StructType::get(IdTy, IdTy, nullptr); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 984 | PtrToObjCSuperTy = llvm::PointerType::getUnqual(ObjCSuperTy); |
| 985 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 986 | llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 987 | |
| 988 | // void objc_exception_throw(id); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 989 | ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy, nullptr); |
| 990 | ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy, nullptr); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 991 | // int objc_sync_enter(id); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 992 | SyncEnterFn.init(&CGM, "objc_sync_enter", IntTy, IdTy, nullptr); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 993 | // int objc_sync_exit(id); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 994 | SyncExitFn.init(&CGM, "objc_sync_exit", IntTy, IdTy, nullptr); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 995 | |
| 996 | // void objc_enumerationMutation (id) |
| 997 | EnumerationMutationFn.init(&CGM, "objc_enumerationMutation", VoidTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 998 | IdTy, nullptr); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 999 | |
| 1000 | // id objc_getProperty(id, SEL, ptrdiff_t, BOOL) |
| 1001 | GetPropertyFn.init(&CGM, "objc_getProperty", IdTy, IdTy, SelectorTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1002 | PtrDiffTy, BoolTy, nullptr); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1003 | // void objc_setProperty(id, SEL, ptrdiff_t, id, BOOL, BOOL) |
| 1004 | SetPropertyFn.init(&CGM, "objc_setProperty", VoidTy, IdTy, SelectorTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1005 | PtrDiffTy, IdTy, BoolTy, BoolTy, nullptr); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1006 | // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL) |
| 1007 | GetStructPropertyFn.init(&CGM, "objc_getPropertyStruct", VoidTy, PtrTy, PtrTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1008 | PtrDiffTy, BoolTy, BoolTy, nullptr); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1009 | // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL) |
| 1010 | SetStructPropertyFn.init(&CGM, "objc_setPropertyStruct", VoidTy, PtrTy, PtrTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1011 | PtrDiffTy, BoolTy, BoolTy, nullptr); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1012 | |
Chris Lattner | 4bd5596 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 1013 | // IMP type |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1014 | llvm::Type *IMPArgs[] = { IdTy, SelectorTy }; |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1015 | IMPTy = llvm::PointerType::getUnqual(llvm::FunctionType::get(IdTy, IMPArgs, |
| 1016 | true)); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 1017 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1018 | const LangOptions &Opts = CGM.getLangOpts(); |
Douglas Gregor | 79a9141 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 1019 | if ((Opts.getGC() != LangOptions::NonGC) || Opts.ObjCAutoRefCount) |
David Chisnall | a918b88 | 2011-07-07 11:22:31 +0000 | [diff] [blame] | 1020 | RuntimeVersion = 10; |
| 1021 | |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 1022 | // Don't bother initialising the GC stuff unless we're compiling in GC mode |
Douglas Gregor | 79a9141 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 1023 | if (Opts.getGC() != LangOptions::NonGC) { |
David Chisnall | 5c51177 | 2011-05-22 22:37:08 +0000 | [diff] [blame] | 1024 | // This is a bit of an hack. We should sort this out by having a proper |
| 1025 | // CGObjCGNUstep subclass for GC, but we may want to really support the old |
| 1026 | // ABI and GC added in ObjectiveC2.framework, so we fudge it a bit for now |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 1027 | // Get selectors needed in GC mode |
| 1028 | RetainSel = GetNullarySelector("retain", CGM.getContext()); |
| 1029 | ReleaseSel = GetNullarySelector("release", CGM.getContext()); |
| 1030 | AutoreleaseSel = GetNullarySelector("autorelease", CGM.getContext()); |
| 1031 | |
| 1032 | // Get functions needed in GC mode |
| 1033 | |
| 1034 | // id objc_assign_ivar(id, id, ptrdiff_t); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1035 | IvarAssignFn.init(&CGM, "objc_assign_ivar", IdTy, IdTy, IdTy, PtrDiffTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1036 | nullptr); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 1037 | // id objc_assign_strongCast (id, id*) |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1038 | StrongCastAssignFn.init(&CGM, "objc_assign_strongCast", IdTy, IdTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1039 | PtrToIdTy, nullptr); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 1040 | // id objc_assign_global(id, id*); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1041 | GlobalAssignFn.init(&CGM, "objc_assign_global", IdTy, IdTy, PtrToIdTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1042 | nullptr); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 1043 | // id objc_assign_weak(id, id*); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1044 | WeakAssignFn.init(&CGM, "objc_assign_weak", IdTy, IdTy, PtrToIdTy, nullptr); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 1045 | // id objc_read_weak(id*); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1046 | WeakReadFn.init(&CGM, "objc_read_weak", IdTy, PtrToIdTy, nullptr); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 1047 | // void *objc_memmove_collectable(void*, void *, size_t); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1048 | MemMoveFn.init(&CGM, "objc_memmove_collectable", PtrTy, PtrTy, PtrTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1049 | SizeTy, nullptr); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 1050 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1051 | } |
Mike Stump | dd93a19 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 1052 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1053 | llvm::Value *CGObjCGNU::GetClassNamed(CodeGenFunction &CGF, |
Saleem Abdulrasool | a088ad9 | 2016-07-17 22:27:41 +0000 | [diff] [blame] | 1054 | const std::string &Name, bool isWeak) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1055 | llvm::Constant *ClassName = MakeConstantString(Name); |
David Chisnall | df34917 | 2010-01-08 00:14:31 +0000 | [diff] [blame] | 1056 | // With the incompatible ABI, this will need to be replaced with a direct |
| 1057 | // reference to the class symbol. For the compatible nonfragile ABI we are |
| 1058 | // still performing this lookup at run time but emitting the symbol for the |
| 1059 | // class externally so that we can make the switch later. |
David Chisnall | 920e83b | 2011-06-29 13:16:41 +0000 | [diff] [blame] | 1060 | // |
| 1061 | // Libobjc2 contains an LLVM pass that replaces calls to objc_lookup_class |
| 1062 | // with memoized versions or with static references if it's safe to do so. |
David Chisnall | 08d6733 | 2011-06-30 10:14:37 +0000 | [diff] [blame] | 1063 | if (!isWeak) |
| 1064 | EmitClassRef(Name); |
Daniel Dunbar | 7c6d3a7 | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1065 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1066 | llvm::Constant *ClassLookupFn = |
Jay Foad | 5709f7c | 2011-07-29 13:56:53 +0000 | [diff] [blame] | 1067 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, PtrToInt8Ty, true), |
Fariborz Jahanian | 3b636c1 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 1068 | "objc_lookup_class"); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1069 | return CGF.EmitNounwindRuntimeCall(ClassLookupFn, ClassName); |
Chris Lattner | 4bd5596 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
David Chisnall | 920e83b | 2011-06-29 13:16:41 +0000 | [diff] [blame] | 1072 | // This has to perform the lookup every time, since posing and related |
| 1073 | // techniques can modify the name -> class mapping. |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1074 | llvm::Value *CGObjCGNU::GetClass(CodeGenFunction &CGF, |
David Chisnall | 920e83b | 2011-06-29 13:16:41 +0000 | [diff] [blame] | 1075 | const ObjCInterfaceDecl *OID) { |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 1076 | auto *Value = |
| 1077 | GetClassNamed(CGF, OID->getNameAsString(), OID->isWeakImported()); |
| 1078 | if (CGM.getTriple().isOSBinFormatCOFF()) { |
| 1079 | if (auto *ClassSymbol = dyn_cast<llvm::GlobalVariable>(Value)) { |
| 1080 | auto DLLStorage = llvm::GlobalValue::DefaultStorageClass; |
| 1081 | if (OID->hasAttr<DLLExportAttr>()) |
| 1082 | DLLStorage = llvm::GlobalValue::DLLExportStorageClass; |
| 1083 | else if (OID->hasAttr<DLLImportAttr>()) |
| 1084 | DLLStorage = llvm::GlobalValue::DLLImportStorageClass; |
| 1085 | ClassSymbol->setDLLStorageClass(DLLStorage); |
| 1086 | } |
| 1087 | } |
| 1088 | return Value; |
David Chisnall | 920e83b | 2011-06-29 13:16:41 +0000 | [diff] [blame] | 1089 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 1090 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1091 | llvm::Value *CGObjCGNU::EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) { |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 1092 | auto *Value = GetClassNamed(CGF, "NSAutoreleasePool", false); |
| 1093 | if (CGM.getTriple().isOSBinFormatCOFF()) { |
| 1094 | if (auto *ClassSymbol = dyn_cast<llvm::GlobalVariable>(Value)) { |
| 1095 | IdentifierInfo &II = CGF.CGM.getContext().Idents.get("NSAutoreleasePool"); |
| 1096 | TranslationUnitDecl *TUDecl = CGM.getContext().getTranslationUnitDecl(); |
| 1097 | DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl); |
| 1098 | |
| 1099 | const VarDecl *VD = nullptr; |
| 1100 | for (const auto &Result : DC->lookup(&II)) |
| 1101 | if ((VD = dyn_cast<VarDecl>(Result))) |
| 1102 | break; |
| 1103 | |
| 1104 | auto DLLStorage = llvm::GlobalValue::DefaultStorageClass; |
| 1105 | if (!VD || VD->hasAttr<DLLImportAttr>()) |
| 1106 | DLLStorage = llvm::GlobalValue::DLLImportStorageClass; |
| 1107 | else if (VD->hasAttr<DLLExportAttr>()) |
| 1108 | DLLStorage = llvm::GlobalValue::DLLExportStorageClass; |
| 1109 | |
| 1110 | ClassSymbol->setDLLStorageClass(DLLStorage); |
| 1111 | } |
| 1112 | } |
| 1113 | return Value; |
David Chisnall | 920e83b | 2011-06-29 13:16:41 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1116 | llvm::Value *CGObjCGNU::GetSelector(CodeGenFunction &CGF, Selector Sel, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1117 | const std::string &TypeEncoding) { |
Craig Topper | fa159c1 | 2013-07-14 16:47:36 +0000 | [diff] [blame] | 1118 | SmallVectorImpl<TypedSelector> &Types = SelectorTable[Sel]; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1119 | llvm::GlobalAlias *SelValue = nullptr; |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1120 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1121 | for (SmallVectorImpl<TypedSelector>::iterator i = Types.begin(), |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1122 | e = Types.end() ; i!=e ; i++) { |
| 1123 | if (i->first == TypeEncoding) { |
| 1124 | SelValue = i->second; |
| 1125 | break; |
| 1126 | } |
| 1127 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1128 | if (!SelValue) { |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 1129 | SelValue = llvm::GlobalAlias::create( |
David Blaikie | aff29d3 | 2015-09-14 18:02:04 +0000 | [diff] [blame] | 1130 | SelectorTy->getElementType(), 0, llvm::GlobalValue::PrivateLinkage, |
Rafael Espindola | 6172277 | 2014-05-17 19:58:16 +0000 | [diff] [blame] | 1131 | ".objc_selector_" + Sel.getAsString(), &TheModule); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 1132 | Types.emplace_back(TypeEncoding, SelValue); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1135 | return SelValue; |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1138 | Address CGObjCGNU::GetAddrOfSelector(CodeGenFunction &CGF, Selector Sel) { |
| 1139 | llvm::Value *SelValue = GetSelector(CGF, Sel); |
| 1140 | |
| 1141 | // Store it to a temporary. Does this satisfy the semantics of |
| 1142 | // GetAddrOfSelector? Hopefully. |
| 1143 | Address tmp = CGF.CreateTempAlloca(SelValue->getType(), |
| 1144 | CGF.getPointerAlign()); |
| 1145 | CGF.Builder.CreateStore(SelValue, tmp); |
| 1146 | return tmp; |
| 1147 | } |
| 1148 | |
| 1149 | llvm::Value *CGObjCGNU::GetSelector(CodeGenFunction &CGF, Selector Sel) { |
| 1150 | return GetSelector(CGF, Sel, std::string()); |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1153 | llvm::Value *CGObjCGNU::GetSelector(CodeGenFunction &CGF, |
| 1154 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1155 | std::string SelTypes; |
| 1156 | CGM.getContext().getObjCEncodingForMethodDecl(Method, SelTypes); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1157 | return GetSelector(CGF, Method->getSelector(), SelTypes); |
Chris Lattner | 6d522c0 | 2008-06-26 04:37:12 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Fariborz Jahanian | 831f0fc | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1160 | llvm::Constant *CGObjCGNU::GetEHType(QualType T) { |
John McCall | c31d893 | 2012-11-14 09:08:34 +0000 | [diff] [blame] | 1161 | if (T->isObjCIdType() || T->isObjCQualifiedIdType()) { |
| 1162 | // With the old ABI, there was only one kind of catchall, which broke |
| 1163 | // foreign exceptions. With the new ABI, we use __objc_id_typeinfo as |
| 1164 | // a pointer indicating object catchalls, and NULL to indicate real |
| 1165 | // catchalls |
| 1166 | if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) { |
| 1167 | return MakeConstantString("@id"); |
| 1168 | } else { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1169 | return nullptr; |
John McCall | c31d893 | 2012-11-14 09:08:34 +0000 | [diff] [blame] | 1170 | } |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 1171 | } |
John McCall | c31d893 | 2012-11-14 09:08:34 +0000 | [diff] [blame] | 1172 | |
| 1173 | // All other types should be Objective-C interface pointer types. |
| 1174 | const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>(); |
| 1175 | assert(OPT && "Invalid @catch type."); |
| 1176 | const ObjCInterfaceDecl *IDecl = OPT->getObjectType()->getInterface(); |
| 1177 | assert(IDecl && "Invalid @catch type."); |
| 1178 | return MakeConstantString(IDecl->getIdentifier()->getName()); |
| 1179 | } |
| 1180 | |
| 1181 | llvm::Constant *CGObjCGNUstep::GetEHType(QualType T) { |
| 1182 | if (!CGM.getLangOpts().CPlusPlus) |
| 1183 | return CGObjCGNU::GetEHType(T); |
| 1184 | |
David Chisnall | e1d2584d | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 1185 | // For Objective-C++, we want to provide the ability to catch both C++ and |
| 1186 | // Objective-C objects in the same function. |
| 1187 | |
| 1188 | // There's a particular fixed type info for 'id'. |
| 1189 | if (T->isObjCIdType() || |
| 1190 | T->isObjCQualifiedIdType()) { |
| 1191 | llvm::Constant *IDEHType = |
| 1192 | CGM.getModule().getGlobalVariable("__objc_id_type_info"); |
| 1193 | if (!IDEHType) |
| 1194 | IDEHType = |
| 1195 | new llvm::GlobalVariable(CGM.getModule(), PtrToInt8Ty, |
| 1196 | false, |
| 1197 | llvm::GlobalValue::ExternalLinkage, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1198 | nullptr, "__objc_id_type_info"); |
David Chisnall | e1d2584d | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 1199 | return llvm::ConstantExpr::getBitCast(IDEHType, PtrToInt8Ty); |
| 1200 | } |
| 1201 | |
| 1202 | const ObjCObjectPointerType *PT = |
| 1203 | T->getAs<ObjCObjectPointerType>(); |
| 1204 | assert(PT && "Invalid @catch type."); |
| 1205 | const ObjCInterfaceType *IT = PT->getInterfaceType(); |
| 1206 | assert(IT && "Invalid @catch type."); |
| 1207 | std::string className = IT->getDecl()->getIdentifier()->getName(); |
| 1208 | |
| 1209 | std::string typeinfoName = "__objc_eh_typeinfo_" + className; |
| 1210 | |
| 1211 | // Return the existing typeinfo if it exists |
| 1212 | llvm::Constant *typeinfo = TheModule.getGlobalVariable(typeinfoName); |
David Chisnall | d663934 | 2012-03-20 16:25:52 +0000 | [diff] [blame] | 1213 | if (typeinfo) |
| 1214 | return llvm::ConstantExpr::getBitCast(typeinfo, PtrToInt8Ty); |
David Chisnall | e1d2584d | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 1215 | |
| 1216 | // Otherwise create it. |
| 1217 | |
| 1218 | // vtable for gnustep::libobjc::__objc_class_type_info |
| 1219 | // It's quite ugly hard-coding this. Ideally we'd generate it using the host |
| 1220 | // platform's name mangling. |
| 1221 | const char *vtableName = "_ZTVN7gnustep7libobjc22__objc_class_type_infoE"; |
David Blaikie | e3b172a | 2015-04-02 18:55:21 +0000 | [diff] [blame] | 1222 | auto *Vtable = TheModule.getGlobalVariable(vtableName); |
David Chisnall | e1d2584d | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 1223 | if (!Vtable) { |
| 1224 | Vtable = new llvm::GlobalVariable(TheModule, PtrToInt8Ty, true, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1225 | llvm::GlobalValue::ExternalLinkage, |
| 1226 | nullptr, vtableName); |
David Chisnall | e1d2584d | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 1227 | } |
| 1228 | llvm::Constant *Two = llvm::ConstantInt::get(IntTy, 2); |
David Blaikie | e3b172a | 2015-04-02 18:55:21 +0000 | [diff] [blame] | 1229 | auto *BVtable = llvm::ConstantExpr::getBitCast( |
| 1230 | llvm::ConstantExpr::getGetElementPtr(Vtable->getValueType(), Vtable, Two), |
| 1231 | PtrToInt8Ty); |
David Chisnall | e1d2584d | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 1232 | |
| 1233 | llvm::Constant *typeName = |
| 1234 | ExportUniqueString(className, "__objc_eh_typename_"); |
| 1235 | |
| 1236 | std::vector<llvm::Constant*> fields; |
David Blaikie | e3b172a | 2015-04-02 18:55:21 +0000 | [diff] [blame] | 1237 | fields.push_back(BVtable); |
David Chisnall | e1d2584d | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 1238 | fields.push_back(typeName); |
| 1239 | llvm::Constant *TI = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1240 | MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, nullptr), |
| 1241 | fields, CGM.getPointerAlign(), |
| 1242 | "__objc_eh_typeinfo_" + className, |
David Chisnall | e1d2584d | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 1243 | llvm::GlobalValue::LinkOnceODRLinkage); |
| 1244 | return llvm::ConstantExpr::getBitCast(TI, PtrToInt8Ty); |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1247 | /// Generate an NSConstantString object. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1248 | ConstantAddress CGObjCGNU::GenerateConstantString(const StringLiteral *SL) { |
David Chisnall | 358e751 | 2010-01-27 12:49:23 +0000 | [diff] [blame] | 1249 | |
Benjamin Kramer | 35b077e | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 1250 | std::string Str = SL->getString().str(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1251 | CharUnits Align = CGM.getPointerAlign(); |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 1252 | |
David Chisnall | 358e751 | 2010-01-27 12:49:23 +0000 | [diff] [blame] | 1253 | // Look for an existing one |
| 1254 | llvm::StringMap<llvm::Constant*>::iterator old = ObjCStrings.find(Str); |
| 1255 | if (old != ObjCStrings.end()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1256 | return ConstantAddress(old->getValue(), Align); |
David Chisnall | 358e751 | 2010-01-27 12:49:23 +0000 | [diff] [blame] | 1257 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1258 | StringRef StringClass = CGM.getLangOpts().ObjCConstantStringClass; |
David Chisnall | 207a630 | 2012-01-04 12:02:13 +0000 | [diff] [blame] | 1259 | |
| 1260 | if (StringClass.empty()) StringClass = "NXConstantString"; |
| 1261 | |
| 1262 | std::string Sym = "_OBJC_CLASS_"; |
| 1263 | Sym += StringClass; |
| 1264 | |
| 1265 | llvm::Constant *isa = TheModule.getNamedGlobal(Sym); |
| 1266 | |
| 1267 | if (!isa) |
| 1268 | isa = new llvm::GlobalVariable(TheModule, IdTy, /* isConstant */false, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1269 | llvm::GlobalValue::ExternalWeakLinkage, nullptr, Sym); |
David Chisnall | 207a630 | 2012-01-04 12:02:13 +0000 | [diff] [blame] | 1270 | else if (isa->getType() != PtrToIdTy) |
| 1271 | isa = llvm::ConstantExpr::getBitCast(isa, PtrToIdTy); |
| 1272 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1273 | std::vector<llvm::Constant*> Ivars; |
David Chisnall | 207a630 | 2012-01-04 12:02:13 +0000 | [diff] [blame] | 1274 | Ivars.push_back(isa); |
Chris Lattner | 091f698 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 1275 | Ivars.push_back(MakeConstantString(Str)); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1276 | Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size())); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1277 | llvm::Constant *ObjCStr = MakeGlobal( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1278 | llvm::StructType::get(PtrToIdTy, PtrToInt8Ty, IntTy, nullptr), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1279 | Ivars, Align, ".objc_str"); |
David Chisnall | 358e751 | 2010-01-27 12:49:23 +0000 | [diff] [blame] | 1280 | ObjCStr = llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty); |
| 1281 | ObjCStrings[Str] = ObjCStr; |
| 1282 | ConstantStrings.push_back(ObjCStr); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1283 | return ConstantAddress(ObjCStr, Align); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | ///Generates a message send where the super is the receiver. This is a message |
| 1287 | ///send to self with special delivery semantics indicating which class's method |
| 1288 | ///should be called. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1289 | RValue |
| 1290 | CGObjCGNU::GenerateMessageSendSuper(CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1291 | ReturnValueSlot Return, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1292 | QualType ResultType, |
| 1293 | Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1294 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1295 | bool isCategoryImpl, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1296 | llvm::Value *Receiver, |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1297 | bool IsClassMessage, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1298 | const CallArgList &CallArgs, |
| 1299 | const ObjCMethodDecl *Method) { |
David Chisnall | 8a42d19 | 2011-05-28 14:09:01 +0000 | [diff] [blame] | 1300 | CGBuilderTy &Builder = CGF.Builder; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1301 | if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) { |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 1302 | if (Sel == RetainSel || Sel == AutoreleaseSel) { |
David Chisnall | 8a42d19 | 2011-05-28 14:09:01 +0000 | [diff] [blame] | 1303 | return RValue::get(EnforceType(Builder, Receiver, |
| 1304 | CGM.getTypes().ConvertType(ResultType))); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 1305 | } |
| 1306 | if (Sel == ReleaseSel) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1307 | return RValue::get(nullptr); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 1308 | } |
| 1309 | } |
David Chisnall | ea529a4 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 1310 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1311 | llvm::Value *cmd = GetSelector(CGF, Sel); |
Fariborz Jahanian | b73a23e | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 1312 | CallArgList ActualArgs; |
| 1313 | |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1314 | ActualArgs.add(RValue::get(EnforceType(Builder, Receiver, IdTy)), ASTIdTy); |
| 1315 | ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1316 | ActualArgs.addFrom(CallArgs); |
Fariborz Jahanian | b73a23e | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 1317 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1318 | MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs); |
Fariborz Jahanian | b73a23e | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 1319 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1320 | llvm::Value *ReceiverClass = nullptr; |
Chris Lattner | a02cb80 | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 1321 | if (isCategoryImpl) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1322 | llvm::Constant *classLookupFunction = nullptr; |
Chris Lattner | a02cb80 | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 1323 | if (IsClassMessage) { |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1324 | classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get( |
Jay Foad | 5709f7c | 2011-07-29 13:56:53 +0000 | [diff] [blame] | 1325 | IdTy, PtrTy, true), "objc_get_meta_class"); |
Chris Lattner | a02cb80 | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 1326 | } else { |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1327 | classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get( |
Jay Foad | 5709f7c | 2011-07-29 13:56:53 +0000 | [diff] [blame] | 1328 | IdTy, PtrTy, true), "objc_get_class"); |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1329 | } |
David Chisnall | ea529a4 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 1330 | ReceiverClass = Builder.CreateCall(classLookupFunction, |
Chris Lattner | a02cb80 | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 1331 | MakeConstantString(Class->getNameAsString())); |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1332 | } else { |
Chris Lattner | a02cb80 | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 1333 | // Set up global aliases for the metaclass or class pointer if they do not |
| 1334 | // already exist. These will are forward-references which will be set to |
Mike Stump | dd93a19 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 1335 | // pointers to the class and metaclass structure created for the runtime |
| 1336 | // 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] | 1337 | // super_class pointer from either the class or metaclass structure. |
| 1338 | if (IsClassMessage) { |
| 1339 | if (!MetaClassPtrAlias) { |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 1340 | MetaClassPtrAlias = llvm::GlobalAlias::create( |
David Blaikie | aff29d3 | 2015-09-14 18:02:04 +0000 | [diff] [blame] | 1341 | IdTy->getElementType(), 0, llvm::GlobalValue::InternalLinkage, |
Rafael Espindola | 6172277 | 2014-05-17 19:58:16 +0000 | [diff] [blame] | 1342 | ".objc_metaclass_ref" + Class->getNameAsString(), &TheModule); |
Chris Lattner | a02cb80 | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 1343 | } |
| 1344 | ReceiverClass = MetaClassPtrAlias; |
| 1345 | } else { |
| 1346 | if (!ClassPtrAlias) { |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 1347 | ClassPtrAlias = llvm::GlobalAlias::create( |
David Blaikie | aff29d3 | 2015-09-14 18:02:04 +0000 | [diff] [blame] | 1348 | IdTy->getElementType(), 0, llvm::GlobalValue::InternalLinkage, |
Rafael Espindola | 6172277 | 2014-05-17 19:58:16 +0000 | [diff] [blame] | 1349 | ".objc_class_ref" + Class->getNameAsString(), &TheModule); |
Chris Lattner | a02cb80 | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 1350 | } |
| 1351 | ReceiverClass = ClassPtrAlias; |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1352 | } |
Chris Lattner | c06ce0f | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 1353 | } |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1354 | // Cast the pointer to a simplified version of the class structure |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 1355 | llvm::Type *CastTy = llvm::StructType::get(IdTy, IdTy, nullptr); |
David Chisnall | ea529a4 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 1356 | ReceiverClass = Builder.CreateBitCast(ReceiverClass, |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 1357 | llvm::PointerType::getUnqual(CastTy)); |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1358 | // Get the superclass pointer |
David Blaikie | 1ed728c | 2015-04-05 22:45:47 +0000 | [diff] [blame] | 1359 | ReceiverClass = Builder.CreateStructGEP(CastTy, ReceiverClass, 1); |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1360 | // Load the superclass pointer |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1361 | ReceiverClass = |
| 1362 | Builder.CreateAlignedLoad(ReceiverClass, CGF.getPointerAlign()); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1363 | // Construct the structure used to look up the IMP |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1364 | llvm::StructType *ObjCSuperTy = llvm::StructType::get( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1365 | Receiver->getType(), IdTy, nullptr); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1366 | |
| 1367 | // FIXME: Is this really supposed to be a dynamic alloca? |
| 1368 | Address ObjCSuper = Address(Builder.CreateAlloca(ObjCSuperTy), |
| 1369 | CGF.getPointerAlign()); |
Fariborz Jahanian | b73a23e | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 1370 | |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 1371 | Builder.CreateStore(Receiver, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1372 | Builder.CreateStructGEP(ObjCSuper, 0, CharUnits::Zero())); |
David Blaikie | 2e80428 | 2015-04-05 22:47:07 +0000 | [diff] [blame] | 1373 | Builder.CreateStore(ReceiverClass, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1374 | Builder.CreateStructGEP(ObjCSuper, 1, CGF.getPointerSize())); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1375 | |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1376 | ObjCSuper = EnforceType(Builder, ObjCSuper, PtrToObjCSuperTy); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1377 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1378 | // Get the IMP |
Eli Friedman | f24bd3b | 2013-07-26 00:53:29 +0000 | [diff] [blame] | 1379 | llvm::Value *imp = LookupIMPSuper(CGF, ObjCSuper, cmd, MSI); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1380 | imp = EnforceType(Builder, imp, MSI.MessengerType); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1381 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1382 | llvm::Metadata *impMD[] = { |
David Chisnall | 9eecafa | 2010-05-01 11:15:56 +0000 | [diff] [blame] | 1383 | llvm::MDString::get(VMContext, Sel.getAsString()), |
| 1384 | llvm::MDString::get(VMContext, Class->getSuperClass()->getNameAsString()), |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1385 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 1386 | llvm::Type::getInt1Ty(VMContext), IsClassMessage))}; |
Jay Foad | ea324f1 | 2011-04-21 19:59:12 +0000 | [diff] [blame] | 1387 | llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD); |
David Chisnall | 9eecafa | 2010-05-01 11:15:56 +0000 | [diff] [blame] | 1388 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame^] | 1389 | CGCallee callee(CGCalleeInfo(), imp); |
| 1390 | |
David Chisnall | ff5f88c | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 1391 | llvm::Instruction *call; |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame^] | 1392 | RValue msgRet = CGF.EmitCall(MSI.CallInfo, callee, Return, ActualArgs, &call); |
David Chisnall | ff5f88c | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 1393 | call->setMetadata(msgSendMDKind, node); |
| 1394 | return msgRet; |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1397 | /// Generate code for a message send expression. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1398 | RValue |
| 1399 | CGObjCGNU::GenerateMessageSend(CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1400 | ReturnValueSlot Return, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1401 | QualType ResultType, |
| 1402 | Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1403 | llvm::Value *Receiver, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1404 | const CallArgList &CallArgs, |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1405 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1406 | const ObjCMethodDecl *Method) { |
David Chisnall | 8a42d19 | 2011-05-28 14:09:01 +0000 | [diff] [blame] | 1407 | CGBuilderTy &Builder = CGF.Builder; |
| 1408 | |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1409 | // Strip out message sends to retain / release in GC mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1410 | if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) { |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 1411 | if (Sel == RetainSel || Sel == AutoreleaseSel) { |
David Chisnall | 8a42d19 | 2011-05-28 14:09:01 +0000 | [diff] [blame] | 1412 | return RValue::get(EnforceType(Builder, Receiver, |
| 1413 | CGM.getTypes().ConvertType(ResultType))); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 1414 | } |
| 1415 | if (Sel == ReleaseSel) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1416 | return RValue::get(nullptr); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 1417 | } |
| 1418 | } |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1419 | |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1420 | // If the return type is something that goes in an integer register, the |
| 1421 | // runtime will handle 0 returns. For other cases, we fill in the 0 value |
| 1422 | // ourselves. |
| 1423 | // |
| 1424 | // The language spec says the result of this kind of message send is |
| 1425 | // undefined, but lots of people seem to have forgotten to read that |
| 1426 | // paragraph and insist on sending messages to nil that have structure |
| 1427 | // returns. With GCC, this generates a random return value (whatever happens |
| 1428 | // 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] | 1429 | // and generates an illegal instruction trap on SPARC. With LLVM it corrupts |
| 1430 | // the stack. |
| 1431 | bool isPointerSizedReturn = (ResultType->isAnyPointerType() || |
| 1432 | ResultType->isIntegralOrEnumerationType() || ResultType->isVoidType()); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1433 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1434 | llvm::BasicBlock *startBB = nullptr; |
| 1435 | llvm::BasicBlock *messageBB = nullptr; |
| 1436 | llvm::BasicBlock *continueBB = nullptr; |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1437 | |
| 1438 | if (!isPointerSizedReturn) { |
| 1439 | startBB = Builder.GetInsertBlock(); |
| 1440 | messageBB = CGF.createBasicBlock("msgSend"); |
David Chisnall | 29cefd1 | 2010-05-20 13:45:48 +0000 | [diff] [blame] | 1441 | continueBB = CGF.createBasicBlock("continue"); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1442 | |
| 1443 | llvm::Value *isNil = Builder.CreateICmpEQ(Receiver, |
| 1444 | llvm::Constant::getNullValue(Receiver->getType())); |
David Chisnall | 29cefd1 | 2010-05-20 13:45:48 +0000 | [diff] [blame] | 1445 | Builder.CreateCondBr(isNil, continueBB, messageBB); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1446 | CGF.EmitBlock(messageBB); |
| 1447 | } |
| 1448 | |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 1449 | IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy)); |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1450 | llvm::Value *cmd; |
| 1451 | if (Method) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1452 | cmd = GetSelector(CGF, Method); |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1453 | else |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1454 | cmd = GetSelector(CGF, Sel); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1455 | cmd = EnforceType(Builder, cmd, SelectorTy); |
| 1456 | Receiver = EnforceType(Builder, Receiver, IdTy); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1457 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 1458 | llvm::Metadata *impMD[] = { |
| 1459 | llvm::MDString::get(VMContext, Sel.getAsString()), |
| 1460 | llvm::MDString::get(VMContext, Class ? Class->getNameAsString() : ""), |
| 1461 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 1462 | llvm::Type::getInt1Ty(VMContext), Class != nullptr))}; |
Jay Foad | ea324f1 | 2011-04-21 19:59:12 +0000 | [diff] [blame] | 1463 | llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1464 | |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1465 | CallArgList ActualArgs; |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1466 | ActualArgs.add(RValue::get(Receiver), ASTIdTy); |
| 1467 | ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1468 | ActualArgs.addFrom(CallArgs); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1469 | |
| 1470 | MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs); |
| 1471 | |
David Chisnall | 8c93cf2 | 2011-10-24 14:07:03 +0000 | [diff] [blame] | 1472 | // Get the IMP to call |
| 1473 | llvm::Value *imp; |
| 1474 | |
| 1475 | // If we have non-legacy dispatch specified, we try using the objc_msgSend() |
| 1476 | // functions. These are not supported on all platforms (or all runtimes on a |
| 1477 | // given platform), so we |
| 1478 | switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) { |
David Chisnall | 8c93cf2 | 2011-10-24 14:07:03 +0000 | [diff] [blame] | 1479 | case CodeGenOptions::Legacy: |
Eli Friedman | f24bd3b | 2013-07-26 00:53:29 +0000 | [diff] [blame] | 1480 | imp = LookupIMP(CGF, Receiver, cmd, node, MSI); |
David Chisnall | 8c93cf2 | 2011-10-24 14:07:03 +0000 | [diff] [blame] | 1481 | break; |
| 1482 | case CodeGenOptions::Mixed: |
David Chisnall | 8c93cf2 | 2011-10-24 14:07:03 +0000 | [diff] [blame] | 1483 | case CodeGenOptions::NonLegacy: |
David Chisnall | 0cc83e7 | 2011-10-28 17:55:06 +0000 | [diff] [blame] | 1484 | if (CGM.ReturnTypeUsesFPRet(ResultType)) { |
| 1485 | imp = CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true), |
| 1486 | "objc_msgSend_fpret"); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1487 | } else if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) { |
David Chisnall | 8c93cf2 | 2011-10-24 14:07:03 +0000 | [diff] [blame] | 1488 | // The actual types here don't matter - we're going to bitcast the |
| 1489 | // function anyway |
| 1490 | imp = CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true), |
| 1491 | "objc_msgSend_stret"); |
| 1492 | } else { |
| 1493 | imp = CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true), |
| 1494 | "objc_msgSend"); |
| 1495 | } |
| 1496 | } |
| 1497 | |
David Chisnall | 6aec31a | 2011-12-01 18:40:09 +0000 | [diff] [blame] | 1498 | // Reset the receiver in case the lookup modified it |
| 1499 | ActualArgs[0] = CallArg(RValue::get(Receiver), ASTIdTy, false); |
David Chisnall | 8c93cf2 | 2011-10-24 14:07:03 +0000 | [diff] [blame] | 1500 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1501 | imp = EnforceType(Builder, imp, MSI.MessengerType); |
David Chisnall | c0cf422 | 2010-05-01 12:56:56 +0000 | [diff] [blame] | 1502 | |
David Chisnall | ff5f88c | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 1503 | llvm::Instruction *call; |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame^] | 1504 | CGCallee callee(CGCalleeInfo(), imp); |
| 1505 | RValue msgRet = CGF.EmitCall(MSI.CallInfo, callee, Return, ActualArgs, &call); |
David Chisnall | ff5f88c | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 1506 | call->setMetadata(msgSendMDKind, node); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1507 | |
David Chisnall | 29cefd1 | 2010-05-20 13:45:48 +0000 | [diff] [blame] | 1508 | |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1509 | if (!isPointerSizedReturn) { |
David Chisnall | 29cefd1 | 2010-05-20 13:45:48 +0000 | [diff] [blame] | 1510 | messageBB = CGF.Builder.GetInsertBlock(); |
| 1511 | CGF.Builder.CreateBr(continueBB); |
| 1512 | CGF.EmitBlock(continueBB); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1513 | if (msgRet.isScalar()) { |
| 1514 | llvm::Value *v = msgRet.getScalarVal(); |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 1515 | llvm::PHINode *phi = Builder.CreatePHI(v->getType(), 2); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1516 | phi->addIncoming(v, messageBB); |
| 1517 | phi->addIncoming(llvm::Constant::getNullValue(v->getType()), startBB); |
| 1518 | msgRet = RValue::get(phi); |
| 1519 | } else if (msgRet.isAggregate()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1520 | Address v = msgRet.getAggregateAddress(); |
| 1521 | llvm::PHINode *phi = Builder.CreatePHI(v.getType(), 2); |
| 1522 | llvm::Type *RetTy = v.getElementType(); |
| 1523 | Address NullVal = CGF.CreateTempAlloca(RetTy, v.getAlignment(), "null"); |
| 1524 | CGF.InitTempAlloca(NullVal, llvm::Constant::getNullValue(RetTy)); |
| 1525 | phi->addIncoming(v.getPointer(), messageBB); |
| 1526 | phi->addIncoming(NullVal.getPointer(), startBB); |
| 1527 | msgRet = RValue::getAggregate(Address(phi, v.getAlignment())); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1528 | } else /* isComplex() */ { |
| 1529 | std::pair<llvm::Value*,llvm::Value*> v = msgRet.getComplexVal(); |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 1530 | llvm::PHINode *phi = Builder.CreatePHI(v.first->getType(), 2); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1531 | phi->addIncoming(v.first, messageBB); |
| 1532 | phi->addIncoming(llvm::Constant::getNullValue(v.first->getType()), |
| 1533 | startBB); |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 1534 | llvm::PHINode *phi2 = Builder.CreatePHI(v.second->getType(), 2); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1535 | phi2->addIncoming(v.second, messageBB); |
| 1536 | phi2->addIncoming(llvm::Constant::getNullValue(v.second->getType()), |
| 1537 | startBB); |
| 1538 | msgRet = RValue::getComplex(phi, phi2); |
| 1539 | } |
| 1540 | } |
| 1541 | return msgRet; |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1544 | /// Generates a MethodList. Used in construction of a objc_class and |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1545 | /// objc_category structures. |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 1546 | llvm::Constant *CGObjCGNU:: |
Craig Topper | bf3e327 | 2014-08-30 16:55:52 +0000 | [diff] [blame] | 1547 | GenerateMethodList(StringRef ClassName, |
| 1548 | StringRef CategoryName, |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 1549 | ArrayRef<Selector> MethodSels, |
| 1550 | ArrayRef<llvm::Constant *> MethodTypes, |
| 1551 | bool isClassMethodList) { |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 1552 | if (MethodSels.empty()) |
| 1553 | return NULLPtr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1554 | // Get the method structure type. |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1555 | llvm::StructType *ObjCMethodTy = llvm::StructType::get( |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1556 | PtrToInt8Ty, // Really a selector, but the runtime creates it us. |
| 1557 | PtrToInt8Ty, // Method types |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1558 | IMPTy, //Method pointer |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1559 | nullptr); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1560 | std::vector<llvm::Constant*> Methods; |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1561 | for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) { |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1562 | llvm::Constant *Method = |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1563 | TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName, |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1564 | MethodSels[i], |
| 1565 | isClassMethodList)); |
| 1566 | assert(Method && "Can't generate metadata for method that doesn't exist"); |
| 1567 | llvm::Constant *C = MakeConstantString(MethodSels[i].getAsString()); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1568 | Method = llvm::ConstantExpr::getBitCast(Method, |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1569 | IMPTy); |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 1570 | Methods.push_back( |
| 1571 | llvm::ConstantStruct::get(ObjCMethodTy, {C, MethodTypes[i], Method})); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | // Array of method structures |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1575 | llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy, |
Fariborz Jahanian | 078cd52 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 1576 | Methods.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 1577 | llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy, |
Chris Lattner | 882034d | 2008-06-26 04:52:29 +0000 | [diff] [blame] | 1578 | Methods); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1579 | |
| 1580 | // Structure containing list pointer, array and array count |
Chris Lattner | 5ec04a5 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 1581 | llvm::StructType *ObjCMethodListTy = llvm::StructType::create(VMContext); |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1582 | llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(ObjCMethodListTy); |
| 1583 | ObjCMethodListTy->setBody( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1584 | NextPtrTy, |
| 1585 | IntTy, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1586 | ObjCMethodArrayTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1587 | nullptr); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1588 | |
| 1589 | Methods.clear(); |
Owen Anderson | 7ec07a5 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 1590 | Methods.push_back(llvm::ConstantPointerNull::get( |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1591 | llvm::PointerType::getUnqual(ObjCMethodListTy))); |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 1592 | Methods.push_back(llvm::ConstantInt::get(Int32Ty, MethodTypes.size())); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1593 | Methods.push_back(MethodArray); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1594 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1595 | // Create an instance of the structure |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1596 | return MakeGlobal(ObjCMethodListTy, Methods, CGM.getPointerAlign(), |
| 1597 | ".objc_method_list"); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | /// Generates an IvarList. Used in construction of a objc_class. |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 1601 | llvm::Constant *CGObjCGNU:: |
| 1602 | GenerateIvarList(ArrayRef<llvm::Constant *> IvarNames, |
| 1603 | ArrayRef<llvm::Constant *> IvarTypes, |
| 1604 | ArrayRef<llvm::Constant *> IvarOffsets) { |
David Chisnall | b3b44ce | 2009-11-16 19:05:54 +0000 | [diff] [blame] | 1605 | if (IvarNames.size() == 0) |
| 1606 | return NULLPtr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1607 | // Get the method structure type. |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1608 | llvm::StructType *ObjCIvarTy = llvm::StructType::get( |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1609 | PtrToInt8Ty, |
| 1610 | PtrToInt8Ty, |
| 1611 | IntTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1612 | nullptr); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1613 | std::vector<llvm::Constant*> Ivars; |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1614 | for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) { |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 1615 | Ivars.push_back(llvm::ConstantStruct::get( |
| 1616 | ObjCIvarTy, {IvarNames[i], IvarTypes[i], IvarOffsets[i]})); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
| 1619 | // Array of method structures |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1620 | llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1621 | IvarNames.size()); |
| 1622 | |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 1623 | llvm::Constant *Elements[] = { |
| 1624 | llvm::ConstantInt::get(IntTy, (int)IvarNames.size()), |
| 1625 | llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars)}; |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1626 | // Structure containing array and array count |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1627 | llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1628 | ObjCIvarArrayTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1629 | nullptr); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1630 | |
| 1631 | // Create an instance of the structure |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1632 | return MakeGlobal(ObjCIvarListTy, Elements, CGM.getPointerAlign(), |
| 1633 | ".objc_ivar_list"); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | /// Generate a class structure |
| 1637 | llvm::Constant *CGObjCGNU::GenerateClassStructure( |
| 1638 | llvm::Constant *MetaClass, |
| 1639 | llvm::Constant *SuperClass, |
| 1640 | unsigned info, |
Chris Lattner | da35bc8 | 2008-06-26 04:47:04 +0000 | [diff] [blame] | 1641 | const char *Name, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1642 | llvm::Constant *Version, |
| 1643 | llvm::Constant *InstanceSize, |
| 1644 | llvm::Constant *IVars, |
| 1645 | llvm::Constant *Methods, |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1646 | llvm::Constant *Protocols, |
| 1647 | llvm::Constant *IvarOffsets, |
David Chisnall | d472c85 | 2010-04-28 14:29:56 +0000 | [diff] [blame] | 1648 | llvm::Constant *Properties, |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 1649 | llvm::Constant *StrongIvarBitmap, |
| 1650 | llvm::Constant *WeakIvarBitmap, |
David Chisnall | d472c85 | 2010-04-28 14:29:56 +0000 | [diff] [blame] | 1651 | bool isMeta) { |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1652 | // Set up the class structure |
| 1653 | // Note: Several of these are char*s when they should be ids. This is |
| 1654 | // because the runtime performs this translation on load. |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1655 | // |
| 1656 | // Fields marked New ABI are part of the GNUstep runtime. We emit them |
| 1657 | // anyway; the classes will still work with the GNU runtime, they will just |
| 1658 | // be ignored. |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1659 | llvm::StructType *ClassTy = llvm::StructType::get( |
David Chisnall | 207a630 | 2012-01-04 12:02:13 +0000 | [diff] [blame] | 1660 | PtrToInt8Ty, // isa |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1661 | PtrToInt8Ty, // super_class |
| 1662 | PtrToInt8Ty, // name |
| 1663 | LongTy, // version |
| 1664 | LongTy, // info |
| 1665 | LongTy, // instance_size |
| 1666 | IVars->getType(), // ivars |
| 1667 | Methods->getType(), // methods |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1668 | // These are all filled in by the runtime, so we pretend |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1669 | PtrTy, // dtable |
| 1670 | PtrTy, // subclass_list |
| 1671 | PtrTy, // sibling_class |
| 1672 | PtrTy, // protocols |
| 1673 | PtrTy, // gc_object_type |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1674 | // New ABI: |
| 1675 | LongTy, // abi_version |
| 1676 | IvarOffsets->getType(), // ivar_offsets |
| 1677 | Properties->getType(), // properties |
David Chisnall | e89ac06 | 2011-10-25 10:12:21 +0000 | [diff] [blame] | 1678 | IntPtrTy, // strong_pointers |
| 1679 | IntPtrTy, // weak_pointers |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1680 | nullptr); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1681 | llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1682 | // Fill in the structure |
| 1683 | std::vector<llvm::Constant*> Elements; |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1684 | Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty)); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1685 | Elements.push_back(SuperClass); |
Chris Lattner | da35bc8 | 2008-06-26 04:47:04 +0000 | [diff] [blame] | 1686 | Elements.push_back(MakeConstantString(Name, ".class_name")); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1687 | Elements.push_back(Zero); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1688 | Elements.push_back(llvm::ConstantInt::get(LongTy, info)); |
David Chisnall | 055f064 | 2011-02-21 23:47:40 +0000 | [diff] [blame] | 1689 | if (isMeta) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1690 | llvm::DataLayout td(&TheModule); |
Ken Dyck | 0fed10e | 2011-04-22 17:59:22 +0000 | [diff] [blame] | 1691 | Elements.push_back( |
| 1692 | llvm::ConstantInt::get(LongTy, |
| 1693 | td.getTypeSizeInBits(ClassTy) / |
| 1694 | CGM.getContext().getCharWidth())); |
David Chisnall | 055f064 | 2011-02-21 23:47:40 +0000 | [diff] [blame] | 1695 | } else |
| 1696 | Elements.push_back(InstanceSize); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1697 | Elements.push_back(IVars); |
| 1698 | Elements.push_back(Methods); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1699 | Elements.push_back(NULLPtr); |
| 1700 | Elements.push_back(NULLPtr); |
| 1701 | Elements.push_back(NULLPtr); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1702 | Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy)); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1703 | Elements.push_back(NULLPtr); |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 1704 | Elements.push_back(llvm::ConstantInt::get(LongTy, 1)); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1705 | Elements.push_back(IvarOffsets); |
| 1706 | Elements.push_back(Properties); |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 1707 | Elements.push_back(StrongIvarBitmap); |
| 1708 | Elements.push_back(WeakIvarBitmap); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1709 | // Create an instance of the structure |
David Chisnall | df34917 | 2010-01-08 00:14:31 +0000 | [diff] [blame] | 1710 | // This is now an externally visible symbol, so that we can speed up class |
David Chisnall | 207a630 | 2012-01-04 12:02:13 +0000 | [diff] [blame] | 1711 | // messages in the next ABI. We may already have some weak references to |
| 1712 | // this, so check and fix them properly. |
| 1713 | std::string ClassSym((isMeta ? "_OBJC_METACLASS_": "_OBJC_CLASS_") + |
| 1714 | std::string(Name)); |
| 1715 | llvm::GlobalVariable *ClassRef = TheModule.getNamedGlobal(ClassSym); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1716 | llvm::Constant *Class = |
| 1717 | MakeGlobal(ClassTy, Elements, CGM.getPointerAlign(), ClassSym, |
| 1718 | llvm::GlobalValue::ExternalLinkage); |
David Chisnall | 207a630 | 2012-01-04 12:02:13 +0000 | [diff] [blame] | 1719 | if (ClassRef) { |
| 1720 | ClassRef->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(Class, |
| 1721 | ClassRef->getType())); |
| 1722 | ClassRef->removeFromParent(); |
| 1723 | Class->setName(ClassSym); |
| 1724 | } |
| 1725 | return Class; |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 1728 | llvm::Constant *CGObjCGNU:: |
| 1729 | GenerateProtocolMethodList(ArrayRef<llvm::Constant *> MethodNames, |
| 1730 | ArrayRef<llvm::Constant *> MethodTypes) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1731 | // Get the method structure type. |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1732 | llvm::StructType *ObjCMethodDescTy = llvm::StructType::get( |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1733 | PtrToInt8Ty, // Really a selector, but the runtime does the casting for us. |
| 1734 | PtrToInt8Ty, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1735 | nullptr); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1736 | std::vector<llvm::Constant*> Methods; |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1737 | for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) { |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 1738 | Methods.push_back(llvm::ConstantStruct::get( |
| 1739 | ObjCMethodDescTy, {MethodNames[i], MethodTypes[i]})); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1740 | } |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1741 | llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1742 | MethodNames.size()); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 1743 | llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, |
Mike Stump | dd93a19 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 1744 | Methods); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1745 | llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get( |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1746 | IntTy, ObjCMethodArrayTy, nullptr); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1747 | Methods.clear(); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1748 | Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size())); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1749 | Methods.push_back(Array); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1750 | return MakeGlobal(ObjCMethodDescListTy, Methods, CGM.getPointerAlign(), |
| 1751 | ".objc_method_list"); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1752 | } |
Mike Stump | dd93a19 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 1753 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1754 | // Create the protocol list structure used in classes, categories and so on |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 1755 | llvm::Constant *CGObjCGNU::GenerateProtocolList(ArrayRef<std::string>Protocols){ |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1756 | llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1757 | Protocols.size()); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1758 | llvm::StructType *ProtocolListTy = llvm::StructType::get( |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1759 | PtrTy, //Should be a recurisve pointer, but it's always NULL here. |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1760 | SizeTy, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1761 | ProtocolArrayTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1762 | nullptr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1763 | std::vector<llvm::Constant*> Elements; |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1764 | for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end(); |
| 1765 | iter != endIter ; iter++) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1766 | llvm::Constant *protocol = nullptr; |
David Chisnall | bc8bdea | 2009-11-20 14:50:59 +0000 | [diff] [blame] | 1767 | llvm::StringMap<llvm::Constant*>::iterator value = |
| 1768 | ExistingProtocols.find(*iter); |
| 1769 | if (value == ExistingProtocols.end()) { |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1770 | protocol = GenerateEmptyProtocol(*iter); |
David Chisnall | bc8bdea | 2009-11-20 14:50:59 +0000 | [diff] [blame] | 1771 | } else { |
| 1772 | protocol = value->getValue(); |
| 1773 | } |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1774 | llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(protocol, |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1775 | PtrToInt8Ty); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1776 | Elements.push_back(Ptr); |
| 1777 | } |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 1778 | llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1779 | Elements); |
| 1780 | Elements.clear(); |
| 1781 | Elements.push_back(NULLPtr); |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1782 | Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size())); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1783 | Elements.push_back(ProtocolArray); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1784 | return MakeGlobal(ProtocolListTy, Elements, CGM.getPointerAlign(), |
| 1785 | ".objc_protocol_list"); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1788 | llvm::Value *CGObjCGNU::GenerateProtocolRef(CodeGenFunction &CGF, |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1789 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1790 | llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()]; |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1791 | llvm::Type *T = |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1792 | CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType()); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 1793 | return CGF.Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T)); |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | llvm::Constant *CGObjCGNU::GenerateEmptyProtocol( |
| 1797 | const std::string &ProtocolName) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1798 | SmallVector<std::string, 0> EmptyStringVector; |
| 1799 | SmallVector<llvm::Constant*, 0> EmptyConstantVector; |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1800 | |
| 1801 | llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1802 | llvm::Constant *MethodList = |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1803 | GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector); |
| 1804 | // Protocols are objects containing lists of the methods implemented and |
| 1805 | // protocols adopted. |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1806 | llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy, |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1807 | PtrToInt8Ty, |
| 1808 | ProtocolList->getType(), |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1809 | MethodList->getType(), |
| 1810 | MethodList->getType(), |
| 1811 | MethodList->getType(), |
| 1812 | MethodList->getType(), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1813 | nullptr); |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1814 | // The isa pointer must be set to a magic number so the runtime knows it's |
| 1815 | // the correct layout. |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 1816 | llvm::Constant *Elements[] = { |
| 1817 | llvm::ConstantExpr::getIntToPtr( |
| 1818 | llvm::ConstantInt::get(Int32Ty, ProtocolVersion), IdTy), |
| 1819 | MakeConstantString(ProtocolName, ".objc_protocol_name"), ProtocolList, |
| 1820 | MethodList, MethodList, MethodList, MethodList}; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1821 | return MakeGlobal(ProtocolTy, Elements, CGM.getPointerAlign(), |
| 1822 | ".objc_protocol"); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1823 | } |
| 1824 | |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1825 | void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) { |
| 1826 | ASTContext &Context = CGM.getContext(); |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1827 | std::string ProtocolName = PD->getNameAsString(); |
Douglas Gregor | a715bff | 2012-01-01 19:51:50 +0000 | [diff] [blame] | 1828 | |
| 1829 | // Use the protocol definition, if there is one. |
| 1830 | if (const ObjCProtocolDecl *Def = PD->getDefinition()) |
| 1831 | PD = Def; |
| 1832 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1833 | SmallVector<std::string, 16> Protocols; |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 1834 | for (const auto *PI : PD->protocols()) |
| 1835 | Protocols.push_back(PI->getNameAsString()); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1836 | SmallVector<llvm::Constant*, 16> InstanceMethodNames; |
| 1837 | SmallVector<llvm::Constant*, 16> InstanceMethodTypes; |
| 1838 | SmallVector<llvm::Constant*, 16> OptionalInstanceMethodNames; |
| 1839 | SmallVector<llvm::Constant*, 16> OptionalInstanceMethodTypes; |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 1840 | for (const auto *I : PD->instance_methods()) { |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1841 | std::string TypeStr; |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 1842 | Context.getObjCEncodingForMethodDecl(I, TypeStr); |
| 1843 | if (I->getImplementationControl() == ObjCMethodDecl::Optional) { |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1844 | OptionalInstanceMethodNames.push_back( |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 1845 | MakeConstantString(I->getSelector().getAsString())); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1846 | OptionalInstanceMethodTypes.push_back(MakeConstantString(TypeStr)); |
David Chisnall | 12d8135 | 2012-08-23 12:17:21 +0000 | [diff] [blame] | 1847 | } else { |
| 1848 | InstanceMethodNames.push_back( |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 1849 | MakeConstantString(I->getSelector().getAsString())); |
David Chisnall | 12d8135 | 2012-08-23 12:17:21 +0000 | [diff] [blame] | 1850 | InstanceMethodTypes.push_back(MakeConstantString(TypeStr)); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1851 | } |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1852 | } |
| 1853 | // Collect information about class methods: |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1854 | SmallVector<llvm::Constant*, 16> ClassMethodNames; |
| 1855 | SmallVector<llvm::Constant*, 16> ClassMethodTypes; |
| 1856 | SmallVector<llvm::Constant*, 16> OptionalClassMethodNames; |
| 1857 | SmallVector<llvm::Constant*, 16> OptionalClassMethodTypes; |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 1858 | for (const auto *I : PD->class_methods()) { |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1859 | std::string TypeStr; |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 1860 | Context.getObjCEncodingForMethodDecl(I,TypeStr); |
| 1861 | if (I->getImplementationControl() == ObjCMethodDecl::Optional) { |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1862 | OptionalClassMethodNames.push_back( |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 1863 | MakeConstantString(I->getSelector().getAsString())); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1864 | OptionalClassMethodTypes.push_back(MakeConstantString(TypeStr)); |
David Chisnall | 12d8135 | 2012-08-23 12:17:21 +0000 | [diff] [blame] | 1865 | } else { |
| 1866 | ClassMethodNames.push_back( |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 1867 | MakeConstantString(I->getSelector().getAsString())); |
David Chisnall | 12d8135 | 2012-08-23 12:17:21 +0000 | [diff] [blame] | 1868 | ClassMethodTypes.push_back(MakeConstantString(TypeStr)); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1869 | } |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1870 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1871 | |
| 1872 | llvm::Constant *ProtocolList = GenerateProtocolList(Protocols); |
| 1873 | llvm::Constant *InstanceMethodList = |
| 1874 | GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes); |
| 1875 | llvm::Constant *ClassMethodList = |
| 1876 | GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1877 | llvm::Constant *OptionalInstanceMethodList = |
| 1878 | GenerateProtocolMethodList(OptionalInstanceMethodNames, |
| 1879 | OptionalInstanceMethodTypes); |
| 1880 | llvm::Constant *OptionalClassMethodList = |
| 1881 | GenerateProtocolMethodList(OptionalClassMethodNames, |
| 1882 | OptionalClassMethodTypes); |
| 1883 | |
| 1884 | // Property metadata: name, attributes, isSynthesized, setter name, setter |
| 1885 | // types, getter name, getter types. |
| 1886 | // The isSynthesized value is always set to 0 in a protocol. It exists to |
| 1887 | // simplify the runtime library by allowing it to use the same data |
| 1888 | // structures for protocol metadata everywhere. |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1889 | llvm::StructType *PropertyMetadataTy = llvm::StructType::get( |
David Chisnall | beb8013 | 2013-02-28 13:59:29 +0000 | [diff] [blame] | 1890 | PtrToInt8Ty, Int8Ty, Int8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1891 | PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty, nullptr); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1892 | std::vector<llvm::Constant*> Properties; |
| 1893 | std::vector<llvm::Constant*> OptionalProperties; |
| 1894 | |
| 1895 | // Add all of the property methods need adding to the method list and to the |
| 1896 | // property metadata list. |
Manman Ren | a7a8b1f | 2016-01-26 18:05:23 +0000 | [diff] [blame] | 1897 | for (auto *property : PD->instance_properties()) { |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1898 | std::vector<llvm::Constant*> Fields; |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1899 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1900 | Fields.push_back(MakePropertyEncodingString(property, nullptr)); |
David Chisnall | beb8013 | 2013-02-28 13:59:29 +0000 | [diff] [blame] | 1901 | PushPropertyAttributes(Fields, property); |
David Chisnall | a5f5941 | 2012-10-16 15:11:55 +0000 | [diff] [blame] | 1902 | |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1903 | if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) { |
| 1904 | std::string TypeStr; |
| 1905 | Context.getObjCEncodingForMethodDecl(getter,TypeStr); |
| 1906 | llvm::Constant *TypeEncoding = MakeConstantString(TypeStr); |
| 1907 | InstanceMethodTypes.push_back(TypeEncoding); |
| 1908 | Fields.push_back(MakeConstantString(getter->getSelector().getAsString())); |
| 1909 | Fields.push_back(TypeEncoding); |
| 1910 | } else { |
| 1911 | Fields.push_back(NULLPtr); |
| 1912 | Fields.push_back(NULLPtr); |
| 1913 | } |
| 1914 | if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) { |
| 1915 | std::string TypeStr; |
| 1916 | Context.getObjCEncodingForMethodDecl(setter,TypeStr); |
| 1917 | llvm::Constant *TypeEncoding = MakeConstantString(TypeStr); |
| 1918 | InstanceMethodTypes.push_back(TypeEncoding); |
| 1919 | Fields.push_back(MakeConstantString(setter->getSelector().getAsString())); |
| 1920 | Fields.push_back(TypeEncoding); |
| 1921 | } else { |
| 1922 | Fields.push_back(NULLPtr); |
| 1923 | Fields.push_back(NULLPtr); |
| 1924 | } |
| 1925 | if (property->getPropertyImplementation() == ObjCPropertyDecl::Optional) { |
| 1926 | OptionalProperties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields)); |
| 1927 | } else { |
| 1928 | Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields)); |
| 1929 | } |
| 1930 | } |
| 1931 | llvm::Constant *PropertyArray = llvm::ConstantArray::get( |
| 1932 | llvm::ArrayType::get(PropertyMetadataTy, Properties.size()), Properties); |
| 1933 | llvm::Constant* PropertyListInitFields[] = |
| 1934 | {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray}; |
| 1935 | |
| 1936 | llvm::Constant *PropertyListInit = |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 1937 | llvm::ConstantStruct::getAnon(PropertyListInitFields); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1938 | llvm::Constant *PropertyList = new llvm::GlobalVariable(TheModule, |
| 1939 | PropertyListInit->getType(), false, llvm::GlobalValue::InternalLinkage, |
| 1940 | PropertyListInit, ".objc_property_list"); |
| 1941 | |
| 1942 | llvm::Constant *OptionalPropertyArray = |
| 1943 | llvm::ConstantArray::get(llvm::ArrayType::get(PropertyMetadataTy, |
| 1944 | OptionalProperties.size()) , OptionalProperties); |
| 1945 | llvm::Constant* OptionalPropertyListInitFields[] = { |
| 1946 | llvm::ConstantInt::get(IntTy, OptionalProperties.size()), NULLPtr, |
| 1947 | OptionalPropertyArray }; |
| 1948 | |
| 1949 | llvm::Constant *OptionalPropertyListInit = |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 1950 | llvm::ConstantStruct::getAnon(OptionalPropertyListInitFields); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1951 | llvm::Constant *OptionalPropertyList = new llvm::GlobalVariable(TheModule, |
| 1952 | OptionalPropertyListInit->getType(), false, |
| 1953 | llvm::GlobalValue::InternalLinkage, OptionalPropertyListInit, |
| 1954 | ".objc_property_list"); |
| 1955 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1956 | // Protocols are objects containing lists of the methods implemented and |
| 1957 | // protocols adopted. |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1958 | llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1959 | PtrToInt8Ty, |
| 1960 | ProtocolList->getType(), |
| 1961 | InstanceMethodList->getType(), |
| 1962 | ClassMethodList->getType(), |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1963 | OptionalInstanceMethodList->getType(), |
| 1964 | OptionalClassMethodList->getType(), |
| 1965 | PropertyList->getType(), |
| 1966 | OptionalPropertyList->getType(), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 1967 | nullptr); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1968 | // The isa pointer must be set to a magic number so the runtime knows it's |
| 1969 | // the correct layout. |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 1970 | llvm::Constant *Elements[] = { |
| 1971 | llvm::ConstantExpr::getIntToPtr( |
| 1972 | llvm::ConstantInt::get(Int32Ty, ProtocolVersion), IdTy), |
| 1973 | MakeConstantString(ProtocolName, ".objc_protocol_name"), ProtocolList, |
| 1974 | InstanceMethodList, ClassMethodList, OptionalInstanceMethodList, |
| 1975 | OptionalClassMethodList, PropertyList, OptionalPropertyList}; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1976 | ExistingProtocols[ProtocolName] = |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1977 | llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1978 | CGM.getPointerAlign(), ".objc_protocol"), IdTy); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1979 | } |
Dmitri Gribenko | b2aa923 | 2012-11-15 14:28:07 +0000 | [diff] [blame] | 1980 | void CGObjCGNU::GenerateProtocolHolderCategory() { |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1981 | // Collect information about instance methods |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1982 | SmallVector<Selector, 1> MethodSels; |
| 1983 | SmallVector<llvm::Constant*, 1> MethodTypes; |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1984 | |
| 1985 | std::vector<llvm::Constant*> Elements; |
| 1986 | const std::string ClassName = "__ObjC_Protocol_Holder_Ugly_Hack"; |
| 1987 | const std::string CategoryName = "AnotherHack"; |
| 1988 | Elements.push_back(MakeConstantString(CategoryName)); |
| 1989 | Elements.push_back(MakeConstantString(ClassName)); |
| 1990 | // Instance method list |
| 1991 | Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList( |
| 1992 | ClassName, CategoryName, MethodSels, MethodTypes, false), PtrTy)); |
| 1993 | // Class method list |
| 1994 | Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList( |
| 1995 | ClassName, CategoryName, MethodSels, MethodTypes, true), PtrTy)); |
| 1996 | // Protocol list |
| 1997 | llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrTy, |
| 1998 | ExistingProtocols.size()); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1999 | llvm::StructType *ProtocolListTy = llvm::StructType::get( |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2000 | PtrTy, //Should be a recurisve pointer, but it's always NULL here. |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 2001 | SizeTy, |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2002 | ProtocolArrayTy, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2003 | nullptr); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2004 | std::vector<llvm::Constant*> ProtocolElements; |
| 2005 | for (llvm::StringMapIterator<llvm::Constant*> iter = |
| 2006 | ExistingProtocols.begin(), endIter = ExistingProtocols.end(); |
| 2007 | iter != endIter ; iter++) { |
| 2008 | llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(iter->getValue(), |
| 2009 | PtrTy); |
| 2010 | ProtocolElements.push_back(Ptr); |
| 2011 | } |
| 2012 | llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy, |
| 2013 | ProtocolElements); |
| 2014 | ProtocolElements.clear(); |
| 2015 | ProtocolElements.push_back(NULLPtr); |
| 2016 | ProtocolElements.push_back(llvm::ConstantInt::get(LongTy, |
| 2017 | ExistingProtocols.size())); |
| 2018 | ProtocolElements.push_back(ProtocolArray); |
| 2019 | Elements.push_back(llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolListTy, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2020 | ProtocolElements, CGM.getPointerAlign(), |
| 2021 | ".objc_protocol_list"), PtrTy)); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2022 | Categories.push_back(llvm::ConstantExpr::getBitCast( |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2023 | MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2024 | PtrTy, PtrTy, PtrTy, nullptr), Elements, CGM.getPointerAlign()), |
| 2025 | PtrTy)); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2026 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2027 | |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 2028 | /// Libobjc2 uses a bitfield representation where small(ish) bitfields are |
| 2029 | /// stored in a 64-bit value with the low bit set to 1 and the remaining 63 |
| 2030 | /// bits set to their values, LSB first, while larger ones are stored in a |
| 2031 | /// structure of this / form: |
| 2032 | /// |
| 2033 | /// struct { int32_t length; int32_t values[length]; }; |
| 2034 | /// |
| 2035 | /// The values in the array are stored in host-endian format, with the least |
| 2036 | /// significant bit being assumed to come first in the bitfield. Therefore, a |
| 2037 | /// bitfield with the 64th bit set will be (int64_t)&{ 2, [0, 1<<31] }, while a |
| 2038 | /// bitfield / with the 63rd bit set will be 1<<64. |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 2039 | llvm::Constant *CGObjCGNU::MakeBitField(ArrayRef<bool> bits) { |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 2040 | int bitCount = bits.size(); |
Rafael Espindola | 3cc5c2d | 2014-01-09 21:32:51 +0000 | [diff] [blame] | 2041 | int ptrBits = CGM.getDataLayout().getPointerSizeInBits(); |
David Chisnall | e89ac06 | 2011-10-25 10:12:21 +0000 | [diff] [blame] | 2042 | if (bitCount < ptrBits) { |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 2043 | uint64_t val = 1; |
| 2044 | for (int i=0 ; i<bitCount ; ++i) { |
Eli Friedman | 2352667 | 2011-10-08 01:03:47 +0000 | [diff] [blame] | 2045 | if (bits[i]) val |= 1ULL<<(i+1); |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 2046 | } |
David Chisnall | e89ac06 | 2011-10-25 10:12:21 +0000 | [diff] [blame] | 2047 | return llvm::ConstantInt::get(IntPtrTy, val); |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 2048 | } |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2049 | SmallVector<llvm::Constant *, 8> values; |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 2050 | int v=0; |
| 2051 | while (v < bitCount) { |
| 2052 | int32_t word = 0; |
| 2053 | for (int i=0 ; (i<32) && (v<bitCount) ; ++i) { |
| 2054 | if (bits[v]) word |= 1<<i; |
| 2055 | v++; |
| 2056 | } |
| 2057 | values.push_back(llvm::ConstantInt::get(Int32Ty, word)); |
| 2058 | } |
| 2059 | llvm::ArrayType *arrayTy = llvm::ArrayType::get(Int32Ty, values.size()); |
| 2060 | llvm::Constant *array = llvm::ConstantArray::get(arrayTy, values); |
| 2061 | llvm::Constant *fields[2] = { |
| 2062 | llvm::ConstantInt::get(Int32Ty, values.size()), |
| 2063 | array }; |
| 2064 | llvm::Constant *GS = MakeGlobal(llvm::StructType::get(Int32Ty, arrayTy, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2065 | nullptr), fields, CharUnits::fromQuantity(4)); |
David Chisnall | e0dc7cb | 2011-10-08 08:54:36 +0000 | [diff] [blame] | 2066 | llvm::Constant *ptr = llvm::ConstantExpr::getPtrToInt(GS, IntPtrTy); |
David Chisnall | e0dc7cb | 2011-10-08 08:54:36 +0000 | [diff] [blame] | 2067 | return ptr; |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 2068 | } |
| 2069 | |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2070 | void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2071 | std::string ClassName = OCD->getClassInterface()->getNameAsString(); |
| 2072 | std::string CategoryName = OCD->getNameAsString(); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2073 | // Collect information about instance methods |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2074 | SmallVector<Selector, 16> InstanceMethodSels; |
| 2075 | SmallVector<llvm::Constant*, 16> InstanceMethodTypes; |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2076 | for (const auto *I : OCD->instance_methods()) { |
| 2077 | InstanceMethodSels.push_back(I->getSelector()); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2078 | std::string TypeStr; |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2079 | CGM.getContext().getObjCEncodingForMethodDecl(I,TypeStr); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2080 | InstanceMethodTypes.push_back(MakeConstantString(TypeStr)); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2081 | } |
| 2082 | |
| 2083 | // Collect information about class methods |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2084 | SmallVector<Selector, 16> ClassMethodSels; |
| 2085 | SmallVector<llvm::Constant*, 16> ClassMethodTypes; |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2086 | for (const auto *I : OCD->class_methods()) { |
| 2087 | ClassMethodSels.push_back(I->getSelector()); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2088 | std::string TypeStr; |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2089 | CGM.getContext().getObjCEncodingForMethodDecl(I,TypeStr); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2090 | ClassMethodTypes.push_back(MakeConstantString(TypeStr)); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | // Collect the names of referenced protocols |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2094 | SmallVector<std::string, 16> Protocols; |
David Chisnall | 2bfc50b | 2010-03-13 22:20:45 +0000 | [diff] [blame] | 2095 | const ObjCCategoryDecl *CatDecl = OCD->getCategoryDecl(); |
| 2096 | const ObjCList<ObjCProtocolDecl> &Protos = CatDecl->getReferencedProtocols(); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2097 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(), |
| 2098 | E = Protos.end(); I != E; ++I) |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2099 | Protocols.push_back((*I)->getNameAsString()); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2100 | |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 2101 | llvm::Constant *Elements[] = { |
| 2102 | MakeConstantString(CategoryName), MakeConstantString(ClassName), |
| 2103 | // Instance method list |
| 2104 | llvm::ConstantExpr::getBitCast( |
| 2105 | GenerateMethodList(ClassName, CategoryName, InstanceMethodSels, |
| 2106 | InstanceMethodTypes, false), |
| 2107 | PtrTy), |
| 2108 | // Class method list |
| 2109 | llvm::ConstantExpr::getBitCast(GenerateMethodList(ClassName, CategoryName, |
| 2110 | ClassMethodSels, |
| 2111 | ClassMethodTypes, true), |
| 2112 | PtrTy), |
| 2113 | // Protocol list |
| 2114 | llvm::ConstantExpr::getBitCast(GenerateProtocolList(Protocols), PtrTy)}; |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2115 | Categories.push_back(llvm::ConstantExpr::getBitCast( |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2116 | MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2117 | PtrTy, PtrTy, PtrTy, nullptr), Elements, CGM.getPointerAlign()), |
| 2118 | PtrTy)); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2119 | } |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2120 | |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2121 | llvm::Constant *CGObjCGNU::GeneratePropertyList(const ObjCImplementationDecl *OID, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2122 | SmallVectorImpl<Selector> &InstanceMethodSels, |
| 2123 | SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes) { |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2124 | ASTContext &Context = CGM.getContext(); |
David Chisnall | beb8013 | 2013-02-28 13:59:29 +0000 | [diff] [blame] | 2125 | // Property metadata: name, attributes, attributes2, padding1, padding2, |
| 2126 | // setter name, setter types, getter name, getter types. |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2127 | llvm::StructType *PropertyMetadataTy = llvm::StructType::get( |
David Chisnall | beb8013 | 2013-02-28 13:59:29 +0000 | [diff] [blame] | 2128 | PtrToInt8Ty, Int8Ty, Int8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2129 | PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty, nullptr); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2130 | std::vector<llvm::Constant*> Properties; |
| 2131 | |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2132 | // Add all of the property methods need adding to the method list and to the |
| 2133 | // property metadata list. |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 2134 | for (auto *propertyImpl : OID->property_impls()) { |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2135 | std::vector<llvm::Constant*> Fields; |
Aaron Ballman | d85eff4 | 2014-03-14 15:02:45 +0000 | [diff] [blame] | 2136 | ObjCPropertyDecl *property = propertyImpl->getPropertyDecl(); |
David Chisnall | 36c6320 | 2010-02-26 01:11:38 +0000 | [diff] [blame] | 2137 | bool isSynthesized = (propertyImpl->getPropertyImplementation() == |
| 2138 | ObjCPropertyImplDecl::Synthesize); |
David Chisnall | beb8013 | 2013-02-28 13:59:29 +0000 | [diff] [blame] | 2139 | bool isDynamic = (propertyImpl->getPropertyImplementation() == |
| 2140 | ObjCPropertyImplDecl::Dynamic); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2141 | |
David Chisnall | a5f5941 | 2012-10-16 15:11:55 +0000 | [diff] [blame] | 2142 | Fields.push_back(MakePropertyEncodingString(property, OID)); |
David Chisnall | beb8013 | 2013-02-28 13:59:29 +0000 | [diff] [blame] | 2143 | PushPropertyAttributes(Fields, property, isSynthesized, isDynamic); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2144 | if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) { |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2145 | std::string TypeStr; |
| 2146 | Context.getObjCEncodingForMethodDecl(getter,TypeStr); |
| 2147 | llvm::Constant *TypeEncoding = MakeConstantString(TypeStr); |
David Chisnall | 36c6320 | 2010-02-26 01:11:38 +0000 | [diff] [blame] | 2148 | if (isSynthesized) { |
| 2149 | InstanceMethodTypes.push_back(TypeEncoding); |
| 2150 | InstanceMethodSels.push_back(getter->getSelector()); |
| 2151 | } |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2152 | Fields.push_back(MakeConstantString(getter->getSelector().getAsString())); |
| 2153 | Fields.push_back(TypeEncoding); |
| 2154 | } else { |
| 2155 | Fields.push_back(NULLPtr); |
| 2156 | Fields.push_back(NULLPtr); |
| 2157 | } |
| 2158 | if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) { |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2159 | std::string TypeStr; |
| 2160 | Context.getObjCEncodingForMethodDecl(setter,TypeStr); |
| 2161 | llvm::Constant *TypeEncoding = MakeConstantString(TypeStr); |
David Chisnall | 36c6320 | 2010-02-26 01:11:38 +0000 | [diff] [blame] | 2162 | if (isSynthesized) { |
| 2163 | InstanceMethodTypes.push_back(TypeEncoding); |
| 2164 | InstanceMethodSels.push_back(setter->getSelector()); |
| 2165 | } |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2166 | Fields.push_back(MakeConstantString(setter->getSelector().getAsString())); |
| 2167 | Fields.push_back(TypeEncoding); |
| 2168 | } else { |
| 2169 | Fields.push_back(NULLPtr); |
| 2170 | Fields.push_back(NULLPtr); |
| 2171 | } |
| 2172 | Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields)); |
| 2173 | } |
| 2174 | llvm::ArrayType *PropertyArrayTy = |
| 2175 | llvm::ArrayType::get(PropertyMetadataTy, Properties.size()); |
| 2176 | llvm::Constant *PropertyArray = llvm::ConstantArray::get(PropertyArrayTy, |
| 2177 | Properties); |
| 2178 | llvm::Constant* PropertyListInitFields[] = |
| 2179 | {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray}; |
| 2180 | |
| 2181 | llvm::Constant *PropertyListInit = |
Chris Lattner | e64d7ba | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2182 | llvm::ConstantStruct::getAnon(PropertyListInitFields); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2183 | return new llvm::GlobalVariable(TheModule, PropertyListInit->getType(), false, |
| 2184 | llvm::GlobalValue::InternalLinkage, PropertyListInit, |
| 2185 | ".objc_property_list"); |
| 2186 | } |
| 2187 | |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 2188 | void CGObjCGNU::RegisterAlias(const ObjCCompatibleAliasDecl *OAD) { |
| 2189 | // Get the class declaration for which the alias is specified. |
| 2190 | ObjCInterfaceDecl *ClassDecl = |
| 2191 | const_cast<ObjCInterfaceDecl *>(OAD->getClassInterface()); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 2192 | ClassAliases.emplace_back(ClassDecl->getNameAsString(), |
| 2193 | OAD->getNameAsString()); |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 2194 | } |
| 2195 | |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2196 | void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) { |
| 2197 | ASTContext &Context = CGM.getContext(); |
| 2198 | |
| 2199 | // Get the superclass name. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2200 | const ObjCInterfaceDecl * SuperClassDecl = |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2201 | OID->getClassInterface()->getSuperClass(); |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2202 | std::string SuperClassName; |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 2203 | if (SuperClassDecl) { |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2204 | SuperClassName = SuperClassDecl->getNameAsString(); |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 2205 | EmitClassRef(SuperClassName); |
| 2206 | } |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2207 | |
| 2208 | // Get the class name |
Chris Lattner | 87bc387 | 2009-04-01 02:00:48 +0000 | [diff] [blame] | 2209 | ObjCInterfaceDecl *ClassDecl = |
Saleem Abdulrasool | a088ad9 | 2016-07-17 22:27:41 +0000 | [diff] [blame] | 2210 | const_cast<ObjCInterfaceDecl *>(OID->getClassInterface()); |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2211 | std::string ClassName = ClassDecl->getNameAsString(); |
Saleem Abdulrasool | a088ad9 | 2016-07-17 22:27:41 +0000 | [diff] [blame] | 2212 | |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 2213 | // Emit the symbol that is used to generate linker errors if this class is |
| 2214 | // referenced in other modules but not declared. |
Fariborz Jahanian | bacbed9 | 2009-07-03 15:10:14 +0000 | [diff] [blame] | 2215 | std::string classSymbolName = "__objc_class_name_" + ClassName; |
Saleem Abdulrasool | a088ad9 | 2016-07-17 22:27:41 +0000 | [diff] [blame] | 2216 | if (auto *symbol = TheModule.getGlobalVariable(classSymbolName)) { |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2217 | symbol->setInitializer(llvm::ConstantInt::get(LongTy, 0)); |
Fariborz Jahanian | bacbed9 | 2009-07-03 15:10:14 +0000 | [diff] [blame] | 2218 | } else { |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2219 | new llvm::GlobalVariable(TheModule, LongTy, false, |
Saleem Abdulrasool | a088ad9 | 2016-07-17 22:27:41 +0000 | [diff] [blame] | 2220 | llvm::GlobalValue::ExternalLinkage, |
| 2221 | llvm::ConstantInt::get(LongTy, 0), |
| 2222 | classSymbolName); |
Fariborz Jahanian | bacbed9 | 2009-07-03 15:10:14 +0000 | [diff] [blame] | 2223 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2224 | |
Daniel Dunbar | 12119b9 | 2009-05-03 10:46:44 +0000 | [diff] [blame] | 2225 | // Get the size of instances. |
Ken Dyck | c8ae550 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 2226 | int instanceSize = |
| 2227 | Context.getASTObjCImplementationLayout(OID).getSize().getQuantity(); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2228 | |
| 2229 | // Collect information about instance variables. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2230 | SmallVector<llvm::Constant*, 16> IvarNames; |
| 2231 | SmallVector<llvm::Constant*, 16> IvarTypes; |
| 2232 | SmallVector<llvm::Constant*, 16> IvarOffsets; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2233 | |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2234 | std::vector<llvm::Constant*> IvarOffsetValues; |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 2235 | SmallVector<bool, 16> WeakIvars; |
| 2236 | SmallVector<bool, 16> StrongIvars; |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2237 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2238 | int superInstanceSize = !SuperClassDecl ? 0 : |
Ken Dyck | c8ae550 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 2239 | Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize().getQuantity(); |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2240 | // For non-fragile ivars, set the instance size to 0 - {the size of just this |
| 2241 | // class}. The runtime will then set this to the correct value on load. |
Richard Smith | 9c6890a | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 2242 | if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2243 | instanceSize = 0 - (instanceSize - superInstanceSize); |
| 2244 | } |
David Chisnall | 18cf737 | 2010-04-19 00:45:34 +0000 | [diff] [blame] | 2245 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2246 | for (const ObjCIvarDecl *IVD = ClassDecl->all_declared_ivar_begin(); IVD; |
| 2247 | IVD = IVD->getNextIvar()) { |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2248 | // Store the name |
David Chisnall | 18cf737 | 2010-04-19 00:45:34 +0000 | [diff] [blame] | 2249 | IvarNames.push_back(MakeConstantString(IVD->getNameAsString())); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2250 | // Get the type encoding for this ivar |
| 2251 | std::string TypeStr; |
David Chisnall | 18cf737 | 2010-04-19 00:45:34 +0000 | [diff] [blame] | 2252 | Context.getObjCEncodingForType(IVD->getType(), TypeStr); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2253 | IvarTypes.push_back(MakeConstantString(TypeStr)); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2254 | // Get the offset |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 2255 | uint64_t BaseOffset = ComputeIvarBaseOffset(CGM, OID, IVD); |
David Chisnall | cb1b7bf | 2009-11-17 19:32:15 +0000 | [diff] [blame] | 2256 | uint64_t Offset = BaseOffset; |
Richard Smith | 9c6890a | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 2257 | if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2258 | Offset = BaseOffset - superInstanceSize; |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2259 | } |
David Chisnall | 1bfe6d3 | 2011-07-07 12:34:51 +0000 | [diff] [blame] | 2260 | llvm::Constant *OffsetValue = llvm::ConstantInt::get(IntTy, Offset); |
| 2261 | // Create the direct offset value |
| 2262 | std::string OffsetName = "__objc_ivar_offset_value_" + ClassName +"." + |
| 2263 | IVD->getNameAsString(); |
| 2264 | llvm::GlobalVariable *OffsetVar = TheModule.getGlobalVariable(OffsetName); |
| 2265 | if (OffsetVar) { |
| 2266 | OffsetVar->setInitializer(OffsetValue); |
| 2267 | // If this is the real definition, change its linkage type so that |
| 2268 | // different modules will use this one, rather than their private |
| 2269 | // copy. |
| 2270 | OffsetVar->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 2271 | } else |
| 2272 | OffsetVar = new llvm::GlobalVariable(TheModule, IntTy, |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2273 | false, llvm::GlobalValue::ExternalLinkage, |
David Chisnall | 1bfe6d3 | 2011-07-07 12:34:51 +0000 | [diff] [blame] | 2274 | OffsetValue, |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2275 | "__objc_ivar_offset_value_" + ClassName +"." + |
David Chisnall | 1bfe6d3 | 2011-07-07 12:34:51 +0000 | [diff] [blame] | 2276 | IVD->getNameAsString()); |
| 2277 | IvarOffsets.push_back(OffsetValue); |
| 2278 | IvarOffsetValues.push_back(OffsetVar); |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 2279 | Qualifiers::ObjCLifetime lt = IVD->getType().getQualifiers().getObjCLifetime(); |
| 2280 | switch (lt) { |
| 2281 | case Qualifiers::OCL_Strong: |
| 2282 | StrongIvars.push_back(true); |
| 2283 | WeakIvars.push_back(false); |
| 2284 | break; |
| 2285 | case Qualifiers::OCL_Weak: |
| 2286 | StrongIvars.push_back(false); |
| 2287 | WeakIvars.push_back(true); |
| 2288 | break; |
| 2289 | default: |
| 2290 | StrongIvars.push_back(false); |
| 2291 | WeakIvars.push_back(false); |
| 2292 | } |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2293 | } |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 2294 | llvm::Constant *StrongIvarBitmap = MakeBitField(StrongIvars); |
| 2295 | llvm::Constant *WeakIvarBitmap = MakeBitField(WeakIvars); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2296 | llvm::GlobalVariable *IvarOffsetArray = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2297 | MakeGlobalArray(PtrToIntTy, IvarOffsetValues, CGM.getPointerAlign(), |
| 2298 | ".ivar.offsets"); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2299 | |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2300 | // Collect information about instance methods |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2301 | SmallVector<Selector, 16> InstanceMethodSels; |
| 2302 | SmallVector<llvm::Constant*, 16> InstanceMethodTypes; |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2303 | for (const auto *I : OID->instance_methods()) { |
| 2304 | InstanceMethodSels.push_back(I->getSelector()); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2305 | std::string TypeStr; |
Aaron Ballman | f26acce | 2014-03-13 19:50:17 +0000 | [diff] [blame] | 2306 | Context.getObjCEncodingForMethodDecl(I,TypeStr); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2307 | InstanceMethodTypes.push_back(MakeConstantString(TypeStr)); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2308 | } |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2309 | |
| 2310 | llvm::Constant *Properties = GeneratePropertyList(OID, InstanceMethodSels, |
| 2311 | InstanceMethodTypes); |
| 2312 | |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2313 | // Collect information about class methods |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2314 | SmallVector<Selector, 16> ClassMethodSels; |
| 2315 | SmallVector<llvm::Constant*, 16> ClassMethodTypes; |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2316 | for (const auto *I : OID->class_methods()) { |
| 2317 | ClassMethodSels.push_back(I->getSelector()); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2318 | std::string TypeStr; |
Aaron Ballman | e8a7dc9 | 2014-03-13 20:11:06 +0000 | [diff] [blame] | 2319 | Context.getObjCEncodingForMethodDecl(I,TypeStr); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2320 | ClassMethodTypes.push_back(MakeConstantString(TypeStr)); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2321 | } |
| 2322 | // Collect the names of referenced protocols |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2323 | SmallVector<std::string, 16> Protocols; |
Aaron Ballman | a49c506 | 2014-03-13 20:29:09 +0000 | [diff] [blame] | 2324 | for (const auto *I : ClassDecl->protocols()) |
| 2325 | Protocols.push_back(I->getNameAsString()); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2326 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2327 | // Get the superclass pointer. |
| 2328 | llvm::Constant *SuperClass; |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2329 | if (!SuperClassName.empty()) { |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2330 | SuperClass = MakeConstantString(SuperClassName, ".super_class_name"); |
| 2331 | } else { |
Owen Anderson | 7ec07a5 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 2332 | SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2333 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2334 | // Empty vector used to construct empty method lists |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2335 | SmallVector<llvm::Constant*, 1> empty; |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2336 | // Generate the method and instance variable lists |
| 2337 | llvm::Constant *MethodList = GenerateMethodList(ClassName, "", |
Chris Lattner | bf231a6 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 2338 | InstanceMethodSels, InstanceMethodTypes, false); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2339 | llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "", |
Chris Lattner | bf231a6 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 2340 | ClassMethodSels, ClassMethodTypes, true); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2341 | llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes, |
| 2342 | IvarOffsets); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2343 | // 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] | 2344 | // we emit a symbol containing the offset for each ivar in the class. This |
| 2345 | // allows code compiled for the non-Fragile ABI to inherit from code compiled |
| 2346 | // for the legacy ABI, without causing problems. The converse is also |
| 2347 | // possible, but causes all ivar accesses to be fragile. |
David Chisnall | e8431a7 | 2010-11-03 16:12:44 +0000 | [diff] [blame] | 2348 | |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2349 | // Offset pointer for getting at the correct field in the ivar list when |
| 2350 | // setting up the alias. These are: The base address for the global, the |
| 2351 | // ivar array (second field), the ivar in this list (set for each ivar), and |
| 2352 | // the offset (third field in ivar structure) |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 2353 | llvm::Type *IndexTy = Int32Ty; |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2354 | llvm::Constant *offsetPointerIndexes[] = {Zeros[0], |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2355 | llvm::ConstantInt::get(IndexTy, 1), nullptr, |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2356 | llvm::ConstantInt::get(IndexTy, 2) }; |
| 2357 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2358 | unsigned ivarIndex = 0; |
| 2359 | for (const ObjCIvarDecl *IVD = ClassDecl->all_declared_ivar_begin(); IVD; |
| 2360 | IVD = IVD->getNextIvar()) { |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2361 | const std::string Name = "__objc_ivar_offset_" + ClassName + '.' |
David Chisnall | e8431a7 | 2010-11-03 16:12:44 +0000 | [diff] [blame] | 2362 | + IVD->getNameAsString(); |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2363 | offsetPointerIndexes[2] = llvm::ConstantInt::get(IndexTy, ivarIndex); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2364 | // Get the correct ivar field |
| 2365 | llvm::Constant *offsetValue = llvm::ConstantExpr::getGetElementPtr( |
David Blaikie | e3b172a | 2015-04-02 18:55:21 +0000 | [diff] [blame] | 2366 | cast<llvm::GlobalVariable>(IvarList)->getValueType(), IvarList, |
| 2367 | offsetPointerIndexes); |
David Chisnall | e8431a7 | 2010-11-03 16:12:44 +0000 | [diff] [blame] | 2368 | // Get the existing variable, if one exists. |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2369 | llvm::GlobalVariable *offset = TheModule.getNamedGlobal(Name); |
| 2370 | if (offset) { |
Ted Kremenek | 669669f | 2012-04-04 00:55:25 +0000 | [diff] [blame] | 2371 | offset->setInitializer(offsetValue); |
| 2372 | // If this is the real definition, change its linkage type so that |
| 2373 | // different modules will use this one, rather than their private |
| 2374 | // copy. |
| 2375 | offset->setLinkage(llvm::GlobalValue::ExternalLinkage); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2376 | } else { |
Ted Kremenek | 669669f | 2012-04-04 00:55:25 +0000 | [diff] [blame] | 2377 | // Add a new alias if there isn't one already. |
| 2378 | offset = new llvm::GlobalVariable(TheModule, offsetValue->getType(), |
| 2379 | false, llvm::GlobalValue::ExternalLinkage, offsetValue, Name); |
| 2380 | (void) offset; // Silence dead store warning. |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2381 | } |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2382 | ++ivarIndex; |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2383 | } |
David Chisnall | e89ac06 | 2011-10-25 10:12:21 +0000 | [diff] [blame] | 2384 | llvm::Constant *ZeroPtr = llvm::ConstantInt::get(IntPtrTy, 0); |
Saleem Abdulrasool | a088ad9 | 2016-07-17 22:27:41 +0000 | [diff] [blame] | 2385 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2386 | //Generate metaclass for class methods |
Saleem Abdulrasool | a088ad9 | 2016-07-17 22:27:41 +0000 | [diff] [blame] | 2387 | llvm::Constant *MetaClassStruct = GenerateClassStructure( |
| 2388 | NULLPtr, NULLPtr, 0x12L, ClassName.c_str(), nullptr, Zeros[0], |
| 2389 | GenerateIvarList(empty, empty, empty), ClassMethodList, NULLPtr, NULLPtr, |
| 2390 | NULLPtr, ZeroPtr, ZeroPtr, true); |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 2391 | if (CGM.getTriple().isOSBinFormatCOFF()) { |
| 2392 | auto Storage = llvm::GlobalValue::DefaultStorageClass; |
| 2393 | if (OID->getClassInterface()->hasAttr<DLLImportAttr>()) |
| 2394 | Storage = llvm::GlobalValue::DLLImportStorageClass; |
| 2395 | else if (OID->getClassInterface()->hasAttr<DLLExportAttr>()) |
| 2396 | Storage = llvm::GlobalValue::DLLExportStorageClass; |
| 2397 | cast<llvm::GlobalValue>(MetaClassStruct)->setDLLStorageClass(Storage); |
| 2398 | } |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 2399 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2400 | // Generate the class structure |
Saleem Abdulrasool | a088ad9 | 2016-07-17 22:27:41 +0000 | [diff] [blame] | 2401 | llvm::Constant *ClassStruct = GenerateClassStructure( |
| 2402 | MetaClassStruct, SuperClass, 0x11L, ClassName.c_str(), nullptr, |
| 2403 | llvm::ConstantInt::get(LongTy, instanceSize), IvarList, MethodList, |
| 2404 | GenerateProtocolList(Protocols), IvarOffsetArray, Properties, |
| 2405 | StrongIvarBitmap, WeakIvarBitmap); |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 2406 | if (CGM.getTriple().isOSBinFormatCOFF()) { |
| 2407 | auto Storage = llvm::GlobalValue::DefaultStorageClass; |
| 2408 | if (OID->getClassInterface()->hasAttr<DLLImportAttr>()) |
| 2409 | Storage = llvm::GlobalValue::DLLImportStorageClass; |
| 2410 | else if (OID->getClassInterface()->hasAttr<DLLExportAttr>()) |
| 2411 | Storage = llvm::GlobalValue::DLLExportStorageClass; |
| 2412 | cast<llvm::GlobalValue>(ClassStruct)->setDLLStorageClass(Storage); |
| 2413 | } |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 2414 | |
| 2415 | // Resolve the class aliases, if they exist. |
| 2416 | if (ClassPtrAlias) { |
David Chisnall | 82f755c | 2010-11-09 11:21:43 +0000 | [diff] [blame] | 2417 | ClassPtrAlias->replaceAllUsesWith( |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2418 | llvm::ConstantExpr::getBitCast(ClassStruct, IdTy)); |
David Chisnall | 82f755c | 2010-11-09 11:21:43 +0000 | [diff] [blame] | 2419 | ClassPtrAlias->eraseFromParent(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2420 | ClassPtrAlias = nullptr; |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 2421 | } |
| 2422 | if (MetaClassPtrAlias) { |
David Chisnall | 82f755c | 2010-11-09 11:21:43 +0000 | [diff] [blame] | 2423 | MetaClassPtrAlias->replaceAllUsesWith( |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2424 | llvm::ConstantExpr::getBitCast(MetaClassStruct, IdTy)); |
David Chisnall | 82f755c | 2010-11-09 11:21:43 +0000 | [diff] [blame] | 2425 | MetaClassPtrAlias->eraseFromParent(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2426 | MetaClassPtrAlias = nullptr; |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 2427 | } |
| 2428 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2429 | // Add class structure to list to be added to the symtab later |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2430 | ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2431 | Classes.push_back(ClassStruct); |
| 2432 | } |
| 2433 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2434 | llvm::Function *CGObjCGNU::ModuleInitFunction() { |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2435 | // Only emit an ObjC load function if no Objective-C stuff has been called |
| 2436 | if (Classes.empty() && Categories.empty() && ConstantStrings.empty() && |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2437 | ExistingProtocols.empty() && SelectorTable.empty()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2438 | return nullptr; |
Eli Friedman | 412c668 | 2008-06-01 16:00:02 +0000 | [diff] [blame] | 2439 | |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2440 | // Add all referenced protocols to a category. |
| 2441 | GenerateProtocolHolderCategory(); |
| 2442 | |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2443 | llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>( |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 2444 | SelectorTy->getElementType()); |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2445 | llvm::Type *SelStructPtrTy = SelectorTy; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2446 | if (!SelStructTy) { |
| 2447 | SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, nullptr); |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2448 | SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy); |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2451 | std::vector<llvm::Constant*> Elements; |
Chris Lattner | c06ce0f | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 2452 | llvm::Constant *Statics = NULLPtr; |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2453 | // Generate statics list: |
Alexander Kornienko | 6ee521c | 2015-01-23 15:36:10 +0000 | [diff] [blame] | 2454 | if (!ConstantStrings.empty()) { |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2455 | llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty, |
Chris Lattner | c06ce0f | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 2456 | ConstantStrings.size() + 1); |
| 2457 | ConstantStrings.push_back(NULLPtr); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2458 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2459 | StringRef StringClass = CGM.getLangOpts().ObjCConstantStringClass; |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2460 | |
Daniel Dunbar | 75fa84e | 2009-11-29 02:38:47 +0000 | [diff] [blame] | 2461 | if (StringClass.empty()) StringClass = "NXConstantString"; |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2462 | |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2463 | Elements.push_back(MakeConstantString(StringClass, |
| 2464 | ".objc_static_class_name")); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2465 | Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, |
Chris Lattner | c06ce0f | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 2466 | ConstantStrings)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2467 | llvm::StructType *StaticsListTy = |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2468 | llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, nullptr); |
Owen Anderson | 170229f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2469 | llvm::Type *StaticsListPtrTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2470 | llvm::PointerType::getUnqual(StaticsListTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2471 | Statics = MakeGlobal(StaticsListTy, Elements, CGM.getPointerAlign(), |
| 2472 | ".objc_statics"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2473 | llvm::ArrayType *StaticsListArrayTy = |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2474 | llvm::ArrayType::get(StaticsListPtrTy, 2); |
Chris Lattner | c06ce0f | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 2475 | Elements.clear(); |
| 2476 | Elements.push_back(Statics); |
Owen Anderson | 0b75f23 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2477 | Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy)); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2478 | Statics = MakeGlobal(StaticsListArrayTy, Elements, |
| 2479 | CGM.getPointerAlign(), ".objc_statics_ptr"); |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2480 | Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy); |
Chris Lattner | c06ce0f | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 2481 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2482 | // Array of classes, categories, and constant objects |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2483 | llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2484 | Classes.size() + Categories.size() + 2); |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2485 | llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy, |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2486 | llvm::Type::getInt16Ty(VMContext), |
| 2487 | llvm::Type::getInt16Ty(VMContext), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2488 | ClassListTy, nullptr); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2489 | |
| 2490 | Elements.clear(); |
| 2491 | // Pointer to an array of selectors used in this module. |
| 2492 | std::vector<llvm::Constant*> Selectors; |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2493 | std::vector<llvm::GlobalAlias*> SelectorAliases; |
| 2494 | for (SelectorMap::iterator iter = SelectorTable.begin(), |
| 2495 | iterEnd = SelectorTable.end(); iter != iterEnd ; ++iter) { |
| 2496 | |
| 2497 | std::string SelNameStr = iter->first.getAsString(); |
| 2498 | llvm::Constant *SelName = ExportUniqueString(SelNameStr, ".objc_sel_name"); |
| 2499 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2500 | SmallVectorImpl<TypedSelector> &Types = iter->second; |
| 2501 | for (SmallVectorImpl<TypedSelector>::iterator i = Types.begin(), |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2502 | e = Types.end() ; i!=e ; i++) { |
| 2503 | |
| 2504 | llvm::Constant *SelectorTypeEncoding = NULLPtr; |
| 2505 | if (!i->first.empty()) |
| 2506 | SelectorTypeEncoding = MakeConstantString(i->first, ".objc_sel_types"); |
| 2507 | |
| 2508 | Elements.push_back(SelName); |
| 2509 | Elements.push_back(SelectorTypeEncoding); |
| 2510 | Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements)); |
| 2511 | Elements.clear(); |
| 2512 | |
| 2513 | // Store the selector alias for later replacement |
| 2514 | SelectorAliases.push_back(i->second); |
| 2515 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2516 | } |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2517 | unsigned SelectorCount = Selectors.size(); |
| 2518 | // NULL-terminate the selector list. This should not actually be required, |
| 2519 | // because the selector list has a length field. Unfortunately, the GCC |
| 2520 | // runtime decides to ignore the length field and expects a NULL terminator, |
| 2521 | // and GCC cooperates with this by always setting the length to 0. |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2522 | Elements.push_back(NULLPtr); |
| 2523 | Elements.push_back(NULLPtr); |
Owen Anderson | 0e0189d | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2524 | Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements)); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2525 | Elements.clear(); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2526 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2527 | // Number of static selectors |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2528 | Elements.push_back(llvm::ConstantInt::get(LongTy, SelectorCount)); |
David Blaikie | e3b172a | 2015-04-02 18:55:21 +0000 | [diff] [blame] | 2529 | llvm::GlobalVariable *SelectorList = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2530 | MakeGlobalArray(SelStructTy, Selectors, CGM.getPointerAlign(), |
| 2531 | ".objc_selector_list"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2532 | Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList, |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 2533 | SelStructPtrTy)); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2534 | |
| 2535 | // Now that all of the static selectors exist, create pointers to them. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2536 | for (unsigned int i=0 ; i<SelectorCount ; i++) { |
| 2537 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2538 | llvm::Constant *Idxs[] = {Zeros[0], |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 2539 | llvm::ConstantInt::get(Int32Ty, i), Zeros[0]}; |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2540 | // FIXME: We're generating redundant loads and stores here! |
David Blaikie | e3b172a | 2015-04-02 18:55:21 +0000 | [diff] [blame] | 2541 | llvm::Constant *SelPtr = llvm::ConstantExpr::getGetElementPtr( |
| 2542 | SelectorList->getValueType(), SelectorList, makeArrayRef(Idxs, 2)); |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 2543 | // If selectors are defined as an opaque type, cast the pointer to this |
| 2544 | // type. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 2545 | SelPtr = llvm::ConstantExpr::getBitCast(SelPtr, SelectorTy); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2546 | SelectorAliases[i]->replaceAllUsesWith(SelPtr); |
| 2547 | SelectorAliases[i]->eraseFromParent(); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2548 | } |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2549 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2550 | // Number of classes defined. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2551 | Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext), |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2552 | Classes.size())); |
| 2553 | // Number of categories defined |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2554 | Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext), |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2555 | Categories.size())); |
| 2556 | // Create an array of classes, then categories, then static object instances |
| 2557 | Classes.insert(Classes.end(), Categories.begin(), Categories.end()); |
| 2558 | // NULL-terminated list of static object instances (mainly constant strings) |
| 2559 | Classes.push_back(Statics); |
| 2560 | Classes.push_back(NULLPtr); |
Owen Anderson | 47034e1 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2561 | llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2562 | Elements.push_back(ClassList); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2563 | // Construct the symbol table |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2564 | llvm::Constant *SymTab = |
| 2565 | MakeGlobal(SymTabTy, Elements, CGM.getPointerAlign()); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2566 | |
| 2567 | // The symbol table is contained in a module which has some version-checking |
| 2568 | // constants |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2569 | llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy, |
David Chisnall | 5c51177 | 2011-05-22 22:37:08 +0000 | [diff] [blame] | 2570 | PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2571 | (RuntimeVersion >= 10) ? IntTy : nullptr, nullptr); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2572 | Elements.clear(); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2573 | // Runtime version, used for ABI compatibility checking. |
| 2574 | Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion)); |
Fariborz Jahanian | c2d5618 | 2009-04-01 19:49:42 +0000 | [diff] [blame] | 2575 | // sizeof(ModuleTy) |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2576 | llvm::DataLayout td(&TheModule); |
Ken Dyck | 0fed10e | 2011-04-22 17:59:22 +0000 | [diff] [blame] | 2577 | Elements.push_back( |
| 2578 | llvm::ConstantInt::get(LongTy, |
| 2579 | td.getTypeSizeInBits(ModuleTy) / |
| 2580 | CGM.getContext().getCharWidth())); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2581 | |
| 2582 | // The path to the source file where this module was declared |
| 2583 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 2584 | const FileEntry *mainFile = SM.getFileEntryForID(SM.getMainFileID()); |
| 2585 | std::string path = |
Mehdi Amini | 004b9c7 | 2016-10-10 22:52:47 +0000 | [diff] [blame] | 2586 | (Twine(mainFile->getDir()->getName()) + "/" + mainFile->getName()).str(); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2587 | Elements.push_back(MakeConstantString(path, ".objc_source_file_name")); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2588 | Elements.push_back(SymTab); |
David Chisnall | 5c51177 | 2011-05-22 22:37:08 +0000 | [diff] [blame] | 2589 | |
David Chisnall | a918b88 | 2011-07-07 11:22:31 +0000 | [diff] [blame] | 2590 | if (RuntimeVersion >= 10) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2591 | switch (CGM.getLangOpts().getGC()) { |
David Chisnall | a918b88 | 2011-07-07 11:22:31 +0000 | [diff] [blame] | 2592 | case LangOptions::GCOnly: |
David Chisnall | 5c51177 | 2011-05-22 22:37:08 +0000 | [diff] [blame] | 2593 | Elements.push_back(llvm::ConstantInt::get(IntTy, 2)); |
David Chisnall | 5c51177 | 2011-05-22 22:37:08 +0000 | [diff] [blame] | 2594 | break; |
David Chisnall | a918b88 | 2011-07-07 11:22:31 +0000 | [diff] [blame] | 2595 | case LangOptions::NonGC: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2596 | if (CGM.getLangOpts().ObjCAutoRefCount) |
David Chisnall | a918b88 | 2011-07-07 11:22:31 +0000 | [diff] [blame] | 2597 | Elements.push_back(llvm::ConstantInt::get(IntTy, 1)); |
| 2598 | else |
| 2599 | Elements.push_back(llvm::ConstantInt::get(IntTy, 0)); |
| 2600 | break; |
| 2601 | case LangOptions::HybridGC: |
| 2602 | Elements.push_back(llvm::ConstantInt::get(IntTy, 1)); |
| 2603 | break; |
| 2604 | } |
David Chisnall | 5c51177 | 2011-05-22 22:37:08 +0000 | [diff] [blame] | 2605 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2606 | llvm::Value *Module = MakeGlobal(ModuleTy, Elements, CGM.getPointerAlign()); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2607 | |
| 2608 | // Create the load function calling the runtime entry point with the module |
| 2609 | // structure |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2610 | llvm::Function * LoadFunction = llvm::Function::Create( |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2611 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false), |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2612 | llvm::GlobalValue::InternalLinkage, ".objc_load_function", |
| 2613 | &TheModule); |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2614 | llvm::BasicBlock *EntryBB = |
| 2615 | llvm::BasicBlock::Create(VMContext, "entry", LoadFunction); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2616 | CGBuilderTy Builder(CGM, VMContext); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2617 | Builder.SetInsertPoint(EntryBB); |
Fariborz Jahanian | 3b636c1 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 2618 | |
Benjamin Kramer | df1fb13 | 2011-05-28 14:26:31 +0000 | [diff] [blame] | 2619 | llvm::FunctionType *FT = |
Jay Foad | 5709f7c | 2011-07-29 13:56:53 +0000 | [diff] [blame] | 2620 | llvm::FunctionType::get(Builder.getVoidTy(), |
| 2621 | llvm::PointerType::getUnqual(ModuleTy), true); |
Benjamin Kramer | df1fb13 | 2011-05-28 14:26:31 +0000 | [diff] [blame] | 2622 | llvm::Value *Register = CGM.CreateRuntimeFunction(FT, "__objc_exec_class"); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2623 | Builder.CreateCall(Register, Module); |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 2624 | |
David Chisnall | af066bbb | 2012-02-01 19:16:56 +0000 | [diff] [blame] | 2625 | if (!ClassAliases.empty()) { |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 2626 | llvm::Type *ArgTypes[2] = {PtrTy, PtrToInt8Ty}; |
| 2627 | llvm::FunctionType *RegisterAliasTy = |
| 2628 | llvm::FunctionType::get(Builder.getVoidTy(), |
| 2629 | ArgTypes, false); |
| 2630 | llvm::Function *RegisterAlias = llvm::Function::Create( |
| 2631 | RegisterAliasTy, |
| 2632 | llvm::GlobalValue::ExternalWeakLinkage, "class_registerAlias_np", |
| 2633 | &TheModule); |
| 2634 | llvm::BasicBlock *AliasBB = |
| 2635 | llvm::BasicBlock::Create(VMContext, "alias", LoadFunction); |
| 2636 | llvm::BasicBlock *NoAliasBB = |
| 2637 | llvm::BasicBlock::Create(VMContext, "no_alias", LoadFunction); |
| 2638 | |
| 2639 | // Branch based on whether the runtime provided class_registerAlias_np() |
| 2640 | llvm::Value *HasRegisterAlias = Builder.CreateICmpNE(RegisterAlias, |
| 2641 | llvm::Constant::getNullValue(RegisterAlias->getType())); |
| 2642 | Builder.CreateCondBr(HasRegisterAlias, AliasBB, NoAliasBB); |
| 2643 | |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 2644 | // The true branch (has alias registration function): |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 2645 | Builder.SetInsertPoint(AliasBB); |
| 2646 | // Emit alias registration calls: |
| 2647 | for (std::vector<ClassAliasPair>::iterator iter = ClassAliases.begin(); |
| 2648 | iter != ClassAliases.end(); ++iter) { |
| 2649 | llvm::Constant *TheClass = |
| 2650 | TheModule.getGlobalVariable(("_OBJC_CLASS_" + iter->first).c_str(), |
| 2651 | true); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2652 | if (TheClass) { |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 2653 | TheClass = llvm::ConstantExpr::getBitCast(TheClass, PtrTy); |
David Blaikie | 43f9bb7 | 2015-05-18 22:14:03 +0000 | [diff] [blame] | 2654 | Builder.CreateCall(RegisterAlias, |
| 2655 | {TheClass, MakeConstantString(iter->second)}); |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 2656 | } |
| 2657 | } |
| 2658 | // Jump to end: |
| 2659 | Builder.CreateBr(NoAliasBB); |
| 2660 | |
| 2661 | // Missing alias registration function, just return from the function: |
| 2662 | Builder.SetInsertPoint(NoAliasBB); |
| 2663 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2664 | Builder.CreateRetVoid(); |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2665 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2666 | return LoadFunction; |
| 2667 | } |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2668 | |
Fariborz Jahanian | 0196a1c | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2669 | llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2670 | const ObjCContainerDecl *CD) { |
| 2671 | const ObjCCategoryImplDecl *OCD = |
Steve Naroff | 11b387f | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 2672 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext()); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2673 | StringRef CategoryName = OCD ? OCD->getName() : ""; |
| 2674 | StringRef ClassName = CD->getName(); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2675 | Selector MethodName = OMD->getSelector(); |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 2676 | bool isClassMethod = !OMD->isInstanceMethod(); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2677 | |
Daniel Dunbar | bf8c24a | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2678 | CodeGenTypes &Types = CGM.getTypes(); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2679 | llvm::FunctionType *MethodTy = |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2680 | Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD)); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2681 | std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName, |
| 2682 | MethodName, isClassMethod); |
| 2683 | |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 2684 | llvm::Function *Method |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2685 | = llvm::Function::Create(MethodTy, |
| 2686 | llvm::GlobalValue::InternalLinkage, |
| 2687 | FunctionName, |
| 2688 | &TheModule); |
Chris Lattner | 4bd5596 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 2689 | return Method; |
| 2690 | } |
| 2691 | |
David Chisnall | 3fe8956 | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 2692 | llvm::Constant *CGObjCGNU::GetPropertyGetFunction() { |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2693 | return GetPropertyFn; |
Daniel Dunbar | a91c3e0 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
David Chisnall | 3fe8956 | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 2696 | llvm::Constant *CGObjCGNU::GetPropertySetFunction() { |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2697 | return SetPropertyFn; |
Daniel Dunbar | a91c3e0 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2698 | } |
| 2699 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2700 | llvm::Constant *CGObjCGNU::GetOptimizedPropertySetFunction(bool atomic, |
| 2701 | bool copy) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2702 | return nullptr; |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2703 | } |
| 2704 | |
David Chisnall | 3fe8956 | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 2705 | llvm::Constant *CGObjCGNU::GetGetStructFunction() { |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2706 | return GetStructPropertyFn; |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 2707 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 2708 | |
David Chisnall | 3fe8956 | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 2709 | llvm::Constant *CGObjCGNU::GetSetStructFunction() { |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2710 | return SetStructPropertyFn; |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 2711 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 2712 | |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 2713 | llvm::Constant *CGObjCGNU::GetCppAtomicObjectGetFunction() { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2714 | return nullptr; |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 2715 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 2716 | |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 2717 | llvm::Constant *CGObjCGNU::GetCppAtomicObjectSetFunction() { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2718 | return nullptr; |
Fariborz Jahanian | 1e1b549 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 2719 | } |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 2720 | |
Daniel Dunbar | c46a079 | 2009-07-24 07:40:24 +0000 | [diff] [blame] | 2721 | llvm::Constant *CGObjCGNU::EnumerationMutationFunction() { |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2722 | return EnumerationMutationFn; |
Anders Carlsson | 3f35a26 | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2723 | } |
| 2724 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2725 | void CGObjCGNU::EmitSynchronizedStmt(CodeGenFunction &CGF, |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2726 | const ObjCAtSynchronizedStmt &S) { |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 2727 | EmitAtSynchronizedStmt(CGF, S, SyncEnterFn, SyncExitFn); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2728 | } |
Chris Lattner | b6e9eb6 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 2729 | |
David Chisnall | 3a509cd | 2009-12-24 02:26:34 +0000 | [diff] [blame] | 2730 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2731 | void CGObjCGNU::EmitTryStmt(CodeGenFunction &CGF, |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2732 | const ObjCAtTryStmt &S) { |
| 2733 | // Unlike the Apple non-fragile runtimes, which also uses |
| 2734 | // unwind-based zero cost exceptions, the GNU Objective C runtime's |
| 2735 | // EH support isn't a veneer over C++ EH. Instead, exception |
David Chisnall | 9a837be | 2012-11-07 16:50:40 +0000 | [diff] [blame] | 2736 | // objects are created by objc_exception_throw and destroyed by |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2737 | // the personality function; this avoids the need for bracketing |
| 2738 | // catch handlers with calls to __blah_begin_catch/__blah_end_catch |
| 2739 | // (or even _Unwind_DeleteException), but probably doesn't |
| 2740 | // interoperate very well with foreign exceptions. |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 2741 | // |
David Chisnall | e1d2584d | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 2742 | // In Objective-C++ mode, we actually emit something equivalent to the C++ |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 2743 | // exception handler. |
| 2744 | EmitTryCatchStmt(CGF, S, EnterCatchFn, ExitCatchFn, ExceptionReThrowFn); |
Anders Carlsson | 1963b0c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2747 | void CGObjCGNU::EmitThrowStmt(CodeGenFunction &CGF, |
Fariborz Jahanian | 1eab052 | 2013-01-10 19:02:56 +0000 | [diff] [blame] | 2748 | const ObjCAtThrowStmt &S, |
| 2749 | bool ClearInsertionPoint) { |
Chris Lattner | b6e9eb6 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 2750 | llvm::Value *ExceptionAsObject; |
| 2751 | |
Chris Lattner | b6e9eb6 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 2752 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
John McCall | 248512a | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 2753 | llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr); |
Fariborz Jahanian | 078cd52 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 2754 | ExceptionAsObject = Exception; |
Chris Lattner | b6e9eb6 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 2755 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2756 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Chris Lattner | b6e9eb6 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 2757 | "Unexpected rethrow outside @catch block."); |
| 2758 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
| 2759 | } |
Benjamin Kramer | 76399eb | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 2760 | ExceptionAsObject = CGF.Builder.CreateBitCast(ExceptionAsObject, IdTy); |
David Chisnall | 9a837be | 2012-11-07 16:50:40 +0000 | [diff] [blame] | 2761 | llvm::CallSite Throw = |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2762 | CGF.EmitRuntimeCallOrInvoke(ExceptionThrowFn, ExceptionAsObject); |
David Chisnall | 9a837be | 2012-11-07 16:50:40 +0000 | [diff] [blame] | 2763 | Throw.setDoesNotReturn(); |
Eli Friedman | dc009da | 2012-08-10 21:26:17 +0000 | [diff] [blame] | 2764 | CGF.Builder.CreateUnreachable(); |
Fariborz Jahanian | 1eab052 | 2013-01-10 19:02:56 +0000 | [diff] [blame] | 2765 | if (ClearInsertionPoint) |
| 2766 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 1963b0c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2767 | } |
| 2768 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2769 | llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2770 | Address AddrWeakObj) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2771 | CGBuilderTy &B = CGF.Builder; |
David Chisnall | fcb37e9 | 2011-05-30 12:00:26 +0000 | [diff] [blame] | 2772 | AddrWeakObj = EnforceType(B, AddrWeakObj, PtrToIdTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2773 | return B.CreateCall(WeakReadFn.getType(), WeakReadFn, |
| 2774 | AddrWeakObj.getPointer()); |
Fariborz Jahanian | f5125d1 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2775 | } |
| 2776 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2777 | void CGObjCGNU::EmitObjCWeakAssign(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2778 | llvm::Value *src, Address dst) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2779 | CGBuilderTy &B = CGF.Builder; |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2780 | src = EnforceType(B, src, IdTy); |
| 2781 | dst = EnforceType(B, dst, PtrToIdTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2782 | B.CreateCall(WeakAssignFn.getType(), WeakAssignFn, |
| 2783 | {src, dst.getPointer()}); |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2784 | } |
| 2785 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2786 | void CGObjCGNU::EmitObjCGlobalAssign(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2787 | llvm::Value *src, Address dst, |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 2788 | bool threadlocal) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2789 | CGBuilderTy &B = CGF.Builder; |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2790 | src = EnforceType(B, src, IdTy); |
| 2791 | dst = EnforceType(B, dst, PtrToIdTy); |
David Blaikie | 43f9bb7 | 2015-05-18 22:14:03 +0000 | [diff] [blame] | 2792 | // FIXME. Add threadloca assign API |
| 2793 | assert(!threadlocal && "EmitObjCGlobalAssign - Threal Local API NYI"); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2794 | B.CreateCall(GlobalAssignFn.getType(), GlobalAssignFn, |
| 2795 | {src, dst.getPointer()}); |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2798 | void CGObjCGNU::EmitObjCIvarAssign(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2799 | llvm::Value *src, Address dst, |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 2800 | llvm::Value *ivarOffset) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2801 | CGBuilderTy &B = CGF.Builder; |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2802 | src = EnforceType(B, src, IdTy); |
David Chisnall | e4e5c0f | 2011-05-25 20:33:17 +0000 | [diff] [blame] | 2803 | dst = EnforceType(B, dst, IdTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2804 | B.CreateCall(IvarAssignFn.getType(), IvarAssignFn, |
| 2805 | {src, dst.getPointer(), ivarOffset}); |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2808 | void CGObjCGNU::EmitObjCStrongCastAssign(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2809 | llvm::Value *src, Address dst) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2810 | CGBuilderTy &B = CGF.Builder; |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2811 | src = EnforceType(B, src, IdTy); |
| 2812 | dst = EnforceType(B, dst, PtrToIdTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2813 | B.CreateCall(StrongCastAssignFn.getType(), StrongCastAssignFn, |
| 2814 | {src, dst.getPointer()}); |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2815 | } |
| 2816 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2817 | void CGObjCGNU::EmitGCMemmoveCollectable(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2818 | Address DestPtr, |
| 2819 | Address SrcPtr, |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 2820 | llvm::Value *Size) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2821 | CGBuilderTy &B = CGF.Builder; |
David Chisnall | 7441d88 | 2011-05-28 14:23:43 +0000 | [diff] [blame] | 2822 | DestPtr = EnforceType(B, DestPtr, PtrTy); |
| 2823 | SrcPtr = EnforceType(B, SrcPtr, PtrTy); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2824 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2825 | B.CreateCall(MemMoveFn.getType(), MemMoveFn, |
| 2826 | {DestPtr.getPointer(), SrcPtr.getPointer(), Size}); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2829 | llvm::GlobalVariable *CGObjCGNU::ObjCIvarOffsetVariable( |
| 2830 | const ObjCInterfaceDecl *ID, |
| 2831 | const ObjCIvarDecl *Ivar) { |
| 2832 | const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString() |
| 2833 | + '.' + Ivar->getNameAsString(); |
| 2834 | // Emit the variable and initialize it with what we think the correct value |
| 2835 | // is. This allows code compiled with non-fragile ivars to work correctly |
| 2836 | // when linked against code which isn't (most of the time). |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2837 | llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name); |
| 2838 | if (!IvarOffsetPointer) { |
David Chisnall | e8431a7 | 2010-11-03 16:12:44 +0000 | [diff] [blame] | 2839 | // This will cause a run-time crash if we accidentally use it. A value of |
| 2840 | // 0 would seem more sensible, but will silently overwrite the isa pointer |
| 2841 | // causing a great deal of confusion. |
| 2842 | uint64_t Offset = -1; |
| 2843 | // We can't call ComputeIvarBaseOffset() here if we have the |
| 2844 | // implementation, because it will create an invalid ASTRecordLayout object |
| 2845 | // that we are then stuck with forever, so we only initialize the ivar |
| 2846 | // offset variable with a guess if we only have the interface. The |
| 2847 | // initializer will be reset later anyway, when we are generating the class |
| 2848 | // description. |
| 2849 | if (!CGM.getContext().getObjCImplementation( |
Dan Gohman | 145f3f1 | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 2850 | const_cast<ObjCInterfaceDecl *>(ID))) |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 2851 | Offset = ComputeIvarBaseOffset(CGM, ID, Ivar); |
David Chisnall | 44ec555 | 2010-04-19 01:37:25 +0000 | [diff] [blame] | 2852 | |
David Chisnall | e0dc7cb | 2011-10-08 08:54:36 +0000 | [diff] [blame] | 2853 | llvm::ConstantInt *OffsetGuess = llvm::ConstantInt::get(Int32Ty, Offset, |
Richard Trieu | e4f3180 | 2011-09-21 02:46:06 +0000 | [diff] [blame] | 2854 | /*isSigned*/true); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2855 | // Don't emit the guess in non-PIC code because the linker will not be able |
| 2856 | // to replace it with the real version for a library. In non-PIC code you |
| 2857 | // 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] | 2858 | // GCC-compiled class. |
Rafael Espindola | c9d336e | 2016-06-23 15:07:32 +0000 | [diff] [blame] | 2859 | if (CGM.getLangOpts().PICLevel) { |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2860 | llvm::GlobalVariable *IvarOffsetGV = new llvm::GlobalVariable(TheModule, |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 2861 | Int32Ty, false, |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2862 | llvm::GlobalValue::PrivateLinkage, OffsetGuess, Name+".guess"); |
| 2863 | IvarOffsetPointer = new llvm::GlobalVariable(TheModule, |
| 2864 | IvarOffsetGV->getType(), false, llvm::GlobalValue::LinkOnceAnyLinkage, |
| 2865 | IvarOffsetGV, Name); |
| 2866 | } else { |
| 2867 | IvarOffsetPointer = new llvm::GlobalVariable(TheModule, |
Benjamin Kramer | abd5b90 | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 2868 | llvm::Type::getInt32PtrTy(VMContext), false, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2869 | llvm::GlobalValue::ExternalLinkage, nullptr, Name); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2870 | } |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2871 | } |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2872 | return IvarOffsetPointer; |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2873 | } |
| 2874 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2875 | LValue CGObjCGNU::EmitObjCValueForIvar(CodeGenFunction &CGF, |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2876 | QualType ObjectTy, |
| 2877 | llvm::Value *BaseValue, |
| 2878 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2879 | unsigned CVRQualifiers) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2880 | const ObjCInterfaceDecl *ID = |
| 2881 | ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Daniel Dunbar | 9fd114d | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2882 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 2883 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 9f84b78 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2884 | } |
Mike Stump | dd93a19 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 2885 | |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2886 | static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context, |
| 2887 | const ObjCInterfaceDecl *OID, |
| 2888 | const ObjCIvarDecl *OIVD) { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2889 | for (const ObjCIvarDecl *next = OID->all_declared_ivar_begin(); next; |
| 2890 | next = next->getNextIvar()) { |
| 2891 | if (OIVD == next) |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2892 | return OID; |
| 2893 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2894 | |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2895 | // Otherwise check in the super class. |
| 2896 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
| 2897 | return FindIvarInterface(Context, Super, OIVD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2898 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2899 | return nullptr; |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2900 | } |
Fariborz Jahanian | 9f84b78 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2901 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2902 | llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGenFunction &CGF, |
Daniel Dunbar | 722f424 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 2903 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2904 | const ObjCIvarDecl *Ivar) { |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 2905 | if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2906 | Interface = FindIvarInterface(CGM.getContext(), Interface, Ivar); |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 2907 | |
| 2908 | // The MSVC linker cannot have a single global defined as LinkOnceAnyLinkage |
| 2909 | // and ExternalLinkage, so create a reference to the ivar global and rely on |
| 2910 | // the definition being created as part of GenerateClass. |
| 2911 | if (RuntimeVersion < 10 || |
| 2912 | CGF.CGM.getTarget().getTriple().isKnownWindowsMSVCEnvironment()) |
David Chisnall | 1bfe6d3 | 2011-07-07 12:34:51 +0000 | [diff] [blame] | 2913 | return CGF.Builder.CreateZExtOrBitCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2914 | CGF.Builder.CreateDefaultAlignedLoad(CGF.Builder.CreateAlignedLoad( |
| 2915 | ObjCIvarOffsetVariable(Interface, Ivar), |
| 2916 | CGF.getPointerAlign(), "ivar")), |
David Chisnall | 1bfe6d3 | 2011-07-07 12:34:51 +0000 | [diff] [blame] | 2917 | PtrDiffTy); |
| 2918 | std::string name = "__objc_ivar_offset_value_" + |
| 2919 | Interface->getNameAsString() +"." + Ivar->getNameAsString(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2920 | CharUnits Align = CGM.getIntAlign(); |
David Chisnall | 1bfe6d3 | 2011-07-07 12:34:51 +0000 | [diff] [blame] | 2921 | llvm::Value *Offset = TheModule.getGlobalVariable(name); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2922 | if (!Offset) { |
| 2923 | auto GV = new llvm::GlobalVariable(TheModule, IntTy, |
David Chisnall | 28dc7f9 | 2011-08-01 17:36:53 +0000 | [diff] [blame] | 2924 | false, llvm::GlobalValue::LinkOnceAnyLinkage, |
| 2925 | llvm::Constant::getNullValue(IntTy), name); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2926 | GV->setAlignment(Align.getQuantity()); |
| 2927 | Offset = GV; |
| 2928 | } |
| 2929 | Offset = CGF.Builder.CreateAlignedLoad(Offset, Align); |
David Chisnall | a79b469 | 2012-04-06 15:39:12 +0000 | [diff] [blame] | 2930 | if (Offset->getType() != PtrDiffTy) |
| 2931 | Offset = CGF.Builder.CreateZExtOrBitCast(Offset, PtrDiffTy); |
| 2932 | return Offset; |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2933 | } |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 2934 | uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar); |
| 2935 | return llvm::ConstantInt::get(PtrDiffTy, Offset, /*isSigned*/true); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2936 | } |
| 2937 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2938 | CGObjCRuntime * |
| 2939 | clang::CodeGen::CreateGNUObjCRuntime(CodeGenModule &CGM) { |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 2940 | switch (CGM.getLangOpts().ObjCRuntime.getKind()) { |
David Chisnall | b601c96 | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 2941 | case ObjCRuntime::GNUstep: |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2942 | return new CGObjCGNUstep(CGM); |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 2943 | |
David Chisnall | b601c96 | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 2944 | case ObjCRuntime::GCC: |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 2945 | return new CGObjCGCC(CGM); |
| 2946 | |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 2947 | case ObjCRuntime::ObjFW: |
| 2948 | return new CGObjCObjFW(CGM); |
| 2949 | |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 2950 | case ObjCRuntime::FragileMacOSX: |
| 2951 | case ObjCRuntime::MacOSX: |
| 2952 | case ObjCRuntime::iOS: |
Tim Northover | 756447a | 2015-10-30 16:30:36 +0000 | [diff] [blame] | 2953 | case ObjCRuntime::WatchOS: |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 2954 | llvm_unreachable("these runtimes are not GNU runtimes"); |
| 2955 | } |
| 2956 | llvm_unreachable("bad runtime"); |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 2957 | } |