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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 9 | // This provides Objective-C code generation targeting the GNU runtime. The |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 10 | // class in this file generates structures used by the GNU Objective-C runtime |
| 11 | // library. These structures are defined in objc/objc.h and objc/objc-api.h in |
| 12 | // the GNU runtime distribution. |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Reid Kleckner | 9803178 | 2019-12-09 16:11:56 -0800 | [diff] [blame] | 16 | #include "CGCXXABI.h" |
John McCall | ed1ae86 | 2011-01-28 11:13:47 +0000 | [diff] [blame] | 17 | #include "CGCleanup.h" |
Reid Kleckner | 9803178 | 2019-12-09 16:11:56 -0800 | [diff] [blame] | 18 | #include "CGObjCRuntime.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" |
Reid Kleckner | 9803178 | 2019-12-09 16:11:56 -0800 | [diff] [blame] | 22 | #include "clang/AST/Attr.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 23 | #include "clang/AST/Decl.h" |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 15b73de | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 25 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | f0b64d7 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 26 | #include "clang/AST/StmtObjC.h" |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 27 | #include "clang/Basic/FileManager.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 28 | #include "clang/Basic/SourceManager.h" |
Reid Kleckner | 9803178 | 2019-12-09 16:11:56 -0800 | [diff] [blame] | 29 | #include "clang/CodeGen/ConstantInitBuilder.h" |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallVector.h" |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/StringMap.h" |
Chandler Carruth | ffd5551 | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 32 | #include "llvm/IR/DataLayout.h" |
| 33 | #include "llvm/IR/Intrinsics.h" |
| 34 | #include "llvm/IR/LLVMContext.h" |
| 35 | #include "llvm/IR/Module.h" |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Compiler.h" |
David Chisnall | 79356ee | 2018-05-22 06:09:23 +0000 | [diff] [blame] | 37 | #include "llvm/Support/ConvertUTF.h" |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 38 | #include <cctype> |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 87ab27d | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 40 | using namespace clang; |
Daniel Dunbar | 41cf9de | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 41 | using namespace CodeGen; |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 42 | |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 43 | namespace { |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 44 | |
| 45 | std::string SymbolNameForMethod( StringRef ClassName, |
| 46 | StringRef CategoryName, const Selector MethodName, |
| 47 | bool isClassMethod) { |
| 48 | std::string MethodNameColonStripped = MethodName.getAsString(); |
| 49 | std::replace(MethodNameColonStripped.begin(), MethodNameColonStripped.end(), |
| 50 | ':', '_'); |
| 51 | return (Twine(isClassMethod ? "_c_" : "_i_") + ClassName + "_" + |
| 52 | CategoryName + "_" + MethodNameColonStripped).str(); |
| 53 | } |
| 54 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 55 | /// Class that lazily initialises the runtime function. Avoids inserting the |
| 56 | /// types and the function declaration into a module if they're not used, and |
| 57 | /// 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] | 58 | class LazyRuntimeFunction { |
| 59 | CodeGenModule *CGM; |
David Blaikie | bf178d3 | 2015-05-19 21:31:34 +0000 | [diff] [blame] | 60 | llvm::FunctionType *FTy; |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 61 | const char *FunctionName; |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 62 | llvm::FunctionCallee Function; |
David Blaikie | 7d9e792 | 2015-05-18 22:51:39 +0000 | [diff] [blame] | 63 | |
| 64 | public: |
| 65 | /// Constructor leaves this class uninitialized, because it is intended to |
| 66 | /// be used as a field in another class and not all of the types that are |
| 67 | /// used as arguments will necessarily be available at construction time. |
| 68 | LazyRuntimeFunction() |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 69 | : CGM(nullptr), FunctionName(nullptr), Function(nullptr) {} |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 70 | |
David Blaikie | 7d9e792 | 2015-05-18 22:51:39 +0000 | [diff] [blame] | 71 | /// Initialises the lazy function with the name, return type, and the types |
| 72 | /// of the arguments. |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 73 | template <typename... Tys> |
| 74 | void init(CodeGenModule *Mod, const char *name, llvm::Type *RetTy, |
| 75 | Tys *... Types) { |
David Blaikie | 7d9e792 | 2015-05-18 22:51:39 +0000 | [diff] [blame] | 76 | CGM = Mod; |
| 77 | FunctionName = name; |
| 78 | Function = nullptr; |
Serge Guelton | 29405c9 | 2017-05-09 21:19:44 +0000 | [diff] [blame] | 79 | if(sizeof...(Tys)) { |
| 80 | SmallVector<llvm::Type *, 8> ArgTys({Types...}); |
| 81 | FTy = llvm::FunctionType::get(RetTy, ArgTys, false); |
| 82 | } |
| 83 | else { |
| 84 | FTy = llvm::FunctionType::get(RetTy, None, false); |
| 85 | } |
David Blaikie | 7d9e792 | 2015-05-18 22:51:39 +0000 | [diff] [blame] | 86 | } |
David Blaikie | bf178d3 | 2015-05-19 21:31:34 +0000 | [diff] [blame] | 87 | |
| 88 | llvm::FunctionType *getType() { return FTy; } |
| 89 | |
David Blaikie | 7d9e792 | 2015-05-18 22:51:39 +0000 | [diff] [blame] | 90 | /// Overloaded cast operator, allows the class to be implicitly cast to an |
| 91 | /// LLVM constant. |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 92 | operator llvm::FunctionCallee() { |
David Blaikie | 7d9e792 | 2015-05-18 22:51:39 +0000 | [diff] [blame] | 93 | if (!Function) { |
| 94 | if (!FunctionName) |
| 95 | return nullptr; |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 96 | Function = CGM->CreateRuntimeFunction(FTy, FunctionName); |
David Blaikie | 7d9e792 | 2015-05-18 22:51:39 +0000 | [diff] [blame] | 97 | } |
| 98 | return Function; |
| 99 | } |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 103 | /// GNU Objective-C runtime code generation. This class implements the parts of |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 104 | /// Objective-C support that are specific to the GNU family of runtimes (GCC, |
| 105 | /// GNUstep and ObjFW). |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 106 | class CGObjCGNU : public CGObjCRuntime { |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 107 | protected: |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 108 | /// The LLVM module into which output is inserted |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 109 | llvm::Module &TheModule; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 110 | /// strut objc_super. Used for sending messages to super. This structure |
| 111 | /// contains the receiver (object) and the expected class. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 112 | llvm::StructType *ObjCSuperTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 113 | /// struct objc_super*. The type of the argument to the superclass message |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 114 | /// lookup functions. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 115 | llvm::PointerType *PtrToObjCSuperTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 116 | /// LLVM type for selectors. Opaque pointer (i8*) unless a header declaring |
| 117 | /// SEL is included in a header somewhere, in which case it will be whatever |
| 118 | /// type is declared in that header, most likely {i8*, i8*}. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 119 | llvm::PointerType *SelectorTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 120 | /// LLVM i8 type. Cached here to avoid repeatedly getting it in all of the |
| 121 | /// places where it's used |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 122 | llvm::IntegerType *Int8Ty; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 123 | /// Pointer to i8 - LLVM type of char*, for all of the places where the |
| 124 | /// runtime needs to deal with C strings. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 125 | llvm::PointerType *PtrToInt8Ty; |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 126 | /// struct objc_protocol type |
| 127 | llvm::StructType *ProtocolTy; |
| 128 | /// Protocol * type. |
| 129 | llvm::PointerType *ProtocolPtrTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 130 | /// Instance Method Pointer type. This is a pointer to a function that takes, |
| 131 | /// at a minimum, an object and a selector, and is the generic type for |
| 132 | /// Objective-C methods. Due to differences between variadic / non-variadic |
| 133 | /// calling conventions, it must always be cast to the correct type before |
| 134 | /// actually being used. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 135 | llvm::PointerType *IMPTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 136 | /// Type of an untyped Objective-C object. Clang treats id as a built-in type |
| 137 | /// when compiling Objective-C code, so this may be an opaque pointer (i8*), |
| 138 | /// but if the runtime header declaring it is included then it may be a |
| 139 | /// pointer to a structure. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 140 | llvm::PointerType *IdTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 141 | /// Pointer to a pointer to an Objective-C object. Used in the new ABI |
| 142 | /// message lookup function and some GC-related functions. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 143 | llvm::PointerType *PtrToIdTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 144 | /// The clang type of id. Used when using the clang CGCall infrastructure to |
| 145 | /// call Objective-C methods. |
John McCall | 2da83a3 | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 146 | CanQualType ASTIdTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 147 | /// LLVM type for C int type. |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 148 | llvm::IntegerType *IntTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 149 | /// LLVM type for an opaque pointer. This is identical to PtrToInt8Ty, but is |
| 150 | /// used in the code to document the difference between i8* meaning a pointer |
| 151 | /// 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] | 152 | llvm::PointerType *PtrTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 153 | /// LLVM type for C long type. The runtime uses this in a lot of places where |
| 154 | /// it should be using intptr_t, but we can't fix this without breaking |
| 155 | /// compatibility with GCC... |
Jay Foad | 7c57be3 | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 156 | llvm::IntegerType *LongTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 157 | /// LLVM type for C size_t. Used in various runtime data structures. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 158 | llvm::IntegerType *SizeTy; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 159 | /// LLVM type for C intptr_t. |
David Chisnall | e0dc7cb | 2011-10-08 08:54:36 +0000 | [diff] [blame] | 160 | llvm::IntegerType *IntPtrTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 161 | /// LLVM type for C ptrdiff_t. Mainly used in property accessor functions. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 162 | llvm::IntegerType *PtrDiffTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 163 | /// LLVM type for C int*. Used for GCC-ABI-compatible non-fragile instance |
| 164 | /// variables. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 165 | llvm::PointerType *PtrToIntTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 166 | /// LLVM type for Objective-C BOOL type. |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 167 | llvm::Type *BoolTy; |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 168 | /// 32-bit integer type, to save us needing to look it up every time it's used. |
| 169 | llvm::IntegerType *Int32Ty; |
| 170 | /// 64-bit integer type, to save us needing to look it up every time it's used. |
| 171 | llvm::IntegerType *Int64Ty; |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 172 | /// The type of struct objc_property. |
| 173 | llvm::StructType *PropertyMetadataTy; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 174 | /// Metadata kind used to tie method lookups to message sends. The GNUstep |
| 175 | /// runtime provides some LLVM passes that can use this to do things like |
| 176 | /// automatic IMP caching and speculative inlining. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 177 | unsigned msgSendMDKind; |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 178 | /// Does the current target use SEH-based exceptions? False implies |
| 179 | /// Itanium-style DWARF unwinding. |
| 180 | bool usesSEHExceptions; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 181 | |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 182 | /// Helper to check if we are targeting a specific runtime version or later. |
| 183 | bool isRuntime(ObjCRuntime::Kind kind, unsigned major, unsigned minor=0) { |
| 184 | const ObjCRuntime &R = CGM.getLangOpts().ObjCRuntime; |
| 185 | return (R.getKind() == kind) && |
| 186 | (R.getVersion() >= VersionTuple(major, minor)); |
| 187 | } |
| 188 | |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 189 | std::string ManglePublicSymbol(StringRef Name) { |
| 190 | return (StringRef(CGM.getTriple().isOSBinFormatCOFF() ? "$_" : "._") + Name).str(); |
| 191 | } |
| 192 | |
| 193 | std::string SymbolForProtocol(Twine Name) { |
| 194 | return (ManglePublicSymbol("OBJC_PROTOCOL_") + Name).str(); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | std::string SymbolForProtocolRef(StringRef Name) { |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 198 | return (ManglePublicSymbol("OBJC_REF_PROTOCOL_") + Name).str(); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 202 | /// Helper function that generates a constant string and returns a pointer to |
| 203 | /// the start of the string. The result of this function can be used anywhere |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 204 | /// where the C code specifies const char*. |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 205 | llvm::Constant *MakeConstantString(StringRef Str, const char *Name = "") { |
| 206 | ConstantAddress Array = CGM.GetAddrOfConstantCString(Str, Name); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 207 | return llvm::ConstantExpr::getGetElementPtr(Array.getElementType(), |
| 208 | Array.getPointer(), Zeros); |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 209 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 210 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 211 | /// Emits a linkonce_odr string, whose name is the prefix followed by the |
| 212 | /// string value. This allows the linker to combine the strings between |
| 213 | /// different modules. Used for EH typeinfo names, selector strings, and a |
| 214 | /// few other things. |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 215 | llvm::Constant *ExportUniqueString(const std::string &Str, |
| 216 | const std::string &prefix, |
| 217 | bool Private=false) { |
| 218 | std::string name = prefix + Str; |
| 219 | auto *ConstStr = TheModule.getGlobalVariable(name); |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 220 | if (!ConstStr) { |
Chris Lattner | 9c81833 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 221 | llvm::Constant *value = llvm::ConstantDataArray::getString(VMContext,Str); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 222 | auto *GV = new llvm::GlobalVariable(TheModule, value->getType(), true, |
| 223 | llvm::GlobalValue::LinkOnceODRLinkage, value, name); |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 224 | GV->setComdat(TheModule.getOrInsertComdat(name)); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 225 | if (Private) |
| 226 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 227 | ConstStr = GV; |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 228 | } |
David Blaikie | e3b172a | 2015-04-02 18:55:21 +0000 | [diff] [blame] | 229 | return llvm::ConstantExpr::getGetElementPtr(ConstStr->getValueType(), |
| 230 | ConstStr, Zeros); |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 231 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 232 | |
David Chisnall | a5f5941 | 2012-10-16 15:11:55 +0000 | [diff] [blame] | 233 | /// Returns a property name and encoding string. |
| 234 | llvm::Constant *MakePropertyEncodingString(const ObjCPropertyDecl *PD, |
| 235 | const Decl *Container) { |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 236 | assert(!isRuntime(ObjCRuntime::GNUstep, 2)); |
| 237 | if (isRuntime(ObjCRuntime::GNUstep, 1, 6)) { |
David Chisnall | a5f5941 | 2012-10-16 15:11:55 +0000 | [diff] [blame] | 238 | std::string NameAndAttributes; |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 239 | std::string TypeStr = |
| 240 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container); |
David Chisnall | a5f5941 | 2012-10-16 15:11:55 +0000 | [diff] [blame] | 241 | NameAndAttributes += '\0'; |
| 242 | NameAndAttributes += TypeStr.length() + 3; |
| 243 | NameAndAttributes += TypeStr; |
| 244 | NameAndAttributes += '\0'; |
| 245 | NameAndAttributes += PD->getNameAsString(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 246 | return MakeConstantString(NameAndAttributes); |
David Chisnall | a5f5941 | 2012-10-16 15:11:55 +0000 | [diff] [blame] | 247 | } |
| 248 | return MakeConstantString(PD->getNameAsString()); |
| 249 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 250 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 251 | /// Push the property attributes into two structure fields. |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 252 | void PushPropertyAttributes(ConstantStructBuilder &Fields, |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 253 | const ObjCPropertyDecl *property, bool isSynthesized=true, bool |
David Chisnall | beb8013 | 2013-02-28 13:59:29 +0000 | [diff] [blame] | 254 | isDynamic=true) { |
| 255 | int attrs = property->getPropertyAttributes(); |
| 256 | // For read-only properties, clear the copy and retain flags |
| 257 | if (attrs & ObjCPropertyDecl::OBJC_PR_readonly) { |
| 258 | attrs &= ~ObjCPropertyDecl::OBJC_PR_copy; |
| 259 | attrs &= ~ObjCPropertyDecl::OBJC_PR_retain; |
| 260 | attrs &= ~ObjCPropertyDecl::OBJC_PR_weak; |
| 261 | attrs &= ~ObjCPropertyDecl::OBJC_PR_strong; |
| 262 | } |
| 263 | // The first flags field has the same attribute values as clang uses internally |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 264 | Fields.addInt(Int8Ty, attrs & 0xff); |
David Chisnall | beb8013 | 2013-02-28 13:59:29 +0000 | [diff] [blame] | 265 | attrs >>= 8; |
| 266 | attrs <<= 2; |
| 267 | // For protocol properties, synthesized and dynamic have no meaning, so we |
| 268 | // reuse these flags to indicate that this is a protocol property (both set |
| 269 | // has no meaning, as a property can't be both synthesized and dynamic) |
| 270 | attrs |= isSynthesized ? (1<<0) : 0; |
| 271 | attrs |= isDynamic ? (1<<1) : 0; |
| 272 | // The second field is the next four fields left shifted by two, with the |
| 273 | // low bit set to indicate whether the field is synthesized or dynamic. |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 274 | Fields.addInt(Int8Ty, attrs & 0xff); |
David Chisnall | beb8013 | 2013-02-28 13:59:29 +0000 | [diff] [blame] | 275 | // Two padding fields |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 276 | Fields.addInt(Int8Ty, 0); |
| 277 | Fields.addInt(Int8Ty, 0); |
David Chisnall | beb8013 | 2013-02-28 13:59:29 +0000 | [diff] [blame] | 278 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 279 | |
David Chisnall | 386477a | 2018-12-28 17:44:54 +0000 | [diff] [blame] | 280 | virtual llvm::Constant *GenerateCategoryProtocolList(const |
| 281 | ObjCCategoryDecl *OCD); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 282 | virtual ConstantArrayBuilder PushPropertyListHeader(ConstantStructBuilder &Fields, |
| 283 | int count) { |
| 284 | // int count; |
| 285 | Fields.addInt(IntTy, count); |
| 286 | // int size; (only in GNUstep v2 ABI. |
| 287 | if (isRuntime(ObjCRuntime::GNUstep, 2)) { |
| 288 | llvm::DataLayout td(&TheModule); |
| 289 | Fields.addInt(IntTy, td.getTypeSizeInBits(PropertyMetadataTy) / |
| 290 | CGM.getContext().getCharWidth()); |
| 291 | } |
| 292 | // struct objc_property_list *next; |
| 293 | Fields.add(NULLPtr); |
| 294 | // struct objc_property properties[] |
| 295 | return Fields.beginArray(PropertyMetadataTy); |
| 296 | } |
| 297 | virtual void PushProperty(ConstantArrayBuilder &PropertiesArray, |
| 298 | const ObjCPropertyDecl *property, |
| 299 | const Decl *OCD, |
| 300 | bool isSynthesized=true, bool |
| 301 | isDynamic=true) { |
| 302 | auto Fields = PropertiesArray.beginStruct(PropertyMetadataTy); |
| 303 | ASTContext &Context = CGM.getContext(); |
| 304 | Fields.add(MakePropertyEncodingString(property, OCD)); |
| 305 | PushPropertyAttributes(Fields, property, isSynthesized, isDynamic); |
| 306 | auto addPropertyMethod = [&](const ObjCMethodDecl *accessor) { |
| 307 | if (accessor) { |
| 308 | std::string TypeStr = Context.getObjCEncodingForMethodDecl(accessor); |
| 309 | llvm::Constant *TypeEncoding = MakeConstantString(TypeStr); |
| 310 | Fields.add(MakeConstantString(accessor->getSelector().getAsString())); |
| 311 | Fields.add(TypeEncoding); |
| 312 | } else { |
| 313 | Fields.add(NULLPtr); |
| 314 | Fields.add(NULLPtr); |
| 315 | } |
| 316 | }; |
| 317 | addPropertyMethod(property->getGetterMethodDecl()); |
| 318 | addPropertyMethod(property->getSetterMethodDecl()); |
| 319 | Fields.finishAndAddTo(PropertiesArray); |
| 320 | } |
| 321 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 322 | /// Ensures that the value has the required type, by inserting a bitcast if |
| 323 | /// required. This function lets us avoid inserting bitcasts that are |
| 324 | /// redundant. |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 325 | llvm::Value* EnforceType(CGBuilderTy &B, llvm::Value *V, llvm::Type *Ty) { |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 326 | if (V->getType() == Ty) return V; |
| 327 | return B.CreateBitCast(V, Ty); |
| 328 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 329 | Address EnforceType(CGBuilderTy &B, Address V, llvm::Type *Ty) { |
| 330 | if (V.getType() == Ty) return V; |
| 331 | return B.CreateBitCast(V, Ty); |
| 332 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 333 | |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 334 | // Some zeros used for GEPs in lots of places. |
| 335 | llvm::Constant *Zeros[2]; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 336 | /// Null pointer value. Mainly used as a terminator in various arrays. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 337 | llvm::Constant *NULLPtr; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 338 | /// LLVM context. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 339 | llvm::LLVMContext &VMContext; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 340 | |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 341 | protected: |
| 342 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 343 | /// Placeholder for the class. Lots of things refer to the class before we've |
| 344 | /// actually emitted it. We use this alias as a placeholder, and then replace |
| 345 | /// it with a pointer to the class structure before finally emitting the |
| 346 | /// module. |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 347 | llvm::GlobalAlias *ClassPtrAlias; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 348 | /// Placeholder for the metaclass. Lots of things refer to the class before |
| 349 | /// we've / actually emitted it. We use this alias as a placeholder, and then |
| 350 | /// replace / it with a pointer to the metaclass structure before finally |
| 351 | /// emitting the / module. |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 352 | llvm::GlobalAlias *MetaClassPtrAlias; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 353 | /// All of the classes that have been generated for this compilation units. |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 354 | std::vector<llvm::Constant*> Classes; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 355 | /// All of the categories that have been generated for this compilation units. |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 356 | std::vector<llvm::Constant*> Categories; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 357 | /// All of the Objective-C constant strings that have been generated for this |
| 358 | /// compilation units. |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 359 | std::vector<llvm::Constant*> ConstantStrings; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 360 | /// Map from string values to Objective-C constant strings in the output. |
| 361 | /// Used to prevent emitting Objective-C strings more than once. This should |
| 362 | /// not be required at all - CodeGenModule should manage this list. |
David Chisnall | 358e751 | 2010-01-27 12:49:23 +0000 | [diff] [blame] | 363 | llvm::StringMap<llvm::Constant*> ObjCStrings; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 364 | /// All of the protocols that have been declared. |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 365 | llvm::StringMap<llvm::Constant*> ExistingProtocols; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 366 | /// For each variant of a selector, we store the type encoding and a |
| 367 | /// placeholder value. For an untyped selector, the type will be the empty |
| 368 | /// string. Selector references are all done via the module's selector table, |
| 369 | /// so we create an alias as a placeholder and then replace it with the real |
| 370 | /// value later. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 371 | typedef std::pair<std::string, llvm::GlobalAlias*> TypedSelector; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 372 | /// Type of the selector map. This is roughly equivalent to the structure |
| 373 | /// used in the GNUstep runtime, which maintains a list of all of the valid |
| 374 | /// types for a selector in a table. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 375 | typedef llvm::DenseMap<Selector, SmallVector<TypedSelector, 2> > |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 376 | SelectorMap; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 377 | /// A map from selectors to selector types. This allows us to emit all |
| 378 | /// selectors of the same name and type together. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 379 | SelectorMap SelectorTable; |
| 380 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 381 | /// Selectors related to memory management. When compiling in GC mode, we |
| 382 | /// omit these. |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 383 | Selector RetainSel, ReleaseSel, AutoreleaseSel; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 384 | /// Runtime functions used for memory management in GC mode. Note that clang |
| 385 | /// supports code generation for calling these functions, but neither GNU |
| 386 | /// runtime actually supports this API properly yet. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 387 | LazyRuntimeFunction IvarAssignFn, StrongCastAssignFn, MemMoveFn, WeakReadFn, |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 388 | WeakAssignFn, GlobalAssignFn; |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 389 | |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 390 | typedef std::pair<std::string, std::string> ClassAliasPair; |
| 391 | /// All classes that have aliases set for them. |
| 392 | std::vector<ClassAliasPair> ClassAliases; |
| 393 | |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 394 | protected: |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 395 | /// Function used for throwing Objective-C exceptions. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 396 | LazyRuntimeFunction ExceptionThrowFn; |
James Dennett | b9199ee | 2012-06-13 22:07:09 +0000 | [diff] [blame] | 397 | /// Function used for rethrowing exceptions, used at the end of \@finally or |
| 398 | /// \@synchronize blocks. |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 399 | LazyRuntimeFunction ExceptionReThrowFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 400 | /// Function called when entering a catch function. This is required for |
| 401 | /// differentiating Objective-C exceptions and foreign exceptions. |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 402 | LazyRuntimeFunction EnterCatchFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 403 | /// Function called when exiting from a catch block. Used to do exception |
| 404 | /// cleanup. |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 405 | LazyRuntimeFunction ExitCatchFn; |
James Dennett | b9199ee | 2012-06-13 22:07:09 +0000 | [diff] [blame] | 406 | /// Function called when entering an \@synchronize block. Acquires the lock. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 407 | LazyRuntimeFunction SyncEnterFn; |
James Dennett | b9199ee | 2012-06-13 22:07:09 +0000 | [diff] [blame] | 408 | /// Function called when exiting an \@synchronize block. Releases the lock. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 409 | LazyRuntimeFunction SyncExitFn; |
| 410 | |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 411 | private: |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 412 | /// Function called if fast enumeration detects that the collection is |
| 413 | /// modified during the update. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 414 | LazyRuntimeFunction EnumerationMutationFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 415 | /// Function for implementing synthesized property getters that return an |
| 416 | /// object. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 417 | LazyRuntimeFunction GetPropertyFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 418 | /// Function for implementing synthesized property setters that return an |
| 419 | /// object. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 420 | LazyRuntimeFunction SetPropertyFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 421 | /// Function used for non-object declared property getters. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 422 | LazyRuntimeFunction GetStructPropertyFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 423 | /// Function used for non-object declared property setters. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 424 | LazyRuntimeFunction SetStructPropertyFn; |
| 425 | |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 426 | protected: |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 427 | /// The version of the runtime that this class targets. Must match the |
| 428 | /// version in the runtime. |
David Chisnall | 5c51177 | 2011-05-22 22:37:08 +0000 | [diff] [blame] | 429 | int RuntimeVersion; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 430 | /// The version of the protocol class. Used to differentiate between ObjC1 |
| 431 | /// and ObjC2 protocols. Objective-C 1 protocols can not contain optional |
| 432 | /// components and can not contain declared properties. We always emit |
| 433 | /// Objective-C 2 property structures, but we have to pretend that they're |
| 434 | /// Objective-C 1 property structures when targeting the GCC runtime or it |
| 435 | /// will abort. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 436 | const int ProtocolVersion; |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 437 | /// The version of the class ABI. This value is used in the class structure |
| 438 | /// and indicates how various fields should be interpreted. |
| 439 | const int ClassABIVersion; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 440 | /// Generates an instance variable list structure. This is a structure |
| 441 | /// containing a size and an array of structures containing instance variable |
| 442 | /// metadata. This is used purely for introspection in the fragile ABI. In |
| 443 | /// the non-fragile ABI, it's used for instance variable fixup. |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 444 | virtual llvm::Constant *GenerateIvarList(ArrayRef<llvm::Constant *> IvarNames, |
| 445 | ArrayRef<llvm::Constant *> IvarTypes, |
| 446 | ArrayRef<llvm::Constant *> IvarOffsets, |
| 447 | ArrayRef<llvm::Constant *> IvarAlign, |
| 448 | ArrayRef<Qualifiers::ObjCLifetime> IvarOwnership); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 449 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 450 | /// Generates a method list structure. This is a structure containing a size |
| 451 | /// and an array of structures containing method metadata. |
| 452 | /// |
| 453 | /// This structure is used by both classes and categories, and contains a next |
| 454 | /// pointer allowing them to be chained together in a linked list. |
Craig Topper | bf3e327 | 2014-08-30 16:55:52 +0000 | [diff] [blame] | 455 | llvm::Constant *GenerateMethodList(StringRef ClassName, |
| 456 | StringRef CategoryName, |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 457 | ArrayRef<const ObjCMethodDecl*> Methods, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 458 | bool isClassMethodList); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 459 | |
James Dennett | b9199ee | 2012-06-13 22:07:09 +0000 | [diff] [blame] | 460 | /// Emits an empty protocol. This is used for \@protocol() where no protocol |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 461 | /// is found. The runtime will (hopefully) fix up the pointer to refer to the |
| 462 | /// real protocol. |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 463 | virtual llvm::Constant *GenerateEmptyProtocol(StringRef ProtocolName); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 464 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 465 | /// Generates a list of property metadata structures. This follows the same |
| 466 | /// pattern as method and instance variable metadata lists. |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 467 | llvm::Constant *GeneratePropertyList(const Decl *Container, |
| 468 | const ObjCContainerDecl *OCD, |
| 469 | bool isClassProperty=false, |
| 470 | bool protocolOptionalProperties=false); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 471 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 472 | /// Generates a list of referenced protocols. Classes, categories, and |
| 473 | /// protocols all use this structure. |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 474 | llvm::Constant *GenerateProtocolList(ArrayRef<std::string> Protocols); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 475 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 476 | /// To ensure that all protocols are seen by the runtime, we add a category on |
| 477 | /// a class defined in the runtime, declaring no methods, but adopting the |
| 478 | /// protocols. This is a horribly ugly hack, but it allows us to collect all |
| 479 | /// of the protocols without changing the ABI. |
Dmitri Gribenko | b2aa923 | 2012-11-15 14:28:07 +0000 | [diff] [blame] | 480 | void GenerateProtocolHolderCategory(); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 481 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 482 | /// Generates a class structure. |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 483 | llvm::Constant *GenerateClassStructure( |
| 484 | llvm::Constant *MetaClass, |
| 485 | llvm::Constant *SuperClass, |
| 486 | unsigned info, |
Chris Lattner | da35bc8 | 2008-06-26 04:47:04 +0000 | [diff] [blame] | 487 | const char *Name, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 488 | llvm::Constant *Version, |
| 489 | llvm::Constant *InstanceSize, |
| 490 | llvm::Constant *IVars, |
| 491 | llvm::Constant *Methods, |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 492 | llvm::Constant *Protocols, |
| 493 | llvm::Constant *IvarOffsets, |
David Chisnall | d472c85 | 2010-04-28 14:29:56 +0000 | [diff] [blame] | 494 | llvm::Constant *Properties, |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 495 | llvm::Constant *StrongIvarBitmap, |
| 496 | llvm::Constant *WeakIvarBitmap, |
David Chisnall | d472c85 | 2010-04-28 14:29:56 +0000 | [diff] [blame] | 497 | bool isMeta=false); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 498 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 499 | /// Generates a method list. This is used by protocols to define the required |
| 500 | /// and optional methods. |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 501 | virtual llvm::Constant *GenerateProtocolMethodList( |
| 502 | ArrayRef<const ObjCMethodDecl*> Methods); |
| 503 | /// Emits optional and required method lists. |
| 504 | template<class T> |
| 505 | void EmitProtocolMethodList(T &&Methods, llvm::Constant *&Required, |
| 506 | llvm::Constant *&Optional) { |
| 507 | SmallVector<const ObjCMethodDecl*, 16> RequiredMethods; |
| 508 | SmallVector<const ObjCMethodDecl*, 16> OptionalMethods; |
| 509 | for (const auto *I : Methods) |
| 510 | if (I->isOptional()) |
| 511 | OptionalMethods.push_back(I); |
| 512 | else |
| 513 | RequiredMethods.push_back(I); |
| 514 | Required = GenerateProtocolMethodList(RequiredMethods); |
| 515 | Optional = GenerateProtocolMethodList(OptionalMethods); |
| 516 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 517 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 518 | /// Returns a selector with the specified type encoding. An empty string is |
| 519 | /// used to return an untyped selector (with the types field set to NULL). |
Simon Pilgrim | 04c5a34 | 2018-08-08 15:53:14 +0000 | [diff] [blame] | 520 | virtual llvm::Value *GetTypedSelector(CodeGenFunction &CGF, Selector Sel, |
| 521 | const std::string &TypeEncoding); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 522 | |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 523 | /// Returns the name of ivar offset variables. In the GNUstep v1 ABI, this |
| 524 | /// contains the class and ivar names, in the v2 ABI this contains the type |
| 525 | /// encoding as well. |
| 526 | virtual std::string GetIVarOffsetVariableName(const ObjCInterfaceDecl *ID, |
| 527 | const ObjCIvarDecl *Ivar) { |
| 528 | const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString() |
| 529 | + '.' + Ivar->getNameAsString(); |
| 530 | return Name; |
| 531 | } |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 532 | /// Returns the variable used to store the offset of an instance variable. |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 533 | llvm::GlobalVariable *ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID, |
| 534 | const ObjCIvarDecl *Ivar); |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 535 | /// Emits a reference to a class. This allows the linker to object if there |
| 536 | /// is no class of the matching name. |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 537 | void EmitClassRef(const std::string &className); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 538 | |
David Chisnall | 920e83b | 2011-06-29 13:16:41 +0000 | [diff] [blame] | 539 | /// Emits a pointer to the named class |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 540 | virtual llvm::Value *GetClassNamed(CodeGenFunction &CGF, |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 541 | const std::string &Name, bool isWeak); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 542 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 543 | /// Looks up the method for sending a message to the specified object. This |
| 544 | /// mechanism differs between the GCC and GNU runtimes, so this method must be |
| 545 | /// overridden in subclasses. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 546 | virtual llvm::Value *LookupIMP(CodeGenFunction &CGF, |
| 547 | llvm::Value *&Receiver, |
| 548 | llvm::Value *cmd, |
Eli Friedman | f24bd3b | 2013-07-26 00:53:29 +0000 | [diff] [blame] | 549 | llvm::MDNode *node, |
| 550 | MessageSendInfo &MSI) = 0; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 551 | |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 552 | /// Looks up the method for sending a message to a superclass. This |
| 553 | /// mechanism differs between the GCC and GNU runtimes, so this method must |
| 554 | /// be overridden in subclasses. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 555 | virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 556 | Address ObjCSuper, |
Eli Friedman | f24bd3b | 2013-07-26 00:53:29 +0000 | [diff] [blame] | 557 | llvm::Value *cmd, |
| 558 | MessageSendInfo &MSI) = 0; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 559 | |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 560 | /// Libobjc2 uses a bitfield representation where small(ish) bitfields are |
| 561 | /// stored in a 64-bit value with the low bit set to 1 and the remaining 63 |
| 562 | /// bits set to their values, LSB first, while larger ones are stored in a |
| 563 | /// structure of this / form: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 564 | /// |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 565 | /// struct { int32_t length; int32_t values[length]; }; |
| 566 | /// |
| 567 | /// The values in the array are stored in host-endian format, with the least |
| 568 | /// significant bit being assumed to come first in the bitfield. Therefore, |
| 569 | /// a bitfield with the 64th bit set will be (int64_t)&{ 2, [0, 1<<31] }, |
| 570 | /// while a bitfield / with the 63rd bit set will be 1<<64. |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 571 | llvm::Constant *MakeBitField(ArrayRef<bool> bits); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 572 | |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 573 | public: |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 574 | CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion, |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 575 | unsigned protocolClassVersion, unsigned classABI=1); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 576 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 577 | ConstantAddress GenerateConstantString(const StringLiteral *) override; |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 578 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 579 | RValue |
| 580 | GenerateMessageSend(CodeGenFunction &CGF, ReturnValueSlot Return, |
| 581 | QualType ResultType, Selector Sel, |
| 582 | llvm::Value *Receiver, const CallArgList &CallArgs, |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 583 | const ObjCInterfaceDecl *Class, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 584 | const ObjCMethodDecl *Method) override; |
| 585 | RValue |
| 586 | GenerateMessageSendSuper(CodeGenFunction &CGF, ReturnValueSlot Return, |
| 587 | QualType ResultType, Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 588 | const ObjCInterfaceDecl *Class, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 589 | bool isCategoryImpl, llvm::Value *Receiver, |
| 590 | bool IsClassMessage, const CallArgList &CallArgs, |
| 591 | const ObjCMethodDecl *Method) override; |
| 592 | llvm::Value *GetClass(CodeGenFunction &CGF, |
| 593 | const ObjCInterfaceDecl *OID) override; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 594 | llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel) override; |
| 595 | Address GetAddrOfSelector(CodeGenFunction &CGF, Selector Sel) override; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 596 | llvm::Value *GetSelector(CodeGenFunction &CGF, |
| 597 | const ObjCMethodDecl *Method) override; |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 598 | virtual llvm::Constant *GetConstantSelector(Selector Sel, |
| 599 | const std::string &TypeEncoding) { |
| 600 | llvm_unreachable("Runtime unable to generate constant selector"); |
| 601 | } |
| 602 | llvm::Constant *GetConstantSelector(const ObjCMethodDecl *M) { |
| 603 | return GetConstantSelector(M->getSelector(), |
| 604 | CGM.getContext().getObjCEncodingForMethodDecl(M)); |
| 605 | } |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 606 | llvm::Constant *GetEHType(QualType T) override; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 607 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 608 | llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 609 | const ObjCContainerDecl *CD) override; |
Pierre Habouzit | d4e1ba3 | 2019-11-07 23:14:58 -0800 | [diff] [blame] | 610 | void GenerateDirectMethodPrologue(CodeGenFunction &CGF, llvm::Function *Fn, |
| 611 | const ObjCMethodDecl *OMD, |
| 612 | const ObjCContainerDecl *CD) override; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 613 | void GenerateCategory(const ObjCCategoryImplDecl *CMD) override; |
| 614 | void GenerateClass(const ObjCImplementationDecl *ClassDecl) override; |
| 615 | void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) override; |
| 616 | llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF, |
| 617 | const ObjCProtocolDecl *PD) override; |
| 618 | void GenerateProtocol(const ObjCProtocolDecl *PD) override; |
| 619 | llvm::Function *ModuleInitFunction() override; |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 620 | llvm::FunctionCallee GetPropertyGetFunction() override; |
| 621 | llvm::FunctionCallee GetPropertySetFunction() override; |
| 622 | llvm::FunctionCallee GetOptimizedPropertySetFunction(bool atomic, |
| 623 | bool copy) override; |
| 624 | llvm::FunctionCallee GetSetStructFunction() override; |
| 625 | llvm::FunctionCallee GetGetStructFunction() override; |
| 626 | llvm::FunctionCallee GetCppAtomicObjectGetFunction() override; |
| 627 | llvm::FunctionCallee GetCppAtomicObjectSetFunction() override; |
| 628 | llvm::FunctionCallee EnumerationMutationFunction() override; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 629 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 630 | void EmitTryStmt(CodeGenFunction &CGF, |
| 631 | const ObjCAtTryStmt &S) override; |
| 632 | void EmitSynchronizedStmt(CodeGenFunction &CGF, |
| 633 | const ObjCAtSynchronizedStmt &S) override; |
| 634 | void EmitThrowStmt(CodeGenFunction &CGF, |
| 635 | const ObjCAtThrowStmt &S, |
| 636 | bool ClearInsertionPoint=true) override; |
| 637 | llvm::Value * EmitObjCWeakRead(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 638 | Address AddrWeakObj) override; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 639 | void EmitObjCWeakAssign(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 640 | llvm::Value *src, Address dst) override; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 641 | void EmitObjCGlobalAssign(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 642 | llvm::Value *src, Address dest, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 643 | bool threadlocal=false) override; |
| 644 | void EmitObjCIvarAssign(CodeGenFunction &CGF, llvm::Value *src, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 645 | Address dest, llvm::Value *ivarOffset) override; |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 646 | void EmitObjCStrongCastAssign(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 647 | llvm::Value *src, Address dest) override; |
| 648 | void EmitGCMemmoveCollectable(CodeGenFunction &CGF, Address DestPtr, |
| 649 | Address SrcPtr, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 650 | llvm::Value *Size) override; |
| 651 | LValue EmitObjCValueForIvar(CodeGenFunction &CGF, QualType ObjectTy, |
| 652 | llvm::Value *BaseValue, const ObjCIvarDecl *Ivar, |
| 653 | unsigned CVRQualifiers) override; |
| 654 | llvm::Value *EmitIvarOffset(CodeGenFunction &CGF, |
| 655 | const ObjCInterfaceDecl *Interface, |
| 656 | const ObjCIvarDecl *Ivar) override; |
| 657 | llvm::Value *EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) override; |
| 658 | llvm::Constant *BuildGCBlockLayout(CodeGenModule &CGM, |
| 659 | const CGBlockInfo &blockInfo) override { |
Fariborz Jahanian | c05349e | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 660 | return NULLPtr; |
| 661 | } |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 662 | llvm::Constant *BuildRCBlockLayout(CodeGenModule &CGM, |
| 663 | const CGBlockInfo &blockInfo) override { |
Fariborz Jahanian | 0c58ce9 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 664 | return NULLPtr; |
| 665 | } |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 666 | |
| 667 | llvm::Constant *BuildByrefLayout(CodeGenModule &CGM, QualType T) override { |
Fariborz Jahanian | a9d4464 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 668 | return NULLPtr; |
| 669 | } |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 670 | }; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 671 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 672 | /// Class representing the legacy GCC Objective-C ABI. This is the default when |
| 673 | /// -fobjc-nonfragile-abi is not specified. |
| 674 | /// |
| 675 | /// The GCC ABI target actually generates code that is approximately compatible |
| 676 | /// with the new GNUstep runtime ABI, but refrains from using any features that |
| 677 | /// would not work with the GCC runtime. For example, clang always generates |
| 678 | /// the extended form of the class structure, and the extra fields are simply |
| 679 | /// ignored by GCC libobjc. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 680 | class CGObjCGCC : public CGObjCGNU { |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 681 | /// The GCC ABI message lookup function. Returns an IMP pointing to the |
| 682 | /// method implementation for this message. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 683 | LazyRuntimeFunction MsgLookupFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 684 | /// The GCC ABI superclass message lookup function. Takes a pointer to a |
| 685 | /// structure describing the receiver and the class, and a selector as |
| 686 | /// arguments. Returns the IMP for the corresponding method. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 687 | LazyRuntimeFunction MsgLookupSuperFn; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 688 | |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 689 | protected: |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 690 | llvm::Value *LookupIMP(CodeGenFunction &CGF, llvm::Value *&Receiver, |
| 691 | llvm::Value *cmd, llvm::MDNode *node, |
| 692 | MessageSendInfo &MSI) override { |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 693 | CGBuilderTy &Builder = CGF.Builder; |
David Chisnall | 0cc83e7 | 2011-10-28 17:55:06 +0000 | [diff] [blame] | 694 | llvm::Value *args[] = { |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 695 | EnforceType(Builder, Receiver, IdTy), |
David Chisnall | 0cc83e7 | 2011-10-28 17:55:06 +0000 | [diff] [blame] | 696 | EnforceType(Builder, cmd, SelectorTy) }; |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 697 | llvm::CallBase *imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFn, args); |
David Chisnall | 0cc83e7 | 2011-10-28 17:55:06 +0000 | [diff] [blame] | 698 | imp->setMetadata(msgSendMDKind, node); |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 699 | return imp; |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 700 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 701 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 702 | llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, Address ObjCSuper, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 703 | llvm::Value *cmd, MessageSendInfo &MSI) override { |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 704 | CGBuilderTy &Builder = CGF.Builder; |
| 705 | llvm::Value *lookupArgs[] = {EnforceType(Builder, ObjCSuper, |
| 706 | PtrToObjCSuperTy).getPointer(), cmd}; |
| 707 | return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFn, lookupArgs); |
| 708 | } |
| 709 | |
| 710 | public: |
| 711 | CGObjCGCC(CodeGenModule &Mod) : CGObjCGNU(Mod, 8, 2) { |
| 712 | // IMP objc_msg_lookup(id, SEL); |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 713 | MsgLookupFn.init(&CGM, "objc_msg_lookup", IMPTy, IdTy, SelectorTy); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 714 | // IMP objc_msg_lookup_super(struct objc_super*, SEL); |
| 715 | MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy, |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 716 | PtrToObjCSuperTy, SelectorTy); |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 717 | } |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 718 | }; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 719 | |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 720 | /// Class used when targeting the new GNUstep runtime ABI. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 721 | class CGObjCGNUstep : public CGObjCGNU { |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 722 | /// The slot lookup function. Returns a pointer to a cacheable structure |
| 723 | /// that contains (among other things) the IMP. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 724 | LazyRuntimeFunction SlotLookupFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 725 | /// The GNUstep ABI superclass message lookup function. Takes a pointer to |
| 726 | /// a structure describing the receiver and the class, and a selector as |
| 727 | /// arguments. Returns the slot for the corresponding method. Superclass |
| 728 | /// message lookup rarely changes, so this is a good caching opportunity. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 729 | LazyRuntimeFunction SlotLookupSuperFn; |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 730 | /// Specialised function for setting atomic retain properties |
| 731 | LazyRuntimeFunction SetPropertyAtomic; |
| 732 | /// Specialised function for setting atomic copy properties |
| 733 | LazyRuntimeFunction SetPropertyAtomicCopy; |
| 734 | /// Specialised function for setting nonatomic retain properties |
| 735 | LazyRuntimeFunction SetPropertyNonAtomic; |
| 736 | /// Specialised function for setting nonatomic copy properties |
| 737 | LazyRuntimeFunction SetPropertyNonAtomicCopy; |
| 738 | /// Function to perform atomic copies of C++ objects with nontrivial copy |
| 739 | /// constructors from Objective-C ivars. |
| 740 | LazyRuntimeFunction CxxAtomicObjectGetFn; |
| 741 | /// Function to perform atomic copies of C++ objects with nontrivial copy |
| 742 | /// constructors to Objective-C ivars. |
| 743 | LazyRuntimeFunction CxxAtomicObjectSetFn; |
David Chisnall | 34d0005 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 744 | /// Type of an slot structure pointer. This is returned by the various |
| 745 | /// lookup functions. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 746 | llvm::Type *SlotTy; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 747 | |
John McCall | c31d893 | 2012-11-14 09:08:34 +0000 | [diff] [blame] | 748 | public: |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 749 | llvm::Constant *GetEHType(QualType T) override; |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 750 | |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 751 | protected: |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 752 | llvm::Value *LookupIMP(CodeGenFunction &CGF, llvm::Value *&Receiver, |
| 753 | llvm::Value *cmd, llvm::MDNode *node, |
| 754 | MessageSendInfo &MSI) override { |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 755 | CGBuilderTy &Builder = CGF.Builder; |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 756 | llvm::FunctionCallee LookupFn = SlotLookupFn; |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 757 | |
| 758 | // 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] | 759 | Address ReceiverPtr = |
| 760 | CGF.CreateTempAlloca(Receiver->getType(), CGF.getPointerAlign()); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 761 | Builder.CreateStore(Receiver, ReceiverPtr); |
| 762 | |
| 763 | llvm::Value *self; |
| 764 | |
| 765 | if (isa<ObjCMethodDecl>(CGF.CurCodeDecl)) { |
| 766 | self = CGF.LoadObjCSelf(); |
| 767 | } else { |
| 768 | self = llvm::ConstantPointerNull::get(IdTy); |
| 769 | } |
| 770 | |
| 771 | // The lookup function is guaranteed not to capture the receiver pointer. |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 772 | if (auto *LookupFn2 = dyn_cast<llvm::Function>(LookupFn.getCallee())) |
| 773 | LookupFn2->addParamAttr(0, llvm::Attribute::NoCapture); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 774 | |
David Chisnall | 0cc83e7 | 2011-10-28 17:55:06 +0000 | [diff] [blame] | 775 | llvm::Value *args[] = { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 776 | EnforceType(Builder, ReceiverPtr.getPointer(), PtrToIdTy), |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 777 | EnforceType(Builder, cmd, SelectorTy), |
David Chisnall | 0cc83e7 | 2011-10-28 17:55:06 +0000 | [diff] [blame] | 778 | EnforceType(Builder, self, IdTy) }; |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 779 | llvm::CallBase *slot = CGF.EmitRuntimeCallOrInvoke(LookupFn, args); |
| 780 | slot->setOnlyReadsMemory(); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 781 | slot->setMetadata(msgSendMDKind, node); |
| 782 | |
| 783 | // Load the imp from the slot |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 784 | llvm::Value *imp = Builder.CreateAlignedLoad( |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 785 | Builder.CreateStructGEP(nullptr, slot, 4), CGF.getPointerAlign()); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 786 | |
| 787 | // The lookup function may have changed the receiver, so make sure we use |
| 788 | // the new one. |
| 789 | Receiver = Builder.CreateLoad(ReceiverPtr, true); |
| 790 | return imp; |
| 791 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 792 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 793 | llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, Address ObjCSuper, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 794 | llvm::Value *cmd, |
| 795 | MessageSendInfo &MSI) override { |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 796 | CGBuilderTy &Builder = CGF.Builder; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 797 | llvm::Value *lookupArgs[] = {ObjCSuper.getPointer(), cmd}; |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 798 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 799 | llvm::CallInst *slot = |
| 800 | CGF.EmitNounwindRuntimeCall(SlotLookupSuperFn, lookupArgs); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 801 | slot->setOnlyReadsMemory(); |
| 802 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 803 | return Builder.CreateAlignedLoad(Builder.CreateStructGEP(nullptr, slot, 4), |
| 804 | CGF.getPointerAlign()); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 805 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 806 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 807 | public: |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 808 | CGObjCGNUstep(CodeGenModule &Mod) : CGObjCGNUstep(Mod, 9, 3, 1) {} |
| 809 | CGObjCGNUstep(CodeGenModule &Mod, unsigned ABI, unsigned ProtocolABI, |
| 810 | unsigned ClassABI) : |
| 811 | CGObjCGNU(Mod, ABI, ProtocolABI, ClassABI) { |
David Chisnall | beb8013 | 2013-02-28 13:59:29 +0000 | [diff] [blame] | 812 | const ObjCRuntime &R = CGM.getLangOpts().ObjCRuntime; |
David Chisnall | 2ec1b10d | 2013-01-11 15:33:01 +0000 | [diff] [blame] | 813 | |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 814 | llvm::StructType *SlotStructTy = |
| 815 | llvm::StructType::get(PtrTy, PtrTy, PtrTy, IntTy, IMPTy); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 816 | SlotTy = llvm::PointerType::getUnqual(SlotStructTy); |
| 817 | // Slot_t objc_msg_lookup_sender(id *receiver, SEL selector, id sender); |
| 818 | SlotLookupFn.init(&CGM, "objc_msg_lookup_sender", SlotTy, PtrToIdTy, |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 819 | SelectorTy, IdTy); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 820 | // Slot_t objc_slot_lookup_super(struct objc_super*, SEL); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 821 | SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy, |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 822 | PtrToObjCSuperTy, SelectorTy); |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 823 | // If we're in ObjC++ mode, then we want to make |
| 824 | if (usesSEHExceptions) { |
| 825 | llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext); |
| 826 | // void objc_exception_rethrow(void) |
| 827 | ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy); |
| 828 | } else if (CGM.getLangOpts().CPlusPlus) { |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 829 | llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext); |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 830 | // void *__cxa_begin_catch(void *e) |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 831 | EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy); |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 832 | // void __cxa_end_catch(void) |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 833 | ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy); |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 834 | // void _Unwind_Resume_or_Rethrow(void*) |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 835 | ExceptionReThrowFn.init(&CGM, "_Unwind_Resume_or_Rethrow", VoidTy, |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 836 | PtrTy); |
David Chisnall | 2ec1b10d | 2013-01-11 15:33:01 +0000 | [diff] [blame] | 837 | } else if (R.getVersion() >= VersionTuple(1, 7)) { |
| 838 | llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext); |
| 839 | // id objc_begin_catch(void *e) |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 840 | EnterCatchFn.init(&CGM, "objc_begin_catch", IdTy, PtrTy); |
David Chisnall | 2ec1b10d | 2013-01-11 15:33:01 +0000 | [diff] [blame] | 841 | // void objc_end_catch(void) |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 842 | ExitCatchFn.init(&CGM, "objc_end_catch", VoidTy); |
David Chisnall | 2ec1b10d | 2013-01-11 15:33:01 +0000 | [diff] [blame] | 843 | // void _Unwind_Resume_or_Rethrow(void*) |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 844 | ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy, PtrTy); |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 845 | } |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 846 | llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext); |
| 847 | SetPropertyAtomic.init(&CGM, "objc_setProperty_atomic", VoidTy, IdTy, |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 848 | SelectorTy, IdTy, PtrDiffTy); |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 849 | SetPropertyAtomicCopy.init(&CGM, "objc_setProperty_atomic_copy", VoidTy, |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 850 | IdTy, SelectorTy, IdTy, PtrDiffTy); |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 851 | SetPropertyNonAtomic.init(&CGM, "objc_setProperty_nonatomic", VoidTy, |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 852 | IdTy, SelectorTy, IdTy, PtrDiffTy); |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 853 | SetPropertyNonAtomicCopy.init(&CGM, "objc_setProperty_nonatomic_copy", |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 854 | VoidTy, IdTy, SelectorTy, IdTy, PtrDiffTy); |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 855 | // void objc_setCppObjectAtomic(void *dest, const void *src, void |
| 856 | // *helper); |
| 857 | CxxAtomicObjectSetFn.init(&CGM, "objc_setCppObjectAtomic", VoidTy, PtrTy, |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 858 | PtrTy, PtrTy); |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 859 | // void objc_getCppObjectAtomic(void *dest, const void *src, void |
| 860 | // *helper); |
| 861 | CxxAtomicObjectGetFn.init(&CGM, "objc_getCppObjectAtomic", VoidTy, PtrTy, |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 862 | PtrTy, PtrTy); |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 863 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 864 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 865 | llvm::FunctionCallee GetCppAtomicObjectGetFunction() override { |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 866 | // The optimised functions were added in version 1.7 of the GNUstep |
| 867 | // runtime. |
| 868 | assert (CGM.getLangOpts().ObjCRuntime.getVersion() >= |
| 869 | VersionTuple(1, 7)); |
| 870 | return CxxAtomicObjectGetFn; |
| 871 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 872 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 873 | llvm::FunctionCallee GetCppAtomicObjectSetFunction() override { |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 874 | // The optimised functions were added in version 1.7 of the GNUstep |
| 875 | // runtime. |
| 876 | assert (CGM.getLangOpts().ObjCRuntime.getVersion() >= |
| 877 | VersionTuple(1, 7)); |
| 878 | return CxxAtomicObjectSetFn; |
| 879 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 880 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 881 | llvm::FunctionCallee GetOptimizedPropertySetFunction(bool atomic, |
| 882 | bool copy) override { |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 883 | // The optimised property functions omit the GC check, and so are not |
| 884 | // safe to use in GC mode. The standard functions are fast in GC mode, |
| 885 | // so there is less advantage in using them. |
| 886 | assert ((CGM.getLangOpts().getGC() == LangOptions::NonGC)); |
| 887 | // The optimised functions were added in version 1.7 of the GNUstep |
| 888 | // runtime. |
| 889 | assert (CGM.getLangOpts().ObjCRuntime.getVersion() >= |
| 890 | VersionTuple(1, 7)); |
| 891 | |
| 892 | if (atomic) { |
| 893 | if (copy) return SetPropertyAtomicCopy; |
| 894 | return SetPropertyAtomic; |
| 895 | } |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 896 | |
Ted Kremenek | 090a273 | 2014-03-07 18:53:05 +0000 | [diff] [blame] | 897 | return copy ? SetPropertyNonAtomicCopy : SetPropertyNonAtomic; |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 898 | } |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 899 | }; |
| 900 | |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 901 | /// GNUstep Objective-C ABI version 2 implementation. |
| 902 | /// This is the ABI that provides a clean break with the legacy GCC ABI and |
| 903 | /// cleans up a number of things that were added to work around 1980s linkers. |
| 904 | class CGObjCGNUstep2 : public CGObjCGNUstep { |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 905 | enum SectionKind |
| 906 | { |
| 907 | SelectorSection = 0, |
| 908 | ClassSection, |
| 909 | ClassReferenceSection, |
| 910 | CategorySection, |
| 911 | ProtocolSection, |
| 912 | ProtocolReferenceSection, |
| 913 | ClassAliasSection, |
| 914 | ConstantStringSection |
| 915 | }; |
| 916 | static const char *const SectionsBaseNames[8]; |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 917 | static const char *const PECOFFSectionsBaseNames[8]; |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 918 | template<SectionKind K> |
| 919 | std::string sectionName() { |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 920 | if (CGM.getTriple().isOSBinFormatCOFF()) { |
| 921 | std::string name(PECOFFSectionsBaseNames[K]); |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 922 | name += "$m"; |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 923 | return name; |
| 924 | } |
| 925 | return SectionsBaseNames[K]; |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 926 | } |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 927 | /// The GCC ABI superclass message lookup function. Takes a pointer to a |
| 928 | /// structure describing the receiver and the class, and a selector as |
| 929 | /// arguments. Returns the IMP for the corresponding method. |
| 930 | LazyRuntimeFunction MsgLookupSuperFn; |
| 931 | /// A flag indicating if we've emitted at least one protocol. |
| 932 | /// If we haven't, then we need to emit an empty protocol, to ensure that the |
| 933 | /// __start__objc_protocols and __stop__objc_protocols sections exist. |
| 934 | bool EmittedProtocol = false; |
| 935 | /// A flag indicating if we've emitted at least one protocol reference. |
| 936 | /// If we haven't, then we need to emit an empty protocol, to ensure that the |
| 937 | /// __start__objc_protocol_refs and __stop__objc_protocol_refs sections |
| 938 | /// exist. |
| 939 | bool EmittedProtocolRef = false; |
| 940 | /// A flag indicating if we've emitted at least one class. |
| 941 | /// If we haven't, then we need to emit an empty protocol, to ensure that the |
| 942 | /// __start__objc_classes and __stop__objc_classes sections / exist. |
| 943 | bool EmittedClass = false; |
| 944 | /// Generate the name of a symbol for a reference to a class. Accesses to |
| 945 | /// classes should be indirected via this. |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 946 | |
| 947 | typedef std::pair<std::string, std::pair<llvm::Constant*, int>> EarlyInitPair; |
| 948 | std::vector<EarlyInitPair> EarlyInitList; |
| 949 | |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 950 | std::string SymbolForClassRef(StringRef Name, bool isWeak) { |
| 951 | if (isWeak) |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 952 | return (ManglePublicSymbol("OBJC_WEAK_REF_CLASS_") + Name).str(); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 953 | else |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 954 | return (ManglePublicSymbol("OBJC_REF_CLASS_") + Name).str(); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 955 | } |
| 956 | /// Generate the name of a class symbol. |
| 957 | std::string SymbolForClass(StringRef Name) { |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 958 | return (ManglePublicSymbol("OBJC_CLASS_") + Name).str(); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 959 | } |
| 960 | void CallRuntimeFunction(CGBuilderTy &B, StringRef FunctionName, |
| 961 | ArrayRef<llvm::Value*> Args) { |
| 962 | SmallVector<llvm::Type *,8> Types; |
| 963 | for (auto *Arg : Args) |
| 964 | Types.push_back(Arg->getType()); |
| 965 | llvm::FunctionType *FT = llvm::FunctionType::get(B.getVoidTy(), Types, |
| 966 | false); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 967 | llvm::FunctionCallee Fn = CGM.CreateRuntimeFunction(FT, FunctionName); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 968 | B.CreateCall(Fn, Args); |
| 969 | } |
| 970 | |
| 971 | ConstantAddress GenerateConstantString(const StringLiteral *SL) override { |
| 972 | |
| 973 | auto Str = SL->getString(); |
| 974 | CharUnits Align = CGM.getPointerAlign(); |
| 975 | |
| 976 | // Look for an existing one |
| 977 | llvm::StringMap<llvm::Constant*>::iterator old = ObjCStrings.find(Str); |
| 978 | if (old != ObjCStrings.end()) |
| 979 | return ConstantAddress(old->getValue(), Align); |
| 980 | |
| 981 | bool isNonASCII = SL->containsNonAscii(); |
| 982 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 983 | auto LiteralLength = SL->getLength(); |
| 984 | |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 985 | if ((CGM.getTarget().getPointerWidth(0) == 64) && |
| 986 | (LiteralLength < 9) && !isNonASCII) { |
| 987 | // Tiny strings are only used on 64-bit platforms. They store 8 7-bit |
| 988 | // ASCII characters in the high 56 bits, followed by a 4-bit length and a |
| 989 | // 3-bit tag (which is always 4). |
| 990 | uint64_t str = 0; |
| 991 | // Fill in the characters |
| 992 | for (unsigned i=0 ; i<LiteralLength ; i++) |
| 993 | str |= ((uint64_t)SL->getCodeUnit(i)) << ((64 - 4 - 3) - (i*7)); |
| 994 | // Fill in the length |
| 995 | str |= LiteralLength << 3; |
| 996 | // Set the tag |
| 997 | str |= 4; |
| 998 | auto *ObjCStr = llvm::ConstantExpr::getIntToPtr( |
| 999 | llvm::ConstantInt::get(Int64Ty, str), IdTy); |
| 1000 | ObjCStrings[Str] = ObjCStr; |
| 1001 | return ConstantAddress(ObjCStr, Align); |
| 1002 | } |
| 1003 | |
| 1004 | StringRef StringClass = CGM.getLangOpts().ObjCConstantStringClass; |
| 1005 | |
| 1006 | if (StringClass.empty()) StringClass = "NSConstantString"; |
| 1007 | |
| 1008 | std::string Sym = SymbolForClass(StringClass); |
| 1009 | |
| 1010 | llvm::Constant *isa = TheModule.getNamedGlobal(Sym); |
| 1011 | |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 1012 | if (!isa) { |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1013 | isa = new llvm::GlobalVariable(TheModule, IdTy, /* isConstant */false, |
| 1014 | llvm::GlobalValue::ExternalLinkage, nullptr, Sym); |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 1015 | if (CGM.getTriple().isOSBinFormatCOFF()) { |
| 1016 | cast<llvm::GlobalValue>(isa)->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); |
| 1017 | } |
| 1018 | } else if (isa->getType() != PtrToIdTy) |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1019 | isa = llvm::ConstantExpr::getBitCast(isa, PtrToIdTy); |
| 1020 | |
| 1021 | // struct |
| 1022 | // { |
| 1023 | // Class isa; |
| 1024 | // uint32_t flags; |
| 1025 | // uint32_t length; // Number of codepoints |
| 1026 | // uint32_t size; // Number of bytes |
| 1027 | // uint32_t hash; |
| 1028 | // const char *data; |
| 1029 | // }; |
| 1030 | |
| 1031 | ConstantInitBuilder Builder(CGM); |
| 1032 | auto Fields = Builder.beginStruct(); |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 1033 | if (!CGM.getTriple().isOSBinFormatCOFF()) { |
| 1034 | Fields.add(isa); |
| 1035 | } else { |
| 1036 | Fields.addNullPointer(PtrTy); |
| 1037 | } |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1038 | // For now, all non-ASCII strings are represented as UTF-16. As such, the |
| 1039 | // number of bytes is simply double the number of UTF-16 codepoints. In |
| 1040 | // ASCII strings, the number of bytes is equal to the number of non-ASCII |
| 1041 | // codepoints. |
| 1042 | if (isNonASCII) { |
| 1043 | unsigned NumU8CodeUnits = Str.size(); |
| 1044 | // A UTF-16 representation of a unicode string contains at most the same |
| 1045 | // number of code units as a UTF-8 representation. Allocate that much |
| 1046 | // space, plus one for the final null character. |
| 1047 | SmallVector<llvm::UTF16, 128> ToBuf(NumU8CodeUnits + 1); |
| 1048 | const llvm::UTF8 *FromPtr = (const llvm::UTF8 *)Str.data(); |
| 1049 | llvm::UTF16 *ToPtr = &ToBuf[0]; |
| 1050 | (void)llvm::ConvertUTF8toUTF16(&FromPtr, FromPtr + NumU8CodeUnits, |
| 1051 | &ToPtr, ToPtr + NumU8CodeUnits, llvm::strictConversion); |
| 1052 | uint32_t StringLength = ToPtr - &ToBuf[0]; |
| 1053 | // Add null terminator |
| 1054 | *ToPtr = 0; |
| 1055 | // Flags: 2 indicates UTF-16 encoding |
| 1056 | Fields.addInt(Int32Ty, 2); |
| 1057 | // Number of UTF-16 codepoints |
| 1058 | Fields.addInt(Int32Ty, StringLength); |
| 1059 | // Number of bytes |
| 1060 | Fields.addInt(Int32Ty, StringLength * 2); |
| 1061 | // Hash. Not currently initialised by the compiler. |
| 1062 | Fields.addInt(Int32Ty, 0); |
| 1063 | // pointer to the data string. |
| 1064 | auto Arr = llvm::makeArrayRef(&ToBuf[0], ToPtr+1); |
| 1065 | auto *C = llvm::ConstantDataArray::get(VMContext, Arr); |
| 1066 | auto *Buffer = new llvm::GlobalVariable(TheModule, C->getType(), |
| 1067 | /*isConstant=*/true, llvm::GlobalValue::PrivateLinkage, C, ".str"); |
| 1068 | Buffer->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global); |
| 1069 | Fields.add(Buffer); |
| 1070 | } else { |
| 1071 | // Flags: 0 indicates ASCII encoding |
| 1072 | Fields.addInt(Int32Ty, 0); |
| 1073 | // Number of UTF-16 codepoints, each ASCII byte is a UTF-16 codepoint |
| 1074 | Fields.addInt(Int32Ty, Str.size()); |
| 1075 | // Number of bytes |
| 1076 | Fields.addInt(Int32Ty, Str.size()); |
| 1077 | // Hash. Not currently initialised by the compiler. |
| 1078 | Fields.addInt(Int32Ty, 0); |
| 1079 | // Data pointer |
| 1080 | Fields.add(MakeConstantString(Str)); |
| 1081 | } |
| 1082 | std::string StringName; |
| 1083 | bool isNamed = !isNonASCII; |
| 1084 | if (isNamed) { |
| 1085 | StringName = ".objc_str_"; |
| 1086 | for (int i=0,e=Str.size() ; i<e ; ++i) { |
David Chisnall | 48a7afa | 2018-05-22 10:13:17 +0000 | [diff] [blame] | 1087 | unsigned char c = Str[i]; |
David Chisnall | 88e754f | 2018-05-22 10:13:11 +0000 | [diff] [blame] | 1088 | if (isalnum(c)) |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1089 | StringName += c; |
| 1090 | else if (c == ' ') |
| 1091 | StringName += '_'; |
| 1092 | else { |
| 1093 | isNamed = false; |
| 1094 | break; |
| 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | auto *ObjCStrGV = |
| 1099 | Fields.finishAndCreateGlobal( |
| 1100 | isNamed ? StringRef(StringName) : ".objc_string", |
| 1101 | Align, false, isNamed ? llvm::GlobalValue::LinkOnceODRLinkage |
| 1102 | : llvm::GlobalValue::PrivateLinkage); |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 1103 | ObjCStrGV->setSection(sectionName<ConstantStringSection>()); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1104 | if (isNamed) { |
| 1105 | ObjCStrGV->setComdat(TheModule.getOrInsertComdat(StringName)); |
| 1106 | ObjCStrGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 1107 | } |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 1108 | if (CGM.getTriple().isOSBinFormatCOFF()) { |
| 1109 | std::pair<llvm::Constant*, int> v{ObjCStrGV, 0}; |
| 1110 | EarlyInitList.emplace_back(Sym, v); |
| 1111 | } |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1112 | llvm::Constant *ObjCStr = llvm::ConstantExpr::getBitCast(ObjCStrGV, IdTy); |
| 1113 | ObjCStrings[Str] = ObjCStr; |
| 1114 | ConstantStrings.push_back(ObjCStr); |
| 1115 | return ConstantAddress(ObjCStr, Align); |
| 1116 | } |
| 1117 | |
| 1118 | void PushProperty(ConstantArrayBuilder &PropertiesArray, |
| 1119 | const ObjCPropertyDecl *property, |
| 1120 | const Decl *OCD, |
| 1121 | bool isSynthesized=true, bool |
| 1122 | isDynamic=true) override { |
| 1123 | // struct objc_property |
| 1124 | // { |
| 1125 | // const char *name; |
| 1126 | // const char *attributes; |
| 1127 | // const char *type; |
| 1128 | // SEL getter; |
| 1129 | // SEL setter; |
| 1130 | // }; |
| 1131 | auto Fields = PropertiesArray.beginStruct(PropertyMetadataTy); |
| 1132 | ASTContext &Context = CGM.getContext(); |
| 1133 | Fields.add(MakeConstantString(property->getNameAsString())); |
| 1134 | std::string TypeStr = |
| 1135 | CGM.getContext().getObjCEncodingForPropertyDecl(property, OCD); |
| 1136 | Fields.add(MakeConstantString(TypeStr)); |
| 1137 | std::string typeStr; |
| 1138 | Context.getObjCEncodingForType(property->getType(), typeStr); |
| 1139 | Fields.add(MakeConstantString(typeStr)); |
| 1140 | auto addPropertyMethod = [&](const ObjCMethodDecl *accessor) { |
| 1141 | if (accessor) { |
| 1142 | std::string TypeStr = Context.getObjCEncodingForMethodDecl(accessor); |
| 1143 | Fields.add(GetConstantSelector(accessor->getSelector(), TypeStr)); |
| 1144 | } else { |
| 1145 | Fields.add(NULLPtr); |
| 1146 | } |
| 1147 | }; |
| 1148 | addPropertyMethod(property->getGetterMethodDecl()); |
| 1149 | addPropertyMethod(property->getSetterMethodDecl()); |
| 1150 | Fields.finishAndAddTo(PropertiesArray); |
| 1151 | } |
| 1152 | |
| 1153 | llvm::Constant * |
| 1154 | GenerateProtocolMethodList(ArrayRef<const ObjCMethodDecl*> Methods) override { |
| 1155 | // struct objc_protocol_method_description |
| 1156 | // { |
| 1157 | // SEL selector; |
| 1158 | // const char *types; |
| 1159 | // }; |
| 1160 | llvm::StructType *ObjCMethodDescTy = |
| 1161 | llvm::StructType::get(CGM.getLLVMContext(), |
| 1162 | { PtrToInt8Ty, PtrToInt8Ty }); |
| 1163 | ASTContext &Context = CGM.getContext(); |
| 1164 | ConstantInitBuilder Builder(CGM); |
| 1165 | // struct objc_protocol_method_description_list |
| 1166 | // { |
| 1167 | // int count; |
| 1168 | // int size; |
| 1169 | // struct objc_protocol_method_description methods[]; |
| 1170 | // }; |
| 1171 | auto MethodList = Builder.beginStruct(); |
| 1172 | // int count; |
| 1173 | MethodList.addInt(IntTy, Methods.size()); |
| 1174 | // int size; // sizeof(struct objc_method_description) |
| 1175 | llvm::DataLayout td(&TheModule); |
| 1176 | MethodList.addInt(IntTy, td.getTypeSizeInBits(ObjCMethodDescTy) / |
| 1177 | CGM.getContext().getCharWidth()); |
| 1178 | // struct objc_method_description[] |
| 1179 | auto MethodArray = MethodList.beginArray(ObjCMethodDescTy); |
| 1180 | for (auto *M : Methods) { |
| 1181 | auto Method = MethodArray.beginStruct(ObjCMethodDescTy); |
| 1182 | Method.add(CGObjCGNU::GetConstantSelector(M)); |
| 1183 | Method.add(GetTypeString(Context.getObjCEncodingForMethodDecl(M, true))); |
| 1184 | Method.finishAndAddTo(MethodArray); |
| 1185 | } |
| 1186 | MethodArray.finishAndAddTo(MethodList); |
| 1187 | return MethodList.finishAndCreateGlobal(".objc_protocol_method_list", |
| 1188 | CGM.getPointerAlign()); |
| 1189 | } |
David Chisnall | 386477a | 2018-12-28 17:44:54 +0000 | [diff] [blame] | 1190 | llvm::Constant *GenerateCategoryProtocolList(const ObjCCategoryDecl *OCD) |
| 1191 | override { |
| 1192 | SmallVector<llvm::Constant*, 16> Protocols; |
| 1193 | for (const auto *PI : OCD->getReferencedProtocols()) |
| 1194 | Protocols.push_back( |
| 1195 | llvm::ConstantExpr::getBitCast(GenerateProtocolRef(PI), |
| 1196 | ProtocolPtrTy)); |
| 1197 | return GenerateProtocolList(Protocols); |
| 1198 | } |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1199 | |
| 1200 | llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, Address ObjCSuper, |
| 1201 | llvm::Value *cmd, MessageSendInfo &MSI) override { |
| 1202 | // Don't access the slot unless we're trying to cache the result. |
| 1203 | CGBuilderTy &Builder = CGF.Builder; |
| 1204 | llvm::Value *lookupArgs[] = {CGObjCGNU::EnforceType(Builder, ObjCSuper, |
| 1205 | PtrToObjCSuperTy).getPointer(), cmd}; |
| 1206 | return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFn, lookupArgs); |
| 1207 | } |
| 1208 | |
| 1209 | llvm::GlobalVariable *GetClassVar(StringRef Name, bool isWeak=false) { |
| 1210 | std::string SymbolName = SymbolForClassRef(Name, isWeak); |
| 1211 | auto *ClassSymbol = TheModule.getNamedGlobal(SymbolName); |
| 1212 | if (ClassSymbol) |
| 1213 | return ClassSymbol; |
| 1214 | ClassSymbol = new llvm::GlobalVariable(TheModule, |
| 1215 | IdTy, false, llvm::GlobalValue::ExternalLinkage, |
| 1216 | nullptr, SymbolName); |
| 1217 | // If this is a weak symbol, then we are creating a valid definition for |
| 1218 | // the symbol, pointing to a weak definition of the real class pointer. If |
| 1219 | // this is not a weak reference, then we are expecting another compilation |
| 1220 | // unit to provide the real indirection symbol. |
| 1221 | if (isWeak) |
| 1222 | ClassSymbol->setInitializer(new llvm::GlobalVariable(TheModule, |
| 1223 | Int8Ty, false, llvm::GlobalValue::ExternalWeakLinkage, |
| 1224 | nullptr, SymbolForClass(Name))); |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 1225 | else { |
| 1226 | if (CGM.getTriple().isOSBinFormatCOFF()) { |
| 1227 | IdentifierInfo &II = CGM.getContext().Idents.get(Name); |
| 1228 | TranslationUnitDecl *TUDecl = CGM.getContext().getTranslationUnitDecl(); |
| 1229 | DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl); |
| 1230 | |
| 1231 | const ObjCInterfaceDecl *OID = nullptr; |
| 1232 | for (const auto &Result : DC->lookup(&II)) |
| 1233 | if ((OID = dyn_cast<ObjCInterfaceDecl>(Result))) |
| 1234 | break; |
| 1235 | |
| 1236 | // The first Interface we find may be a @class, |
| 1237 | // which should only be treated as the source of |
| 1238 | // truth in the absence of a true declaration. |
| 1239 | const ObjCInterfaceDecl *OIDDef = OID->getDefinition(); |
| 1240 | if (OIDDef != nullptr) |
| 1241 | OID = OIDDef; |
| 1242 | |
| 1243 | auto Storage = llvm::GlobalValue::DefaultStorageClass; |
| 1244 | if (OID->hasAttr<DLLImportAttr>()) |
| 1245 | Storage = llvm::GlobalValue::DLLImportStorageClass; |
| 1246 | else if (OID->hasAttr<DLLExportAttr>()) |
| 1247 | Storage = llvm::GlobalValue::DLLExportStorageClass; |
| 1248 | |
| 1249 | cast<llvm::GlobalValue>(ClassSymbol)->setDLLStorageClass(Storage); |
| 1250 | } |
| 1251 | } |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1252 | assert(ClassSymbol->getName() == SymbolName); |
| 1253 | return ClassSymbol; |
| 1254 | } |
| 1255 | llvm::Value *GetClassNamed(CodeGenFunction &CGF, |
| 1256 | const std::string &Name, |
| 1257 | bool isWeak) override { |
| 1258 | return CGF.Builder.CreateLoad(Address(GetClassVar(Name, isWeak), |
| 1259 | CGM.getPointerAlign())); |
| 1260 | } |
| 1261 | int32_t FlagsForOwnership(Qualifiers::ObjCLifetime Ownership) { |
| 1262 | // typedef enum { |
| 1263 | // ownership_invalid = 0, |
| 1264 | // ownership_strong = 1, |
| 1265 | // ownership_weak = 2, |
| 1266 | // ownership_unsafe = 3 |
| 1267 | // } ivar_ownership; |
| 1268 | int Flag; |
| 1269 | switch (Ownership) { |
| 1270 | case Qualifiers::OCL_Strong: |
| 1271 | Flag = 1; |
| 1272 | break; |
| 1273 | case Qualifiers::OCL_Weak: |
| 1274 | Flag = 2; |
| 1275 | break; |
| 1276 | case Qualifiers::OCL_ExplicitNone: |
| 1277 | Flag = 3; |
| 1278 | break; |
| 1279 | case Qualifiers::OCL_None: |
| 1280 | case Qualifiers::OCL_Autoreleasing: |
| 1281 | assert(Ownership != Qualifiers::OCL_Autoreleasing); |
| 1282 | Flag = 0; |
| 1283 | } |
| 1284 | return Flag; |
| 1285 | } |
| 1286 | llvm::Constant *GenerateIvarList(ArrayRef<llvm::Constant *> IvarNames, |
| 1287 | ArrayRef<llvm::Constant *> IvarTypes, |
| 1288 | ArrayRef<llvm::Constant *> IvarOffsets, |
| 1289 | ArrayRef<llvm::Constant *> IvarAlign, |
| 1290 | ArrayRef<Qualifiers::ObjCLifetime> IvarOwnership) override { |
| 1291 | llvm_unreachable("Method should not be called!"); |
| 1292 | } |
| 1293 | |
| 1294 | llvm::Constant *GenerateEmptyProtocol(StringRef ProtocolName) override { |
| 1295 | std::string Name = SymbolForProtocol(ProtocolName); |
| 1296 | auto *GV = TheModule.getGlobalVariable(Name); |
| 1297 | if (!GV) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1298 | // Emit a placeholder symbol. |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1299 | GV = new llvm::GlobalVariable(TheModule, ProtocolTy, false, |
| 1300 | llvm::GlobalValue::ExternalLinkage, nullptr, Name); |
Guillaume Chatelet | c79099e | 2019-10-03 13:00:29 +0000 | [diff] [blame] | 1301 | GV->setAlignment(CGM.getPointerAlign().getAsAlign()); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1302 | } |
| 1303 | return llvm::ConstantExpr::getBitCast(GV, ProtocolPtrTy); |
| 1304 | } |
| 1305 | |
| 1306 | /// Existing protocol references. |
| 1307 | llvm::StringMap<llvm::Constant*> ExistingProtocolRefs; |
| 1308 | |
| 1309 | llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF, |
| 1310 | const ObjCProtocolDecl *PD) override { |
| 1311 | auto Name = PD->getNameAsString(); |
| 1312 | auto *&Ref = ExistingProtocolRefs[Name]; |
| 1313 | if (!Ref) { |
| 1314 | auto *&Protocol = ExistingProtocols[Name]; |
| 1315 | if (!Protocol) |
| 1316 | Protocol = GenerateProtocolRef(PD); |
| 1317 | std::string RefName = SymbolForProtocolRef(Name); |
| 1318 | assert(!TheModule.getGlobalVariable(RefName)); |
| 1319 | // Emit a reference symbol. |
| 1320 | auto GV = new llvm::GlobalVariable(TheModule, ProtocolPtrTy, |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 1321 | false, llvm::GlobalValue::LinkOnceODRLinkage, |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1322 | llvm::ConstantExpr::getBitCast(Protocol, ProtocolPtrTy), RefName); |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 1323 | GV->setComdat(TheModule.getOrInsertComdat(RefName)); |
| 1324 | GV->setSection(sectionName<ProtocolReferenceSection>()); |
Guillaume Chatelet | c79099e | 2019-10-03 13:00:29 +0000 | [diff] [blame] | 1325 | GV->setAlignment(CGM.getPointerAlign().getAsAlign()); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1326 | Ref = GV; |
| 1327 | } |
| 1328 | EmittedProtocolRef = true; |
| 1329 | return CGF.Builder.CreateAlignedLoad(Ref, CGM.getPointerAlign()); |
| 1330 | } |
| 1331 | |
| 1332 | llvm::Constant *GenerateProtocolList(ArrayRef<llvm::Constant*> Protocols) { |
| 1333 | llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(ProtocolPtrTy, |
| 1334 | Protocols.size()); |
| 1335 | llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy, |
| 1336 | Protocols); |
| 1337 | ConstantInitBuilder builder(CGM); |
| 1338 | auto ProtocolBuilder = builder.beginStruct(); |
| 1339 | ProtocolBuilder.addNullPointer(PtrTy); |
| 1340 | ProtocolBuilder.addInt(SizeTy, Protocols.size()); |
| 1341 | ProtocolBuilder.add(ProtocolArray); |
| 1342 | return ProtocolBuilder.finishAndCreateGlobal(".objc_protocol_list", |
| 1343 | CGM.getPointerAlign(), false, llvm::GlobalValue::InternalLinkage); |
| 1344 | } |
| 1345 | |
| 1346 | void GenerateProtocol(const ObjCProtocolDecl *PD) override { |
| 1347 | // Do nothing - we only emit referenced protocols. |
| 1348 | } |
| 1349 | llvm::Constant *GenerateProtocolRef(const ObjCProtocolDecl *PD) { |
| 1350 | std::string ProtocolName = PD->getNameAsString(); |
| 1351 | auto *&Protocol = ExistingProtocols[ProtocolName]; |
| 1352 | if (Protocol) |
| 1353 | return Protocol; |
| 1354 | |
| 1355 | EmittedProtocol = true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1356 | |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 1357 | auto SymName = SymbolForProtocol(ProtocolName); |
| 1358 | auto *OldGV = TheModule.getGlobalVariable(SymName); |
| 1359 | |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1360 | // Use the protocol definition, if there is one. |
| 1361 | if (const ObjCProtocolDecl *Def = PD->getDefinition()) |
| 1362 | PD = Def; |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 1363 | else { |
| 1364 | // If there is no definition, then create an external linkage symbol and |
| 1365 | // hope that someone else fills it in for us (and fail to link if they |
| 1366 | // don't). |
| 1367 | assert(!OldGV); |
| 1368 | Protocol = new llvm::GlobalVariable(TheModule, ProtocolTy, |
| 1369 | /*isConstant*/false, |
| 1370 | llvm::GlobalValue::ExternalLinkage, nullptr, SymName); |
| 1371 | return Protocol; |
| 1372 | } |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1373 | |
| 1374 | SmallVector<llvm::Constant*, 16> Protocols; |
| 1375 | for (const auto *PI : PD->protocols()) |
| 1376 | Protocols.push_back( |
| 1377 | llvm::ConstantExpr::getBitCast(GenerateProtocolRef(PI), |
| 1378 | ProtocolPtrTy)); |
| 1379 | llvm::Constant *ProtocolList = GenerateProtocolList(Protocols); |
| 1380 | |
| 1381 | // Collect information about methods |
| 1382 | llvm::Constant *InstanceMethodList, *OptionalInstanceMethodList; |
| 1383 | llvm::Constant *ClassMethodList, *OptionalClassMethodList; |
| 1384 | EmitProtocolMethodList(PD->instance_methods(), InstanceMethodList, |
| 1385 | OptionalInstanceMethodList); |
| 1386 | EmitProtocolMethodList(PD->class_methods(), ClassMethodList, |
| 1387 | OptionalClassMethodList); |
| 1388 | |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1389 | // The isa pointer must be set to a magic number so the runtime knows it's |
| 1390 | // the correct layout. |
| 1391 | ConstantInitBuilder builder(CGM); |
| 1392 | auto ProtocolBuilder = builder.beginStruct(); |
| 1393 | ProtocolBuilder.add(llvm::ConstantExpr::getIntToPtr( |
| 1394 | llvm::ConstantInt::get(Int32Ty, ProtocolVersion), IdTy)); |
| 1395 | ProtocolBuilder.add(MakeConstantString(ProtocolName)); |
| 1396 | ProtocolBuilder.add(ProtocolList); |
| 1397 | ProtocolBuilder.add(InstanceMethodList); |
| 1398 | ProtocolBuilder.add(ClassMethodList); |
| 1399 | ProtocolBuilder.add(OptionalInstanceMethodList); |
| 1400 | ProtocolBuilder.add(OptionalClassMethodList); |
| 1401 | // Required instance properties |
| 1402 | ProtocolBuilder.add(GeneratePropertyList(nullptr, PD, false, false)); |
| 1403 | // Optional instance properties |
| 1404 | ProtocolBuilder.add(GeneratePropertyList(nullptr, PD, false, true)); |
| 1405 | // Required class properties |
| 1406 | ProtocolBuilder.add(GeneratePropertyList(nullptr, PD, true, false)); |
| 1407 | // Optional class properties |
| 1408 | ProtocolBuilder.add(GeneratePropertyList(nullptr, PD, true, true)); |
| 1409 | |
| 1410 | auto *GV = ProtocolBuilder.finishAndCreateGlobal(SymName, |
| 1411 | CGM.getPointerAlign(), false, llvm::GlobalValue::ExternalLinkage); |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 1412 | GV->setSection(sectionName<ProtocolSection>()); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1413 | GV->setComdat(TheModule.getOrInsertComdat(SymName)); |
| 1414 | if (OldGV) { |
| 1415 | OldGV->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GV, |
| 1416 | OldGV->getType())); |
| 1417 | OldGV->removeFromParent(); |
| 1418 | GV->setName(SymName); |
| 1419 | } |
| 1420 | Protocol = GV; |
| 1421 | return GV; |
| 1422 | } |
| 1423 | llvm::Constant *EnforceType(llvm::Constant *Val, llvm::Type *Ty) { |
| 1424 | if (Val->getType() == Ty) |
| 1425 | return Val; |
| 1426 | return llvm::ConstantExpr::getBitCast(Val, Ty); |
| 1427 | } |
Simon Pilgrim | 04c5a34 | 2018-08-08 15:53:14 +0000 | [diff] [blame] | 1428 | llvm::Value *GetTypedSelector(CodeGenFunction &CGF, Selector Sel, |
| 1429 | const std::string &TypeEncoding) override { |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1430 | return GetConstantSelector(Sel, TypeEncoding); |
| 1431 | } |
| 1432 | llvm::Constant *GetTypeString(llvm::StringRef TypeEncoding) { |
| 1433 | if (TypeEncoding.empty()) |
| 1434 | return NULLPtr; |
| 1435 | std::string MangledTypes = TypeEncoding; |
| 1436 | std::replace(MangledTypes.begin(), MangledTypes.end(), |
| 1437 | '@', '\1'); |
| 1438 | std::string TypesVarName = ".objc_sel_types_" + MangledTypes; |
| 1439 | auto *TypesGlobal = TheModule.getGlobalVariable(TypesVarName); |
| 1440 | if (!TypesGlobal) { |
| 1441 | llvm::Constant *Init = llvm::ConstantDataArray::getString(VMContext, |
| 1442 | TypeEncoding); |
| 1443 | auto *GV = new llvm::GlobalVariable(TheModule, Init->getType(), |
| 1444 | true, llvm::GlobalValue::LinkOnceODRLinkage, Init, TypesVarName); |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 1445 | GV->setComdat(TheModule.getOrInsertComdat(TypesVarName)); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1446 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 1447 | TypesGlobal = GV; |
| 1448 | } |
| 1449 | return llvm::ConstantExpr::getGetElementPtr(TypesGlobal->getValueType(), |
| 1450 | TypesGlobal, Zeros); |
| 1451 | } |
| 1452 | llvm::Constant *GetConstantSelector(Selector Sel, |
| 1453 | const std::string &TypeEncoding) override { |
| 1454 | // @ is used as a special character in symbol names (used for symbol |
| 1455 | // versioning), so mangle the name to not include it. Replace it with a |
| 1456 | // character that is not a valid type encoding character (and, being |
| 1457 | // non-printable, never will be!) |
| 1458 | std::string MangledTypes = TypeEncoding; |
| 1459 | std::replace(MangledTypes.begin(), MangledTypes.end(), |
| 1460 | '@', '\1'); |
| 1461 | auto SelVarName = (StringRef(".objc_selector_") + Sel.getAsString() + "_" + |
| 1462 | MangledTypes).str(); |
| 1463 | if (auto *GV = TheModule.getNamedGlobal(SelVarName)) |
| 1464 | return EnforceType(GV, SelectorTy); |
| 1465 | ConstantInitBuilder builder(CGM); |
| 1466 | auto SelBuilder = builder.beginStruct(); |
| 1467 | SelBuilder.add(ExportUniqueString(Sel.getAsString(), ".objc_sel_name_", |
| 1468 | true)); |
| 1469 | SelBuilder.add(GetTypeString(TypeEncoding)); |
| 1470 | auto *GV = SelBuilder.finishAndCreateGlobal(SelVarName, |
| 1471 | CGM.getPointerAlign(), false, llvm::GlobalValue::LinkOnceODRLinkage); |
| 1472 | GV->setComdat(TheModule.getOrInsertComdat(SelVarName)); |
| 1473 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 1474 | GV->setSection(sectionName<SelectorSection>()); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1475 | auto *SelVal = EnforceType(GV, SelectorTy); |
| 1476 | return SelVal; |
| 1477 | } |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 1478 | llvm::StructType *emptyStruct = nullptr; |
| 1479 | |
| 1480 | /// Return pointers to the start and end of a section. On ELF platforms, we |
| 1481 | /// use the __start_ and __stop_ symbols that GNU-compatible linkers will set |
| 1482 | /// to the start and end of section names, as long as those section names are |
| 1483 | /// valid identifiers and the symbols are referenced but not defined. On |
| 1484 | /// Windows, we use the fact that MSVC-compatible linkers will lexically sort |
| 1485 | /// by subsections and place everything that we want to reference in a middle |
| 1486 | /// subsection and then insert zero-sized symbols in subsections a and z. |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1487 | std::pair<llvm::Constant*,llvm::Constant*> |
| 1488 | GetSectionBounds(StringRef Section) { |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 1489 | if (CGM.getTriple().isOSBinFormatCOFF()) { |
| 1490 | if (emptyStruct == nullptr) { |
| 1491 | emptyStruct = llvm::StructType::create(VMContext, ".objc_section_sentinel"); |
| 1492 | emptyStruct->setBody({}, /*isPacked*/true); |
| 1493 | } |
| 1494 | auto ZeroInit = llvm::Constant::getNullValue(emptyStruct); |
| 1495 | auto Sym = [&](StringRef Prefix, StringRef SecSuffix) { |
| 1496 | auto *Sym = new llvm::GlobalVariable(TheModule, emptyStruct, |
| 1497 | /*isConstant*/false, |
| 1498 | llvm::GlobalValue::LinkOnceODRLinkage, ZeroInit, Prefix + |
| 1499 | Section); |
| 1500 | Sym->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 1501 | Sym->setSection((Section + SecSuffix).str()); |
| 1502 | Sym->setComdat(TheModule.getOrInsertComdat((Prefix + |
| 1503 | Section).str())); |
Guillaume Chatelet | c79099e | 2019-10-03 13:00:29 +0000 | [diff] [blame] | 1504 | Sym->setAlignment(CGM.getPointerAlign().getAsAlign()); |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 1505 | return Sym; |
| 1506 | }; |
| 1507 | return { Sym("__start_", "$a"), Sym("__stop", "$z") }; |
| 1508 | } |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1509 | auto *Start = new llvm::GlobalVariable(TheModule, PtrTy, |
| 1510 | /*isConstant*/false, |
| 1511 | llvm::GlobalValue::ExternalLinkage, nullptr, StringRef("__start_") + |
| 1512 | Section); |
| 1513 | Start->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 1514 | auto *Stop = new llvm::GlobalVariable(TheModule, PtrTy, |
| 1515 | /*isConstant*/false, |
| 1516 | llvm::GlobalValue::ExternalLinkage, nullptr, StringRef("__stop_") + |
| 1517 | Section); |
| 1518 | Stop->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 1519 | return { Start, Stop }; |
| 1520 | } |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 1521 | CatchTypeInfo getCatchAllTypeInfo() override { |
| 1522 | return CGM.getCXXABI().getCatchAllTypeInfo(); |
| 1523 | } |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1524 | llvm::Function *ModuleInitFunction() override { |
| 1525 | llvm::Function *LoadFunction = llvm::Function::Create( |
| 1526 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false), |
| 1527 | llvm::GlobalValue::LinkOnceODRLinkage, ".objcv2_load_function", |
| 1528 | &TheModule); |
| 1529 | LoadFunction->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 1530 | LoadFunction->setComdat(TheModule.getOrInsertComdat(".objcv2_load_function")); |
| 1531 | |
| 1532 | llvm::BasicBlock *EntryBB = |
| 1533 | llvm::BasicBlock::Create(VMContext, "entry", LoadFunction); |
| 1534 | CGBuilderTy B(CGM, VMContext); |
| 1535 | B.SetInsertPoint(EntryBB); |
| 1536 | ConstantInitBuilder builder(CGM); |
| 1537 | auto InitStructBuilder = builder.beginStruct(); |
| 1538 | InitStructBuilder.addInt(Int64Ty, 0); |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 1539 | auto §ionVec = CGM.getTriple().isOSBinFormatCOFF() ? PECOFFSectionsBaseNames : SectionsBaseNames; |
| 1540 | for (auto *s : sectionVec) { |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 1541 | auto bounds = GetSectionBounds(s); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1542 | InitStructBuilder.add(bounds.first); |
| 1543 | InitStructBuilder.add(bounds.second); |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 1544 | } |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1545 | auto *InitStruct = InitStructBuilder.finishAndCreateGlobal(".objc_init", |
| 1546 | CGM.getPointerAlign(), false, llvm::GlobalValue::LinkOnceODRLinkage); |
| 1547 | InitStruct->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 1548 | InitStruct->setComdat(TheModule.getOrInsertComdat(".objc_init")); |
| 1549 | |
| 1550 | CallRuntimeFunction(B, "__objc_load", {InitStruct});; |
| 1551 | B.CreateRetVoid(); |
| 1552 | // Make sure that the optimisers don't delete this function. |
| 1553 | CGM.addCompilerUsedGlobal(LoadFunction); |
| 1554 | // FIXME: Currently ELF only! |
| 1555 | // We have to do this by hand, rather than with @llvm.ctors, so that the |
| 1556 | // linker can remove the duplicate invocations. |
| 1557 | auto *InitVar = new llvm::GlobalVariable(TheModule, LoadFunction->getType(), |
| 1558 | /*isConstant*/true, llvm::GlobalValue::LinkOnceAnyLinkage, |
| 1559 | LoadFunction, ".objc_ctor"); |
| 1560 | // Check that this hasn't been renamed. This shouldn't happen, because |
| 1561 | // this function should be called precisely once. |
| 1562 | assert(InitVar->getName() == ".objc_ctor"); |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 1563 | // In Windows, initialisers are sorted by the suffix. XCL is for library |
| 1564 | // initialisers, which run before user initialisers. We are running |
| 1565 | // Objective-C loads at the end of library load. This means +load methods |
| 1566 | // will run before any other static constructors, but that static |
| 1567 | // constructors can see a fully initialised Objective-C state. |
| 1568 | if (CGM.getTriple().isOSBinFormatCOFF()) |
| 1569 | InitVar->setSection(".CRT$XCLz"); |
| 1570 | else |
David Chisnall | 0e9e02c | 2019-03-31 11:22:19 +0000 | [diff] [blame] | 1571 | { |
| 1572 | if (CGM.getCodeGenOpts().UseInitArray) |
| 1573 | InitVar->setSection(".init_array"); |
| 1574 | else |
| 1575 | InitVar->setSection(".ctors"); |
| 1576 | } |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1577 | InitVar->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 1578 | InitVar->setComdat(TheModule.getOrInsertComdat(".objc_ctor")); |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 1579 | CGM.addUsedGlobal(InitVar); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1580 | for (auto *C : Categories) { |
| 1581 | auto *Cat = cast<llvm::GlobalVariable>(C->stripPointerCasts()); |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 1582 | Cat->setSection(sectionName<CategorySection>()); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1583 | CGM.addUsedGlobal(Cat); |
| 1584 | } |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1585 | auto createNullGlobal = [&](StringRef Name, ArrayRef<llvm::Constant*> Init, |
| 1586 | StringRef Section) { |
| 1587 | auto nullBuilder = builder.beginStruct(); |
| 1588 | for (auto *F : Init) |
| 1589 | nullBuilder.add(F); |
| 1590 | auto GV = nullBuilder.finishAndCreateGlobal(Name, CGM.getPointerAlign(), |
| 1591 | false, llvm::GlobalValue::LinkOnceODRLinkage); |
| 1592 | GV->setSection(Section); |
| 1593 | GV->setComdat(TheModule.getOrInsertComdat(Name)); |
| 1594 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
| 1595 | CGM.addUsedGlobal(GV); |
| 1596 | return GV; |
| 1597 | }; |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 1598 | for (auto clsAlias : ClassAliases) |
| 1599 | createNullGlobal(std::string(".objc_class_alias") + |
| 1600 | clsAlias.second, { MakeConstantString(clsAlias.second), |
| 1601 | GetClassVar(clsAlias.first) }, sectionName<ClassAliasSection>()); |
| 1602 | // On ELF platforms, add a null value for each special section so that we |
| 1603 | // can always guarantee that the _start and _stop symbols will exist and be |
| 1604 | // meaningful. This is not required on COFF platforms, where our start and |
| 1605 | // stop symbols will create the section. |
| 1606 | if (!CGM.getTriple().isOSBinFormatCOFF()) { |
| 1607 | createNullGlobal(".objc_null_selector", {NULLPtr, NULLPtr}, |
| 1608 | sectionName<SelectorSection>()); |
| 1609 | if (Categories.empty()) |
| 1610 | createNullGlobal(".objc_null_category", {NULLPtr, NULLPtr, |
| 1611 | NULLPtr, NULLPtr, NULLPtr, NULLPtr, NULLPtr}, |
| 1612 | sectionName<CategorySection>()); |
| 1613 | if (!EmittedClass) { |
| 1614 | createNullGlobal(".objc_null_cls_init_ref", NULLPtr, |
David Chisnall | ddd0682 | 2018-12-27 14:44:36 +0000 | [diff] [blame] | 1615 | sectionName<ClassSection>()); |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 1616 | createNullGlobal(".objc_null_class_ref", { NULLPtr, NULLPtr }, |
| 1617 | sectionName<ClassReferenceSection>()); |
| 1618 | } |
| 1619 | if (!EmittedProtocol) |
| 1620 | createNullGlobal(".objc_null_protocol", {NULLPtr, NULLPtr, NULLPtr, |
| 1621 | NULLPtr, NULLPtr, NULLPtr, NULLPtr, NULLPtr, NULLPtr, NULLPtr, |
| 1622 | NULLPtr}, sectionName<ProtocolSection>()); |
| 1623 | if (!EmittedProtocolRef) |
| 1624 | createNullGlobal(".objc_null_protocol_ref", {NULLPtr}, |
| 1625 | sectionName<ProtocolReferenceSection>()); |
| 1626 | if (ClassAliases.empty()) |
| 1627 | createNullGlobal(".objc_null_class_alias", { NULLPtr, NULLPtr }, |
| 1628 | sectionName<ClassAliasSection>()); |
| 1629 | if (ConstantStrings.empty()) { |
| 1630 | auto i32Zero = llvm::ConstantInt::get(Int32Ty, 0); |
| 1631 | createNullGlobal(".objc_null_constant_string", { NULLPtr, i32Zero, |
| 1632 | i32Zero, i32Zero, i32Zero, NULLPtr }, |
| 1633 | sectionName<ConstantStringSection>()); |
| 1634 | } |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1635 | } |
| 1636 | ConstantStrings.clear(); |
| 1637 | Categories.clear(); |
| 1638 | Classes.clear(); |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 1639 | |
| 1640 | if (EarlyInitList.size() > 0) { |
| 1641 | auto *Init = llvm::Function::Create(llvm::FunctionType::get(CGM.VoidTy, |
| 1642 | {}), llvm::GlobalValue::InternalLinkage, ".objc_early_init", |
| 1643 | &CGM.getModule()); |
| 1644 | llvm::IRBuilder<> b(llvm::BasicBlock::Create(CGM.getLLVMContext(), "entry", |
| 1645 | Init)); |
| 1646 | for (const auto &lateInit : EarlyInitList) { |
| 1647 | auto *global = TheModule.getGlobalVariable(lateInit.first); |
| 1648 | if (global) { |
| 1649 | b.CreateAlignedStore(global, |
| 1650 | b.CreateStructGEP(lateInit.second.first, lateInit.second.second), CGM.getPointerAlign().getQuantity()); |
| 1651 | } |
| 1652 | } |
| 1653 | b.CreateRetVoid(); |
| 1654 | // We can't use the normal LLVM global initialisation array, because we |
| 1655 | // need to specify that this runs early in library initialisation. |
| 1656 | auto *InitVar = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), |
| 1657 | /*isConstant*/true, llvm::GlobalValue::InternalLinkage, |
| 1658 | Init, ".objc_early_init_ptr"); |
| 1659 | InitVar->setSection(".CRT$XCLb"); |
| 1660 | CGM.addUsedGlobal(InitVar); |
| 1661 | } |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 1662 | return nullptr; |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1663 | } |
| 1664 | /// In the v2 ABI, ivar offset variables use the type encoding in their name |
| 1665 | /// to trigger linker failures if the types don't match. |
| 1666 | std::string GetIVarOffsetVariableName(const ObjCInterfaceDecl *ID, |
| 1667 | const ObjCIvarDecl *Ivar) override { |
| 1668 | std::string TypeEncoding; |
| 1669 | CGM.getContext().getObjCEncodingForType(Ivar->getType(), TypeEncoding); |
| 1670 | // Prevent the @ from being interpreted as a symbol version. |
| 1671 | std::replace(TypeEncoding.begin(), TypeEncoding.end(), |
| 1672 | '@', '\1'); |
| 1673 | const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString() |
| 1674 | + '.' + Ivar->getNameAsString() + '.' + TypeEncoding; |
| 1675 | return Name; |
| 1676 | } |
| 1677 | llvm::Value *EmitIvarOffset(CodeGenFunction &CGF, |
| 1678 | const ObjCInterfaceDecl *Interface, |
| 1679 | const ObjCIvarDecl *Ivar) override { |
| 1680 | const std::string Name = GetIVarOffsetVariableName(Ivar->getContainingInterface(), Ivar); |
| 1681 | llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name); |
| 1682 | if (!IvarOffsetPointer) |
| 1683 | IvarOffsetPointer = new llvm::GlobalVariable(TheModule, IntTy, false, |
| 1684 | llvm::GlobalValue::ExternalLinkage, nullptr, Name); |
| 1685 | CharUnits Align = CGM.getIntAlign(); |
| 1686 | llvm::Value *Offset = CGF.Builder.CreateAlignedLoad(IvarOffsetPointer, Align); |
| 1687 | if (Offset->getType() != PtrDiffTy) |
| 1688 | Offset = CGF.Builder.CreateZExtOrBitCast(Offset, PtrDiffTy); |
| 1689 | return Offset; |
| 1690 | } |
| 1691 | void GenerateClass(const ObjCImplementationDecl *OID) override { |
| 1692 | ASTContext &Context = CGM.getContext(); |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 1693 | bool IsCOFF = CGM.getTriple().isOSBinFormatCOFF(); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1694 | |
| 1695 | // Get the class name |
| 1696 | ObjCInterfaceDecl *classDecl = |
| 1697 | const_cast<ObjCInterfaceDecl *>(OID->getClassInterface()); |
| 1698 | std::string className = classDecl->getNameAsString(); |
| 1699 | auto *classNameConstant = MakeConstantString(className); |
| 1700 | |
| 1701 | ConstantInitBuilder builder(CGM); |
| 1702 | auto metaclassFields = builder.beginStruct(); |
| 1703 | // struct objc_class *isa; |
| 1704 | metaclassFields.addNullPointer(PtrTy); |
| 1705 | // struct objc_class *super_class; |
| 1706 | metaclassFields.addNullPointer(PtrTy); |
| 1707 | // const char *name; |
| 1708 | metaclassFields.add(classNameConstant); |
| 1709 | // long version; |
| 1710 | metaclassFields.addInt(LongTy, 0); |
| 1711 | // unsigned long info; |
| 1712 | // objc_class_flag_meta |
| 1713 | metaclassFields.addInt(LongTy, 1); |
| 1714 | // long instance_size; |
| 1715 | // Setting this to zero is consistent with the older ABI, but it might be |
| 1716 | // more sensible to set this to sizeof(struct objc_class) |
| 1717 | metaclassFields.addInt(LongTy, 0); |
| 1718 | // struct objc_ivar_list *ivars; |
| 1719 | metaclassFields.addNullPointer(PtrTy); |
| 1720 | // struct objc_method_list *methods |
| 1721 | // FIXME: Almost identical code is copied and pasted below for the |
| 1722 | // class, but refactoring it cleanly requires C++14 generic lambdas. |
| 1723 | if (OID->classmeth_begin() == OID->classmeth_end()) |
| 1724 | metaclassFields.addNullPointer(PtrTy); |
| 1725 | else { |
| 1726 | SmallVector<ObjCMethodDecl*, 16> ClassMethods; |
| 1727 | ClassMethods.insert(ClassMethods.begin(), OID->classmeth_begin(), |
| 1728 | OID->classmeth_end()); |
| 1729 | metaclassFields.addBitCast( |
| 1730 | GenerateMethodList(className, "", ClassMethods, true), |
| 1731 | PtrTy); |
| 1732 | } |
| 1733 | // void *dtable; |
| 1734 | metaclassFields.addNullPointer(PtrTy); |
| 1735 | // IMP cxx_construct; |
| 1736 | metaclassFields.addNullPointer(PtrTy); |
| 1737 | // IMP cxx_destruct; |
| 1738 | metaclassFields.addNullPointer(PtrTy); |
| 1739 | // struct objc_class *subclass_list |
| 1740 | metaclassFields.addNullPointer(PtrTy); |
| 1741 | // struct objc_class *sibling_class |
| 1742 | metaclassFields.addNullPointer(PtrTy); |
| 1743 | // struct objc_protocol_list *protocols; |
| 1744 | metaclassFields.addNullPointer(PtrTy); |
| 1745 | // struct reference_list *extra_data; |
| 1746 | metaclassFields.addNullPointer(PtrTy); |
| 1747 | // long abi_version; |
| 1748 | metaclassFields.addInt(LongTy, 0); |
| 1749 | // struct objc_property_list *properties |
| 1750 | metaclassFields.add(GeneratePropertyList(OID, classDecl, /*isClassProperty*/true)); |
| 1751 | |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 1752 | auto *metaclass = metaclassFields.finishAndCreateGlobal( |
| 1753 | ManglePublicSymbol("OBJC_METACLASS_") + className, |
| 1754 | CGM.getPointerAlign()); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1755 | |
| 1756 | auto classFields = builder.beginStruct(); |
| 1757 | // struct objc_class *isa; |
| 1758 | classFields.add(metaclass); |
| 1759 | // struct objc_class *super_class; |
| 1760 | // Get the superclass name. |
| 1761 | const ObjCInterfaceDecl * SuperClassDecl = |
| 1762 | OID->getClassInterface()->getSuperClass(); |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 1763 | llvm::Constant *SuperClass = nullptr; |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1764 | if (SuperClassDecl) { |
| 1765 | auto SuperClassName = SymbolForClass(SuperClassDecl->getNameAsString()); |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 1766 | SuperClass = TheModule.getNamedGlobal(SuperClassName); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1767 | if (!SuperClass) |
| 1768 | { |
| 1769 | SuperClass = new llvm::GlobalVariable(TheModule, PtrTy, false, |
| 1770 | llvm::GlobalValue::ExternalLinkage, nullptr, SuperClassName); |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 1771 | if (IsCOFF) { |
| 1772 | auto Storage = llvm::GlobalValue::DefaultStorageClass; |
| 1773 | if (SuperClassDecl->hasAttr<DLLImportAttr>()) |
| 1774 | Storage = llvm::GlobalValue::DLLImportStorageClass; |
| 1775 | else if (SuperClassDecl->hasAttr<DLLExportAttr>()) |
| 1776 | Storage = llvm::GlobalValue::DLLExportStorageClass; |
| 1777 | |
| 1778 | cast<llvm::GlobalValue>(SuperClass)->setDLLStorageClass(Storage); |
| 1779 | } |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1780 | } |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 1781 | if (!IsCOFF) |
| 1782 | classFields.add(llvm::ConstantExpr::getBitCast(SuperClass, PtrTy)); |
| 1783 | else |
| 1784 | classFields.addNullPointer(PtrTy); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1785 | } else |
| 1786 | classFields.addNullPointer(PtrTy); |
| 1787 | // const char *name; |
| 1788 | classFields.add(classNameConstant); |
| 1789 | // long version; |
| 1790 | classFields.addInt(LongTy, 0); |
| 1791 | // unsigned long info; |
| 1792 | // !objc_class_flag_meta |
| 1793 | classFields.addInt(LongTy, 0); |
| 1794 | // long instance_size; |
| 1795 | int superInstanceSize = !SuperClassDecl ? 0 : |
| 1796 | Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize().getQuantity(); |
| 1797 | // Instance size is negative for classes that have not yet had their ivar |
| 1798 | // layout calculated. |
| 1799 | classFields.addInt(LongTy, |
| 1800 | 0 - (Context.getASTObjCImplementationLayout(OID).getSize().getQuantity() - |
| 1801 | superInstanceSize)); |
| 1802 | |
| 1803 | if (classDecl->all_declared_ivar_begin() == nullptr) |
| 1804 | classFields.addNullPointer(PtrTy); |
| 1805 | else { |
| 1806 | int ivar_count = 0; |
| 1807 | for (const ObjCIvarDecl *IVD = classDecl->all_declared_ivar_begin(); IVD; |
| 1808 | IVD = IVD->getNextIvar()) ivar_count++; |
| 1809 | llvm::DataLayout td(&TheModule); |
| 1810 | // struct objc_ivar_list *ivars; |
| 1811 | ConstantInitBuilder b(CGM); |
| 1812 | auto ivarListBuilder = b.beginStruct(); |
| 1813 | // int count; |
| 1814 | ivarListBuilder.addInt(IntTy, ivar_count); |
| 1815 | // size_t size; |
| 1816 | llvm::StructType *ObjCIvarTy = llvm::StructType::get( |
| 1817 | PtrToInt8Ty, |
| 1818 | PtrToInt8Ty, |
| 1819 | PtrToInt8Ty, |
| 1820 | Int32Ty, |
| 1821 | Int32Ty); |
| 1822 | ivarListBuilder.addInt(SizeTy, td.getTypeSizeInBits(ObjCIvarTy) / |
| 1823 | CGM.getContext().getCharWidth()); |
| 1824 | // struct objc_ivar ivars[] |
| 1825 | auto ivarArrayBuilder = ivarListBuilder.beginArray(); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1826 | for (const ObjCIvarDecl *IVD = classDecl->all_declared_ivar_begin(); IVD; |
| 1827 | IVD = IVD->getNextIvar()) { |
| 1828 | auto ivarTy = IVD->getType(); |
| 1829 | auto ivarBuilder = ivarArrayBuilder.beginStruct(); |
| 1830 | // const char *name; |
| 1831 | ivarBuilder.add(MakeConstantString(IVD->getNameAsString())); |
| 1832 | // const char *type; |
| 1833 | std::string TypeStr; |
| 1834 | //Context.getObjCEncodingForType(ivarTy, TypeStr, IVD, true); |
| 1835 | Context.getObjCEncodingForMethodParameter(Decl::OBJC_TQ_None, ivarTy, TypeStr, true); |
| 1836 | ivarBuilder.add(MakeConstantString(TypeStr)); |
| 1837 | // int *offset; |
| 1838 | uint64_t BaseOffset = ComputeIvarBaseOffset(CGM, OID, IVD); |
| 1839 | uint64_t Offset = BaseOffset - superInstanceSize; |
| 1840 | llvm::Constant *OffsetValue = llvm::ConstantInt::get(IntTy, Offset); |
| 1841 | std::string OffsetName = GetIVarOffsetVariableName(classDecl, IVD); |
| 1842 | llvm::GlobalVariable *OffsetVar = TheModule.getGlobalVariable(OffsetName); |
| 1843 | if (OffsetVar) |
| 1844 | OffsetVar->setInitializer(OffsetValue); |
| 1845 | else |
| 1846 | OffsetVar = new llvm::GlobalVariable(TheModule, IntTy, |
| 1847 | false, llvm::GlobalValue::ExternalLinkage, |
| 1848 | OffsetValue, OffsetName); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1849 | auto ivarVisibility = |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1850 | (IVD->getAccessControl() == ObjCIvarDecl::Private || |
| 1851 | IVD->getAccessControl() == ObjCIvarDecl::Package || |
| 1852 | classDecl->getVisibility() == HiddenVisibility) ? |
| 1853 | llvm::GlobalValue::HiddenVisibility : |
| 1854 | llvm::GlobalValue::DefaultVisibility; |
| 1855 | OffsetVar->setVisibility(ivarVisibility); |
| 1856 | ivarBuilder.add(OffsetVar); |
| 1857 | // Ivar size |
| 1858 | ivarBuilder.addInt(Int32Ty, |
David Chisnall | ccc4286 | 2019-02-03 15:05:52 +0000 | [diff] [blame] | 1859 | CGM.getContext().getTypeSizeInChars(ivarTy).getQuantity()); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1860 | // Alignment will be stored as a base-2 log of the alignment. |
Simon Pilgrim | d06ee79 | 2019-10-02 11:49:32 +0000 | [diff] [blame] | 1861 | unsigned align = |
| 1862 | llvm::Log2_32(Context.getTypeAlignInChars(ivarTy).getQuantity()); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1863 | // Objects that require more than 2^64-byte alignment should be impossible! |
| 1864 | assert(align < 64); |
| 1865 | // uint32_t flags; |
| 1866 | // Bits 0-1 are ownership. |
| 1867 | // Bit 2 indicates an extended type encoding |
| 1868 | // Bits 3-8 contain log2(aligment) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1869 | ivarBuilder.addInt(Int32Ty, |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1870 | (align << 3) | (1<<2) | |
| 1871 | FlagsForOwnership(ivarTy.getQualifiers().getObjCLifetime())); |
| 1872 | ivarBuilder.finishAndAddTo(ivarArrayBuilder); |
| 1873 | } |
| 1874 | ivarArrayBuilder.finishAndAddTo(ivarListBuilder); |
| 1875 | auto ivarList = ivarListBuilder.finishAndCreateGlobal(".objc_ivar_list", |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1876 | CGM.getPointerAlign(), /*constant*/ false, |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1877 | llvm::GlobalValue::PrivateLinkage); |
| 1878 | classFields.add(ivarList); |
| 1879 | } |
| 1880 | // struct objc_method_list *methods |
| 1881 | SmallVector<const ObjCMethodDecl*, 16> InstanceMethods; |
| 1882 | InstanceMethods.insert(InstanceMethods.begin(), OID->instmeth_begin(), |
| 1883 | OID->instmeth_end()); |
| 1884 | for (auto *propImpl : OID->property_impls()) |
| 1885 | if (propImpl->getPropertyImplementation() == |
| 1886 | ObjCPropertyImplDecl::Synthesize) { |
Adrian Prantl | 2073dd2 | 2019-11-04 14:28:14 -0800 | [diff] [blame] | 1887 | auto addIfExists = [&](const ObjCMethodDecl *OMD) { |
| 1888 | if (OMD && OMD->hasBody()) |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1889 | InstanceMethods.push_back(OMD); |
| 1890 | }; |
Adrian Prantl | 2073dd2 | 2019-11-04 14:28:14 -0800 | [diff] [blame] | 1891 | addIfExists(propImpl->getGetterMethodDecl()); |
| 1892 | addIfExists(propImpl->getSetterMethodDecl()); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1893 | } |
| 1894 | |
| 1895 | if (InstanceMethods.size() == 0) |
| 1896 | classFields.addNullPointer(PtrTy); |
| 1897 | else |
| 1898 | classFields.addBitCast( |
| 1899 | GenerateMethodList(className, "", InstanceMethods, false), |
| 1900 | PtrTy); |
| 1901 | // void *dtable; |
| 1902 | classFields.addNullPointer(PtrTy); |
| 1903 | // IMP cxx_construct; |
| 1904 | classFields.addNullPointer(PtrTy); |
| 1905 | // IMP cxx_destruct; |
| 1906 | classFields.addNullPointer(PtrTy); |
| 1907 | // struct objc_class *subclass_list |
| 1908 | classFields.addNullPointer(PtrTy); |
| 1909 | // struct objc_class *sibling_class |
| 1910 | classFields.addNullPointer(PtrTy); |
| 1911 | // struct objc_protocol_list *protocols; |
| 1912 | SmallVector<llvm::Constant*, 16> Protocols; |
| 1913 | for (const auto *I : classDecl->protocols()) |
| 1914 | Protocols.push_back( |
| 1915 | llvm::ConstantExpr::getBitCast(GenerateProtocolRef(I), |
| 1916 | ProtocolPtrTy)); |
| 1917 | if (Protocols.empty()) |
| 1918 | classFields.addNullPointer(PtrTy); |
| 1919 | else |
| 1920 | classFields.add(GenerateProtocolList(Protocols)); |
| 1921 | // struct reference_list *extra_data; |
| 1922 | classFields.addNullPointer(PtrTy); |
| 1923 | // long abi_version; |
| 1924 | classFields.addInt(LongTy, 0); |
| 1925 | // struct objc_property_list *properties |
| 1926 | classFields.add(GeneratePropertyList(OID, classDecl)); |
| 1927 | |
| 1928 | auto *classStruct = |
| 1929 | classFields.finishAndCreateGlobal(SymbolForClass(className), |
| 1930 | CGM.getPointerAlign(), false, llvm::GlobalValue::ExternalLinkage); |
| 1931 | |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1932 | auto *classRefSymbol = GetClassVar(className); |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 1933 | classRefSymbol->setSection(sectionName<ClassReferenceSection>()); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1934 | classRefSymbol->setInitializer(llvm::ConstantExpr::getBitCast(classStruct, IdTy)); |
| 1935 | |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 1936 | if (IsCOFF) { |
| 1937 | // we can't import a class struct. |
| 1938 | if (OID->getClassInterface()->hasAttr<DLLExportAttr>()) { |
| 1939 | cast<llvm::GlobalValue>(classStruct)->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); |
| 1940 | cast<llvm::GlobalValue>(classRefSymbol)->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass); |
| 1941 | } |
| 1942 | |
| 1943 | if (SuperClass) { |
| 1944 | std::pair<llvm::Constant*, int> v{classStruct, 1}; |
| 1945 | EarlyInitList.emplace_back(SuperClass->getName(), std::move(v)); |
| 1946 | } |
| 1947 | |
| 1948 | } |
| 1949 | |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1950 | |
| 1951 | // Resolve the class aliases, if they exist. |
| 1952 | // FIXME: Class pointer aliases shouldn't exist! |
| 1953 | if (ClassPtrAlias) { |
| 1954 | ClassPtrAlias->replaceAllUsesWith( |
| 1955 | llvm::ConstantExpr::getBitCast(classStruct, IdTy)); |
| 1956 | ClassPtrAlias->eraseFromParent(); |
| 1957 | ClassPtrAlias = nullptr; |
| 1958 | } |
| 1959 | if (auto Placeholder = |
| 1960 | TheModule.getNamedGlobal(SymbolForClass(className))) |
| 1961 | if (Placeholder != classStruct) { |
| 1962 | Placeholder->replaceAllUsesWith( |
| 1963 | llvm::ConstantExpr::getBitCast(classStruct, Placeholder->getType())); |
| 1964 | Placeholder->eraseFromParent(); |
| 1965 | classStruct->setName(SymbolForClass(className)); |
| 1966 | } |
| 1967 | if (MetaClassPtrAlias) { |
| 1968 | MetaClassPtrAlias->replaceAllUsesWith( |
| 1969 | llvm::ConstantExpr::getBitCast(metaclass, IdTy)); |
| 1970 | MetaClassPtrAlias->eraseFromParent(); |
| 1971 | MetaClassPtrAlias = nullptr; |
| 1972 | } |
| 1973 | assert(classStruct->getName() == SymbolForClass(className)); |
| 1974 | |
| 1975 | auto classInitRef = new llvm::GlobalVariable(TheModule, |
| 1976 | classStruct->getType(), false, llvm::GlobalValue::ExternalLinkage, |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 1977 | classStruct, ManglePublicSymbol("OBJC_INIT_CLASS_") + className); |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 1978 | classInitRef->setSection(sectionName<ClassSection>()); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 1979 | CGM.addUsedGlobal(classInitRef); |
| 1980 | |
| 1981 | EmittedClass = true; |
| 1982 | } |
| 1983 | public: |
| 1984 | CGObjCGNUstep2(CodeGenModule &Mod) : CGObjCGNUstep(Mod, 10, 4, 2) { |
| 1985 | MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy, |
| 1986 | PtrToObjCSuperTy, SelectorTy); |
| 1987 | // struct objc_property |
| 1988 | // { |
| 1989 | // const char *name; |
| 1990 | // const char *attributes; |
| 1991 | // const char *type; |
| 1992 | // SEL getter; |
| 1993 | // SEL setter; |
| 1994 | // } |
| 1995 | PropertyMetadataTy = |
| 1996 | llvm::StructType::get(CGM.getLLVMContext(), |
| 1997 | { PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty }); |
| 1998 | } |
| 1999 | |
| 2000 | }; |
| 2001 | |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 2002 | const char *const CGObjCGNUstep2::SectionsBaseNames[8] = |
| 2003 | { |
| 2004 | "__objc_selectors", |
| 2005 | "__objc_classes", |
| 2006 | "__objc_class_refs", |
| 2007 | "__objc_cats", |
| 2008 | "__objc_protocols", |
| 2009 | "__objc_protocol_refs", |
| 2010 | "__objc_class_aliases", |
| 2011 | "__objc_constant_string" |
| 2012 | }; |
| 2013 | |
David Chisnall | 7b36a86 | 2019-03-31 11:22:33 +0000 | [diff] [blame] | 2014 | const char *const CGObjCGNUstep2::PECOFFSectionsBaseNames[8] = |
| 2015 | { |
| 2016 | ".objcrt$SEL", |
| 2017 | ".objcrt$CLS", |
| 2018 | ".objcrt$CLR", |
| 2019 | ".objcrt$CAT", |
| 2020 | ".objcrt$PCL", |
| 2021 | ".objcrt$PCR", |
| 2022 | ".objcrt$CAL", |
| 2023 | ".objcrt$STR" |
| 2024 | }; |
| 2025 | |
Alp Toker | 272e9bc | 2013-11-25 00:40:53 +0000 | [diff] [blame] | 2026 | /// Support for the ObjFW runtime. |
John McCall | 3deb1ad | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 2027 | class CGObjCObjFW: public CGObjCGNU { |
| 2028 | protected: |
| 2029 | /// The GCC ABI message lookup function. Returns an IMP pointing to the |
| 2030 | /// method implementation for this message. |
| 2031 | LazyRuntimeFunction MsgLookupFn; |
Eli Friedman | f24bd3b | 2013-07-26 00:53:29 +0000 | [diff] [blame] | 2032 | /// stret lookup function. While this does not seem to make sense at the |
| 2033 | /// first look, this is required to call the correct forwarding function. |
| 2034 | LazyRuntimeFunction MsgLookupFnSRet; |
John McCall | 3deb1ad | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 2035 | /// The GCC ABI superclass message lookup function. Takes a pointer to a |
| 2036 | /// structure describing the receiver and the class, and a selector as |
| 2037 | /// arguments. Returns the IMP for the corresponding method. |
Eli Friedman | f24bd3b | 2013-07-26 00:53:29 +0000 | [diff] [blame] | 2038 | LazyRuntimeFunction MsgLookupSuperFn, MsgLookupSuperFnSRet; |
John McCall | 3deb1ad | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 2039 | |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2040 | llvm::Value *LookupIMP(CodeGenFunction &CGF, llvm::Value *&Receiver, |
| 2041 | llvm::Value *cmd, llvm::MDNode *node, |
| 2042 | MessageSendInfo &MSI) override { |
John McCall | 3deb1ad | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 2043 | CGBuilderTy &Builder = CGF.Builder; |
| 2044 | llvm::Value *args[] = { |
| 2045 | EnforceType(Builder, Receiver, IdTy), |
| 2046 | EnforceType(Builder, cmd, SelectorTy) }; |
Eli Friedman | f24bd3b | 2013-07-26 00:53:29 +0000 | [diff] [blame] | 2047 | |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 2048 | llvm::CallBase *imp; |
Eli Friedman | f24bd3b | 2013-07-26 00:53:29 +0000 | [diff] [blame] | 2049 | if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) |
| 2050 | imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFnSRet, args); |
| 2051 | else |
| 2052 | imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFn, args); |
| 2053 | |
John McCall | 3deb1ad | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 2054 | imp->setMetadata(msgSendMDKind, node); |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 2055 | return imp; |
John McCall | 3deb1ad | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2058 | llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, Address ObjCSuper, |
Craig Topper | 4f12f10 | 2014-03-12 06:41:41 +0000 | [diff] [blame] | 2059 | llvm::Value *cmd, MessageSendInfo &MSI) override { |
Saleem Abdulrasool | a088ad9 | 2016-07-17 22:27:41 +0000 | [diff] [blame] | 2060 | CGBuilderTy &Builder = CGF.Builder; |
| 2061 | llvm::Value *lookupArgs[] = { |
| 2062 | EnforceType(Builder, ObjCSuper.getPointer(), PtrToObjCSuperTy), cmd, |
| 2063 | }; |
Eli Friedman | f24bd3b | 2013-07-26 00:53:29 +0000 | [diff] [blame] | 2064 | |
Saleem Abdulrasool | a088ad9 | 2016-07-17 22:27:41 +0000 | [diff] [blame] | 2065 | if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) |
| 2066 | return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFnSRet, lookupArgs); |
| 2067 | else |
| 2068 | return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFn, lookupArgs); |
| 2069 | } |
John McCall | 3deb1ad | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 2070 | |
Saleem Abdulrasool | a088ad9 | 2016-07-17 22:27:41 +0000 | [diff] [blame] | 2071 | llvm::Value *GetClassNamed(CodeGenFunction &CGF, const std::string &Name, |
| 2072 | bool isWeak) override { |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 2073 | if (isWeak) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2074 | return CGObjCGNU::GetClassNamed(CGF, Name, isWeak); |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 2075 | |
| 2076 | EmitClassRef(Name); |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 2077 | std::string SymbolName = "_OBJC_CLASS_" + Name; |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 2078 | llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(SymbolName); |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 2079 | if (!ClassSymbol) |
| 2080 | ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false, |
| 2081 | llvm::GlobalValue::ExternalLinkage, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2082 | nullptr, SymbolName); |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 2083 | return ClassSymbol; |
| 2084 | } |
| 2085 | |
| 2086 | public: |
John McCall | 3deb1ad | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 2087 | CGObjCObjFW(CodeGenModule &Mod): CGObjCGNU(Mod, 9, 3) { |
| 2088 | // IMP objc_msg_lookup(id, SEL); |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2089 | MsgLookupFn.init(&CGM, "objc_msg_lookup", IMPTy, IdTy, SelectorTy); |
Eli Friedman | f24bd3b | 2013-07-26 00:53:29 +0000 | [diff] [blame] | 2090 | MsgLookupFnSRet.init(&CGM, "objc_msg_lookup_stret", IMPTy, IdTy, |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2091 | SelectorTy); |
John McCall | 3deb1ad | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 2092 | // IMP objc_msg_lookup_super(struct objc_super*, SEL); |
| 2093 | MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy, |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2094 | PtrToObjCSuperTy, SelectorTy); |
Eli Friedman | f24bd3b | 2013-07-26 00:53:29 +0000 | [diff] [blame] | 2095 | MsgLookupSuperFnSRet.init(&CGM, "objc_msg_lookup_super_stret", IMPTy, |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2096 | PtrToObjCSuperTy, SelectorTy); |
John McCall | 3deb1ad | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 2097 | } |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 2098 | }; |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 2099 | } // end anonymous namespace |
| 2100 | |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 2101 | /// Emits a reference to a dummy variable which is emitted with each class. |
| 2102 | /// This ensures that a linker error will be generated when trying to link |
| 2103 | /// together modules where a referenced class is not defined. |
Mike Stump | dd93a19 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 2104 | void CGObjCGNU::EmitClassRef(const std::string &className) { |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 2105 | std::string symbolRef = "__objc_class_ref_" + className; |
| 2106 | // Don't emit two copies of the same symbol |
Mike Stump | dd93a19 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 2107 | if (TheModule.getGlobalVariable(symbolRef)) |
| 2108 | return; |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 2109 | std::string symbolName = "__objc_class_name_" + className; |
| 2110 | llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(symbolName); |
| 2111 | if (!ClassSymbol) { |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2112 | ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2113 | llvm::GlobalValue::ExternalLinkage, |
| 2114 | nullptr, symbolName); |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 2115 | } |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2116 | new llvm::GlobalVariable(TheModule, ClassSymbol->getType(), true, |
Chris Lattner | c58e569 | 2009-08-05 05:25:18 +0000 | [diff] [blame] | 2117 | llvm::GlobalValue::WeakAnyLinkage, ClassSymbol, symbolRef); |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 2118 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2119 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2120 | CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion, |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2121 | unsigned protocolClassVersion, unsigned classABI) |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2122 | : CGObjCRuntime(cgm), TheModule(CGM.getModule()), |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2123 | VMContext(cgm.getLLVMContext()), ClassPtrAlias(nullptr), |
| 2124 | MetaClassPtrAlias(nullptr), RuntimeVersion(runtimeABIVersion), |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2125 | ProtocolVersion(protocolClassVersion), ClassABIVersion(classABI) { |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 2126 | |
| 2127 | msgSendMDKind = VMContext.getMDKindID("GNUObjCMessageSend"); |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 2128 | usesSEHExceptions = |
| 2129 | cgm.getContext().getTargetInfo().getTriple().isWindowsMSVCEnvironment(); |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 2130 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2131 | CodeGenTypes &Types = CGM.getTypes(); |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 2132 | IntTy = cast<llvm::IntegerType>( |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2133 | Types.ConvertType(CGM.getContext().IntTy)); |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 2134 | LongTy = cast<llvm::IntegerType>( |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2135 | Types.ConvertType(CGM.getContext().LongTy)); |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 2136 | SizeTy = cast<llvm::IntegerType>( |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2137 | Types.ConvertType(CGM.getContext().getSizeType())); |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 2138 | PtrDiffTy = cast<llvm::IntegerType>( |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2139 | Types.ConvertType(CGM.getContext().getPointerDiffType())); |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 2140 | BoolTy = CGM.getTypes().ConvertType(CGM.getContext().BoolTy); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2141 | |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2142 | Int8Ty = llvm::Type::getInt8Ty(VMContext); |
| 2143 | // C string type. Used in lots of places. |
| 2144 | PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2145 | ProtocolPtrTy = llvm::PointerType::getUnqual( |
| 2146 | Types.ConvertType(CGM.getContext().getObjCProtoType())); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2147 | |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2148 | Zeros[0] = llvm::ConstantInt::get(LongTy, 0); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2149 | Zeros[1] = Zeros[0]; |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2150 | NULLPtr = llvm::ConstantPointerNull::get(PtrToInt8Ty); |
Chris Lattner | 4bd5596 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 2151 | // Get the selector Type. |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 2152 | QualType selTy = CGM.getContext().getObjCSelType(); |
| 2153 | if (QualType() == selTy) { |
| 2154 | SelectorTy = PtrToInt8Ty; |
| 2155 | } else { |
| 2156 | SelectorTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(selTy)); |
| 2157 | } |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 2158 | |
Owen Anderson | 9793f0e | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2159 | PtrToIntTy = llvm::PointerType::getUnqual(IntTy); |
Chris Lattner | 4bd5596 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 2160 | PtrTy = PtrToInt8Ty; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2161 | |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 2162 | Int32Ty = llvm::Type::getInt32Ty(VMContext); |
| 2163 | Int64Ty = llvm::Type::getInt64Ty(VMContext); |
| 2164 | |
Rafael Espindola | 3cc5c2d | 2014-01-09 21:32:51 +0000 | [diff] [blame] | 2165 | IntPtrTy = |
| 2166 | CGM.getDataLayout().getPointerSizeInBits() == 32 ? Int32Ty : Int64Ty; |
David Chisnall | e0dc7cb | 2011-10-08 08:54:36 +0000 | [diff] [blame] | 2167 | |
Chris Lattner | 4bd5596 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 2168 | // Object type |
David Chisnall | 10d2ded | 2011-04-29 14:10:35 +0000 | [diff] [blame] | 2169 | QualType UnqualIdTy = CGM.getContext().getObjCIdType(); |
| 2170 | ASTIdTy = CanQualType(); |
| 2171 | if (UnqualIdTy != QualType()) { |
| 2172 | ASTIdTy = CGM.getContext().getCanonicalType(UnqualIdTy); |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 2173 | IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy)); |
David Chisnall | 10d2ded | 2011-04-29 14:10:35 +0000 | [diff] [blame] | 2174 | } else { |
| 2175 | IdTy = PtrToInt8Ty; |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 2176 | } |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2177 | PtrToIdTy = llvm::PointerType::getUnqual(IdTy); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2178 | ProtocolTy = llvm::StructType::get(IdTy, |
| 2179 | PtrToInt8Ty, // name |
| 2180 | PtrToInt8Ty, // protocols |
| 2181 | PtrToInt8Ty, // instance methods |
| 2182 | PtrToInt8Ty, // class methods |
| 2183 | PtrToInt8Ty, // optional instance methods |
| 2184 | PtrToInt8Ty, // optional class methods |
| 2185 | PtrToInt8Ty, // properties |
| 2186 | PtrToInt8Ty);// optional properties |
| 2187 | |
| 2188 | // struct objc_property_gsv1 |
| 2189 | // { |
| 2190 | // const char *name; |
| 2191 | // char attributes; |
| 2192 | // char attributes2; |
| 2193 | // char unused1; |
| 2194 | // char unused2; |
| 2195 | // const char *getter_name; |
| 2196 | // const char *getter_types; |
| 2197 | // const char *setter_name; |
| 2198 | // const char *setter_types; |
| 2199 | // } |
| 2200 | PropertyMetadataTy = llvm::StructType::get(CGM.getLLVMContext(), { |
| 2201 | PtrToInt8Ty, Int8Ty, Int8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty, |
| 2202 | PtrToInt8Ty, PtrToInt8Ty }); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2203 | |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2204 | ObjCSuperTy = llvm::StructType::get(IdTy, IdTy); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 2205 | PtrToObjCSuperTy = llvm::PointerType::getUnqual(ObjCSuperTy); |
| 2206 | |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2207 | llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2208 | |
| 2209 | // void objc_exception_throw(id); |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2210 | ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy); |
| 2211 | ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2212 | // int objc_sync_enter(id); |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2213 | SyncEnterFn.init(&CGM, "objc_sync_enter", IntTy, IdTy); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2214 | // int objc_sync_exit(id); |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2215 | SyncExitFn.init(&CGM, "objc_sync_exit", IntTy, IdTy); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2216 | |
| 2217 | // void objc_enumerationMutation (id) |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2218 | EnumerationMutationFn.init(&CGM, "objc_enumerationMutation", VoidTy, IdTy); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2219 | |
| 2220 | // id objc_getProperty(id, SEL, ptrdiff_t, BOOL) |
| 2221 | GetPropertyFn.init(&CGM, "objc_getProperty", IdTy, IdTy, SelectorTy, |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2222 | PtrDiffTy, BoolTy); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2223 | // void objc_setProperty(id, SEL, ptrdiff_t, id, BOOL, BOOL) |
| 2224 | SetPropertyFn.init(&CGM, "objc_setProperty", VoidTy, IdTy, SelectorTy, |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2225 | PtrDiffTy, IdTy, BoolTy, BoolTy); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2226 | // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL) |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2227 | GetStructPropertyFn.init(&CGM, "objc_getPropertyStruct", VoidTy, PtrTy, PtrTy, |
| 2228 | PtrDiffTy, BoolTy, BoolTy); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2229 | // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL) |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2230 | SetStructPropertyFn.init(&CGM, "objc_setPropertyStruct", VoidTy, PtrTy, PtrTy, |
| 2231 | PtrDiffTy, BoolTy, BoolTy); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2232 | |
Chris Lattner | 4bd5596 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 2233 | // IMP type |
Chris Lattner | a5f58b0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2234 | llvm::Type *IMPArgs[] = { IdTy, SelectorTy }; |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 2235 | IMPTy = llvm::PointerType::getUnqual(llvm::FunctionType::get(IdTy, IMPArgs, |
| 2236 | true)); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2237 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2238 | const LangOptions &Opts = CGM.getLangOpts(); |
Douglas Gregor | 79a9141 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 2239 | if ((Opts.getGC() != LangOptions::NonGC) || Opts.ObjCAutoRefCount) |
David Chisnall | a918b88 | 2011-07-07 11:22:31 +0000 | [diff] [blame] | 2240 | RuntimeVersion = 10; |
| 2241 | |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 2242 | // 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] | 2243 | if (Opts.getGC() != LangOptions::NonGC) { |
David Chisnall | 5c51177 | 2011-05-22 22:37:08 +0000 | [diff] [blame] | 2244 | // This is a bit of an hack. We should sort this out by having a proper |
| 2245 | // CGObjCGNUstep subclass for GC, but we may want to really support the old |
| 2246 | // 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] | 2247 | // Get selectors needed in GC mode |
| 2248 | RetainSel = GetNullarySelector("retain", CGM.getContext()); |
| 2249 | ReleaseSel = GetNullarySelector("release", CGM.getContext()); |
| 2250 | AutoreleaseSel = GetNullarySelector("autorelease", CGM.getContext()); |
| 2251 | |
| 2252 | // Get functions needed in GC mode |
| 2253 | |
| 2254 | // id objc_assign_ivar(id, id, ptrdiff_t); |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2255 | IvarAssignFn.init(&CGM, "objc_assign_ivar", IdTy, IdTy, IdTy, PtrDiffTy); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2256 | // id objc_assign_strongCast (id, id*) |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2257 | StrongCastAssignFn.init(&CGM, "objc_assign_strongCast", IdTy, IdTy, |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2258 | PtrToIdTy); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2259 | // id objc_assign_global(id, id*); |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2260 | GlobalAssignFn.init(&CGM, "objc_assign_global", IdTy, IdTy, PtrToIdTy); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2261 | // id objc_assign_weak(id, id*); |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2262 | WeakAssignFn.init(&CGM, "objc_assign_weak", IdTy, IdTy, PtrToIdTy); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2263 | // id objc_read_weak(id*); |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2264 | WeakReadFn.init(&CGM, "objc_read_weak", IdTy, PtrToIdTy); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2265 | // void *objc_memmove_collectable(void*, void *, size_t); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2266 | MemMoveFn.init(&CGM, "objc_memmove_collectable", PtrTy, PtrTy, PtrTy, |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2267 | SizeTy); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2268 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2269 | } |
Mike Stump | dd93a19 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 2270 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2271 | llvm::Value *CGObjCGNU::GetClassNamed(CodeGenFunction &CGF, |
Saleem Abdulrasool | a088ad9 | 2016-07-17 22:27:41 +0000 | [diff] [blame] | 2272 | const std::string &Name, bool isWeak) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2273 | llvm::Constant *ClassName = MakeConstantString(Name); |
David Chisnall | df34917 | 2010-01-08 00:14:31 +0000 | [diff] [blame] | 2274 | // With the incompatible ABI, this will need to be replaced with a direct |
| 2275 | // reference to the class symbol. For the compatible nonfragile ABI we are |
| 2276 | // still performing this lookup at run time but emitting the symbol for the |
| 2277 | // class externally so that we can make the switch later. |
David Chisnall | 920e83b | 2011-06-29 13:16:41 +0000 | [diff] [blame] | 2278 | // |
| 2279 | // Libobjc2 contains an LLVM pass that replaces calls to objc_lookup_class |
| 2280 | // 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] | 2281 | if (!isWeak) |
| 2282 | EmitClassRef(Name); |
Daniel Dunbar | 7c6d3a7 | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 2283 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 2284 | llvm::FunctionCallee ClassLookupFn = CGM.CreateRuntimeFunction( |
| 2285 | llvm::FunctionType::get(IdTy, PtrToInt8Ty, true), "objc_lookup_class"); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2286 | return CGF.EmitNounwindRuntimeCall(ClassLookupFn, ClassName); |
Chris Lattner | 4bd5596 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
David Chisnall | 920e83b | 2011-06-29 13:16:41 +0000 | [diff] [blame] | 2289 | // This has to perform the lookup every time, since posing and related |
| 2290 | // techniques can modify the name -> class mapping. |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2291 | llvm::Value *CGObjCGNU::GetClass(CodeGenFunction &CGF, |
David Chisnall | 920e83b | 2011-06-29 13:16:41 +0000 | [diff] [blame] | 2292 | const ObjCInterfaceDecl *OID) { |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 2293 | auto *Value = |
| 2294 | GetClassNamed(CGF, OID->getNameAsString(), OID->isWeakImported()); |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 2295 | if (auto *ClassSymbol = dyn_cast<llvm::GlobalVariable>(Value)) |
| 2296 | CGM.setGVProperties(ClassSymbol, OID); |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 2297 | return Value; |
David Chisnall | 920e83b | 2011-06-29 13:16:41 +0000 | [diff] [blame] | 2298 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 2299 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2300 | llvm::Value *CGObjCGNU::EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) { |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 2301 | auto *Value = GetClassNamed(CGF, "NSAutoreleasePool", false); |
| 2302 | if (CGM.getTriple().isOSBinFormatCOFF()) { |
| 2303 | if (auto *ClassSymbol = dyn_cast<llvm::GlobalVariable>(Value)) { |
| 2304 | IdentifierInfo &II = CGF.CGM.getContext().Idents.get("NSAutoreleasePool"); |
| 2305 | TranslationUnitDecl *TUDecl = CGM.getContext().getTranslationUnitDecl(); |
| 2306 | DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl); |
| 2307 | |
| 2308 | const VarDecl *VD = nullptr; |
| 2309 | for (const auto &Result : DC->lookup(&II)) |
| 2310 | if ((VD = dyn_cast<VarDecl>(Result))) |
| 2311 | break; |
| 2312 | |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 2313 | CGM.setGVProperties(ClassSymbol, VD); |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 2314 | } |
| 2315 | } |
| 2316 | return Value; |
David Chisnall | 920e83b | 2011-06-29 13:16:41 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
Simon Pilgrim | 04c5a34 | 2018-08-08 15:53:14 +0000 | [diff] [blame] | 2319 | llvm::Value *CGObjCGNU::GetTypedSelector(CodeGenFunction &CGF, Selector Sel, |
| 2320 | const std::string &TypeEncoding) { |
Craig Topper | fa159c1 | 2013-07-14 16:47:36 +0000 | [diff] [blame] | 2321 | SmallVectorImpl<TypedSelector> &Types = SelectorTable[Sel]; |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2322 | llvm::GlobalAlias *SelValue = nullptr; |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2323 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2324 | for (SmallVectorImpl<TypedSelector>::iterator i = Types.begin(), |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2325 | e = Types.end() ; i!=e ; i++) { |
| 2326 | if (i->first == TypeEncoding) { |
| 2327 | SelValue = i->second; |
| 2328 | break; |
| 2329 | } |
| 2330 | } |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2331 | if (!SelValue) { |
Rafael Espindola | 234405b | 2014-05-17 21:30:14 +0000 | [diff] [blame] | 2332 | SelValue = llvm::GlobalAlias::create( |
David Blaikie | aff29d3 | 2015-09-14 18:02:04 +0000 | [diff] [blame] | 2333 | SelectorTy->getElementType(), 0, llvm::GlobalValue::PrivateLinkage, |
Rafael Espindola | 6172277 | 2014-05-17 19:58:16 +0000 | [diff] [blame] | 2334 | ".objc_selector_" + Sel.getAsString(), &TheModule); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 2335 | Types.emplace_back(TypeEncoding, SelValue); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2336 | } |
| 2337 | |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 2338 | return SelValue; |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2339 | } |
| 2340 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2341 | Address CGObjCGNU::GetAddrOfSelector(CodeGenFunction &CGF, Selector Sel) { |
| 2342 | llvm::Value *SelValue = GetSelector(CGF, Sel); |
| 2343 | |
| 2344 | // Store it to a temporary. Does this satisfy the semantics of |
| 2345 | // GetAddrOfSelector? Hopefully. |
| 2346 | Address tmp = CGF.CreateTempAlloca(SelValue->getType(), |
| 2347 | CGF.getPointerAlign()); |
| 2348 | CGF.Builder.CreateStore(SelValue, tmp); |
| 2349 | return tmp; |
| 2350 | } |
| 2351 | |
| 2352 | llvm::Value *CGObjCGNU::GetSelector(CodeGenFunction &CGF, Selector Sel) { |
Simon Pilgrim | 04c5a34 | 2018-08-08 15:53:14 +0000 | [diff] [blame] | 2353 | return GetTypedSelector(CGF, Sel, std::string()); |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 2354 | } |
| 2355 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2356 | llvm::Value *CGObjCGNU::GetSelector(CodeGenFunction &CGF, |
| 2357 | const ObjCMethodDecl *Method) { |
John McCall | 843dfcc | 2016-11-29 21:57:00 +0000 | [diff] [blame] | 2358 | std::string SelTypes = CGM.getContext().getObjCEncodingForMethodDecl(Method); |
Simon Pilgrim | 04c5a34 | 2018-08-08 15:53:14 +0000 | [diff] [blame] | 2359 | return GetTypedSelector(CGF, Method->getSelector(), SelTypes); |
Chris Lattner | 6d522c0 | 2008-06-26 04:37:12 +0000 | [diff] [blame] | 2360 | } |
| 2361 | |
Fariborz Jahanian | 831f0fc | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 2362 | llvm::Constant *CGObjCGNU::GetEHType(QualType T) { |
John McCall | c31d893 | 2012-11-14 09:08:34 +0000 | [diff] [blame] | 2363 | if (T->isObjCIdType() || T->isObjCQualifiedIdType()) { |
| 2364 | // With the old ABI, there was only one kind of catchall, which broke |
| 2365 | // foreign exceptions. With the new ABI, we use __objc_id_typeinfo as |
| 2366 | // a pointer indicating object catchalls, and NULL to indicate real |
| 2367 | // catchalls |
| 2368 | if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) { |
| 2369 | return MakeConstantString("@id"); |
| 2370 | } else { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2371 | return nullptr; |
John McCall | c31d893 | 2012-11-14 09:08:34 +0000 | [diff] [blame] | 2372 | } |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 2373 | } |
John McCall | c31d893 | 2012-11-14 09:08:34 +0000 | [diff] [blame] | 2374 | |
| 2375 | // All other types should be Objective-C interface pointer types. |
| 2376 | const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>(); |
| 2377 | assert(OPT && "Invalid @catch type."); |
| 2378 | const ObjCInterfaceDecl *IDecl = OPT->getObjectType()->getInterface(); |
| 2379 | assert(IDecl && "Invalid @catch type."); |
| 2380 | return MakeConstantString(IDecl->getIdentifier()->getName()); |
| 2381 | } |
| 2382 | |
| 2383 | llvm::Constant *CGObjCGNUstep::GetEHType(QualType T) { |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 2384 | if (usesSEHExceptions) |
| 2385 | return CGM.getCXXABI().getAddrOfRTTIDescriptor(T); |
| 2386 | |
John McCall | c31d893 | 2012-11-14 09:08:34 +0000 | [diff] [blame] | 2387 | if (!CGM.getLangOpts().CPlusPlus) |
| 2388 | return CGObjCGNU::GetEHType(T); |
| 2389 | |
David Chisnall | e1d2584d | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 2390 | // For Objective-C++, we want to provide the ability to catch both C++ and |
| 2391 | // Objective-C objects in the same function. |
| 2392 | |
| 2393 | // There's a particular fixed type info for 'id'. |
| 2394 | if (T->isObjCIdType() || |
| 2395 | T->isObjCQualifiedIdType()) { |
| 2396 | llvm::Constant *IDEHType = |
| 2397 | CGM.getModule().getGlobalVariable("__objc_id_type_info"); |
| 2398 | if (!IDEHType) |
| 2399 | IDEHType = |
| 2400 | new llvm::GlobalVariable(CGM.getModule(), PtrToInt8Ty, |
| 2401 | false, |
| 2402 | llvm::GlobalValue::ExternalLinkage, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2403 | nullptr, "__objc_id_type_info"); |
David Chisnall | e1d2584d | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 2404 | return llvm::ConstantExpr::getBitCast(IDEHType, PtrToInt8Ty); |
| 2405 | } |
| 2406 | |
| 2407 | const ObjCObjectPointerType *PT = |
| 2408 | T->getAs<ObjCObjectPointerType>(); |
| 2409 | assert(PT && "Invalid @catch type."); |
| 2410 | const ObjCInterfaceType *IT = PT->getInterfaceType(); |
| 2411 | assert(IT && "Invalid @catch type."); |
| 2412 | std::string className = IT->getDecl()->getIdentifier()->getName(); |
| 2413 | |
| 2414 | std::string typeinfoName = "__objc_eh_typeinfo_" + className; |
| 2415 | |
| 2416 | // Return the existing typeinfo if it exists |
| 2417 | llvm::Constant *typeinfo = TheModule.getGlobalVariable(typeinfoName); |
David Chisnall | d663934 | 2012-03-20 16:25:52 +0000 | [diff] [blame] | 2418 | if (typeinfo) |
| 2419 | return llvm::ConstantExpr::getBitCast(typeinfo, PtrToInt8Ty); |
David Chisnall | e1d2584d | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 2420 | |
| 2421 | // Otherwise create it. |
| 2422 | |
| 2423 | // vtable for gnustep::libobjc::__objc_class_type_info |
| 2424 | // It's quite ugly hard-coding this. Ideally we'd generate it using the host |
| 2425 | // platform's name mangling. |
| 2426 | const char *vtableName = "_ZTVN7gnustep7libobjc22__objc_class_type_infoE"; |
David Blaikie | e3b172a | 2015-04-02 18:55:21 +0000 | [diff] [blame] | 2427 | auto *Vtable = TheModule.getGlobalVariable(vtableName); |
David Chisnall | e1d2584d | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 2428 | if (!Vtable) { |
| 2429 | Vtable = new llvm::GlobalVariable(TheModule, PtrToInt8Ty, true, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2430 | llvm::GlobalValue::ExternalLinkage, |
| 2431 | nullptr, vtableName); |
David Chisnall | e1d2584d | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 2432 | } |
| 2433 | llvm::Constant *Two = llvm::ConstantInt::get(IntTy, 2); |
David Blaikie | e3b172a | 2015-04-02 18:55:21 +0000 | [diff] [blame] | 2434 | auto *BVtable = llvm::ConstantExpr::getBitCast( |
| 2435 | llvm::ConstantExpr::getGetElementPtr(Vtable->getValueType(), Vtable, Two), |
| 2436 | PtrToInt8Ty); |
David Chisnall | e1d2584d | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 2437 | |
| 2438 | llvm::Constant *typeName = |
| 2439 | ExportUniqueString(className, "__objc_eh_typename_"); |
| 2440 | |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 2441 | ConstantInitBuilder builder(CGM); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2442 | auto fields = builder.beginStruct(); |
| 2443 | fields.add(BVtable); |
| 2444 | fields.add(typeName); |
| 2445 | llvm::Constant *TI = |
| 2446 | fields.finishAndCreateGlobal("__objc_eh_typeinfo_" + className, |
| 2447 | CGM.getPointerAlign(), |
| 2448 | /*constant*/ false, |
| 2449 | llvm::GlobalValue::LinkOnceODRLinkage); |
David Chisnall | e1d2584d | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 2450 | return llvm::ConstantExpr::getBitCast(TI, PtrToInt8Ty); |
John McCall | 2ca705e | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 2451 | } |
| 2452 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2453 | /// Generate an NSConstantString object. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2454 | ConstantAddress CGObjCGNU::GenerateConstantString(const StringLiteral *SL) { |
David Chisnall | 358e751 | 2010-01-27 12:49:23 +0000 | [diff] [blame] | 2455 | |
Benjamin Kramer | 35b077e | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 2456 | std::string Str = SL->getString().str(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2457 | CharUnits Align = CGM.getPointerAlign(); |
David Chisnall | 481e3a8 | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 2458 | |
David Chisnall | 358e751 | 2010-01-27 12:49:23 +0000 | [diff] [blame] | 2459 | // Look for an existing one |
| 2460 | llvm::StringMap<llvm::Constant*>::iterator old = ObjCStrings.find(Str); |
| 2461 | if (old != ObjCStrings.end()) |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2462 | return ConstantAddress(old->getValue(), Align); |
David Chisnall | 358e751 | 2010-01-27 12:49:23 +0000 | [diff] [blame] | 2463 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2464 | StringRef StringClass = CGM.getLangOpts().ObjCConstantStringClass; |
David Chisnall | 207a630 | 2012-01-04 12:02:13 +0000 | [diff] [blame] | 2465 | |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2466 | if (StringClass.empty()) StringClass = "NSConstantString"; |
David Chisnall | 207a630 | 2012-01-04 12:02:13 +0000 | [diff] [blame] | 2467 | |
| 2468 | std::string Sym = "_OBJC_CLASS_"; |
| 2469 | Sym += StringClass; |
| 2470 | |
| 2471 | llvm::Constant *isa = TheModule.getNamedGlobal(Sym); |
| 2472 | |
| 2473 | if (!isa) |
| 2474 | isa = new llvm::GlobalVariable(TheModule, IdTy, /* isConstant */false, |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2475 | llvm::GlobalValue::ExternalWeakLinkage, nullptr, Sym); |
David Chisnall | 207a630 | 2012-01-04 12:02:13 +0000 | [diff] [blame] | 2476 | else if (isa->getType() != PtrToIdTy) |
| 2477 | isa = llvm::ConstantExpr::getBitCast(isa, PtrToIdTy); |
| 2478 | |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 2479 | ConstantInitBuilder Builder(CGM); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2480 | auto Fields = Builder.beginStruct(); |
| 2481 | Fields.add(isa); |
| 2482 | Fields.add(MakeConstantString(Str)); |
| 2483 | Fields.addInt(IntTy, Str.size()); |
| 2484 | llvm::Constant *ObjCStr = |
| 2485 | Fields.finishAndCreateGlobal(".objc_str", Align); |
David Chisnall | 358e751 | 2010-01-27 12:49:23 +0000 | [diff] [blame] | 2486 | ObjCStr = llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty); |
| 2487 | ObjCStrings[Str] = ObjCStr; |
| 2488 | ConstantStrings.push_back(ObjCStr); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2489 | return ConstantAddress(ObjCStr, Align); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
| 2492 | ///Generates a message send where the super is the receiver. This is a message |
| 2493 | ///send to self with special delivery semantics indicating which class's method |
| 2494 | ///should be called. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2495 | RValue |
| 2496 | CGObjCGNU::GenerateMessageSendSuper(CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 2497 | ReturnValueSlot Return, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 2498 | QualType ResultType, |
| 2499 | Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2500 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | bac73ac | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 2501 | bool isCategoryImpl, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2502 | llvm::Value *Receiver, |
Daniel Dunbar | c722b85 | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 2503 | bool IsClassMessage, |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 2504 | const CallArgList &CallArgs, |
| 2505 | const ObjCMethodDecl *Method) { |
David Chisnall | 8a42d19 | 2011-05-28 14:09:01 +0000 | [diff] [blame] | 2506 | CGBuilderTy &Builder = CGF.Builder; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2507 | if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) { |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2508 | if (Sel == RetainSel || Sel == AutoreleaseSel) { |
David Chisnall | 8a42d19 | 2011-05-28 14:09:01 +0000 | [diff] [blame] | 2509 | return RValue::get(EnforceType(Builder, Receiver, |
| 2510 | CGM.getTypes().ConvertType(ResultType))); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2511 | } |
| 2512 | if (Sel == ReleaseSel) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2513 | return RValue::get(nullptr); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2514 | } |
| 2515 | } |
David Chisnall | ea529a4 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 2516 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2517 | llvm::Value *cmd = GetSelector(CGF, Sel); |
Fariborz Jahanian | b73a23e | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 2518 | CallArgList ActualArgs; |
| 2519 | |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 2520 | ActualArgs.add(RValue::get(EnforceType(Builder, Receiver, IdTy)), ASTIdTy); |
| 2521 | ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2522 | ActualArgs.addFrom(CallArgs); |
Fariborz Jahanian | b73a23e | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 2523 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2524 | MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs); |
Fariborz Jahanian | b73a23e | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 2525 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2526 | llvm::Value *ReceiverClass = nullptr; |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2527 | bool isV2ABI = isRuntime(ObjCRuntime::GNUstep, 2); |
| 2528 | if (isV2ABI) { |
| 2529 | ReceiverClass = GetClassNamed(CGF, |
| 2530 | Class->getSuperClass()->getNameAsString(), /*isWeak*/false); |
Chris Lattner | a02cb80 | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 2531 | if (IsClassMessage) { |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2532 | // Load the isa pointer of the superclass is this is a class method. |
| 2533 | ReceiverClass = Builder.CreateBitCast(ReceiverClass, |
| 2534 | llvm::PointerType::getUnqual(IdTy)); |
| 2535 | ReceiverClass = |
| 2536 | Builder.CreateAlignedLoad(ReceiverClass, CGF.getPointerAlign()); |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 2537 | } |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2538 | ReceiverClass = EnforceType(Builder, ReceiverClass, IdTy); |
Bjorn Pettersson | 8446633 | 2018-05-22 08:16:45 +0000 | [diff] [blame] | 2539 | } else { |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2540 | if (isCategoryImpl) { |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 2541 | llvm::FunctionCallee classLookupFunction = nullptr; |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2542 | if (IsClassMessage) { |
| 2543 | classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get( |
| 2544 | IdTy, PtrTy, true), "objc_get_meta_class"); |
| 2545 | } else { |
| 2546 | classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get( |
| 2547 | IdTy, PtrTy, true), "objc_get_class"); |
Bjorn Pettersson | 8446633 | 2018-05-22 08:16:45 +0000 | [diff] [blame] | 2548 | } |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2549 | ReceiverClass = Builder.CreateCall(classLookupFunction, |
| 2550 | MakeConstantString(Class->getNameAsString())); |
Bjorn Pettersson | 8446633 | 2018-05-22 08:16:45 +0000 | [diff] [blame] | 2551 | } else { |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2552 | // Set up global aliases for the metaclass or class pointer if they do not |
| 2553 | // already exist. These will are forward-references which will be set to |
| 2554 | // pointers to the class and metaclass structure created for the runtime |
| 2555 | // load function. To send a message to super, we look up the value of the |
| 2556 | // super_class pointer from either the class or metaclass structure. |
| 2557 | if (IsClassMessage) { |
| 2558 | if (!MetaClassPtrAlias) { |
| 2559 | MetaClassPtrAlias = llvm::GlobalAlias::create( |
| 2560 | IdTy->getElementType(), 0, llvm::GlobalValue::InternalLinkage, |
| 2561 | ".objc_metaclass_ref" + Class->getNameAsString(), &TheModule); |
| 2562 | } |
| 2563 | ReceiverClass = MetaClassPtrAlias; |
| 2564 | } else { |
| 2565 | if (!ClassPtrAlias) { |
| 2566 | ClassPtrAlias = llvm::GlobalAlias::create( |
| 2567 | IdTy->getElementType(), 0, llvm::GlobalValue::InternalLinkage, |
| 2568 | ".objc_class_ref" + Class->getNameAsString(), &TheModule); |
| 2569 | } |
| 2570 | ReceiverClass = ClassPtrAlias; |
Bjorn Pettersson | 8446633 | 2018-05-22 08:16:45 +0000 | [diff] [blame] | 2571 | } |
Bjorn Pettersson | 8446633 | 2018-05-22 08:16:45 +0000 | [diff] [blame] | 2572 | } |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2573 | // Cast the pointer to a simplified version of the class structure |
| 2574 | llvm::Type *CastTy = llvm::StructType::get(IdTy, IdTy); |
| 2575 | ReceiverClass = Builder.CreateBitCast(ReceiverClass, |
| 2576 | llvm::PointerType::getUnqual(CastTy)); |
| 2577 | // Get the superclass pointer |
| 2578 | ReceiverClass = Builder.CreateStructGEP(CastTy, ReceiverClass, 1); |
| 2579 | // Load the superclass pointer |
| 2580 | ReceiverClass = |
| 2581 | Builder.CreateAlignedLoad(ReceiverClass, CGF.getPointerAlign()); |
Chris Lattner | c06ce0f | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 2582 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2583 | // Construct the structure used to look up the IMP |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2584 | llvm::StructType *ObjCSuperTy = |
| 2585 | llvm::StructType::get(Receiver->getType(), IdTy); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2586 | |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2587 | Address ObjCSuper = CGF.CreateTempAlloca(ObjCSuperTy, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2588 | CGF.getPointerAlign()); |
Fariborz Jahanian | b73a23e | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 2589 | |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 2590 | Builder.CreateStore(Receiver, Builder.CreateStructGEP(ObjCSuper, 0)); |
| 2591 | Builder.CreateStore(ReceiverClass, Builder.CreateStructGEP(ObjCSuper, 1)); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2592 | |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 2593 | ObjCSuper = EnforceType(Builder, ObjCSuper, PtrToObjCSuperTy); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 2594 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2595 | // Get the IMP |
Eli Friedman | f24bd3b | 2013-07-26 00:53:29 +0000 | [diff] [blame] | 2596 | llvm::Value *imp = LookupIMPSuper(CGF, ObjCSuper, cmd, MSI); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2597 | imp = EnforceType(Builder, imp, MSI.MessengerType); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2598 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2599 | llvm::Metadata *impMD[] = { |
David Chisnall | 9eecafa | 2010-05-01 11:15:56 +0000 | [diff] [blame] | 2600 | llvm::MDString::get(VMContext, Sel.getAsString()), |
| 2601 | llvm::MDString::get(VMContext, Class->getSuperClass()->getNameAsString()), |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2602 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 2603 | llvm::Type::getInt1Ty(VMContext), IsClassMessage))}; |
Jay Foad | ea324f1 | 2011-04-21 19:59:12 +0000 | [diff] [blame] | 2604 | llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD); |
David Chisnall | 9eecafa | 2010-05-01 11:15:56 +0000 | [diff] [blame] | 2605 | |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 2606 | CGCallee callee(CGCalleeInfo(), imp); |
| 2607 | |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 2608 | llvm::CallBase *call; |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 2609 | RValue msgRet = CGF.EmitCall(MSI.CallInfo, callee, Return, ActualArgs, &call); |
David Chisnall | ff5f88c | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 2610 | call->setMetadata(msgSendMDKind, node); |
| 2611 | return msgRet; |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2612 | } |
| 2613 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2614 | /// Generate code for a message send expression. |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2615 | RValue |
| 2616 | CGObjCGNU::GenerateMessageSend(CodeGenFunction &CGF, |
John McCall | 78a1511 | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 2617 | ReturnValueSlot Return, |
Daniel Dunbar | 4b8c6db | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 2618 | QualType ResultType, |
| 2619 | Selector Sel, |
Daniel Dunbar | ca8531a | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2620 | llvm::Value *Receiver, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 2621 | const CallArgList &CallArgs, |
David Chisnall | 01aa467 | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 2622 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 2623 | const ObjCMethodDecl *Method) { |
David Chisnall | 8a42d19 | 2011-05-28 14:09:01 +0000 | [diff] [blame] | 2624 | CGBuilderTy &Builder = CGF.Builder; |
| 2625 | |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 2626 | // Strip out message sends to retain / release in GC mode |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2627 | if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) { |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2628 | if (Sel == RetainSel || Sel == AutoreleaseSel) { |
David Chisnall | 8a42d19 | 2011-05-28 14:09:01 +0000 | [diff] [blame] | 2629 | return RValue::get(EnforceType(Builder, Receiver, |
| 2630 | CGM.getTypes().ConvertType(ResultType))); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2631 | } |
| 2632 | if (Sel == ReleaseSel) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2633 | return RValue::get(nullptr); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2634 | } |
| 2635 | } |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 2636 | |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 2637 | // If the return type is something that goes in an integer register, the |
| 2638 | // runtime will handle 0 returns. For other cases, we fill in the 0 value |
| 2639 | // ourselves. |
| 2640 | // |
| 2641 | // The language spec says the result of this kind of message send is |
| 2642 | // undefined, but lots of people seem to have forgotten to read that |
| 2643 | // paragraph and insist on sending messages to nil that have structure |
| 2644 | // returns. With GCC, this generates a random return value (whatever happens |
| 2645 | // 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] | 2646 | // and generates an illegal instruction trap on SPARC. With LLVM it corrupts |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2647 | // the stack. |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 2648 | bool isPointerSizedReturn = (ResultType->isAnyPointerType() || |
| 2649 | ResultType->isIntegralOrEnumerationType() || ResultType->isVoidType()); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 2650 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 2651 | llvm::BasicBlock *startBB = nullptr; |
| 2652 | llvm::BasicBlock *messageBB = nullptr; |
| 2653 | llvm::BasicBlock *continueBB = nullptr; |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 2654 | |
| 2655 | if (!isPointerSizedReturn) { |
| 2656 | startBB = Builder.GetInsertBlock(); |
| 2657 | messageBB = CGF.createBasicBlock("msgSend"); |
David Chisnall | 29cefd1 | 2010-05-20 13:45:48 +0000 | [diff] [blame] | 2658 | continueBB = CGF.createBasicBlock("continue"); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 2659 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2660 | llvm::Value *isNil = Builder.CreateICmpEQ(Receiver, |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 2661 | llvm::Constant::getNullValue(Receiver->getType())); |
David Chisnall | 29cefd1 | 2010-05-20 13:45:48 +0000 | [diff] [blame] | 2662 | Builder.CreateCondBr(isNil, continueBB, messageBB); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 2663 | CGF.EmitBlock(messageBB); |
| 2664 | } |
| 2665 | |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2666 | IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy)); |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 2667 | llvm::Value *cmd; |
| 2668 | if (Method) |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2669 | cmd = GetSelector(CGF, Method); |
Fariborz Jahanian | f3648b8 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 2670 | else |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 2671 | cmd = GetSelector(CGF, Sel); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 2672 | cmd = EnforceType(Builder, cmd, SelectorTy); |
| 2673 | Receiver = EnforceType(Builder, Receiver, IdTy); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2674 | |
Duncan P. N. Exon Smith | fb49491 | 2014-12-09 18:39:32 +0000 | [diff] [blame] | 2675 | llvm::Metadata *impMD[] = { |
| 2676 | llvm::MDString::get(VMContext, Sel.getAsString()), |
| 2677 | llvm::MDString::get(VMContext, Class ? Class->getNameAsString() : ""), |
| 2678 | llvm::ConstantAsMetadata::get(llvm::ConstantInt::get( |
| 2679 | llvm::Type::getInt1Ty(VMContext), Class != nullptr))}; |
Jay Foad | ea324f1 | 2011-04-21 19:59:12 +0000 | [diff] [blame] | 2680 | llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD); |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 2681 | |
David Chisnall | 7680341 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 2682 | CallArgList ActualArgs; |
Eli Friedman | 43dca6a | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 2683 | ActualArgs.add(RValue::get(Receiver), ASTIdTy); |
| 2684 | ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2685 | ActualArgs.addFrom(CallArgs); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2686 | |
| 2687 | MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs); |
| 2688 | |
David Chisnall | 8c93cf2 | 2011-10-24 14:07:03 +0000 | [diff] [blame] | 2689 | // Get the IMP to call |
| 2690 | llvm::Value *imp; |
| 2691 | |
| 2692 | // If we have non-legacy dispatch specified, we try using the objc_msgSend() |
| 2693 | // functions. These are not supported on all platforms (or all runtimes on a |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2694 | // given platform), so we |
David Chisnall | 8c93cf2 | 2011-10-24 14:07:03 +0000 | [diff] [blame] | 2695 | switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) { |
David Chisnall | 8c93cf2 | 2011-10-24 14:07:03 +0000 | [diff] [blame] | 2696 | case CodeGenOptions::Legacy: |
Eli Friedman | f24bd3b | 2013-07-26 00:53:29 +0000 | [diff] [blame] | 2697 | imp = LookupIMP(CGF, Receiver, cmd, node, MSI); |
David Chisnall | 8c93cf2 | 2011-10-24 14:07:03 +0000 | [diff] [blame] | 2698 | break; |
| 2699 | case CodeGenOptions::Mixed: |
David Chisnall | 8c93cf2 | 2011-10-24 14:07:03 +0000 | [diff] [blame] | 2700 | case CodeGenOptions::NonLegacy: |
David Chisnall | 0cc83e7 | 2011-10-28 17:55:06 +0000 | [diff] [blame] | 2701 | if (CGM.ReturnTypeUsesFPRet(ResultType)) { |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 2702 | imp = |
| 2703 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true), |
| 2704 | "objc_msgSend_fpret") |
| 2705 | .getCallee(); |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2706 | } else if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) { |
David Chisnall | 8c93cf2 | 2011-10-24 14:07:03 +0000 | [diff] [blame] | 2707 | // The actual types here don't matter - we're going to bitcast the |
| 2708 | // function anyway |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 2709 | imp = |
| 2710 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true), |
| 2711 | "objc_msgSend_stret") |
| 2712 | .getCallee(); |
David Chisnall | 8c93cf2 | 2011-10-24 14:07:03 +0000 | [diff] [blame] | 2713 | } else { |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 2714 | imp = CGM.CreateRuntimeFunction( |
| 2715 | llvm::FunctionType::get(IdTy, IdTy, true), "objc_msgSend") |
| 2716 | .getCallee(); |
David Chisnall | 8c93cf2 | 2011-10-24 14:07:03 +0000 | [diff] [blame] | 2717 | } |
| 2718 | } |
| 2719 | |
David Chisnall | 6aec31a | 2011-12-01 18:40:09 +0000 | [diff] [blame] | 2720 | // Reset the receiver in case the lookup modified it |
Yaxun Liu | 5b330e8 | 2018-03-15 15:25:19 +0000 | [diff] [blame] | 2721 | ActualArgs[0] = CallArg(RValue::get(Receiver), ASTIdTy); |
David Chisnall | 8c93cf2 | 2011-10-24 14:07:03 +0000 | [diff] [blame] | 2722 | |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2723 | imp = EnforceType(Builder, imp, MSI.MessengerType); |
David Chisnall | c0cf422 | 2010-05-01 12:56:56 +0000 | [diff] [blame] | 2724 | |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 2725 | llvm::CallBase *call; |
John McCall | b92ab1a | 2016-10-26 23:46:34 +0000 | [diff] [blame] | 2726 | CGCallee callee(CGCalleeInfo(), imp); |
| 2727 | RValue msgRet = CGF.EmitCall(MSI.CallInfo, callee, Return, ActualArgs, &call); |
David Chisnall | ff5f88c | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 2728 | call->setMetadata(msgSendMDKind, node); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 2729 | |
David Chisnall | 29cefd1 | 2010-05-20 13:45:48 +0000 | [diff] [blame] | 2730 | |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 2731 | if (!isPointerSizedReturn) { |
David Chisnall | 29cefd1 | 2010-05-20 13:45:48 +0000 | [diff] [blame] | 2732 | messageBB = CGF.Builder.GetInsertBlock(); |
| 2733 | CGF.Builder.CreateBr(continueBB); |
| 2734 | CGF.EmitBlock(continueBB); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 2735 | if (msgRet.isScalar()) { |
| 2736 | llvm::Value *v = msgRet.getScalarVal(); |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 2737 | llvm::PHINode *phi = Builder.CreatePHI(v->getType(), 2); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 2738 | phi->addIncoming(v, messageBB); |
| 2739 | phi->addIncoming(llvm::Constant::getNullValue(v->getType()), startBB); |
| 2740 | msgRet = RValue::get(phi); |
| 2741 | } else if (msgRet.isAggregate()) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2742 | Address v = msgRet.getAggregateAddress(); |
| 2743 | llvm::PHINode *phi = Builder.CreatePHI(v.getType(), 2); |
| 2744 | llvm::Type *RetTy = v.getElementType(); |
| 2745 | Address NullVal = CGF.CreateTempAlloca(RetTy, v.getAlignment(), "null"); |
| 2746 | CGF.InitTempAlloca(NullVal, llvm::Constant::getNullValue(RetTy)); |
| 2747 | phi->addIncoming(v.getPointer(), messageBB); |
| 2748 | phi->addIncoming(NullVal.getPointer(), startBB); |
| 2749 | msgRet = RValue::getAggregate(Address(phi, v.getAlignment())); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 2750 | } else /* isComplex() */ { |
| 2751 | std::pair<llvm::Value*,llvm::Value*> v = msgRet.getComplexVal(); |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 2752 | llvm::PHINode *phi = Builder.CreatePHI(v.first->getType(), 2); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 2753 | phi->addIncoming(v.first, messageBB); |
| 2754 | phi->addIncoming(llvm::Constant::getNullValue(v.first->getType()), |
| 2755 | startBB); |
Jay Foad | 20c0f02 | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 2756 | llvm::PHINode *phi2 = Builder.CreatePHI(v.second->getType(), 2); |
David Chisnall | 75afda6 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 2757 | phi2->addIncoming(v.second, messageBB); |
| 2758 | phi2->addIncoming(llvm::Constant::getNullValue(v.second->getType()), |
| 2759 | startBB); |
| 2760 | msgRet = RValue::getComplex(phi, phi2); |
| 2761 | } |
| 2762 | } |
| 2763 | return msgRet; |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 2764 | } |
| 2765 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2766 | /// Generates a MethodList. Used in construction of a objc_class and |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2767 | /// objc_category structures. |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 2768 | llvm::Constant *CGObjCGNU:: |
Craig Topper | bf3e327 | 2014-08-30 16:55:52 +0000 | [diff] [blame] | 2769 | GenerateMethodList(StringRef ClassName, |
| 2770 | StringRef CategoryName, |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2771 | ArrayRef<const ObjCMethodDecl*> Methods, |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 2772 | bool isClassMethodList) { |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2773 | if (Methods.empty()) |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2774 | return NULLPtr; |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2775 | |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 2776 | ConstantInitBuilder Builder(CGM); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2777 | |
| 2778 | auto MethodList = Builder.beginStruct(); |
| 2779 | MethodList.addNullPointer(CGM.Int8PtrTy); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2780 | MethodList.addInt(Int32Ty, Methods.size()); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2781 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2782 | // Get the method structure type. |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 2783 | llvm::StructType *ObjCMethodTy = |
| 2784 | llvm::StructType::get(CGM.getLLVMContext(), { |
| 2785 | PtrToInt8Ty, // Really a selector, but the runtime creates it us. |
| 2786 | PtrToInt8Ty, // Method types |
| 2787 | IMPTy // Method pointer |
| 2788 | }); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2789 | bool isV2ABI = isRuntime(ObjCRuntime::GNUstep, 2); |
| 2790 | if (isV2ABI) { |
| 2791 | // size_t size; |
| 2792 | llvm::DataLayout td(&TheModule); |
| 2793 | MethodList.addInt(SizeTy, td.getTypeSizeInBits(ObjCMethodTy) / |
| 2794 | CGM.getContext().getCharWidth()); |
| 2795 | ObjCMethodTy = |
| 2796 | llvm::StructType::get(CGM.getLLVMContext(), { |
| 2797 | IMPTy, // Method pointer |
| 2798 | PtrToInt8Ty, // Selector |
| 2799 | PtrToInt8Ty // Extended type encoding |
| 2800 | }); |
| 2801 | } else { |
| 2802 | ObjCMethodTy = |
| 2803 | llvm::StructType::get(CGM.getLLVMContext(), { |
| 2804 | PtrToInt8Ty, // Really a selector, but the runtime creates it us. |
| 2805 | PtrToInt8Ty, // Method types |
| 2806 | IMPTy // Method pointer |
| 2807 | }); |
| 2808 | } |
| 2809 | auto MethodArray = MethodList.beginArray(); |
| 2810 | ASTContext &Context = CGM.getContext(); |
| 2811 | for (const auto *OMD : Methods) { |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 2812 | llvm::Constant *FnPtr = |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2813 | TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName, |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2814 | OMD->getSelector(), |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2815 | isClassMethodList)); |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 2816 | assert(FnPtr && "Can't generate metadata for method that doesn't exist"); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2817 | auto Method = MethodArray.beginStruct(ObjCMethodTy); |
| 2818 | if (isV2ABI) { |
| 2819 | Method.addBitCast(FnPtr, IMPTy); |
| 2820 | Method.add(GetConstantSelector(OMD->getSelector(), |
| 2821 | Context.getObjCEncodingForMethodDecl(OMD))); |
| 2822 | Method.add(MakeConstantString(Context.getObjCEncodingForMethodDecl(OMD, true))); |
| 2823 | } else { |
| 2824 | Method.add(MakeConstantString(OMD->getSelector().getAsString())); |
| 2825 | Method.add(MakeConstantString(Context.getObjCEncodingForMethodDecl(OMD))); |
| 2826 | Method.addBitCast(FnPtr, IMPTy); |
| 2827 | } |
| 2828 | Method.finishAndAddTo(MethodArray); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2829 | } |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2830 | MethodArray.finishAndAddTo(MethodList); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2831 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2832 | // Create an instance of the structure |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2833 | return MethodList.finishAndCreateGlobal(".objc_method_list", |
| 2834 | CGM.getPointerAlign()); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2835 | } |
| 2836 | |
| 2837 | /// Generates an IvarList. Used in construction of a objc_class. |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 2838 | llvm::Constant *CGObjCGNU:: |
| 2839 | GenerateIvarList(ArrayRef<llvm::Constant *> IvarNames, |
| 2840 | ArrayRef<llvm::Constant *> IvarTypes, |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2841 | ArrayRef<llvm::Constant *> IvarOffsets, |
| 2842 | ArrayRef<llvm::Constant *> IvarAlign, |
| 2843 | ArrayRef<Qualifiers::ObjCLifetime> IvarOwnership) { |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2844 | if (IvarNames.empty()) |
David Chisnall | b3b44ce | 2009-11-16 19:05:54 +0000 | [diff] [blame] | 2845 | return NULLPtr; |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2846 | |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 2847 | ConstantInitBuilder Builder(CGM); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2848 | |
| 2849 | // Structure containing array count followed by array. |
| 2850 | auto IvarList = Builder.beginStruct(); |
| 2851 | IvarList.addInt(IntTy, (int)IvarNames.size()); |
| 2852 | |
| 2853 | // Get the ivar structure type. |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2854 | llvm::StructType *ObjCIvarTy = |
| 2855 | llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2856 | |
| 2857 | // Array of ivar structures. |
| 2858 | auto Ivars = IvarList.beginArray(ObjCIvarTy); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2859 | for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) { |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2860 | auto Ivar = Ivars.beginStruct(ObjCIvarTy); |
| 2861 | Ivar.add(IvarNames[i]); |
| 2862 | Ivar.add(IvarTypes[i]); |
| 2863 | Ivar.add(IvarOffsets[i]); |
John McCall | f178863 | 2016-11-28 22:18:30 +0000 | [diff] [blame] | 2864 | Ivar.finishAndAddTo(Ivars); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2865 | } |
John McCall | f178863 | 2016-11-28 22:18:30 +0000 | [diff] [blame] | 2866 | Ivars.finishAndAddTo(IvarList); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2867 | |
| 2868 | // Create an instance of the structure |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2869 | return IvarList.finishAndCreateGlobal(".objc_ivar_list", |
| 2870 | CGM.getPointerAlign()); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2871 | } |
| 2872 | |
| 2873 | /// Generate a class structure |
| 2874 | llvm::Constant *CGObjCGNU::GenerateClassStructure( |
| 2875 | llvm::Constant *MetaClass, |
| 2876 | llvm::Constant *SuperClass, |
| 2877 | unsigned info, |
Chris Lattner | da35bc8 | 2008-06-26 04:47:04 +0000 | [diff] [blame] | 2878 | const char *Name, |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2879 | llvm::Constant *Version, |
| 2880 | llvm::Constant *InstanceSize, |
| 2881 | llvm::Constant *IVars, |
| 2882 | llvm::Constant *Methods, |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2883 | llvm::Constant *Protocols, |
| 2884 | llvm::Constant *IvarOffsets, |
David Chisnall | d472c85 | 2010-04-28 14:29:56 +0000 | [diff] [blame] | 2885 | llvm::Constant *Properties, |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 2886 | llvm::Constant *StrongIvarBitmap, |
| 2887 | llvm::Constant *WeakIvarBitmap, |
David Chisnall | d472c85 | 2010-04-28 14:29:56 +0000 | [diff] [blame] | 2888 | bool isMeta) { |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2889 | // Set up the class structure |
| 2890 | // Note: Several of these are char*s when they should be ids. This is |
| 2891 | // because the runtime performs this translation on load. |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2892 | // |
| 2893 | // Fields marked New ABI are part of the GNUstep runtime. We emit them |
| 2894 | // anyway; the classes will still work with the GNU runtime, they will just |
| 2895 | // be ignored. |
Chris Lattner | 845511f | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2896 | llvm::StructType *ClassTy = llvm::StructType::get( |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2897 | PtrToInt8Ty, // isa |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2898 | PtrToInt8Ty, // super_class |
| 2899 | PtrToInt8Ty, // name |
| 2900 | LongTy, // version |
| 2901 | LongTy, // info |
| 2902 | LongTy, // instance_size |
| 2903 | IVars->getType(), // ivars |
| 2904 | Methods->getType(), // methods |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2905 | // These are all filled in by the runtime, so we pretend |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2906 | PtrTy, // dtable |
| 2907 | PtrTy, // subclass_list |
| 2908 | PtrTy, // sibling_class |
| 2909 | PtrTy, // protocols |
| 2910 | PtrTy, // gc_object_type |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2911 | // New ABI: |
| 2912 | LongTy, // abi_version |
| 2913 | IvarOffsets->getType(), // ivar_offsets |
| 2914 | Properties->getType(), // properties |
David Chisnall | e89ac06 | 2011-10-25 10:12:21 +0000 | [diff] [blame] | 2915 | IntPtrTy, // strong_pointers |
Serge Guelton | 1d99327 | 2017-05-09 19:31:30 +0000 | [diff] [blame] | 2916 | IntPtrTy // weak_pointers |
| 2917 | ); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2918 | |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 2919 | ConstantInitBuilder Builder(CGM); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2920 | auto Elements = Builder.beginStruct(ClassTy); |
| 2921 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2922 | // Fill in the structure |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2923 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2924 | // isa |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 2925 | Elements.addBitCast(MetaClass, PtrToInt8Ty); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2926 | // super_class |
| 2927 | Elements.add(SuperClass); |
| 2928 | // name |
| 2929 | Elements.add(MakeConstantString(Name, ".class_name")); |
| 2930 | // version |
| 2931 | Elements.addInt(LongTy, 0); |
| 2932 | // info |
| 2933 | Elements.addInt(LongTy, info); |
| 2934 | // instance_size |
David Chisnall | 055f064 | 2011-02-21 23:47:40 +0000 | [diff] [blame] | 2935 | if (isMeta) { |
Micah Villmow | dd31ca1 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2936 | llvm::DataLayout td(&TheModule); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2937 | Elements.addInt(LongTy, |
| 2938 | td.getTypeSizeInBits(ClassTy) / |
| 2939 | CGM.getContext().getCharWidth()); |
David Chisnall | 055f064 | 2011-02-21 23:47:40 +0000 | [diff] [blame] | 2940 | } else |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2941 | Elements.add(InstanceSize); |
| 2942 | // ivars |
| 2943 | Elements.add(IVars); |
| 2944 | // methods |
| 2945 | Elements.add(Methods); |
| 2946 | // These are all filled in by the runtime, so we pretend |
| 2947 | // dtable |
| 2948 | Elements.add(NULLPtr); |
| 2949 | // subclass_list |
| 2950 | Elements.add(NULLPtr); |
| 2951 | // sibling_class |
| 2952 | Elements.add(NULLPtr); |
| 2953 | // protocols |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 2954 | Elements.addBitCast(Protocols, PtrTy); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2955 | // gc_object_type |
| 2956 | Elements.add(NULLPtr); |
| 2957 | // abi_version |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2958 | Elements.addInt(LongTy, ClassABIVersion); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2959 | // ivar_offsets |
| 2960 | Elements.add(IvarOffsets); |
| 2961 | // properties |
| 2962 | Elements.add(Properties); |
| 2963 | // strong_pointers |
| 2964 | Elements.add(StrongIvarBitmap); |
| 2965 | // weak_pointers |
| 2966 | Elements.add(WeakIvarBitmap); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2967 | // Create an instance of the structure |
David Chisnall | df34917 | 2010-01-08 00:14:31 +0000 | [diff] [blame] | 2968 | // 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] | 2969 | // messages in the next ABI. We may already have some weak references to |
| 2970 | // this, so check and fix them properly. |
| 2971 | std::string ClassSym((isMeta ? "_OBJC_METACLASS_": "_OBJC_CLASS_") + |
| 2972 | std::string(Name)); |
| 2973 | llvm::GlobalVariable *ClassRef = TheModule.getNamedGlobal(ClassSym); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2974 | llvm::Constant *Class = |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2975 | Elements.finishAndCreateGlobal(ClassSym, CGM.getPointerAlign(), false, |
| 2976 | llvm::GlobalValue::ExternalLinkage); |
David Chisnall | 207a630 | 2012-01-04 12:02:13 +0000 | [diff] [blame] | 2977 | if (ClassRef) { |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2978 | ClassRef->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(Class, |
David Chisnall | 207a630 | 2012-01-04 12:02:13 +0000 | [diff] [blame] | 2979 | ClassRef->getType())); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2980 | ClassRef->removeFromParent(); |
| 2981 | Class->setName(ClassSym); |
David Chisnall | 207a630 | 2012-01-04 12:02:13 +0000 | [diff] [blame] | 2982 | } |
| 2983 | return Class; |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2984 | } |
| 2985 | |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 2986 | llvm::Constant *CGObjCGNU:: |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2987 | GenerateProtocolMethodList(ArrayRef<const ObjCMethodDecl*> Methods) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2988 | // Get the method structure type. |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2989 | llvm::StructType *ObjCMethodDescTy = |
| 2990 | llvm::StructType::get(CGM.getLLVMContext(), { PtrToInt8Ty, PtrToInt8Ty }); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2991 | ASTContext &Context = CGM.getContext(); |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 2992 | ConstantInitBuilder Builder(CGM); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 2993 | auto MethodList = Builder.beginStruct(); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 2994 | MethodList.addInt(IntTy, Methods.size()); |
| 2995 | auto MethodArray = MethodList.beginArray(ObjCMethodDescTy); |
| 2996 | for (auto *M : Methods) { |
| 2997 | auto Method = MethodArray.beginStruct(ObjCMethodDescTy); |
| 2998 | Method.add(MakeConstantString(M->getSelector().getAsString())); |
| 2999 | Method.add(MakeConstantString(Context.getObjCEncodingForMethodDecl(M))); |
| 3000 | Method.finishAndAddTo(MethodArray); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3001 | } |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3002 | MethodArray.finishAndAddTo(MethodList); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3003 | return MethodList.finishAndCreateGlobal(".objc_method_list", |
| 3004 | CGM.getPointerAlign()); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3005 | } |
Mike Stump | dd93a19 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 3006 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3007 | // Create the protocol list structure used in classes, categories and so on |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3008 | llvm::Constant * |
| 3009 | CGObjCGNU::GenerateProtocolList(ArrayRef<std::string> Protocols) { |
| 3010 | |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 3011 | ConstantInitBuilder Builder(CGM); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3012 | auto ProtocolList = Builder.beginStruct(); |
| 3013 | ProtocolList.add(NULLPtr); |
| 3014 | ProtocolList.addInt(LongTy, Protocols.size()); |
| 3015 | |
| 3016 | auto Elements = ProtocolList.beginArray(PtrToInt8Ty); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3017 | for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end(); |
| 3018 | iter != endIter ; iter++) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3019 | llvm::Constant *protocol = nullptr; |
David Chisnall | bc8bdea | 2009-11-20 14:50:59 +0000 | [diff] [blame] | 3020 | llvm::StringMap<llvm::Constant*>::iterator value = |
| 3021 | ExistingProtocols.find(*iter); |
| 3022 | if (value == ExistingProtocols.end()) { |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 3023 | protocol = GenerateEmptyProtocol(*iter); |
David Chisnall | bc8bdea | 2009-11-20 14:50:59 +0000 | [diff] [blame] | 3024 | } else { |
| 3025 | protocol = value->getValue(); |
| 3026 | } |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3027 | Elements.addBitCast(protocol, PtrToInt8Ty); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3028 | } |
John McCall | f178863 | 2016-11-28 22:18:30 +0000 | [diff] [blame] | 3029 | Elements.finishAndAddTo(ProtocolList); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3030 | return ProtocolList.finishAndCreateGlobal(".objc_protocol_list", |
| 3031 | CGM.getPointerAlign()); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3032 | } |
| 3033 | |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3034 | llvm::Value *CGObjCGNU::GenerateProtocolRef(CodeGenFunction &CGF, |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 3035 | const ObjCProtocolDecl *PD) { |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3036 | llvm::Constant *&protocol = ExistingProtocols[PD->getNameAsString()]; |
| 3037 | if (!protocol) |
| 3038 | GenerateProtocol(PD); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3039 | llvm::Type *T = |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 3040 | CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType()); |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3041 | return CGF.Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T)); |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 3042 | } |
| 3043 | |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3044 | llvm::Constant * |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3045 | CGObjCGNU::GenerateEmptyProtocol(StringRef ProtocolName) { |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3046 | llvm::Constant *ProtocolList = GenerateProtocolList({}); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3047 | llvm::Constant *MethodList = GenerateProtocolMethodList({}); |
| 3048 | MethodList = llvm::ConstantExpr::getBitCast(MethodList, PtrToInt8Ty); |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 3049 | // Protocols are objects containing lists of the methods implemented and |
| 3050 | // protocols adopted. |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 3051 | ConstantInitBuilder Builder(CGM); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3052 | auto Elements = Builder.beginStruct(); |
| 3053 | |
Fariborz Jahanian | 89d2397 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 3054 | // The isa pointer must be set to a magic number so the runtime knows it's |
| 3055 | // the correct layout. |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3056 | Elements.add(llvm::ConstantExpr::getIntToPtr( |
| 3057 | llvm::ConstantInt::get(Int32Ty, ProtocolVersion), IdTy)); |
| 3058 | |
| 3059 | Elements.add(MakeConstantString(ProtocolName, ".objc_protocol_name")); |
David Chisnall | 10e590e | 2018-04-12 06:46:15 +0000 | [diff] [blame] | 3060 | Elements.add(ProtocolList); /* .protocol_list */ |
| 3061 | Elements.add(MethodList); /* .instance_methods */ |
| 3062 | Elements.add(MethodList); /* .class_methods */ |
| 3063 | Elements.add(MethodList); /* .optional_instance_methods */ |
| 3064 | Elements.add(MethodList); /* .optional_class_methods */ |
| 3065 | Elements.add(NULLPtr); /* .properties */ |
| 3066 | Elements.add(NULLPtr); /* .optional_properties */ |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3067 | return Elements.finishAndCreateGlobal(SymbolForProtocol(ProtocolName), |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3068 | CGM.getPointerAlign()); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3069 | } |
| 3070 | |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 3071 | void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3072 | std::string ProtocolName = PD->getNameAsString(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3073 | |
Douglas Gregor | a715bff | 2012-01-01 19:51:50 +0000 | [diff] [blame] | 3074 | // Use the protocol definition, if there is one. |
| 3075 | if (const ObjCProtocolDecl *Def = PD->getDefinition()) |
| 3076 | PD = Def; |
| 3077 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3078 | SmallVector<std::string, 16> Protocols; |
Aaron Ballman | 0f6e64d | 2014-03-13 22:58:06 +0000 | [diff] [blame] | 3079 | for (const auto *PI : PD->protocols()) |
| 3080 | Protocols.push_back(PI->getNameAsString()); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3081 | SmallVector<const ObjCMethodDecl*, 16> InstanceMethods; |
| 3082 | SmallVector<const ObjCMethodDecl*, 16> OptionalInstanceMethods; |
| 3083 | for (const auto *I : PD->instance_methods()) |
| 3084 | if (I->isOptional()) |
| 3085 | OptionalInstanceMethods.push_back(I); |
| 3086 | else |
| 3087 | InstanceMethods.push_back(I); |
Daniel Dunbar | 89da6ad | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 3088 | // Collect information about class methods: |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3089 | SmallVector<const ObjCMethodDecl*, 16> ClassMethods; |
| 3090 | SmallVector<const ObjCMethodDecl*, 16> OptionalClassMethods; |
| 3091 | for (const auto *I : PD->class_methods()) |
| 3092 | if (I->isOptional()) |
| 3093 | OptionalClassMethods.push_back(I); |
| 3094 | else |
| 3095 | ClassMethods.push_back(I); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3096 | |
| 3097 | llvm::Constant *ProtocolList = GenerateProtocolList(Protocols); |
| 3098 | llvm::Constant *InstanceMethodList = |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3099 | GenerateProtocolMethodList(InstanceMethods); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3100 | llvm::Constant *ClassMethodList = |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3101 | GenerateProtocolMethodList(ClassMethods); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3102 | llvm::Constant *OptionalInstanceMethodList = |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3103 | GenerateProtocolMethodList(OptionalInstanceMethods); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3104 | llvm::Constant *OptionalClassMethodList = |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3105 | GenerateProtocolMethodList(OptionalClassMethods); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3106 | |
| 3107 | // Property metadata: name, attributes, isSynthesized, setter name, setter |
| 3108 | // types, getter name, getter types. |
| 3109 | // The isSynthesized value is always set to 0 in a protocol. It exists to |
| 3110 | // simplify the runtime library by allowing it to use the same data |
| 3111 | // structures for protocol metadata everywhere. |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3112 | |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3113 | llvm::Constant *PropertyList = |
| 3114 | GeneratePropertyList(nullptr, PD, false, false); |
| 3115 | llvm::Constant *OptionalPropertyList = |
| 3116 | GeneratePropertyList(nullptr, PD, false, true); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3117 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3118 | // Protocols are objects containing lists of the methods implemented and |
| 3119 | // protocols adopted. |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3120 | // The isa pointer must be set to a magic number so the runtime knows it's |
| 3121 | // the correct layout. |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 3122 | ConstantInitBuilder Builder(CGM); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3123 | auto Elements = Builder.beginStruct(); |
| 3124 | Elements.add( |
Benjamin Kramer | 3093473 | 2016-07-02 11:41:41 +0000 | [diff] [blame] | 3125 | llvm::ConstantExpr::getIntToPtr( |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3126 | llvm::ConstantInt::get(Int32Ty, ProtocolVersion), IdTy)); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3127 | Elements.add(MakeConstantString(ProtocolName)); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3128 | Elements.add(ProtocolList); |
| 3129 | Elements.add(InstanceMethodList); |
| 3130 | Elements.add(ClassMethodList); |
| 3131 | Elements.add(OptionalInstanceMethodList); |
| 3132 | Elements.add(OptionalClassMethodList); |
| 3133 | Elements.add(PropertyList); |
| 3134 | Elements.add(OptionalPropertyList); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3135 | ExistingProtocols[ProtocolName] = |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3136 | llvm::ConstantExpr::getBitCast( |
| 3137 | Elements.finishAndCreateGlobal(".objc_protocol", CGM.getPointerAlign()), |
| 3138 | IdTy); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3139 | } |
Dmitri Gribenko | b2aa923 | 2012-11-15 14:28:07 +0000 | [diff] [blame] | 3140 | void CGObjCGNU::GenerateProtocolHolderCategory() { |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3141 | // Collect information about instance methods |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3142 | |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 3143 | ConstantInitBuilder Builder(CGM); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3144 | auto Elements = Builder.beginStruct(); |
| 3145 | |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3146 | const std::string ClassName = "__ObjC_Protocol_Holder_Ugly_Hack"; |
| 3147 | const std::string CategoryName = "AnotherHack"; |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3148 | Elements.add(MakeConstantString(CategoryName)); |
| 3149 | Elements.add(MakeConstantString(ClassName)); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3150 | // Instance method list |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3151 | Elements.addBitCast(GenerateMethodList( |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3152 | ClassName, CategoryName, {}, false), PtrTy); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3153 | // Class method list |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3154 | Elements.addBitCast(GenerateMethodList( |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3155 | ClassName, CategoryName, {}, true), PtrTy); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3156 | |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3157 | // Protocol list |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 3158 | ConstantInitBuilder ProtocolListBuilder(CGM); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3159 | auto ProtocolList = ProtocolListBuilder.beginStruct(); |
| 3160 | ProtocolList.add(NULLPtr); |
| 3161 | ProtocolList.addInt(LongTy, ExistingProtocols.size()); |
| 3162 | auto ProtocolElements = ProtocolList.beginArray(PtrTy); |
| 3163 | for (auto iter = ExistingProtocols.begin(), endIter = ExistingProtocols.end(); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3164 | iter != endIter ; iter++) { |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3165 | ProtocolElements.addBitCast(iter->getValue(), PtrTy); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3166 | } |
John McCall | f178863 | 2016-11-28 22:18:30 +0000 | [diff] [blame] | 3167 | ProtocolElements.finishAndAddTo(ProtocolList); |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3168 | Elements.addBitCast( |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3169 | ProtocolList.finishAndCreateGlobal(".objc_protocol_list", |
| 3170 | CGM.getPointerAlign()), |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3171 | PtrTy); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3172 | Categories.push_back(llvm::ConstantExpr::getBitCast( |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3173 | Elements.finishAndCreateGlobal("", CGM.getPointerAlign()), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3174 | PtrTy)); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3175 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3176 | |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 3177 | /// Libobjc2 uses a bitfield representation where small(ish) bitfields are |
| 3178 | /// stored in a 64-bit value with the low bit set to 1 and the remaining 63 |
| 3179 | /// bits set to their values, LSB first, while larger ones are stored in a |
| 3180 | /// structure of this / form: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3181 | /// |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 3182 | /// struct { int32_t length; int32_t values[length]; }; |
| 3183 | /// |
| 3184 | /// The values in the array are stored in host-endian format, with the least |
| 3185 | /// significant bit being assumed to come first in the bitfield. Therefore, a |
| 3186 | /// bitfield with the 64th bit set will be (int64_t)&{ 2, [0, 1<<31] }, while a |
| 3187 | /// bitfield / with the 63rd bit set will be 1<<64. |
Bill Wendling | f1a3fca | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 3188 | llvm::Constant *CGObjCGNU::MakeBitField(ArrayRef<bool> bits) { |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 3189 | int bitCount = bits.size(); |
Rafael Espindola | 3cc5c2d | 2014-01-09 21:32:51 +0000 | [diff] [blame] | 3190 | int ptrBits = CGM.getDataLayout().getPointerSizeInBits(); |
David Chisnall | e89ac06 | 2011-10-25 10:12:21 +0000 | [diff] [blame] | 3191 | if (bitCount < ptrBits) { |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 3192 | uint64_t val = 1; |
| 3193 | for (int i=0 ; i<bitCount ; ++i) { |
Eli Friedman | 2352667 | 2011-10-08 01:03:47 +0000 | [diff] [blame] | 3194 | if (bits[i]) val |= 1ULL<<(i+1); |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 3195 | } |
David Chisnall | e89ac06 | 2011-10-25 10:12:21 +0000 | [diff] [blame] | 3196 | return llvm::ConstantInt::get(IntPtrTy, val); |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 3197 | } |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3198 | SmallVector<llvm::Constant *, 8> values; |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 3199 | int v=0; |
| 3200 | while (v < bitCount) { |
| 3201 | int32_t word = 0; |
| 3202 | for (int i=0 ; (i<32) && (v<bitCount) ; ++i) { |
| 3203 | if (bits[v]) word |= 1<<i; |
| 3204 | v++; |
| 3205 | } |
| 3206 | values.push_back(llvm::ConstantInt::get(Int32Ty, word)); |
| 3207 | } |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3208 | |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 3209 | ConstantInitBuilder builder(CGM); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3210 | auto fields = builder.beginStruct(); |
| 3211 | fields.addInt(Int32Ty, values.size()); |
| 3212 | auto array = fields.beginArray(); |
| 3213 | for (auto v : values) array.add(v); |
John McCall | f178863 | 2016-11-28 22:18:30 +0000 | [diff] [blame] | 3214 | array.finishAndAddTo(fields); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3215 | |
| 3216 | llvm::Constant *GS = |
| 3217 | fields.finishAndCreateGlobal("", CharUnits::fromQuantity(4)); |
David Chisnall | e0dc7cb | 2011-10-08 08:54:36 +0000 | [diff] [blame] | 3218 | llvm::Constant *ptr = llvm::ConstantExpr::getPtrToInt(GS, IntPtrTy); |
David Chisnall | e0dc7cb | 2011-10-08 08:54:36 +0000 | [diff] [blame] | 3219 | return ptr; |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 3220 | } |
| 3221 | |
David Chisnall | 386477a | 2018-12-28 17:44:54 +0000 | [diff] [blame] | 3222 | llvm::Constant *CGObjCGNU::GenerateCategoryProtocolList(const |
| 3223 | ObjCCategoryDecl *OCD) { |
| 3224 | SmallVector<std::string, 16> Protocols; |
| 3225 | for (const auto *PD : OCD->getReferencedProtocols()) |
| 3226 | Protocols.push_back(PD->getNameAsString()); |
| 3227 | return GenerateProtocolList(Protocols); |
| 3228 | } |
| 3229 | |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 3230 | void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3231 | const ObjCInterfaceDecl *Class = OCD->getClassInterface(); |
| 3232 | std::string ClassName = Class->getNameAsString(); |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3233 | std::string CategoryName = OCD->getNameAsString(); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 3234 | |
| 3235 | // Collect the names of referenced protocols |
David Chisnall | 2bfc50b | 2010-03-13 22:20:45 +0000 | [diff] [blame] | 3236 | const ObjCCategoryDecl *CatDecl = OCD->getCategoryDecl(); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 3237 | |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 3238 | ConstantInitBuilder Builder(CGM); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3239 | auto Elements = Builder.beginStruct(); |
| 3240 | Elements.add(MakeConstantString(CategoryName)); |
| 3241 | Elements.add(MakeConstantString(ClassName)); |
| 3242 | // Instance method list |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3243 | SmallVector<ObjCMethodDecl*, 16> InstanceMethods; |
| 3244 | InstanceMethods.insert(InstanceMethods.begin(), OCD->instmeth_begin(), |
| 3245 | OCD->instmeth_end()); |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3246 | Elements.addBitCast( |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3247 | GenerateMethodList(ClassName, CategoryName, InstanceMethods, false), |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3248 | PtrTy); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3249 | // Class method list |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3250 | |
| 3251 | SmallVector<ObjCMethodDecl*, 16> ClassMethods; |
| 3252 | ClassMethods.insert(ClassMethods.begin(), OCD->classmeth_begin(), |
| 3253 | OCD->classmeth_end()); |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3254 | Elements.addBitCast( |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3255 | GenerateMethodList(ClassName, CategoryName, ClassMethods, true), |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3256 | PtrTy); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3257 | // Protocol list |
David Chisnall | 386477a | 2018-12-28 17:44:54 +0000 | [diff] [blame] | 3258 | Elements.addBitCast(GenerateCategoryProtocolList(CatDecl), PtrTy); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3259 | if (isRuntime(ObjCRuntime::GNUstep, 2)) { |
| 3260 | const ObjCCategoryDecl *Category = |
| 3261 | Class->FindCategoryDeclaration(OCD->getIdentifier()); |
| 3262 | if (Category) { |
| 3263 | // Instance properties |
| 3264 | Elements.addBitCast(GeneratePropertyList(OCD, Category, false), PtrTy); |
| 3265 | // Class properties |
| 3266 | Elements.addBitCast(GeneratePropertyList(OCD, Category, true), PtrTy); |
| 3267 | } else { |
| 3268 | Elements.addNullPointer(PtrTy); |
| 3269 | Elements.addNullPointer(PtrTy); |
| 3270 | } |
| 3271 | } |
| 3272 | |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3273 | Categories.push_back(llvm::ConstantExpr::getBitCast( |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3274 | Elements.finishAndCreateGlobal( |
| 3275 | std::string(".objc_category_")+ClassName+CategoryName, |
| 3276 | CGM.getPointerAlign()), |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3277 | PtrTy)); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3278 | } |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 3279 | |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3280 | llvm::Constant *CGObjCGNU::GeneratePropertyList(const Decl *Container, |
| 3281 | const ObjCContainerDecl *OCD, |
| 3282 | bool isClassProperty, |
| 3283 | bool protocolOptionalProperties) { |
David Chisnall | 79356ee | 2018-05-22 06:09:23 +0000 | [diff] [blame] | 3284 | |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3285 | SmallVector<const ObjCPropertyDecl *, 16> Properties; |
| 3286 | llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet; |
| 3287 | bool isProtocol = isa<ObjCProtocolDecl>(OCD); |
| 3288 | ASTContext &Context = CGM.getContext(); |
| 3289 | |
| 3290 | std::function<void(const ObjCProtocolDecl *Proto)> collectProtocolProperties |
| 3291 | = [&](const ObjCProtocolDecl *Proto) { |
| 3292 | for (const auto *P : Proto->protocols()) |
| 3293 | collectProtocolProperties(P); |
| 3294 | for (const auto *PD : Proto->properties()) { |
| 3295 | if (isClassProperty != PD->isClassProperty()) |
| 3296 | continue; |
| 3297 | // Skip any properties that are declared in protocols that this class |
| 3298 | // conforms to but are not actually implemented by this class. |
| 3299 | if (!isProtocol && !Context.getObjCPropertyImplDeclForPropertyDecl(PD, Container)) |
| 3300 | continue; |
| 3301 | if (!PropertySet.insert(PD->getIdentifier()).second) |
| 3302 | continue; |
| 3303 | Properties.push_back(PD); |
| 3304 | } |
| 3305 | }; |
| 3306 | |
| 3307 | if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) |
| 3308 | for (const ObjCCategoryDecl *ClassExt : OID->known_extensions()) |
| 3309 | for (auto *PD : ClassExt->properties()) { |
| 3310 | if (isClassProperty != PD->isClassProperty()) |
| 3311 | continue; |
| 3312 | PropertySet.insert(PD->getIdentifier()); |
| 3313 | Properties.push_back(PD); |
| 3314 | } |
| 3315 | |
| 3316 | for (const auto *PD : OCD->properties()) { |
| 3317 | if (isClassProperty != PD->isClassProperty()) |
| 3318 | continue; |
| 3319 | // If we're generating a list for a protocol, skip optional / required ones |
| 3320 | // when generating the other list. |
| 3321 | if (isProtocol && (protocolOptionalProperties != PD->isOptional())) |
| 3322 | continue; |
| 3323 | // Don't emit duplicate metadata for properties that were already in a |
| 3324 | // class extension. |
| 3325 | if (!PropertySet.insert(PD->getIdentifier()).second) |
| 3326 | continue; |
| 3327 | |
| 3328 | Properties.push_back(PD); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3329 | } |
| 3330 | |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3331 | if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) |
| 3332 | for (const auto *P : OID->all_referenced_protocols()) |
| 3333 | collectProtocolProperties(P); |
| 3334 | else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) |
| 3335 | for (const auto *P : CD->protocols()) |
| 3336 | collectProtocolProperties(P); |
| 3337 | |
| 3338 | auto numProperties = Properties.size(); |
| 3339 | |
| 3340 | if (numProperties == 0) |
| 3341 | return NULLPtr; |
| 3342 | |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 3343 | ConstantInitBuilder builder(CGM); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3344 | auto propertyList = builder.beginStruct(); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3345 | auto properties = PushPropertyListHeader(propertyList, numProperties); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3346 | |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3347 | // Add all of the property methods need adding to the method list and to the |
| 3348 | // property metadata list. |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3349 | for (auto *property : Properties) { |
| 3350 | bool isSynthesized = false; |
| 3351 | bool isDynamic = false; |
| 3352 | if (!isProtocol) { |
| 3353 | auto *propertyImpl = Context.getObjCPropertyImplDeclForPropertyDecl(property, Container); |
| 3354 | if (propertyImpl) { |
| 3355 | isSynthesized = (propertyImpl->getPropertyImplementation() == |
| 3356 | ObjCPropertyImplDecl::Synthesize); |
| 3357 | isDynamic = (propertyImpl->getPropertyImplementation() == |
| 3358 | ObjCPropertyImplDecl::Dynamic); |
David Chisnall | 36c6320 | 2010-02-26 01:11:38 +0000 | [diff] [blame] | 3359 | } |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3360 | } |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3361 | PushProperty(properties, property, Container, isSynthesized, isDynamic); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3362 | } |
John McCall | f178863 | 2016-11-28 22:18:30 +0000 | [diff] [blame] | 3363 | properties.finishAndAddTo(propertyList); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3364 | |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3365 | return propertyList.finishAndCreateGlobal(".objc_property_list", |
| 3366 | CGM.getPointerAlign()); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3367 | } |
| 3368 | |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 3369 | void CGObjCGNU::RegisterAlias(const ObjCCompatibleAliasDecl *OAD) { |
| 3370 | // Get the class declaration for which the alias is specified. |
| 3371 | ObjCInterfaceDecl *ClassDecl = |
| 3372 | const_cast<ObjCInterfaceDecl *>(OAD->getClassInterface()); |
Benjamin Kramer | 3204b15 | 2015-05-29 19:42:19 +0000 | [diff] [blame] | 3373 | ClassAliases.emplace_back(ClassDecl->getNameAsString(), |
| 3374 | OAD->getNameAsString()); |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 3375 | } |
| 3376 | |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 3377 | void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) { |
| 3378 | ASTContext &Context = CGM.getContext(); |
| 3379 | |
| 3380 | // Get the superclass name. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3381 | const ObjCInterfaceDecl * SuperClassDecl = |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 3382 | OID->getClassInterface()->getSuperClass(); |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3383 | std::string SuperClassName; |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 3384 | if (SuperClassDecl) { |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3385 | SuperClassName = SuperClassDecl->getNameAsString(); |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 3386 | EmitClassRef(SuperClassName); |
| 3387 | } |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 3388 | |
| 3389 | // Get the class name |
Chris Lattner | 87bc387 | 2009-04-01 02:00:48 +0000 | [diff] [blame] | 3390 | ObjCInterfaceDecl *ClassDecl = |
Saleem Abdulrasool | a088ad9 | 2016-07-17 22:27:41 +0000 | [diff] [blame] | 3391 | const_cast<ObjCInterfaceDecl *>(OID->getClassInterface()); |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3392 | std::string ClassName = ClassDecl->getNameAsString(); |
Saleem Abdulrasool | a088ad9 | 2016-07-17 22:27:41 +0000 | [diff] [blame] | 3393 | |
Chris Lattner | c7d2bfa | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 3394 | // Emit the symbol that is used to generate linker errors if this class is |
| 3395 | // referenced in other modules but not declared. |
Fariborz Jahanian | bacbed9 | 2009-07-03 15:10:14 +0000 | [diff] [blame] | 3396 | std::string classSymbolName = "__objc_class_name_" + ClassName; |
Saleem Abdulrasool | a088ad9 | 2016-07-17 22:27:41 +0000 | [diff] [blame] | 3397 | if (auto *symbol = TheModule.getGlobalVariable(classSymbolName)) { |
Owen Anderson | b7a2fe6 | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 3398 | symbol->setInitializer(llvm::ConstantInt::get(LongTy, 0)); |
Fariborz Jahanian | bacbed9 | 2009-07-03 15:10:14 +0000 | [diff] [blame] | 3399 | } else { |
Owen Anderson | c10c8d3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 3400 | new llvm::GlobalVariable(TheModule, LongTy, false, |
Saleem Abdulrasool | a088ad9 | 2016-07-17 22:27:41 +0000 | [diff] [blame] | 3401 | llvm::GlobalValue::ExternalLinkage, |
| 3402 | llvm::ConstantInt::get(LongTy, 0), |
| 3403 | classSymbolName); |
Fariborz Jahanian | bacbed9 | 2009-07-03 15:10:14 +0000 | [diff] [blame] | 3404 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3405 | |
Daniel Dunbar | 12119b9 | 2009-05-03 10:46:44 +0000 | [diff] [blame] | 3406 | // Get the size of instances. |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3407 | int instanceSize = |
Ken Dyck | c8ae550 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 3408 | Context.getASTObjCImplementationLayout(OID).getSize().getQuantity(); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 3409 | |
| 3410 | // Collect information about instance variables. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3411 | SmallVector<llvm::Constant*, 16> IvarNames; |
| 3412 | SmallVector<llvm::Constant*, 16> IvarTypes; |
| 3413 | SmallVector<llvm::Constant*, 16> IvarOffsets; |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3414 | SmallVector<llvm::Constant*, 16> IvarAligns; |
| 3415 | SmallVector<Qualifiers::ObjCLifetime, 16> IvarOwnership; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3416 | |
John McCall | 23c9dc6 | 2016-11-28 22:18:27 +0000 | [diff] [blame] | 3417 | ConstantInitBuilder IvarOffsetBuilder(CGM); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3418 | auto IvarOffsetValues = IvarOffsetBuilder.beginArray(PtrToIntTy); |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 3419 | SmallVector<bool, 16> WeakIvars; |
| 3420 | SmallVector<bool, 16> StrongIvars; |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3421 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3422 | int superInstanceSize = !SuperClassDecl ? 0 : |
Ken Dyck | c8ae550 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 3423 | Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize().getQuantity(); |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 3424 | // For non-fragile ivars, set the instance size to 0 - {the size of just this |
| 3425 | // 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] | 3426 | if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 3427 | instanceSize = 0 - (instanceSize - superInstanceSize); |
| 3428 | } |
David Chisnall | 18cf737 | 2010-04-19 00:45:34 +0000 | [diff] [blame] | 3429 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3430 | for (const ObjCIvarDecl *IVD = ClassDecl->all_declared_ivar_begin(); IVD; |
| 3431 | IVD = IVD->getNextIvar()) { |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 3432 | // Store the name |
David Chisnall | 18cf737 | 2010-04-19 00:45:34 +0000 | [diff] [blame] | 3433 | IvarNames.push_back(MakeConstantString(IVD->getNameAsString())); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 3434 | // Get the type encoding for this ivar |
| 3435 | std::string TypeStr; |
Akira Hatanaka | ff8534b | 2017-03-14 04:00:52 +0000 | [diff] [blame] | 3436 | Context.getObjCEncodingForType(IVD->getType(), TypeStr, IVD); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 3437 | IvarTypes.push_back(MakeConstantString(TypeStr)); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3438 | IvarAligns.push_back(llvm::ConstantInt::get(IntTy, |
| 3439 | Context.getTypeSize(IVD->getType()))); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 3440 | // Get the offset |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 3441 | uint64_t BaseOffset = ComputeIvarBaseOffset(CGM, OID, IVD); |
David Chisnall | cb1b7bf | 2009-11-17 19:32:15 +0000 | [diff] [blame] | 3442 | uint64_t Offset = BaseOffset; |
Richard Smith | 9c6890a | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 3443 | if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3444 | Offset = BaseOffset - superInstanceSize; |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 3445 | } |
David Chisnall | 1bfe6d3 | 2011-07-07 12:34:51 +0000 | [diff] [blame] | 3446 | llvm::Constant *OffsetValue = llvm::ConstantInt::get(IntTy, Offset); |
| 3447 | // Create the direct offset value |
| 3448 | std::string OffsetName = "__objc_ivar_offset_value_" + ClassName +"." + |
| 3449 | IVD->getNameAsString(); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3450 | |
David Chisnall | 1bfe6d3 | 2011-07-07 12:34:51 +0000 | [diff] [blame] | 3451 | llvm::GlobalVariable *OffsetVar = TheModule.getGlobalVariable(OffsetName); |
| 3452 | if (OffsetVar) { |
| 3453 | OffsetVar->setInitializer(OffsetValue); |
| 3454 | // If this is the real definition, change its linkage type so that |
| 3455 | // different modules will use this one, rather than their private |
| 3456 | // copy. |
| 3457 | OffsetVar->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 3458 | } else |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3459 | OffsetVar = new llvm::GlobalVariable(TheModule, Int32Ty, |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3460 | false, llvm::GlobalValue::ExternalLinkage, |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3461 | OffsetValue, OffsetName); |
David Chisnall | 1bfe6d3 | 2011-07-07 12:34:51 +0000 | [diff] [blame] | 3462 | IvarOffsets.push_back(OffsetValue); |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3463 | IvarOffsetValues.add(OffsetVar); |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 3464 | Qualifiers::ObjCLifetime lt = IVD->getType().getQualifiers().getObjCLifetime(); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3465 | IvarOwnership.push_back(lt); |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 3466 | switch (lt) { |
| 3467 | case Qualifiers::OCL_Strong: |
| 3468 | StrongIvars.push_back(true); |
| 3469 | WeakIvars.push_back(false); |
| 3470 | break; |
| 3471 | case Qualifiers::OCL_Weak: |
| 3472 | StrongIvars.push_back(false); |
| 3473 | WeakIvars.push_back(true); |
| 3474 | break; |
| 3475 | default: |
| 3476 | StrongIvars.push_back(false); |
| 3477 | WeakIvars.push_back(false); |
| 3478 | } |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 3479 | } |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 3480 | llvm::Constant *StrongIvarBitmap = MakeBitField(StrongIvars); |
| 3481 | llvm::Constant *WeakIvarBitmap = MakeBitField(WeakIvars); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3482 | llvm::GlobalVariable *IvarOffsetArray = |
John McCall | 6c9f1fdb | 2016-11-19 08:17:24 +0000 | [diff] [blame] | 3483 | IvarOffsetValues.finishAndCreateGlobal(".ivar.offsets", |
| 3484 | CGM.getPointerAlign()); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3485 | |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 3486 | // Collect information about instance methods |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3487 | SmallVector<const ObjCMethodDecl*, 16> InstanceMethods; |
| 3488 | InstanceMethods.insert(InstanceMethods.begin(), OID->instmeth_begin(), |
| 3489 | OID->instmeth_end()); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3490 | |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3491 | SmallVector<const ObjCMethodDecl*, 16> ClassMethods; |
| 3492 | ClassMethods.insert(ClassMethods.begin(), OID->classmeth_begin(), |
| 3493 | OID->classmeth_end()); |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3494 | |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3495 | // Collect the same information about synthesized properties, which don't |
| 3496 | // show up in the instance method lists. |
| 3497 | for (auto *propertyImpl : OID->property_impls()) |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3498 | if (propertyImpl->getPropertyImplementation() == |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3499 | ObjCPropertyImplDecl::Synthesize) { |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3500 | auto addPropertyMethod = [&](const ObjCMethodDecl *accessor) { |
| 3501 | if (accessor) |
| 3502 | InstanceMethods.push_back(accessor); |
| 3503 | }; |
Adrian Prantl | 2073dd2 | 2019-11-04 14:28:14 -0800 | [diff] [blame] | 3504 | addPropertyMethod(propertyImpl->getGetterMethodDecl()); |
| 3505 | addPropertyMethod(propertyImpl->getSetterMethodDecl()); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3506 | } |
| 3507 | |
| 3508 | llvm::Constant *Properties = GeneratePropertyList(OID, ClassDecl); |
| 3509 | |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 3510 | // Collect the names of referenced protocols |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3511 | SmallVector<std::string, 16> Protocols; |
Aaron Ballman | a49c506 | 2014-03-13 20:29:09 +0000 | [diff] [blame] | 3512 | for (const auto *I : ClassDecl->protocols()) |
| 3513 | Protocols.push_back(I->getNameAsString()); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 3514 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3515 | // Get the superclass pointer. |
| 3516 | llvm::Constant *SuperClass; |
Chris Lattner | 86d7d91 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 3517 | if (!SuperClassName.empty()) { |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3518 | SuperClass = MakeConstantString(SuperClassName, ".super_class_name"); |
| 3519 | } else { |
Owen Anderson | 7ec07a5 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 3520 | SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3521 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3522 | // Empty vector used to construct empty method lists |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3523 | SmallVector<llvm::Constant*, 1> empty; |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3524 | // Generate the method and instance variable lists |
| 3525 | llvm::Constant *MethodList = GenerateMethodList(ClassName, "", |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3526 | InstanceMethods, false); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3527 | llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "", |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3528 | ClassMethods, true); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3529 | llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes, |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3530 | IvarOffsets, IvarAligns, IvarOwnership); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3531 | // 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] | 3532 | // we emit a symbol containing the offset for each ivar in the class. This |
| 3533 | // allows code compiled for the non-Fragile ABI to inherit from code compiled |
| 3534 | // for the legacy ABI, without causing problems. The converse is also |
| 3535 | // possible, but causes all ivar accesses to be fragile. |
David Chisnall | e8431a7 | 2010-11-03 16:12:44 +0000 | [diff] [blame] | 3536 | |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 3537 | // Offset pointer for getting at the correct field in the ivar list when |
| 3538 | // setting up the alias. These are: The base address for the global, the |
| 3539 | // ivar array (second field), the ivar in this list (set for each ivar), and |
| 3540 | // the offset (third field in ivar structure) |
David Chisnall | cdd207e | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 3541 | llvm::Type *IndexTy = Int32Ty; |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 3542 | llvm::Constant *offsetPointerIndexes[] = {Zeros[0], |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3543 | llvm::ConstantInt::get(IndexTy, ClassABIVersion > 1 ? 2 : 1), nullptr, |
| 3544 | llvm::ConstantInt::get(IndexTy, ClassABIVersion > 1 ? 3 : 2) }; |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 3545 | |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3546 | unsigned ivarIndex = 0; |
| 3547 | for (const ObjCIvarDecl *IVD = ClassDecl->all_declared_ivar_begin(); IVD; |
| 3548 | IVD = IVD->getNextIvar()) { |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3549 | const std::string Name = GetIVarOffsetVariableName(ClassDecl, IVD); |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3550 | offsetPointerIndexes[2] = llvm::ConstantInt::get(IndexTy, ivarIndex); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 3551 | // Get the correct ivar field |
| 3552 | llvm::Constant *offsetValue = llvm::ConstantExpr::getGetElementPtr( |
David Blaikie | e3b172a | 2015-04-02 18:55:21 +0000 | [diff] [blame] | 3553 | cast<llvm::GlobalVariable>(IvarList)->getValueType(), IvarList, |
| 3554 | offsetPointerIndexes); |
David Chisnall | e8431a7 | 2010-11-03 16:12:44 +0000 | [diff] [blame] | 3555 | // Get the existing variable, if one exists. |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 3556 | llvm::GlobalVariable *offset = TheModule.getNamedGlobal(Name); |
| 3557 | if (offset) { |
Ted Kremenek | 669669f | 2012-04-04 00:55:25 +0000 | [diff] [blame] | 3558 | offset->setInitializer(offsetValue); |
| 3559 | // If this is the real definition, change its linkage type so that |
| 3560 | // different modules will use this one, rather than their private |
| 3561 | // copy. |
| 3562 | offset->setLinkage(llvm::GlobalValue::ExternalLinkage); |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3563 | } else |
Ted Kremenek | 669669f | 2012-04-04 00:55:25 +0000 | [diff] [blame] | 3564 | // Add a new alias if there isn't one already. |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3565 | new llvm::GlobalVariable(TheModule, offsetValue->getType(), |
Ted Kremenek | 669669f | 2012-04-04 00:55:25 +0000 | [diff] [blame] | 3566 | false, llvm::GlobalValue::ExternalLinkage, offsetValue, Name); |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 3567 | ++ivarIndex; |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 3568 | } |
David Chisnall | e89ac06 | 2011-10-25 10:12:21 +0000 | [diff] [blame] | 3569 | llvm::Constant *ZeroPtr = llvm::ConstantInt::get(IntPtrTy, 0); |
Saleem Abdulrasool | a088ad9 | 2016-07-17 22:27:41 +0000 | [diff] [blame] | 3570 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3571 | //Generate metaclass for class methods |
Saleem Abdulrasool | a088ad9 | 2016-07-17 22:27:41 +0000 | [diff] [blame] | 3572 | llvm::Constant *MetaClassStruct = GenerateClassStructure( |
| 3573 | NULLPtr, NULLPtr, 0x12L, ClassName.c_str(), nullptr, Zeros[0], |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 3574 | NULLPtr, ClassMethodList, NULLPtr, NULLPtr, |
| 3575 | GeneratePropertyList(OID, ClassDecl, true), ZeroPtr, ZeroPtr, true); |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 3576 | CGM.setGVProperties(cast<llvm::GlobalValue>(MetaClassStruct), |
| 3577 | OID->getClassInterface()); |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 3578 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3579 | // Generate the class structure |
Saleem Abdulrasool | a088ad9 | 2016-07-17 22:27:41 +0000 | [diff] [blame] | 3580 | llvm::Constant *ClassStruct = GenerateClassStructure( |
| 3581 | MetaClassStruct, SuperClass, 0x11L, ClassName.c_str(), nullptr, |
| 3582 | llvm::ConstantInt::get(LongTy, instanceSize), IvarList, MethodList, |
| 3583 | GenerateProtocolList(Protocols), IvarOffsetArray, Properties, |
| 3584 | StrongIvarBitmap, WeakIvarBitmap); |
Rafael Espindola | b735004 | 2018-03-01 00:35:47 +0000 | [diff] [blame] | 3585 | CGM.setGVProperties(cast<llvm::GlobalValue>(ClassStruct), |
| 3586 | OID->getClassInterface()); |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 3587 | |
| 3588 | // Resolve the class aliases, if they exist. |
| 3589 | if (ClassPtrAlias) { |
David Chisnall | 82f755c | 2010-11-09 11:21:43 +0000 | [diff] [blame] | 3590 | ClassPtrAlias->replaceAllUsesWith( |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3591 | llvm::ConstantExpr::getBitCast(ClassStruct, IdTy)); |
David Chisnall | 82f755c | 2010-11-09 11:21:43 +0000 | [diff] [blame] | 3592 | ClassPtrAlias->eraseFromParent(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3593 | ClassPtrAlias = nullptr; |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 3594 | } |
| 3595 | if (MetaClassPtrAlias) { |
David Chisnall | 82f755c | 2010-11-09 11:21:43 +0000 | [diff] [blame] | 3596 | MetaClassPtrAlias->replaceAllUsesWith( |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3597 | llvm::ConstantExpr::getBitCast(MetaClassStruct, IdTy)); |
David Chisnall | 82f755c | 2010-11-09 11:21:43 +0000 | [diff] [blame] | 3598 | MetaClassPtrAlias->eraseFromParent(); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3599 | MetaClassPtrAlias = nullptr; |
Daniel Dunbar | 566421c | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 3600 | } |
| 3601 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3602 | // Add class structure to list to be added to the symtab later |
Owen Anderson | ade90fd | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 3603 | ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3604 | Classes.push_back(ClassStruct); |
| 3605 | } |
| 3606 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3607 | llvm::Function *CGObjCGNU::ModuleInitFunction() { |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3608 | // Only emit an ObjC load function if no Objective-C stuff has been called |
| 3609 | if (Classes.empty() && Categories.empty() && ConstantStrings.empty() && |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3610 | ExistingProtocols.empty() && SelectorTable.empty()) |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3611 | return nullptr; |
Eli Friedman | 412c668 | 2008-06-01 16:00:02 +0000 | [diff] [blame] | 3612 | |
Fariborz Jahanian | 2cde203 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 3613 | // Add all referenced protocols to a category. |
| 3614 | GenerateProtocolHolderCategory(); |
| 3615 | |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3616 | llvm::StructType *selStructTy = |
| 3617 | dyn_cast<llvm::StructType>(SelectorTy->getElementType()); |
| 3618 | llvm::Type *selStructPtrTy = SelectorTy; |
| 3619 | if (!selStructTy) { |
| 3620 | selStructTy = llvm::StructType::get(CGM.getLLVMContext(), |
| 3621 | { PtrToInt8Ty, PtrToInt8Ty }); |
| 3622 | selStructPtrTy = llvm::PointerType::getUnqual(selStructTy); |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 3623 | } |
| 3624 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3625 | // Generate statics list: |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3626 | llvm::Constant *statics = NULLPtr; |
Alexander Kornienko | 6ee521c | 2015-01-23 15:36:10 +0000 | [diff] [blame] | 3627 | if (!ConstantStrings.empty()) { |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3628 | llvm::GlobalVariable *fileStatics = [&] { |
| 3629 | ConstantInitBuilder builder(CGM); |
| 3630 | auto staticsStruct = builder.beginStruct(); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 3631 | |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3632 | StringRef stringClass = CGM.getLangOpts().ObjCConstantStringClass; |
| 3633 | if (stringClass.empty()) stringClass = "NXConstantString"; |
| 3634 | staticsStruct.add(MakeConstantString(stringClass, |
| 3635 | ".objc_static_class_name")); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3636 | |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3637 | auto array = staticsStruct.beginArray(); |
| 3638 | array.addAll(ConstantStrings); |
| 3639 | array.add(NULLPtr); |
| 3640 | array.finishAndAddTo(staticsStruct); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3641 | |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3642 | return staticsStruct.finishAndCreateGlobal(".objc_statics", |
| 3643 | CGM.getPointerAlign()); |
| 3644 | }(); |
| 3645 | |
| 3646 | ConstantInitBuilder builder(CGM); |
| 3647 | auto allStaticsArray = builder.beginArray(fileStatics->getType()); |
| 3648 | allStaticsArray.add(fileStatics); |
| 3649 | allStaticsArray.addNullPointer(fileStatics->getType()); |
| 3650 | |
| 3651 | statics = allStaticsArray.finishAndCreateGlobal(".objc_statics_ptr", |
| 3652 | CGM.getPointerAlign()); |
| 3653 | statics = llvm::ConstantExpr::getBitCast(statics, PtrTy); |
Chris Lattner | c06ce0f | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 3654 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3655 | |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3656 | // Array of classes, categories, and constant objects. |
| 3657 | |
| 3658 | SmallVector<llvm::GlobalAlias*, 16> selectorAliases; |
| 3659 | unsigned selectorCount; |
| 3660 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3661 | // Pointer to an array of selectors used in this module. |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3662 | llvm::GlobalVariable *selectorList = [&] { |
| 3663 | ConstantInitBuilder builder(CGM); |
| 3664 | auto selectors = builder.beginArray(selStructTy); |
John McCall | f00e2c0 | 2016-11-30 20:46:55 +0000 | [diff] [blame] | 3665 | auto &table = SelectorTable; // MSVC workaround |
David Chisnall | c66d480 | 2018-08-14 10:05:25 +0000 | [diff] [blame] | 3666 | std::vector<Selector> allSelectors; |
| 3667 | for (auto &entry : table) |
| 3668 | allSelectors.push_back(entry.first); |
Fangrui Song | 55fab26 | 2018-09-26 22:16:28 +0000 | [diff] [blame] | 3669 | llvm::sort(allSelectors); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3670 | |
David Chisnall | c66d480 | 2018-08-14 10:05:25 +0000 | [diff] [blame] | 3671 | for (auto &untypedSel : allSelectors) { |
| 3672 | std::string selNameStr = untypedSel.getAsString(); |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3673 | llvm::Constant *selName = ExportUniqueString(selNameStr, ".objc_sel_name"); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3674 | |
David Chisnall | c66d480 | 2018-08-14 10:05:25 +0000 | [diff] [blame] | 3675 | for (TypedSelector &sel : table[untypedSel]) { |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3676 | llvm::Constant *selectorTypeEncoding = NULLPtr; |
| 3677 | if (!sel.first.empty()) |
| 3678 | selectorTypeEncoding = |
| 3679 | MakeConstantString(sel.first, ".objc_sel_types"); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3680 | |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3681 | auto selStruct = selectors.beginStruct(selStructTy); |
| 3682 | selStruct.add(selName); |
| 3683 | selStruct.add(selectorTypeEncoding); |
| 3684 | selStruct.finishAndAddTo(selectors); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3685 | |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3686 | // Store the selector alias for later replacement |
| 3687 | selectorAliases.push_back(sel.second); |
| 3688 | } |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3689 | } |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3690 | |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3691 | // Remember the number of entries in the selector table. |
| 3692 | selectorCount = selectors.size(); |
| 3693 | |
| 3694 | // NULL-terminate the selector list. This should not actually be required, |
| 3695 | // because the selector list has a length field. Unfortunately, the GCC |
| 3696 | // runtime decides to ignore the length field and expects a NULL terminator, |
| 3697 | // and GCC cooperates with this by always setting the length to 0. |
| 3698 | auto selStruct = selectors.beginStruct(selStructTy); |
| 3699 | selStruct.add(NULLPtr); |
| 3700 | selStruct.add(NULLPtr); |
| 3701 | selStruct.finishAndAddTo(selectors); |
| 3702 | |
| 3703 | return selectors.finishAndCreateGlobal(".objc_selector_list", |
| 3704 | CGM.getPointerAlign()); |
| 3705 | }(); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3706 | |
| 3707 | // Now that all of the static selectors exist, create pointers to them. |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3708 | for (unsigned i = 0; i < selectorCount; ++i) { |
| 3709 | llvm::Constant *idxs[] = { |
| 3710 | Zeros[0], |
| 3711 | llvm::ConstantInt::get(Int32Ty, i) |
| 3712 | }; |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3713 | // FIXME: We're generating redundant loads and stores here! |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3714 | llvm::Constant *selPtr = llvm::ConstantExpr::getGetElementPtr( |
| 3715 | selectorList->getValueType(), selectorList, idxs); |
Chris Lattner | 8d3f4a4 | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 3716 | // If selectors are defined as an opaque type, cast the pointer to this |
| 3717 | // type. |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3718 | selPtr = llvm::ConstantExpr::getBitCast(selPtr, SelectorTy); |
| 3719 | selectorAliases[i]->replaceAllUsesWith(selPtr); |
| 3720 | selectorAliases[i]->eraseFromParent(); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3721 | } |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3722 | |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3723 | llvm::GlobalVariable *symtab = [&] { |
| 3724 | ConstantInitBuilder builder(CGM); |
| 3725 | auto symtab = builder.beginStruct(); |
| 3726 | |
| 3727 | // Number of static selectors |
| 3728 | symtab.addInt(LongTy, selectorCount); |
| 3729 | |
| 3730 | symtab.addBitCast(selectorList, selStructPtrTy); |
| 3731 | |
| 3732 | // Number of classes defined. |
| 3733 | symtab.addInt(CGM.Int16Ty, Classes.size()); |
| 3734 | // Number of categories defined |
| 3735 | symtab.addInt(CGM.Int16Ty, Categories.size()); |
| 3736 | |
| 3737 | // Create an array of classes, then categories, then static object instances |
| 3738 | auto classList = symtab.beginArray(PtrToInt8Ty); |
| 3739 | classList.addAll(Classes); |
| 3740 | classList.addAll(Categories); |
| 3741 | // NULL-terminated list of static object instances (mainly constant strings) |
| 3742 | classList.add(statics); |
| 3743 | classList.add(NULLPtr); |
| 3744 | classList.finishAndAddTo(symtab); |
| 3745 | |
| 3746 | // Construct the symbol table. |
| 3747 | return symtab.finishAndCreateGlobal("", CGM.getPointerAlign()); |
| 3748 | }(); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3749 | |
| 3750 | // The symbol table is contained in a module which has some version-checking |
| 3751 | // constants |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3752 | llvm::Constant *module = [&] { |
| 3753 | llvm::Type *moduleEltTys[] = { |
| 3754 | LongTy, LongTy, PtrToInt8Ty, symtab->getType(), IntTy |
| 3755 | }; |
| 3756 | llvm::StructType *moduleTy = |
| 3757 | llvm::StructType::get(CGM.getLLVMContext(), |
| 3758 | makeArrayRef(moduleEltTys).drop_back(unsigned(RuntimeVersion < 10))); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3759 | |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3760 | ConstantInitBuilder builder(CGM); |
| 3761 | auto module = builder.beginStruct(moduleTy); |
| 3762 | // Runtime version, used for ABI compatibility checking. |
| 3763 | module.addInt(LongTy, RuntimeVersion); |
| 3764 | // sizeof(ModuleTy) |
| 3765 | module.addInt(LongTy, CGM.getDataLayout().getTypeStoreSize(moduleTy)); |
| 3766 | |
| 3767 | // The path to the source file where this module was declared |
| 3768 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 3769 | const FileEntry *mainFile = SM.getFileEntryForID(SM.getMainFileID()); |
| 3770 | std::string path = |
Mehdi Amini | 004b9c7 | 2016-10-10 22:52:47 +0000 | [diff] [blame] | 3771 | (Twine(mainFile->getDir()->getName()) + "/" + mainFile->getName()).str(); |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3772 | module.add(MakeConstantString(path, ".objc_source_file_name")); |
| 3773 | module.add(symtab); |
David Chisnall | 5c51177 | 2011-05-22 22:37:08 +0000 | [diff] [blame] | 3774 | |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3775 | if (RuntimeVersion >= 10) { |
| 3776 | switch (CGM.getLangOpts().getGC()) { |
David Chisnall | a918b88 | 2011-07-07 11:22:31 +0000 | [diff] [blame] | 3777 | case LangOptions::GCOnly: |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3778 | module.addInt(IntTy, 2); |
David Chisnall | 5c51177 | 2011-05-22 22:37:08 +0000 | [diff] [blame] | 3779 | break; |
David Chisnall | a918b88 | 2011-07-07 11:22:31 +0000 | [diff] [blame] | 3780 | case LangOptions::NonGC: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3781 | if (CGM.getLangOpts().ObjCAutoRefCount) |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3782 | module.addInt(IntTy, 1); |
David Chisnall | a918b88 | 2011-07-07 11:22:31 +0000 | [diff] [blame] | 3783 | else |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3784 | module.addInt(IntTy, 0); |
David Chisnall | a918b88 | 2011-07-07 11:22:31 +0000 | [diff] [blame] | 3785 | break; |
| 3786 | case LangOptions::HybridGC: |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3787 | module.addInt(IntTy, 1); |
David Chisnall | a918b88 | 2011-07-07 11:22:31 +0000 | [diff] [blame] | 3788 | break; |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3789 | } |
David Chisnall | a918b88 | 2011-07-07 11:22:31 +0000 | [diff] [blame] | 3790 | } |
David Chisnall | 5c51177 | 2011-05-22 22:37:08 +0000 | [diff] [blame] | 3791 | |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3792 | return module.finishAndCreateGlobal("", CGM.getPointerAlign()); |
| 3793 | }(); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3794 | |
| 3795 | // Create the load function calling the runtime entry point with the module |
| 3796 | // structure |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3797 | llvm::Function * LoadFunction = llvm::Function::Create( |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3798 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false), |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3799 | llvm::GlobalValue::InternalLinkage, ".objc_load_function", |
| 3800 | &TheModule); |
Owen Anderson | 41a7502 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3801 | llvm::BasicBlock *EntryBB = |
| 3802 | llvm::BasicBlock::Create(VMContext, "entry", LoadFunction); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3803 | CGBuilderTy Builder(CGM, VMContext); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3804 | Builder.SetInsertPoint(EntryBB); |
Fariborz Jahanian | 3b636c1 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 3805 | |
Benjamin Kramer | df1fb13 | 2011-05-28 14:26:31 +0000 | [diff] [blame] | 3806 | llvm::FunctionType *FT = |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3807 | llvm::FunctionType::get(Builder.getVoidTy(), module->getType(), true); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3808 | llvm::FunctionCallee Register = |
| 3809 | CGM.CreateRuntimeFunction(FT, "__objc_exec_class"); |
John McCall | ecee86f | 2016-11-30 20:19:46 +0000 | [diff] [blame] | 3810 | Builder.CreateCall(Register, module); |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 3811 | |
David Chisnall | af066bbb | 2012-02-01 19:16:56 +0000 | [diff] [blame] | 3812 | if (!ClassAliases.empty()) { |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 3813 | llvm::Type *ArgTypes[2] = {PtrTy, PtrToInt8Ty}; |
| 3814 | llvm::FunctionType *RegisterAliasTy = |
| 3815 | llvm::FunctionType::get(Builder.getVoidTy(), |
| 3816 | ArgTypes, false); |
| 3817 | llvm::Function *RegisterAlias = llvm::Function::Create( |
| 3818 | RegisterAliasTy, |
| 3819 | llvm::GlobalValue::ExternalWeakLinkage, "class_registerAlias_np", |
| 3820 | &TheModule); |
| 3821 | llvm::BasicBlock *AliasBB = |
| 3822 | llvm::BasicBlock::Create(VMContext, "alias", LoadFunction); |
| 3823 | llvm::BasicBlock *NoAliasBB = |
| 3824 | llvm::BasicBlock::Create(VMContext, "no_alias", LoadFunction); |
| 3825 | |
| 3826 | // Branch based on whether the runtime provided class_registerAlias_np() |
| 3827 | llvm::Value *HasRegisterAlias = Builder.CreateICmpNE(RegisterAlias, |
| 3828 | llvm::Constant::getNullValue(RegisterAlias->getType())); |
| 3829 | Builder.CreateCondBr(HasRegisterAlias, AliasBB, NoAliasBB); |
| 3830 | |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 3831 | // The true branch (has alias registration function): |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 3832 | Builder.SetInsertPoint(AliasBB); |
| 3833 | // Emit alias registration calls: |
| 3834 | for (std::vector<ClassAliasPair>::iterator iter = ClassAliases.begin(); |
| 3835 | iter != ClassAliases.end(); ++iter) { |
| 3836 | llvm::Constant *TheClass = |
Malcolm Parsons | f76f650 | 2016-11-02 10:39:27 +0000 | [diff] [blame] | 3837 | TheModule.getGlobalVariable("_OBJC_CLASS_" + iter->first, true); |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3838 | if (TheClass) { |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 3839 | TheClass = llvm::ConstantExpr::getBitCast(TheClass, PtrTy); |
David Blaikie | 43f9bb7 | 2015-05-18 22:14:03 +0000 | [diff] [blame] | 3840 | Builder.CreateCall(RegisterAlias, |
| 3841 | {TheClass, MakeConstantString(iter->second)}); |
David Chisnall | 92d436b | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 3842 | } |
| 3843 | } |
| 3844 | // Jump to end: |
| 3845 | Builder.CreateBr(NoAliasBB); |
| 3846 | |
| 3847 | // Missing alias registration function, just return from the function: |
| 3848 | Builder.SetInsertPoint(NoAliasBB); |
| 3849 | } |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3850 | Builder.CreateRetVoid(); |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 3851 | |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3852 | return LoadFunction; |
| 3853 | } |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 3854 | |
Fariborz Jahanian | 0196a1c | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 3855 | llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3856 | const ObjCContainerDecl *CD) { |
| 3857 | const ObjCCategoryImplDecl *OCD = |
Steve Naroff | 11b387f | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 3858 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext()); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3859 | StringRef CategoryName = OCD ? OCD->getName() : ""; |
| 3860 | StringRef ClassName = CD->getName(); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3861 | Selector MethodName = OMD->getSelector(); |
Douglas Gregor | ffca3a2 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 3862 | bool isClassMethod = !OMD->isInstanceMethod(); |
Daniel Dunbar | 9299250 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 3863 | |
Daniel Dunbar | bf8c24a | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 3864 | CodeGenTypes &Types = CGM.getTypes(); |
Chris Lattner | 2192fe5 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 3865 | llvm::FunctionType *MethodTy = |
John McCall | a729c62 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 3866 | Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD)); |
Anton Korobeynikov | 1200aca | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 3867 | std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName, |
| 3868 | MethodName, isClassMethod); |
| 3869 | |
Daniel Dunbar | aff9fca | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 3870 | llvm::Function *Method |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3871 | = llvm::Function::Create(MethodTy, |
| 3872 | llvm::GlobalValue::InternalLinkage, |
| 3873 | FunctionName, |
| 3874 | &TheModule); |
Chris Lattner | 4bd5596 | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 3875 | return Method; |
| 3876 | } |
| 3877 | |
Pierre Habouzit | d4e1ba3 | 2019-11-07 23:14:58 -0800 | [diff] [blame] | 3878 | void CGObjCGNU::GenerateDirectMethodPrologue(CodeGenFunction &CGF, |
| 3879 | llvm::Function *Fn, |
| 3880 | const ObjCMethodDecl *OMD, |
| 3881 | const ObjCContainerDecl *CD) { |
| 3882 | // GNU runtime doesn't support direct calls at this time |
| 3883 | } |
| 3884 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3885 | llvm::FunctionCallee CGObjCGNU::GetPropertyGetFunction() { |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3886 | return GetPropertyFn; |
Daniel Dunbar | a91c3e0 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 3887 | } |
| 3888 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3889 | llvm::FunctionCallee CGObjCGNU::GetPropertySetFunction() { |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3890 | return SetPropertyFn; |
Daniel Dunbar | a91c3e0 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 3891 | } |
| 3892 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3893 | llvm::FunctionCallee CGObjCGNU::GetOptimizedPropertySetFunction(bool atomic, |
| 3894 | bool copy) { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3895 | return nullptr; |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 3896 | } |
| 3897 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3898 | llvm::FunctionCallee CGObjCGNU::GetGetStructFunction() { |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3899 | return GetStructPropertyFn; |
David Chisnall | 168b80f | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 3900 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 3901 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3902 | llvm::FunctionCallee CGObjCGNU::GetSetStructFunction() { |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3903 | return SetStructPropertyFn; |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 3904 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 3905 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3906 | llvm::FunctionCallee CGObjCGNU::GetCppAtomicObjectGetFunction() { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3907 | return nullptr; |
David Chisnall | 0d75e06 | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 3908 | } |
Eugene Zelenko | 0a4f3f4 | 2016-02-10 19:11:58 +0000 | [diff] [blame] | 3909 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3910 | llvm::FunctionCallee CGObjCGNU::GetCppAtomicObjectSetFunction() { |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 3911 | return nullptr; |
Fariborz Jahanian | 1e1b549 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 3912 | } |
Fariborz Jahanian | 5a8c203 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 3913 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3914 | llvm::FunctionCallee CGObjCGNU::EnumerationMutationFunction() { |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3915 | return EnumerationMutationFn; |
Anders Carlsson | 3f35a26 | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 3916 | } |
| 3917 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3918 | void CGObjCGNU::EmitSynchronizedStmt(CodeGenFunction &CGF, |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3919 | const ObjCAtSynchronizedStmt &S) { |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 3920 | EmitAtSynchronizedStmt(CGF, S, SyncEnterFn, SyncExitFn); |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3921 | } |
Chris Lattner | b6e9eb6 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 3922 | |
David Chisnall | 3a509cd | 2009-12-24 02:26:34 +0000 | [diff] [blame] | 3923 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3924 | void CGObjCGNU::EmitTryStmt(CodeGenFunction &CGF, |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3925 | const ObjCAtTryStmt &S) { |
| 3926 | // Unlike the Apple non-fragile runtimes, which also uses |
| 3927 | // unwind-based zero cost exceptions, the GNU Objective C runtime's |
| 3928 | // EH support isn't a veneer over C++ EH. Instead, exception |
David Chisnall | 9a837be | 2012-11-07 16:50:40 +0000 | [diff] [blame] | 3929 | // objects are created by objc_exception_throw and destroyed by |
John McCall | bd30929 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 3930 | // the personality function; this avoids the need for bracketing |
| 3931 | // catch handlers with calls to __blah_begin_catch/__blah_end_catch |
| 3932 | // (or even _Unwind_DeleteException), but probably doesn't |
| 3933 | // interoperate very well with foreign exceptions. |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 3934 | // |
David Chisnall | e1d2584d | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 3935 | // In Objective-C++ mode, we actually emit something equivalent to the C++ |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3936 | // exception handler. |
David Chisnall | d3858d6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 3937 | EmitTryCatchStmt(CGF, S, EnterCatchFn, ExitCatchFn, ExceptionReThrowFn); |
Anders Carlsson | 1963b0c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 3938 | } |
| 3939 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3940 | void CGObjCGNU::EmitThrowStmt(CodeGenFunction &CGF, |
Fariborz Jahanian | 1eab052 | 2013-01-10 19:02:56 +0000 | [diff] [blame] | 3941 | const ObjCAtThrowStmt &S, |
| 3942 | bool ClearInsertionPoint) { |
Chris Lattner | b6e9eb6 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 3943 | llvm::Value *ExceptionAsObject; |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 3944 | bool isRethrow = false; |
Chris Lattner | b6e9eb6 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 3945 | |
Chris Lattner | b6e9eb6 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 3946 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
John McCall | 248512a | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 3947 | llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr); |
Fariborz Jahanian | 078cd52 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 3948 | ExceptionAsObject = Exception; |
Chris Lattner | b6e9eb6 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 3949 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3950 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Chris Lattner | b6e9eb6 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 3951 | "Unexpected rethrow outside @catch block."); |
| 3952 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 3953 | isRethrow = true; |
Chris Lattner | b6e9eb6 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 3954 | } |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 3955 | if (isRethrow && usesSEHExceptions) { |
| 3956 | // For SEH, ExceptionAsObject may be undef, because the catch handler is |
| 3957 | // not passed it for catchalls and so it is not visible to the catch |
| 3958 | // funclet. The real thrown object will still be live on the stack at this |
| 3959 | // point and will be rethrown. If we are explicitly rethrowing the object |
| 3960 | // that was passed into the `@catch` block, then this code path is not |
| 3961 | // reached and we will instead call `objc_exception_throw` with an explicit |
| 3962 | // argument. |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 3963 | llvm::CallBase *Throw = CGF.EmitRuntimeCallOrInvoke(ExceptionReThrowFn); |
| 3964 | Throw->setDoesNotReturn(); |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 3965 | } |
| 3966 | else { |
| 3967 | ExceptionAsObject = CGF.Builder.CreateBitCast(ExceptionAsObject, IdTy); |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 3968 | llvm::CallBase *Throw = |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 3969 | CGF.EmitRuntimeCallOrInvoke(ExceptionThrowFn, ExceptionAsObject); |
James Y Knight | 3933add | 2019-01-30 02:54:28 +0000 | [diff] [blame] | 3970 | Throw->setDoesNotReturn(); |
David Chisnall | 93ce018 | 2018-08-10 12:53:13 +0000 | [diff] [blame] | 3971 | } |
Eli Friedman | dc009da | 2012-08-10 21:26:17 +0000 | [diff] [blame] | 3972 | CGF.Builder.CreateUnreachable(); |
Fariborz Jahanian | 1eab052 | 2013-01-10 19:02:56 +0000 | [diff] [blame] | 3973 | if (ClearInsertionPoint) |
| 3974 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 1963b0c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 3975 | } |
| 3976 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3977 | llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3978 | Address AddrWeakObj) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3979 | CGBuilderTy &B = CGF.Builder; |
David Chisnall | fcb37e9 | 2011-05-30 12:00:26 +0000 | [diff] [blame] | 3980 | AddrWeakObj = EnforceType(B, AddrWeakObj, PtrToIdTy); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3981 | return B.CreateCall(WeakReadFn, AddrWeakObj.getPointer()); |
Fariborz Jahanian | f5125d1 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 3982 | } |
| 3983 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3984 | void CGObjCGNU::EmitObjCWeakAssign(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3985 | llvm::Value *src, Address dst) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3986 | CGBuilderTy &B = CGF.Builder; |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 3987 | src = EnforceType(B, src, IdTy); |
| 3988 | dst = EnforceType(B, dst, PtrToIdTy); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3989 | B.CreateCall(WeakAssignFn, {src, dst.getPointer()}); |
Fariborz Jahanian | 83f45b55 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 3990 | } |
| 3991 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 3992 | void CGObjCGNU::EmitObjCGlobalAssign(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3993 | llvm::Value *src, Address dst, |
Fariborz Jahanian | 217af24 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 3994 | bool threadlocal) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 3995 | CGBuilderTy &B = CGF.Builder; |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 3996 | src = EnforceType(B, src, IdTy); |
| 3997 | dst = EnforceType(B, dst, PtrToIdTy); |
David Blaikie | 43f9bb7 | 2015-05-18 22:14:03 +0000 | [diff] [blame] | 3998 | // FIXME. Add threadloca assign API |
| 3999 | assert(!threadlocal && "EmitObjCGlobalAssign - Threal Local API NYI"); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 4000 | B.CreateCall(GlobalAssignFn, {src, dst.getPointer()}); |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 4001 | } |
| 4002 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 4003 | void CGObjCGNU::EmitObjCIvarAssign(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4004 | llvm::Value *src, Address dst, |
Fariborz Jahanian | 7a95d72 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 4005 | llvm::Value *ivarOffset) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4006 | CGBuilderTy &B = CGF.Builder; |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 4007 | src = EnforceType(B, src, IdTy); |
David Chisnall | e4e5c0f | 2011-05-25 20:33:17 +0000 | [diff] [blame] | 4008 | dst = EnforceType(B, dst, IdTy); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 4009 | B.CreateCall(IvarAssignFn, {src, dst.getPointer(), ivarOffset}); |
Fariborz Jahanian | e881b53 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 4010 | } |
| 4011 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 4012 | void CGObjCGNU::EmitObjCStrongCastAssign(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4013 | llvm::Value *src, Address dst) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4014 | CGBuilderTy &B = CGF.Builder; |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 4015 | src = EnforceType(B, src, IdTy); |
| 4016 | dst = EnforceType(B, dst, PtrToIdTy); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 4017 | B.CreateCall(StrongCastAssignFn, {src, dst.getPointer()}); |
Fariborz Jahanian | d7db964 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 4018 | } |
| 4019 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 4020 | void CGObjCGNU::EmitGCMemmoveCollectable(CodeGenFunction &CGF, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4021 | Address DestPtr, |
| 4022 | Address SrcPtr, |
Fariborz Jahanian | 021510e | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 4023 | llvm::Value *Size) { |
John McCall | 882987f | 2013-02-28 19:01:20 +0000 | [diff] [blame] | 4024 | CGBuilderTy &B = CGF.Builder; |
David Chisnall | 7441d88 | 2011-05-28 14:23:43 +0000 | [diff] [blame] | 4025 | DestPtr = EnforceType(B, DestPtr, PtrTy); |
| 4026 | SrcPtr = EnforceType(B, SrcPtr, PtrTy); |
David Chisnall | 5bb4efd | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 4027 | |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 4028 | B.CreateCall(MemMoveFn, {DestPtr.getPointer(), SrcPtr.getPointer(), Size}); |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 4029 | } |
| 4030 | |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 4031 | llvm::GlobalVariable *CGObjCGNU::ObjCIvarOffsetVariable( |
| 4032 | const ObjCInterfaceDecl *ID, |
| 4033 | const ObjCIvarDecl *Ivar) { |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 4034 | const std::string Name = GetIVarOffsetVariableName(ID, Ivar); |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 4035 | // Emit the variable and initialize it with what we think the correct value |
| 4036 | // is. This allows code compiled with non-fragile ivars to work correctly |
| 4037 | // when linked against code which isn't (most of the time). |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 4038 | llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name); |
David Chisnall | 9e31036 | 2018-08-07 12:02:46 +0000 | [diff] [blame] | 4039 | if (!IvarOffsetPointer) |
| 4040 | IvarOffsetPointer = new llvm::GlobalVariable(TheModule, |
| 4041 | llvm::Type::getInt32PtrTy(VMContext), false, |
| 4042 | llvm::GlobalValue::ExternalLinkage, nullptr, Name); |
David Chisnall | 5778fce | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 4043 | return IvarOffsetPointer; |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 4044 | } |
| 4045 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 4046 | LValue CGObjCGNU::EmitObjCValueForIvar(CodeGenFunction &CGF, |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4047 | QualType ObjectTy, |
| 4048 | llvm::Value *BaseValue, |
| 4049 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 712bfa6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4050 | unsigned CVRQualifiers) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4051 | const ObjCInterfaceDecl *ID = |
Simon Pilgrim | 7e38f0c | 2019-10-07 16:42:25 +0000 | [diff] [blame] | 4052 | ObjectTy->castAs<ObjCObjectType>()->getInterface(); |
Daniel Dunbar | 9fd114d | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4053 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 4054 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 9f84b78 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 4055 | } |
Mike Stump | dd93a19 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 4056 | |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 4057 | static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context, |
| 4058 | const ObjCInterfaceDecl *OID, |
| 4059 | const ObjCIvarDecl *OIVD) { |
Jordy Rose | a91768e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 4060 | for (const ObjCIvarDecl *next = OID->all_declared_ivar_begin(); next; |
| 4061 | next = next->getNextIvar()) { |
| 4062 | if (OIVD == next) |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 4063 | return OID; |
| 4064 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4065 | |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 4066 | // Otherwise check in the super class. |
| 4067 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
| 4068 | return FindIvarInterface(Context, Super, OIVD); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4069 | |
Craig Topper | 8a13c41 | 2014-05-21 05:09:00 +0000 | [diff] [blame] | 4070 | return nullptr; |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 4071 | } |
Fariborz Jahanian | 9f84b78 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 4072 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 4073 | llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGenFunction &CGF, |
Daniel Dunbar | 722f424 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 4074 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4075 | const ObjCIvarDecl *Ivar) { |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 4076 | if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 4077 | Interface = FindIvarInterface(CGM.getContext(), Interface, Ivar); |
Saleem Abdulrasool | 7093e21 | 2016-07-17 22:27:44 +0000 | [diff] [blame] | 4078 | |
| 4079 | // The MSVC linker cannot have a single global defined as LinkOnceAnyLinkage |
| 4080 | // and ExternalLinkage, so create a reference to the ivar global and rely on |
| 4081 | // the definition being created as part of GenerateClass. |
| 4082 | if (RuntimeVersion < 10 || |
| 4083 | CGF.CGM.getTarget().getTriple().isKnownWindowsMSVCEnvironment()) |
David Chisnall | 1bfe6d3 | 2011-07-07 12:34:51 +0000 | [diff] [blame] | 4084 | return CGF.Builder.CreateZExtOrBitCast( |
Peter Collingbourne | b367c56 | 2016-11-28 22:30:21 +0000 | [diff] [blame] | 4085 | CGF.Builder.CreateAlignedLoad( |
| 4086 | Int32Ty, CGF.Builder.CreateAlignedLoad( |
| 4087 | ObjCIvarOffsetVariable(Interface, Ivar), |
| 4088 | CGF.getPointerAlign(), "ivar"), |
| 4089 | CharUnits::fromQuantity(4)), |
David Chisnall | 1bfe6d3 | 2011-07-07 12:34:51 +0000 | [diff] [blame] | 4090 | PtrDiffTy); |
| 4091 | std::string name = "__objc_ivar_offset_value_" + |
| 4092 | Interface->getNameAsString() +"." + Ivar->getNameAsString(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4093 | CharUnits Align = CGM.getIntAlign(); |
David Chisnall | 1bfe6d3 | 2011-07-07 12:34:51 +0000 | [diff] [blame] | 4094 | llvm::Value *Offset = TheModule.getGlobalVariable(name); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4095 | if (!Offset) { |
| 4096 | auto GV = new llvm::GlobalVariable(TheModule, IntTy, |
David Chisnall | 28dc7f9 | 2011-08-01 17:36:53 +0000 | [diff] [blame] | 4097 | false, llvm::GlobalValue::LinkOnceAnyLinkage, |
| 4098 | llvm::Constant::getNullValue(IntTy), name); |
Guillaume Chatelet | c79099e | 2019-10-03 13:00:29 +0000 | [diff] [blame] | 4099 | GV->setAlignment(Align.getAsAlign()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 4100 | Offset = GV; |
| 4101 | } |
| 4102 | Offset = CGF.Builder.CreateAlignedLoad(Offset, Align); |
David Chisnall | a79b469 | 2012-04-06 15:39:12 +0000 | [diff] [blame] | 4103 | if (Offset->getType() != PtrDiffTy) |
| 4104 | Offset = CGF.Builder.CreateZExtOrBitCast(Offset, PtrDiffTy); |
| 4105 | return Offset; |
Fariborz Jahanian | d20a03f | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 4106 | } |
Eli Friedman | 8cbca20 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 4107 | uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar); |
| 4108 | return llvm::ConstantInt::get(PtrDiffTy, Offset, /*isSigned*/true); |
Fariborz Jahanian | 21fc74c | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4109 | } |
| 4110 | |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 4111 | CGObjCRuntime * |
| 4112 | clang::CodeGen::CreateGNUObjCRuntime(CodeGenModule &CGM) { |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 4113 | auto Runtime = CGM.getLangOpts().ObjCRuntime; |
| 4114 | switch (Runtime.getKind()) { |
David Chisnall | b601c96 | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 4115 | case ObjCRuntime::GNUstep: |
David Chisnall | 404bbcb | 2018-05-22 10:13:06 +0000 | [diff] [blame] | 4116 | if (Runtime.getVersion() >= VersionTuple(2, 0)) |
| 4117 | return new CGObjCGNUstep2(CGM); |
David Chisnall | d7972f5 | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 4118 | return new CGObjCGNUstep(CGM); |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 4119 | |
David Chisnall | b601c96 | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 4120 | case ObjCRuntime::GCC: |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 4121 | return new CGObjCGCC(CGM); |
| 4122 | |
John McCall | 775086e | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 4123 | case ObjCRuntime::ObjFW: |
| 4124 | return new CGObjCObjFW(CGM); |
| 4125 | |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 4126 | case ObjCRuntime::FragileMacOSX: |
| 4127 | case ObjCRuntime::MacOSX: |
| 4128 | case ObjCRuntime::iOS: |
Tim Northover | 756447a | 2015-10-30 16:30:36 +0000 | [diff] [blame] | 4129 | case ObjCRuntime::WatchOS: |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 4130 | llvm_unreachable("these runtimes are not GNU runtimes"); |
| 4131 | } |
| 4132 | llvm_unreachable("bad runtime"); |
Chris Lattner | b7256cd | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 4133 | } |