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