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