blob: caafef84c3339dec45aead0657fc0a091431583c [file] [log] [blame]
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001//===------- CGObjCGNU.cpp - Emit LLVM Code from ASTs for a Module --------===//
Chris Lattnerb7256cd2008-03-01 08:50:34 +00002//
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 Lattner57540c52011-04-15 05:22:18 +000010// This provides Objective-C code generation targeting the GNU runtime. The
Anton Korobeynikov1200aca2008-06-01 14:13:53 +000011// 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 Lattnerb7256cd2008-03-01 08:50:34 +000014//
15//===----------------------------------------------------------------------===//
16
17#include "CGObjCRuntime.h"
John McCalled1ae862011-01-28 11:13:47 +000018#include "CGCleanup.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "CodeGenFunction.h"
20#include "CodeGenModule.h"
Chris Lattner87ab27d2008-06-26 04:19:03 +000021#include "clang/AST/ASTContext.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000022#include "clang/AST/Decl.h"
Daniel Dunbar89da6ad2008-08-13 00:59:25 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000025#include "clang/AST/StmtObjC.h"
David Chisnalld7972f52011-03-23 16:36:54 +000026#include "clang/Basic/FileManager.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000027#include "clang/Basic/SourceManager.h"
Chris Lattnerb7256cd2008-03-01 08:50:34 +000028#include "llvm/ADT/SmallVector.h"
Anton Korobeynikov1200aca2008-06-01 14:13:53 +000029#include "llvm/ADT/StringMap.h"
Chandler Carruthc80ceea2014-03-04 11:02:08 +000030#include "llvm/IR/CallSite.h"
Chandler Carruthffd55512013-01-02 11:45:17 +000031#include "llvm/IR/DataLayout.h"
32#include "llvm/IR/Intrinsics.h"
33#include "llvm/IR/LLVMContext.h"
34#include "llvm/IR/Module.h"
Daniel Dunbar92992502008-08-15 22:20:32 +000035#include "llvm/Support/Compiler.h"
Chris Lattner0e62c1c2011-07-23 10:55:15 +000036#include <cstdarg>
Chris Lattner8d3f4a42009-01-27 05:06:01 +000037
Chris Lattner87ab27d2008-06-26 04:19:03 +000038using namespace clang;
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000039using namespace CodeGen;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +000040
Chris Lattnerb7256cd2008-03-01 08:50:34 +000041namespace {
David Chisnall34d00052011-03-26 11:48:37 +000042/// Class that lazily initialises the runtime function. Avoids inserting the
43/// types and the function declaration into a module if they're not used, and
44/// avoids constructing the type more than once if it's used more than once.
David Chisnalld7972f52011-03-23 16:36:54 +000045class LazyRuntimeFunction {
46 CodeGenModule *CGM;
David Blaikiebf178d32015-05-19 21:31:34 +000047 llvm::FunctionType *FTy;
David Chisnalld7972f52011-03-23 16:36:54 +000048 const char *FunctionName;
David Chisnall3fe89562011-05-23 22:33:28 +000049 llvm::Constant *Function;
David Blaikie7d9e7922015-05-18 22:51:39 +000050
51public:
52 /// Constructor leaves this class uninitialized, because it is intended to
53 /// be used as a field in another class and not all of the types that are
54 /// used as arguments will necessarily be available at construction time.
55 LazyRuntimeFunction()
Craig Topper8a13c412014-05-21 05:09:00 +000056 : CGM(nullptr), FunctionName(nullptr), Function(nullptr) {}
David Chisnalld7972f52011-03-23 16:36:54 +000057
David Blaikie7d9e7922015-05-18 22:51:39 +000058 /// Initialises the lazy function with the name, return type, and the types
59 /// of the arguments.
60 LLVM_END_WITH_NULL
61 void init(CodeGenModule *Mod, const char *name, llvm::Type *RetTy, ...) {
62 CGM = Mod;
63 FunctionName = name;
64 Function = nullptr;
David Blaikiebf178d32015-05-19 21:31:34 +000065 std::vector<llvm::Type *> ArgTys;
David Blaikie7d9e7922015-05-18 22:51:39 +000066 va_list Args;
67 va_start(Args, RetTy);
68 while (llvm::Type *ArgTy = va_arg(Args, llvm::Type *))
69 ArgTys.push_back(ArgTy);
70 va_end(Args);
David Blaikiebf178d32015-05-19 21:31:34 +000071 FTy = llvm::FunctionType::get(RetTy, ArgTys, false);
David Blaikie7d9e7922015-05-18 22:51:39 +000072 }
David Blaikiebf178d32015-05-19 21:31:34 +000073
74 llvm::FunctionType *getType() { return FTy; }
75
David Blaikie7d9e7922015-05-18 22:51:39 +000076 /// Overloaded cast operator, allows the class to be implicitly cast to an
77 /// LLVM constant.
78 operator llvm::Constant *() {
79 if (!Function) {
80 if (!FunctionName)
81 return nullptr;
David Blaikie7d9e7922015-05-18 22:51:39 +000082 Function =
83 cast<llvm::Constant>(CGM->CreateRuntimeFunction(FTy, FunctionName));
David Blaikie7d9e7922015-05-18 22:51:39 +000084 }
85 return Function;
86 }
87 operator llvm::Function *() {
88 return cast<llvm::Function>((llvm::Constant *)*this);
89 }
David Chisnalld7972f52011-03-23 16:36:54 +000090};
91
92
David Chisnall34d00052011-03-26 11:48:37 +000093/// GNU Objective-C runtime code generation. This class implements the parts of
John McCall775086e2012-07-12 02:07:58 +000094/// Objective-C support that are specific to the GNU family of runtimes (GCC,
95/// GNUstep and ObjFW).
David Chisnalld7972f52011-03-23 16:36:54 +000096class CGObjCGNU : public CGObjCRuntime {
David Chisnall76803412011-03-23 22:52:06 +000097protected:
David Chisnall34d00052011-03-26 11:48:37 +000098 /// The LLVM module into which output is inserted
Chris Lattnerb7256cd2008-03-01 08:50:34 +000099 llvm::Module &TheModule;
David Chisnall34d00052011-03-26 11:48:37 +0000100 /// strut objc_super. Used for sending messages to super. This structure
101 /// contains the receiver (object) and the expected class.
Chris Lattner2192fe52011-07-18 04:24:23 +0000102 llvm::StructType *ObjCSuperTy;
David Chisnall34d00052011-03-26 11:48:37 +0000103 /// struct objc_super*. The type of the argument to the superclass message
104 /// lookup functions.
Chris Lattner2192fe52011-07-18 04:24:23 +0000105 llvm::PointerType *PtrToObjCSuperTy;
David Chisnall34d00052011-03-26 11:48:37 +0000106 /// LLVM type for selectors. Opaque pointer (i8*) unless a header declaring
107 /// SEL is included in a header somewhere, in which case it will be whatever
108 /// type is declared in that header, most likely {i8*, i8*}.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000109 llvm::PointerType *SelectorTy;
David Chisnall34d00052011-03-26 11:48:37 +0000110 /// LLVM i8 type. Cached here to avoid repeatedly getting it in all of the
111 /// places where it's used
Chris Lattner2192fe52011-07-18 04:24:23 +0000112 llvm::IntegerType *Int8Ty;
David Chisnall34d00052011-03-26 11:48:37 +0000113 /// Pointer to i8 - LLVM type of char*, for all of the places where the
114 /// runtime needs to deal with C strings.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000115 llvm::PointerType *PtrToInt8Ty;
David Chisnall34d00052011-03-26 11:48:37 +0000116 /// Instance Method Pointer type. This is a pointer to a function that takes,
117 /// at a minimum, an object and a selector, and is the generic type for
118 /// Objective-C methods. Due to differences between variadic / non-variadic
119 /// calling conventions, it must always be cast to the correct type before
120 /// actually being used.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000121 llvm::PointerType *IMPTy;
David Chisnall34d00052011-03-26 11:48:37 +0000122 /// Type of an untyped Objective-C object. Clang treats id as a built-in type
123 /// when compiling Objective-C code, so this may be an opaque pointer (i8*),
124 /// but if the runtime header declaring it is included then it may be a
125 /// pointer to a structure.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000126 llvm::PointerType *IdTy;
David Chisnall34d00052011-03-26 11:48:37 +0000127 /// Pointer to a pointer to an Objective-C object. Used in the new ABI
128 /// message lookup function and some GC-related functions.
Chris Lattner2192fe52011-07-18 04:24:23 +0000129 llvm::PointerType *PtrToIdTy;
David Chisnall34d00052011-03-26 11:48:37 +0000130 /// The clang type of id. Used when using the clang CGCall infrastructure to
131 /// call Objective-C methods.
John McCall2da83a32010-02-26 00:48:12 +0000132 CanQualType ASTIdTy;
David Chisnall34d00052011-03-26 11:48:37 +0000133 /// LLVM type for C int type.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000134 llvm::IntegerType *IntTy;
David Chisnall34d00052011-03-26 11:48:37 +0000135 /// LLVM type for an opaque pointer. This is identical to PtrToInt8Ty, but is
136 /// used in the code to document the difference between i8* meaning a pointer
137 /// to a C string and i8* meaning a pointer to some opaque type.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000138 llvm::PointerType *PtrTy;
David Chisnall34d00052011-03-26 11:48:37 +0000139 /// LLVM type for C long type. The runtime uses this in a lot of places where
140 /// it should be using intptr_t, but we can't fix this without breaking
141 /// compatibility with GCC...
Jay Foad7c57be32011-07-11 09:56:20 +0000142 llvm::IntegerType *LongTy;
David Chisnall34d00052011-03-26 11:48:37 +0000143 /// LLVM type for C size_t. Used in various runtime data structures.
Chris Lattner2192fe52011-07-18 04:24:23 +0000144 llvm::IntegerType *SizeTy;
David Chisnalle0dc7cb2011-10-08 08:54:36 +0000145 /// LLVM type for C intptr_t.
146 llvm::IntegerType *IntPtrTy;
David Chisnall34d00052011-03-26 11:48:37 +0000147 /// LLVM type for C ptrdiff_t. Mainly used in property accessor functions.
Chris Lattner2192fe52011-07-18 04:24:23 +0000148 llvm::IntegerType *PtrDiffTy;
David Chisnall34d00052011-03-26 11:48:37 +0000149 /// LLVM type for C int*. Used for GCC-ABI-compatible non-fragile instance
150 /// variables.
Chris Lattner2192fe52011-07-18 04:24:23 +0000151 llvm::PointerType *PtrToIntTy;
David Chisnall34d00052011-03-26 11:48:37 +0000152 /// LLVM type for Objective-C BOOL type.
Chris Lattner2192fe52011-07-18 04:24:23 +0000153 llvm::Type *BoolTy;
David Chisnallcdd207e2011-10-04 15:35:30 +0000154 /// 32-bit integer type, to save us needing to look it up every time it's used.
155 llvm::IntegerType *Int32Ty;
156 /// 64-bit integer type, to save us needing to look it up every time it's used.
157 llvm::IntegerType *Int64Ty;
David Chisnall34d00052011-03-26 11:48:37 +0000158 /// Metadata kind used to tie method lookups to message sends. The GNUstep
159 /// runtime provides some LLVM passes that can use this to do things like
160 /// automatic IMP caching and speculative inlining.
David Chisnall76803412011-03-23 22:52:06 +0000161 unsigned msgSendMDKind;
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000162
David Chisnall34d00052011-03-26 11:48:37 +0000163 /// Helper function that generates a constant string and returns a pointer to
164 /// the start of the string. The result of this function can be used anywhere
165 /// where the C code specifies const char*.
David Chisnalld3858d62011-03-25 11:57:33 +0000166 llvm::Constant *MakeConstantString(const std::string &Str,
167 const std::string &Name="") {
John McCall7f416cc2015-09-08 08:05:57 +0000168 ConstantAddress Array = CGM.GetAddrOfConstantCString(Str, Name.c_str());
169 return llvm::ConstantExpr::getGetElementPtr(Array.getElementType(),
170 Array.getPointer(), Zeros);
David Chisnalld3858d62011-03-25 11:57:33 +0000171 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000172
David Chisnall34d00052011-03-26 11:48:37 +0000173 /// Emits a linkonce_odr string, whose name is the prefix followed by the
174 /// string value. This allows the linker to combine the strings between
175 /// different modules. Used for EH typeinfo names, selector strings, and a
176 /// few other things.
David Chisnalld3858d62011-03-25 11:57:33 +0000177 llvm::Constant *ExportUniqueString(const std::string &Str,
178 const std::string prefix) {
179 std::string name = prefix + Str;
David Blaikiee3b172a2015-04-02 18:55:21 +0000180 auto *ConstStr = TheModule.getGlobalVariable(name);
David Chisnalld3858d62011-03-25 11:57:33 +0000181 if (!ConstStr) {
Chris Lattner9c818332012-02-05 02:30:40 +0000182 llvm::Constant *value = llvm::ConstantDataArray::getString(VMContext,Str);
David Chisnalld3858d62011-03-25 11:57:33 +0000183 ConstStr = new llvm::GlobalVariable(TheModule, value->getType(), true,
184 llvm::GlobalValue::LinkOnceODRLinkage, value, prefix + Str);
185 }
David Blaikiee3b172a2015-04-02 18:55:21 +0000186 return llvm::ConstantExpr::getGetElementPtr(ConstStr->getValueType(),
187 ConstStr, Zeros);
David Chisnalld3858d62011-03-25 11:57:33 +0000188 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000189
David Chisnall34d00052011-03-26 11:48:37 +0000190 /// Generates a global structure, initialized by the elements in the vector.
191 /// The element types must match the types of the structure elements in the
192 /// first argument.
Chris Lattner2192fe52011-07-18 04:24:23 +0000193 llvm::GlobalVariable *MakeGlobal(llvm::StructType *Ty,
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000194 ArrayRef<llvm::Constant *> V,
John McCall7f416cc2015-09-08 08:05:57 +0000195 CharUnits Align,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000196 StringRef Name="",
David Chisnalld3858d62011-03-25 11:57:33 +0000197 llvm::GlobalValue::LinkageTypes linkage
198 =llvm::GlobalValue::InternalLinkage) {
199 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
John McCall7f416cc2015-09-08 08:05:57 +0000200 auto GV = new llvm::GlobalVariable(TheModule, Ty, false,
201 linkage, C, Name);
202 GV->setAlignment(Align.getQuantity());
203 return GV;
David Chisnalld3858d62011-03-25 11:57:33 +0000204 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000205
David Chisnall34d00052011-03-26 11:48:37 +0000206 /// Generates a global array. The vector must contain the same number of
207 /// elements that the array type declares, of the type specified as the array
208 /// element type.
Chris Lattner2192fe52011-07-18 04:24:23 +0000209 llvm::GlobalVariable *MakeGlobal(llvm::ArrayType *Ty,
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000210 ArrayRef<llvm::Constant *> V,
John McCall7f416cc2015-09-08 08:05:57 +0000211 CharUnits Align,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000212 StringRef Name="",
David Chisnalld3858d62011-03-25 11:57:33 +0000213 llvm::GlobalValue::LinkageTypes linkage
214 =llvm::GlobalValue::InternalLinkage) {
215 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
John McCall7f416cc2015-09-08 08:05:57 +0000216 auto GV = new llvm::GlobalVariable(TheModule, Ty, false,
217 linkage, C, Name);
218 GV->setAlignment(Align.getQuantity());
219 return GV;
David Chisnalld3858d62011-03-25 11:57:33 +0000220 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000221
David Chisnall34d00052011-03-26 11:48:37 +0000222 /// Generates a global array, inferring the array type from the specified
223 /// element type and the size of the initialiser.
Chris Lattner2192fe52011-07-18 04:24:23 +0000224 llvm::GlobalVariable *MakeGlobalArray(llvm::Type *Ty,
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000225 ArrayRef<llvm::Constant *> V,
John McCall7f416cc2015-09-08 08:05:57 +0000226 CharUnits Align,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000227 StringRef Name="",
David Chisnalld3858d62011-03-25 11:57:33 +0000228 llvm::GlobalValue::LinkageTypes linkage
229 =llvm::GlobalValue::InternalLinkage) {
230 llvm::ArrayType *ArrayTy = llvm::ArrayType::get(Ty, V.size());
John McCall7f416cc2015-09-08 08:05:57 +0000231 return MakeGlobal(ArrayTy, V, Align, Name, linkage);
David Chisnalld3858d62011-03-25 11:57:33 +0000232 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000233
David Chisnalla5f59412012-10-16 15:11:55 +0000234 /// Returns a property name and encoding string.
235 llvm::Constant *MakePropertyEncodingString(const ObjCPropertyDecl *PD,
236 const Decl *Container) {
David Chisnallbeb80132013-02-28 13:59:29 +0000237 const ObjCRuntime &R = CGM.getLangOpts().ObjCRuntime;
David Chisnalla5f59412012-10-16 15:11:55 +0000238 if ((R.getKind() == ObjCRuntime::GNUstep) &&
239 (R.getVersion() >= VersionTuple(1, 6))) {
240 std::string NameAndAttributes;
241 std::string TypeStr;
242 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
243 NameAndAttributes += '\0';
244 NameAndAttributes += TypeStr.length() + 3;
245 NameAndAttributes += TypeStr;
246 NameAndAttributes += '\0';
247 NameAndAttributes += PD->getNameAsString();
John McCall7f416cc2015-09-08 08:05:57 +0000248 return MakeConstantString(NameAndAttributes);
David Chisnalla5f59412012-10-16 15:11:55 +0000249 }
250 return MakeConstantString(PD->getNameAsString());
251 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000252
David Chisnallbeb80132013-02-28 13:59:29 +0000253 /// Push the property attributes into two structure fields.
254 void PushPropertyAttributes(std::vector<llvm::Constant*> &Fields,
255 ObjCPropertyDecl *property, bool isSynthesized=true, bool
256 isDynamic=true) {
257 int attrs = property->getPropertyAttributes();
258 // For read-only properties, clear the copy and retain flags
259 if (attrs & ObjCPropertyDecl::OBJC_PR_readonly) {
260 attrs &= ~ObjCPropertyDecl::OBJC_PR_copy;
261 attrs &= ~ObjCPropertyDecl::OBJC_PR_retain;
262 attrs &= ~ObjCPropertyDecl::OBJC_PR_weak;
263 attrs &= ~ObjCPropertyDecl::OBJC_PR_strong;
264 }
265 // The first flags field has the same attribute values as clang uses internally
266 Fields.push_back(llvm::ConstantInt::get(Int8Ty, attrs & 0xff));
267 attrs >>= 8;
268 attrs <<= 2;
269 // For protocol properties, synthesized and dynamic have no meaning, so we
270 // reuse these flags to indicate that this is a protocol property (both set
271 // has no meaning, as a property can't be both synthesized and dynamic)
272 attrs |= isSynthesized ? (1<<0) : 0;
273 attrs |= isDynamic ? (1<<1) : 0;
274 // The second field is the next four fields left shifted by two, with the
275 // low bit set to indicate whether the field is synthesized or dynamic.
276 Fields.push_back(llvm::ConstantInt::get(Int8Ty, attrs & 0xff));
277 // Two padding fields
278 Fields.push_back(llvm::ConstantInt::get(Int8Ty, 0));
279 Fields.push_back(llvm::ConstantInt::get(Int8Ty, 0));
280 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000281
David Chisnall34d00052011-03-26 11:48:37 +0000282 /// Ensures that the value has the required type, by inserting a bitcast if
283 /// required. This function lets us avoid inserting bitcasts that are
284 /// redundant.
John McCall882987f2013-02-28 19:01:20 +0000285 llvm::Value* EnforceType(CGBuilderTy &B, llvm::Value *V, llvm::Type *Ty) {
David Chisnall76803412011-03-23 22:52:06 +0000286 if (V->getType() == Ty) return V;
287 return B.CreateBitCast(V, Ty);
288 }
John McCall7f416cc2015-09-08 08:05:57 +0000289 Address EnforceType(CGBuilderTy &B, Address V, llvm::Type *Ty) {
290 if (V.getType() == Ty) return V;
291 return B.CreateBitCast(V, Ty);
292 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000293
David Chisnall76803412011-03-23 22:52:06 +0000294 // Some zeros used for GEPs in lots of places.
295 llvm::Constant *Zeros[2];
David Chisnall34d00052011-03-26 11:48:37 +0000296 /// Null pointer value. Mainly used as a terminator in various arrays.
David Chisnall76803412011-03-23 22:52:06 +0000297 llvm::Constant *NULLPtr;
David Chisnall34d00052011-03-26 11:48:37 +0000298 /// LLVM context.
David Chisnall76803412011-03-23 22:52:06 +0000299 llvm::LLVMContext &VMContext;
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000300
David Chisnall76803412011-03-23 22:52:06 +0000301private:
David Chisnall34d00052011-03-26 11:48:37 +0000302 /// Placeholder for the class. Lots of things refer to the class before we've
303 /// actually emitted it. We use this alias as a placeholder, and then replace
304 /// it with a pointer to the class structure before finally emitting the
305 /// module.
Daniel Dunbar566421c2009-05-04 15:31:17 +0000306 llvm::GlobalAlias *ClassPtrAlias;
David Chisnall34d00052011-03-26 11:48:37 +0000307 /// Placeholder for the metaclass. Lots of things refer to the class before
308 /// we've / actually emitted it. We use this alias as a placeholder, and then
309 /// replace / it with a pointer to the metaclass structure before finally
310 /// emitting the / module.
Daniel Dunbar566421c2009-05-04 15:31:17 +0000311 llvm::GlobalAlias *MetaClassPtrAlias;
David Chisnall34d00052011-03-26 11:48:37 +0000312 /// All of the classes that have been generated for this compilation units.
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000313 std::vector<llvm::Constant*> Classes;
David Chisnall34d00052011-03-26 11:48:37 +0000314 /// All of the categories that have been generated for this compilation units.
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000315 std::vector<llvm::Constant*> Categories;
David Chisnall34d00052011-03-26 11:48:37 +0000316 /// All of the Objective-C constant strings that have been generated for this
317 /// compilation units.
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000318 std::vector<llvm::Constant*> ConstantStrings;
David Chisnall34d00052011-03-26 11:48:37 +0000319 /// Map from string values to Objective-C constant strings in the output.
320 /// Used to prevent emitting Objective-C strings more than once. This should
321 /// not be required at all - CodeGenModule should manage this list.
David Chisnall358e7512010-01-27 12:49:23 +0000322 llvm::StringMap<llvm::Constant*> ObjCStrings;
David Chisnall34d00052011-03-26 11:48:37 +0000323 /// All of the protocols that have been declared.
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000324 llvm::StringMap<llvm::Constant*> ExistingProtocols;
David Chisnall34d00052011-03-26 11:48:37 +0000325 /// For each variant of a selector, we store the type encoding and a
326 /// placeholder value. For an untyped selector, the type will be the empty
327 /// string. Selector references are all done via the module's selector table,
328 /// so we create an alias as a placeholder and then replace it with the real
329 /// value later.
David Chisnalld7972f52011-03-23 16:36:54 +0000330 typedef std::pair<std::string, llvm::GlobalAlias*> TypedSelector;
David Chisnall34d00052011-03-26 11:48:37 +0000331 /// Type of the selector map. This is roughly equivalent to the structure
332 /// used in the GNUstep runtime, which maintains a list of all of the valid
333 /// types for a selector in a table.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000334 typedef llvm::DenseMap<Selector, SmallVector<TypedSelector, 2> >
David Chisnalld7972f52011-03-23 16:36:54 +0000335 SelectorMap;
David Chisnall34d00052011-03-26 11:48:37 +0000336 /// A map from selectors to selector types. This allows us to emit all
337 /// selectors of the same name and type together.
David Chisnalld7972f52011-03-23 16:36:54 +0000338 SelectorMap SelectorTable;
339
David Chisnall34d00052011-03-26 11:48:37 +0000340 /// Selectors related to memory management. When compiling in GC mode, we
341 /// omit these.
David Chisnall5bb4efd2010-02-03 15:59:02 +0000342 Selector RetainSel, ReleaseSel, AutoreleaseSel;
David Chisnall34d00052011-03-26 11:48:37 +0000343 /// Runtime functions used for memory management in GC mode. Note that clang
344 /// supports code generation for calling these functions, but neither GNU
345 /// runtime actually supports this API properly yet.
David Chisnalld7972f52011-03-23 16:36:54 +0000346 LazyRuntimeFunction IvarAssignFn, StrongCastAssignFn, MemMoveFn, WeakReadFn,
347 WeakAssignFn, GlobalAssignFn;
David Chisnalld7972f52011-03-23 16:36:54 +0000348
David Chisnall92d436b2012-01-31 18:59:20 +0000349 typedef std::pair<std::string, std::string> ClassAliasPair;
350 /// All classes that have aliases set for them.
351 std::vector<ClassAliasPair> ClassAliases;
352
David Chisnalld3858d62011-03-25 11:57:33 +0000353protected:
David Chisnall34d00052011-03-26 11:48:37 +0000354 /// Function used for throwing Objective-C exceptions.
David Chisnalld7972f52011-03-23 16:36:54 +0000355 LazyRuntimeFunction ExceptionThrowFn;
James Dennettb9199ee2012-06-13 22:07:09 +0000356 /// Function used for rethrowing exceptions, used at the end of \@finally or
357 /// \@synchronize blocks.
David Chisnalld3858d62011-03-25 11:57:33 +0000358 LazyRuntimeFunction ExceptionReThrowFn;
David Chisnall34d00052011-03-26 11:48:37 +0000359 /// Function called when entering a catch function. This is required for
360 /// differentiating Objective-C exceptions and foreign exceptions.
David Chisnalld3858d62011-03-25 11:57:33 +0000361 LazyRuntimeFunction EnterCatchFn;
David Chisnall34d00052011-03-26 11:48:37 +0000362 /// Function called when exiting from a catch block. Used to do exception
363 /// cleanup.
David Chisnalld3858d62011-03-25 11:57:33 +0000364 LazyRuntimeFunction ExitCatchFn;
James Dennettb9199ee2012-06-13 22:07:09 +0000365 /// Function called when entering an \@synchronize block. Acquires the lock.
David Chisnalld7972f52011-03-23 16:36:54 +0000366 LazyRuntimeFunction SyncEnterFn;
James Dennettb9199ee2012-06-13 22:07:09 +0000367 /// Function called when exiting an \@synchronize block. Releases the lock.
David Chisnalld7972f52011-03-23 16:36:54 +0000368 LazyRuntimeFunction SyncExitFn;
369
David Chisnalld3858d62011-03-25 11:57:33 +0000370private:
David Chisnall34d00052011-03-26 11:48:37 +0000371 /// Function called if fast enumeration detects that the collection is
372 /// modified during the update.
David Chisnalld7972f52011-03-23 16:36:54 +0000373 LazyRuntimeFunction EnumerationMutationFn;
David Chisnall34d00052011-03-26 11:48:37 +0000374 /// Function for implementing synthesized property getters that return an
375 /// object.
David Chisnalld7972f52011-03-23 16:36:54 +0000376 LazyRuntimeFunction GetPropertyFn;
David Chisnall34d00052011-03-26 11:48:37 +0000377 /// Function for implementing synthesized property setters that return an
378 /// object.
David Chisnalld7972f52011-03-23 16:36:54 +0000379 LazyRuntimeFunction SetPropertyFn;
David Chisnall34d00052011-03-26 11:48:37 +0000380 /// Function used for non-object declared property getters.
David Chisnalld7972f52011-03-23 16:36:54 +0000381 LazyRuntimeFunction GetStructPropertyFn;
David Chisnall34d00052011-03-26 11:48:37 +0000382 /// Function used for non-object declared property setters.
David Chisnalld7972f52011-03-23 16:36:54 +0000383 LazyRuntimeFunction SetStructPropertyFn;
384
David Chisnall34d00052011-03-26 11:48:37 +0000385 /// The version of the runtime that this class targets. Must match the
386 /// version in the runtime.
David Chisnall5c511772011-05-22 22:37:08 +0000387 int RuntimeVersion;
David Chisnall34d00052011-03-26 11:48:37 +0000388 /// The version of the protocol class. Used to differentiate between ObjC1
389 /// and ObjC2 protocols. Objective-C 1 protocols can not contain optional
390 /// components and can not contain declared properties. We always emit
391 /// Objective-C 2 property structures, but we have to pretend that they're
392 /// Objective-C 1 property structures when targeting the GCC runtime or it
393 /// will abort.
David Chisnalld7972f52011-03-23 16:36:54 +0000394 const int ProtocolVersion;
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000395
David Chisnall34d00052011-03-26 11:48:37 +0000396 /// Generates an instance variable list structure. This is a structure
397 /// containing a size and an array of structures containing instance variable
398 /// metadata. This is used purely for introspection in the fragile ABI. In
399 /// the non-fragile ABI, it's used for instance variable fixup.
Bill Wendlingf1a3fca2012-02-22 09:30:11 +0000400 llvm::Constant *GenerateIvarList(ArrayRef<llvm::Constant *> IvarNames,
401 ArrayRef<llvm::Constant *> IvarTypes,
402 ArrayRef<llvm::Constant *> IvarOffsets);
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000403
David Chisnall34d00052011-03-26 11:48:37 +0000404 /// Generates a method list structure. This is a structure containing a size
405 /// and an array of structures containing method metadata.
406 ///
407 /// This structure is used by both classes and categories, and contains a next
408 /// pointer allowing them to be chained together in a linked list.
Craig Topperbf3e3272014-08-30 16:55:52 +0000409 llvm::Constant *GenerateMethodList(StringRef ClassName,
410 StringRef CategoryName,
Bill Wendlingf1a3fca2012-02-22 09:30:11 +0000411 ArrayRef<Selector> MethodSels,
412 ArrayRef<llvm::Constant *> MethodTypes,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000413 bool isClassMethodList);
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000414
James Dennettb9199ee2012-06-13 22:07:09 +0000415 /// Emits an empty protocol. This is used for \@protocol() where no protocol
David Chisnall34d00052011-03-26 11:48:37 +0000416 /// is found. The runtime will (hopefully) fix up the pointer to refer to the
417 /// real protocol.
Fariborz Jahanian89d23972009-03-31 18:27:22 +0000418 llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName);
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000419
David Chisnall34d00052011-03-26 11:48:37 +0000420 /// Generates a list of property metadata structures. This follows the same
421 /// pattern as method and instance variable metadata lists.
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000422 llvm::Constant *GeneratePropertyList(const ObjCImplementationDecl *OID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000423 SmallVectorImpl<Selector> &InstanceMethodSels,
424 SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes);
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000425
David Chisnall34d00052011-03-26 11:48:37 +0000426 /// Generates a list of referenced protocols. Classes, categories, and
427 /// protocols all use this structure.
Bill Wendlingf1a3fca2012-02-22 09:30:11 +0000428 llvm::Constant *GenerateProtocolList(ArrayRef<std::string> Protocols);
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000429
David Chisnall34d00052011-03-26 11:48:37 +0000430 /// To ensure that all protocols are seen by the runtime, we add a category on
431 /// a class defined in the runtime, declaring no methods, but adopting the
432 /// protocols. This is a horribly ugly hack, but it allows us to collect all
433 /// of the protocols without changing the ABI.
Dmitri Gribenkob2aa9232012-11-15 14:28:07 +0000434 void GenerateProtocolHolderCategory();
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000435
David Chisnall34d00052011-03-26 11:48:37 +0000436 /// Generates a class structure.
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000437 llvm::Constant *GenerateClassStructure(
438 llvm::Constant *MetaClass,
439 llvm::Constant *SuperClass,
440 unsigned info,
Chris Lattnerda35bc82008-06-26 04:47:04 +0000441 const char *Name,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000442 llvm::Constant *Version,
443 llvm::Constant *InstanceSize,
444 llvm::Constant *IVars,
445 llvm::Constant *Methods,
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000446 llvm::Constant *Protocols,
447 llvm::Constant *IvarOffsets,
David Chisnalld472c852010-04-28 14:29:56 +0000448 llvm::Constant *Properties,
David Chisnallcdd207e2011-10-04 15:35:30 +0000449 llvm::Constant *StrongIvarBitmap,
450 llvm::Constant *WeakIvarBitmap,
David Chisnalld472c852010-04-28 14:29:56 +0000451 bool isMeta=false);
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000452
David Chisnall34d00052011-03-26 11:48:37 +0000453 /// Generates a method list. This is used by protocols to define the required
454 /// and optional methods.
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000455 llvm::Constant *GenerateProtocolMethodList(
Bill Wendlingf1a3fca2012-02-22 09:30:11 +0000456 ArrayRef<llvm::Constant *> MethodNames,
457 ArrayRef<llvm::Constant *> MethodTypes);
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000458
David Chisnall34d00052011-03-26 11:48:37 +0000459 /// Returns a selector with the specified type encoding. An empty string is
460 /// used to return an untyped selector (with the types field set to NULL).
John McCall882987f2013-02-28 19:01:20 +0000461 llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel,
John McCall7f416cc2015-09-08 08:05:57 +0000462 const std::string &TypeEncoding);
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000463
David Chisnall34d00052011-03-26 11:48:37 +0000464 /// Returns the variable used to store the offset of an instance variable.
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +0000465 llvm::GlobalVariable *ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
466 const ObjCIvarDecl *Ivar);
David Chisnall34d00052011-03-26 11:48:37 +0000467 /// Emits a reference to a class. This allows the linker to object if there
468 /// is no class of the matching name.
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000469
John McCall775086e2012-07-12 02:07:58 +0000470protected:
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000471 void EmitClassRef(const std::string &className);
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000472
David Chisnall920e83b2011-06-29 13:16:41 +0000473 /// Emits a pointer to the named class
John McCall882987f2013-02-28 19:01:20 +0000474 virtual llvm::Value *GetClassNamed(CodeGenFunction &CGF,
John McCall775086e2012-07-12 02:07:58 +0000475 const std::string &Name, bool isWeak);
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000476
David Chisnall34d00052011-03-26 11:48:37 +0000477 /// Looks up the method for sending a message to the specified object. This
478 /// mechanism differs between the GCC and GNU runtimes, so this method must be
479 /// overridden in subclasses.
David Chisnall76803412011-03-23 22:52:06 +0000480 virtual llvm::Value *LookupIMP(CodeGenFunction &CGF,
481 llvm::Value *&Receiver,
482 llvm::Value *cmd,
Eli Friedmanf24bd3b2013-07-26 00:53:29 +0000483 llvm::MDNode *node,
484 MessageSendInfo &MSI) = 0;
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000485
David Chisnallcdd207e2011-10-04 15:35:30 +0000486 /// Looks up the method for sending a message to a superclass. This
487 /// mechanism differs between the GCC and GNU runtimes, so this method must
488 /// be overridden in subclasses.
David Chisnall76803412011-03-23 22:52:06 +0000489 virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +0000490 Address ObjCSuper,
Eli Friedmanf24bd3b2013-07-26 00:53:29 +0000491 llvm::Value *cmd,
492 MessageSendInfo &MSI) = 0;
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000493
David Chisnallcdd207e2011-10-04 15:35:30 +0000494 /// Libobjc2 uses a bitfield representation where small(ish) bitfields are
495 /// stored in a 64-bit value with the low bit set to 1 and the remaining 63
496 /// bits set to their values, LSB first, while larger ones are stored in a
497 /// structure of this / form:
498 ///
499 /// struct { int32_t length; int32_t values[length]; };
500 ///
501 /// The values in the array are stored in host-endian format, with the least
502 /// significant bit being assumed to come first in the bitfield. Therefore,
503 /// a bitfield with the 64th bit set will be (int64_t)&{ 2, [0, 1<<31] },
504 /// while a bitfield / with the 63rd bit set will be 1<<64.
Bill Wendlingf1a3fca2012-02-22 09:30:11 +0000505 llvm::Constant *MakeBitField(ArrayRef<bool> bits);
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000506
Chris Lattnerb7256cd2008-03-01 08:50:34 +0000507public:
David Chisnalld7972f52011-03-23 16:36:54 +0000508 CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion,
509 unsigned protocolClassVersion);
510
John McCall7f416cc2015-09-08 08:05:57 +0000511 ConstantAddress GenerateConstantString(const StringLiteral *) override;
David Chisnalld7972f52011-03-23 16:36:54 +0000512
Craig Topper4f12f102014-03-12 06:41:41 +0000513 RValue
514 GenerateMessageSend(CodeGenFunction &CGF, ReturnValueSlot Return,
515 QualType ResultType, Selector Sel,
516 llvm::Value *Receiver, const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +0000517 const ObjCInterfaceDecl *Class,
Craig Topper4f12f102014-03-12 06:41:41 +0000518 const ObjCMethodDecl *Method) override;
519 RValue
520 GenerateMessageSendSuper(CodeGenFunction &CGF, ReturnValueSlot Return,
521 QualType ResultType, Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000522 const ObjCInterfaceDecl *Class,
Craig Topper4f12f102014-03-12 06:41:41 +0000523 bool isCategoryImpl, llvm::Value *Receiver,
524 bool IsClassMessage, const CallArgList &CallArgs,
525 const ObjCMethodDecl *Method) override;
526 llvm::Value *GetClass(CodeGenFunction &CGF,
527 const ObjCInterfaceDecl *OID) override;
John McCall7f416cc2015-09-08 08:05:57 +0000528 llvm::Value *GetSelector(CodeGenFunction &CGF, Selector Sel) override;
529 Address GetAddrOfSelector(CodeGenFunction &CGF, Selector Sel) override;
Craig Topper4f12f102014-03-12 06:41:41 +0000530 llvm::Value *GetSelector(CodeGenFunction &CGF,
531 const ObjCMethodDecl *Method) override;
532 llvm::Constant *GetEHType(QualType T) override;
Mike Stump11289f42009-09-09 15:08:12 +0000533
Craig Topper4f12f102014-03-12 06:41:41 +0000534 llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
535 const ObjCContainerDecl *CD) override;
536 void GenerateCategory(const ObjCCategoryImplDecl *CMD) override;
537 void GenerateClass(const ObjCImplementationDecl *ClassDecl) override;
538 void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) override;
539 llvm::Value *GenerateProtocolRef(CodeGenFunction &CGF,
540 const ObjCProtocolDecl *PD) override;
541 void GenerateProtocol(const ObjCProtocolDecl *PD) override;
542 llvm::Function *ModuleInitFunction() override;
543 llvm::Constant *GetPropertyGetFunction() override;
544 llvm::Constant *GetPropertySetFunction() override;
545 llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
546 bool copy) override;
547 llvm::Constant *GetSetStructFunction() override;
548 llvm::Constant *GetGetStructFunction() override;
549 llvm::Constant *GetCppAtomicObjectGetFunction() override;
550 llvm::Constant *GetCppAtomicObjectSetFunction() override;
551 llvm::Constant *EnumerationMutationFunction() override;
Mike Stump11289f42009-09-09 15:08:12 +0000552
Craig Topper4f12f102014-03-12 06:41:41 +0000553 void EmitTryStmt(CodeGenFunction &CGF,
554 const ObjCAtTryStmt &S) override;
555 void EmitSynchronizedStmt(CodeGenFunction &CGF,
556 const ObjCAtSynchronizedStmt &S) override;
557 void EmitThrowStmt(CodeGenFunction &CGF,
558 const ObjCAtThrowStmt &S,
559 bool ClearInsertionPoint=true) override;
560 llvm::Value * EmitObjCWeakRead(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +0000561 Address AddrWeakObj) override;
Craig Topper4f12f102014-03-12 06:41:41 +0000562 void EmitObjCWeakAssign(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +0000563 llvm::Value *src, Address dst) override;
Craig Topper4f12f102014-03-12 06:41:41 +0000564 void EmitObjCGlobalAssign(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +0000565 llvm::Value *src, Address dest,
Craig Topper4f12f102014-03-12 06:41:41 +0000566 bool threadlocal=false) override;
567 void EmitObjCIvarAssign(CodeGenFunction &CGF, llvm::Value *src,
John McCall7f416cc2015-09-08 08:05:57 +0000568 Address dest, llvm::Value *ivarOffset) override;
Craig Topper4f12f102014-03-12 06:41:41 +0000569 void EmitObjCStrongCastAssign(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +0000570 llvm::Value *src, Address dest) override;
571 void EmitGCMemmoveCollectable(CodeGenFunction &CGF, Address DestPtr,
572 Address SrcPtr,
Craig Topper4f12f102014-03-12 06:41:41 +0000573 llvm::Value *Size) override;
574 LValue EmitObjCValueForIvar(CodeGenFunction &CGF, QualType ObjectTy,
575 llvm::Value *BaseValue, const ObjCIvarDecl *Ivar,
576 unsigned CVRQualifiers) override;
577 llvm::Value *EmitIvarOffset(CodeGenFunction &CGF,
578 const ObjCInterfaceDecl *Interface,
579 const ObjCIvarDecl *Ivar) override;
580 llvm::Value *EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) override;
581 llvm::Constant *BuildGCBlockLayout(CodeGenModule &CGM,
582 const CGBlockInfo &blockInfo) override {
Fariborz Jahanianc05349e2010-08-04 16:57:49 +0000583 return NULLPtr;
584 }
Craig Topper4f12f102014-03-12 06:41:41 +0000585 llvm::Constant *BuildRCBlockLayout(CodeGenModule &CGM,
586 const CGBlockInfo &blockInfo) override {
Fariborz Jahanian0c58ce92012-10-27 21:10:38 +0000587 return NULLPtr;
588 }
Craig Topper4f12f102014-03-12 06:41:41 +0000589
590 llvm::Constant *BuildByrefLayout(CodeGenModule &CGM, QualType T) override {
Fariborz Jahaniana9d44642012-11-14 17:15:51 +0000591 return NULLPtr;
592 }
Rafael Espindola554256c2014-02-26 22:25:45 +0000593
Benjamin Kramer0772c422016-02-13 13:42:54 +0000594 llvm::GlobalVariable *GetClassGlobal(StringRef Name,
Craig Toppera798a9d2014-03-02 09:32:10 +0000595 bool Weak = false) override {
Craig Topper8a13c412014-05-21 05:09:00 +0000596 return nullptr;
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +0000597 }
Chris Lattnerb7256cd2008-03-01 08:50:34 +0000598};
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000599
David Chisnall34d00052011-03-26 11:48:37 +0000600/// Class representing the legacy GCC Objective-C ABI. This is the default when
601/// -fobjc-nonfragile-abi is not specified.
602///
603/// The GCC ABI target actually generates code that is approximately compatible
604/// with the new GNUstep runtime ABI, but refrains from using any features that
605/// would not work with the GCC runtime. For example, clang always generates
606/// the extended form of the class structure, and the extra fields are simply
607/// ignored by GCC libobjc.
David Chisnalld7972f52011-03-23 16:36:54 +0000608class CGObjCGCC : public CGObjCGNU {
David Chisnall34d00052011-03-26 11:48:37 +0000609 /// The GCC ABI message lookup function. Returns an IMP pointing to the
610 /// method implementation for this message.
David Chisnall76803412011-03-23 22:52:06 +0000611 LazyRuntimeFunction MsgLookupFn;
David Chisnall34d00052011-03-26 11:48:37 +0000612 /// The GCC ABI superclass message lookup function. Takes a pointer to a
613 /// structure describing the receiver and the class, and a selector as
614 /// arguments. Returns the IMP for the corresponding method.
David Chisnall76803412011-03-23 22:52:06 +0000615 LazyRuntimeFunction MsgLookupSuperFn;
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000616
David Chisnall76803412011-03-23 22:52:06 +0000617protected:
Craig Topper4f12f102014-03-12 06:41:41 +0000618 llvm::Value *LookupIMP(CodeGenFunction &CGF, llvm::Value *&Receiver,
619 llvm::Value *cmd, llvm::MDNode *node,
620 MessageSendInfo &MSI) override {
David Chisnall76803412011-03-23 22:52:06 +0000621 CGBuilderTy &Builder = CGF.Builder;
David Chisnall0cc83e72011-10-28 17:55:06 +0000622 llvm::Value *args[] = {
David Chisnall76803412011-03-23 22:52:06 +0000623 EnforceType(Builder, Receiver, IdTy),
David Chisnall0cc83e72011-10-28 17:55:06 +0000624 EnforceType(Builder, cmd, SelectorTy) };
John McCall882987f2013-02-28 19:01:20 +0000625 llvm::CallSite imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFn, args);
David Chisnall0cc83e72011-10-28 17:55:06 +0000626 imp->setMetadata(msgSendMDKind, node);
627 return imp.getInstruction();
David Chisnall76803412011-03-23 22:52:06 +0000628 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000629
John McCall7f416cc2015-09-08 08:05:57 +0000630 llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, Address ObjCSuper,
Craig Topper4f12f102014-03-12 06:41:41 +0000631 llvm::Value *cmd, MessageSendInfo &MSI) override {
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000632 CGBuilderTy &Builder = CGF.Builder;
633 llvm::Value *lookupArgs[] = {EnforceType(Builder, ObjCSuper,
634 PtrToObjCSuperTy).getPointer(), cmd};
635 return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFn, lookupArgs);
636 }
637
638public:
639 CGObjCGCC(CodeGenModule &Mod) : CGObjCGNU(Mod, 8, 2) {
640 // IMP objc_msg_lookup(id, SEL);
641 MsgLookupFn.init(&CGM, "objc_msg_lookup", IMPTy, IdTy, SelectorTy,
642 nullptr);
643 // IMP objc_msg_lookup_super(struct objc_super*, SEL);
644 MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy,
645 PtrToObjCSuperTy, SelectorTy, nullptr);
646 }
David Chisnalld7972f52011-03-23 16:36:54 +0000647};
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000648
David Chisnall34d00052011-03-26 11:48:37 +0000649/// Class used when targeting the new GNUstep runtime ABI.
David Chisnalld7972f52011-03-23 16:36:54 +0000650class CGObjCGNUstep : public CGObjCGNU {
David Chisnall34d00052011-03-26 11:48:37 +0000651 /// The slot lookup function. Returns a pointer to a cacheable structure
652 /// that contains (among other things) the IMP.
David Chisnall76803412011-03-23 22:52:06 +0000653 LazyRuntimeFunction SlotLookupFn;
David Chisnall34d00052011-03-26 11:48:37 +0000654 /// The GNUstep ABI superclass message lookup function. Takes a pointer to
655 /// a structure describing the receiver and the class, and a selector as
656 /// arguments. Returns the slot for the corresponding method. Superclass
657 /// message lookup rarely changes, so this is a good caching opportunity.
David Chisnall76803412011-03-23 22:52:06 +0000658 LazyRuntimeFunction SlotLookupSuperFn;
David Chisnall0d75e062012-12-17 18:54:24 +0000659 /// Specialised function for setting atomic retain properties
660 LazyRuntimeFunction SetPropertyAtomic;
661 /// Specialised function for setting atomic copy properties
662 LazyRuntimeFunction SetPropertyAtomicCopy;
663 /// Specialised function for setting nonatomic retain properties
664 LazyRuntimeFunction SetPropertyNonAtomic;
665 /// Specialised function for setting nonatomic copy properties
666 LazyRuntimeFunction SetPropertyNonAtomicCopy;
667 /// Function to perform atomic copies of C++ objects with nontrivial copy
668 /// constructors from Objective-C ivars.
669 LazyRuntimeFunction CxxAtomicObjectGetFn;
670 /// Function to perform atomic copies of C++ objects with nontrivial copy
671 /// constructors to Objective-C ivars.
672 LazyRuntimeFunction CxxAtomicObjectSetFn;
David Chisnall34d00052011-03-26 11:48:37 +0000673 /// Type of an slot structure pointer. This is returned by the various
674 /// lookup functions.
David Chisnall76803412011-03-23 22:52:06 +0000675 llvm::Type *SlotTy;
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000676
John McCallc31d8932012-11-14 09:08:34 +0000677 public:
Craig Topper4f12f102014-03-12 06:41:41 +0000678 llvm::Constant *GetEHType(QualType T) override;
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000679
David Chisnall76803412011-03-23 22:52:06 +0000680 protected:
Craig Topper4f12f102014-03-12 06:41:41 +0000681 llvm::Value *LookupIMP(CodeGenFunction &CGF, llvm::Value *&Receiver,
682 llvm::Value *cmd, llvm::MDNode *node,
683 MessageSendInfo &MSI) override {
David Chisnall76803412011-03-23 22:52:06 +0000684 CGBuilderTy &Builder = CGF.Builder;
685 llvm::Function *LookupFn = SlotLookupFn;
686
687 // Store the receiver on the stack so that we can reload it later
John McCall7f416cc2015-09-08 08:05:57 +0000688 Address ReceiverPtr =
689 CGF.CreateTempAlloca(Receiver->getType(), CGF.getPointerAlign());
David Chisnall76803412011-03-23 22:52:06 +0000690 Builder.CreateStore(Receiver, ReceiverPtr);
691
692 llvm::Value *self;
693
694 if (isa<ObjCMethodDecl>(CGF.CurCodeDecl)) {
695 self = CGF.LoadObjCSelf();
696 } else {
697 self = llvm::ConstantPointerNull::get(IdTy);
698 }
699
700 // The lookup function is guaranteed not to capture the receiver pointer.
701 LookupFn->setDoesNotCapture(1);
702
David Chisnall0cc83e72011-10-28 17:55:06 +0000703 llvm::Value *args[] = {
John McCall7f416cc2015-09-08 08:05:57 +0000704 EnforceType(Builder, ReceiverPtr.getPointer(), PtrToIdTy),
David Chisnall76803412011-03-23 22:52:06 +0000705 EnforceType(Builder, cmd, SelectorTy),
David Chisnall0cc83e72011-10-28 17:55:06 +0000706 EnforceType(Builder, self, IdTy) };
John McCall882987f2013-02-28 19:01:20 +0000707 llvm::CallSite slot = CGF.EmitRuntimeCallOrInvoke(LookupFn, args);
David Chisnall0cc83e72011-10-28 17:55:06 +0000708 slot.setOnlyReadsMemory();
David Chisnall76803412011-03-23 22:52:06 +0000709 slot->setMetadata(msgSendMDKind, node);
710
711 // Load the imp from the slot
John McCall7f416cc2015-09-08 08:05:57 +0000712 llvm::Value *imp = Builder.CreateAlignedLoad(
713 Builder.CreateStructGEP(nullptr, slot.getInstruction(), 4),
714 CGF.getPointerAlign());
David Chisnall76803412011-03-23 22:52:06 +0000715
716 // The lookup function may have changed the receiver, so make sure we use
717 // the new one.
718 Receiver = Builder.CreateLoad(ReceiverPtr, true);
719 return imp;
720 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000721
John McCall7f416cc2015-09-08 08:05:57 +0000722 llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, Address ObjCSuper,
Craig Topper4f12f102014-03-12 06:41:41 +0000723 llvm::Value *cmd,
724 MessageSendInfo &MSI) override {
David Chisnall76803412011-03-23 22:52:06 +0000725 CGBuilderTy &Builder = CGF.Builder;
John McCall7f416cc2015-09-08 08:05:57 +0000726 llvm::Value *lookupArgs[] = {ObjCSuper.getPointer(), cmd};
David Chisnall76803412011-03-23 22:52:06 +0000727
John McCall882987f2013-02-28 19:01:20 +0000728 llvm::CallInst *slot =
729 CGF.EmitNounwindRuntimeCall(SlotLookupSuperFn, lookupArgs);
David Chisnall76803412011-03-23 22:52:06 +0000730 slot->setOnlyReadsMemory();
731
John McCall7f416cc2015-09-08 08:05:57 +0000732 return Builder.CreateAlignedLoad(Builder.CreateStructGEP(nullptr, slot, 4),
733 CGF.getPointerAlign());
David Chisnall76803412011-03-23 22:52:06 +0000734 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000735
David Chisnalld7972f52011-03-23 16:36:54 +0000736 public:
David Chisnall76803412011-03-23 22:52:06 +0000737 CGObjCGNUstep(CodeGenModule &Mod) : CGObjCGNU(Mod, 9, 3) {
David Chisnallbeb80132013-02-28 13:59:29 +0000738 const ObjCRuntime &R = CGM.getLangOpts().ObjCRuntime;
David Chisnall2ec1b10d2013-01-11 15:33:01 +0000739
Chris Lattner845511f2011-06-18 22:49:11 +0000740 llvm::StructType *SlotStructTy = llvm::StructType::get(PtrTy,
Craig Topper8a13c412014-05-21 05:09:00 +0000741 PtrTy, PtrTy, IntTy, IMPTy, nullptr);
David Chisnall76803412011-03-23 22:52:06 +0000742 SlotTy = llvm::PointerType::getUnqual(SlotStructTy);
743 // Slot_t objc_msg_lookup_sender(id *receiver, SEL selector, id sender);
744 SlotLookupFn.init(&CGM, "objc_msg_lookup_sender", SlotTy, PtrToIdTy,
Craig Topper8a13c412014-05-21 05:09:00 +0000745 SelectorTy, IdTy, nullptr);
David Chisnall76803412011-03-23 22:52:06 +0000746 // Slot_t objc_msg_lookup_super(struct objc_super*, SEL);
747 SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
Craig Topper8a13c412014-05-21 05:09:00 +0000748 PtrToObjCSuperTy, SelectorTy, nullptr);
David Chisnalld3858d62011-03-25 11:57:33 +0000749 // If we're in ObjC++ mode, then we want to make
David Blaikiebbafb8a2012-03-11 07:00:24 +0000750 if (CGM.getLangOpts().CPlusPlus) {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000751 llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
David Chisnalld3858d62011-03-25 11:57:33 +0000752 // void *__cxa_begin_catch(void *e)
Craig Topper8a13c412014-05-21 05:09:00 +0000753 EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy, nullptr);
David Chisnalld3858d62011-03-25 11:57:33 +0000754 // void __cxa_end_catch(void)
Craig Topper8a13c412014-05-21 05:09:00 +0000755 ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy, nullptr);
David Chisnalld3858d62011-03-25 11:57:33 +0000756 // void _Unwind_Resume_or_Rethrow(void*)
David Chisnall0d75e062012-12-17 18:54:24 +0000757 ExceptionReThrowFn.init(&CGM, "_Unwind_Resume_or_Rethrow", VoidTy,
Craig Topper8a13c412014-05-21 05:09:00 +0000758 PtrTy, nullptr);
David Chisnall2ec1b10d2013-01-11 15:33:01 +0000759 } else if (R.getVersion() >= VersionTuple(1, 7)) {
760 llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
761 // id objc_begin_catch(void *e)
Craig Topper8a13c412014-05-21 05:09:00 +0000762 EnterCatchFn.init(&CGM, "objc_begin_catch", IdTy, PtrTy, nullptr);
David Chisnall2ec1b10d2013-01-11 15:33:01 +0000763 // void objc_end_catch(void)
Craig Topper8a13c412014-05-21 05:09:00 +0000764 ExitCatchFn.init(&CGM, "objc_end_catch", VoidTy, nullptr);
David Chisnall2ec1b10d2013-01-11 15:33:01 +0000765 // void _Unwind_Resume_or_Rethrow(void*)
766 ExceptionReThrowFn.init(&CGM, "objc_exception_rethrow", VoidTy,
Craig Topper8a13c412014-05-21 05:09:00 +0000767 PtrTy, nullptr);
David Chisnalld3858d62011-03-25 11:57:33 +0000768 }
David Chisnall0d75e062012-12-17 18:54:24 +0000769 llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
770 SetPropertyAtomic.init(&CGM, "objc_setProperty_atomic", VoidTy, IdTy,
Craig Topper8a13c412014-05-21 05:09:00 +0000771 SelectorTy, IdTy, PtrDiffTy, nullptr);
David Chisnall0d75e062012-12-17 18:54:24 +0000772 SetPropertyAtomicCopy.init(&CGM, "objc_setProperty_atomic_copy", VoidTy,
Craig Topper8a13c412014-05-21 05:09:00 +0000773 IdTy, SelectorTy, IdTy, PtrDiffTy, nullptr);
David Chisnall0d75e062012-12-17 18:54:24 +0000774 SetPropertyNonAtomic.init(&CGM, "objc_setProperty_nonatomic", VoidTy,
Craig Topper8a13c412014-05-21 05:09:00 +0000775 IdTy, SelectorTy, IdTy, PtrDiffTy, nullptr);
David Chisnall0d75e062012-12-17 18:54:24 +0000776 SetPropertyNonAtomicCopy.init(&CGM, "objc_setProperty_nonatomic_copy",
Craig Topper8a13c412014-05-21 05:09:00 +0000777 VoidTy, IdTy, SelectorTy, IdTy, PtrDiffTy, nullptr);
David Chisnall0d75e062012-12-17 18:54:24 +0000778 // void objc_setCppObjectAtomic(void *dest, const void *src, void
779 // *helper);
780 CxxAtomicObjectSetFn.init(&CGM, "objc_setCppObjectAtomic", VoidTy, PtrTy,
Craig Topper8a13c412014-05-21 05:09:00 +0000781 PtrTy, PtrTy, nullptr);
David Chisnall0d75e062012-12-17 18:54:24 +0000782 // void objc_getCppObjectAtomic(void *dest, const void *src, void
783 // *helper);
784 CxxAtomicObjectGetFn.init(&CGM, "objc_getCppObjectAtomic", VoidTy, PtrTy,
Craig Topper8a13c412014-05-21 05:09:00 +0000785 PtrTy, PtrTy, nullptr);
David Chisnall0d75e062012-12-17 18:54:24 +0000786 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000787
Craig Topper4f12f102014-03-12 06:41:41 +0000788 llvm::Constant *GetCppAtomicObjectGetFunction() override {
David Chisnall0d75e062012-12-17 18:54:24 +0000789 // The optimised functions were added in version 1.7 of the GNUstep
790 // runtime.
791 assert (CGM.getLangOpts().ObjCRuntime.getVersion() >=
792 VersionTuple(1, 7));
793 return CxxAtomicObjectGetFn;
794 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000795
Craig Topper4f12f102014-03-12 06:41:41 +0000796 llvm::Constant *GetCppAtomicObjectSetFunction() override {
David Chisnall0d75e062012-12-17 18:54:24 +0000797 // The optimised functions were added in version 1.7 of the GNUstep
798 // runtime.
799 assert (CGM.getLangOpts().ObjCRuntime.getVersion() >=
800 VersionTuple(1, 7));
801 return CxxAtomicObjectSetFn;
802 }
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +0000803
Craig Topper4f12f102014-03-12 06:41:41 +0000804 llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
805 bool copy) override {
David Chisnall0d75e062012-12-17 18:54:24 +0000806 // The optimised property functions omit the GC check, and so are not
807 // safe to use in GC mode. The standard functions are fast in GC mode,
808 // so there is less advantage in using them.
809 assert ((CGM.getLangOpts().getGC() == LangOptions::NonGC));
810 // The optimised functions were added in version 1.7 of the GNUstep
811 // runtime.
812 assert (CGM.getLangOpts().ObjCRuntime.getVersion() >=
813 VersionTuple(1, 7));
814
815 if (atomic) {
816 if (copy) return SetPropertyAtomicCopy;
817 return SetPropertyAtomic;
818 }
David Chisnall0d75e062012-12-17 18:54:24 +0000819
Ted Kremenek090a2732014-03-07 18:53:05 +0000820 return copy ? SetPropertyNonAtomicCopy : SetPropertyNonAtomic;
David Chisnall76803412011-03-23 22:52:06 +0000821 }
David Chisnalld7972f52011-03-23 16:36:54 +0000822};
823
Alp Toker272e9bc2013-11-25 00:40:53 +0000824/// Support for the ObjFW runtime.
John McCall3deb1ad2012-08-21 02:47:43 +0000825class CGObjCObjFW: public CGObjCGNU {
826protected:
827 /// The GCC ABI message lookup function. Returns an IMP pointing to the
828 /// method implementation for this message.
829 LazyRuntimeFunction MsgLookupFn;
Eli Friedmanf24bd3b2013-07-26 00:53:29 +0000830 /// stret lookup function. While this does not seem to make sense at the
831 /// first look, this is required to call the correct forwarding function.
832 LazyRuntimeFunction MsgLookupFnSRet;
John McCall3deb1ad2012-08-21 02:47:43 +0000833 /// The GCC ABI superclass message lookup function. Takes a pointer to a
834 /// structure describing the receiver and the class, and a selector as
835 /// arguments. Returns the IMP for the corresponding method.
Eli Friedmanf24bd3b2013-07-26 00:53:29 +0000836 LazyRuntimeFunction MsgLookupSuperFn, MsgLookupSuperFnSRet;
John McCall3deb1ad2012-08-21 02:47:43 +0000837
Craig Topper4f12f102014-03-12 06:41:41 +0000838 llvm::Value *LookupIMP(CodeGenFunction &CGF, llvm::Value *&Receiver,
839 llvm::Value *cmd, llvm::MDNode *node,
840 MessageSendInfo &MSI) override {
John McCall3deb1ad2012-08-21 02:47:43 +0000841 CGBuilderTy &Builder = CGF.Builder;
842 llvm::Value *args[] = {
843 EnforceType(Builder, Receiver, IdTy),
844 EnforceType(Builder, cmd, SelectorTy) };
Eli Friedmanf24bd3b2013-07-26 00:53:29 +0000845
846 llvm::CallSite imp;
847 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo))
848 imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFnSRet, args);
849 else
850 imp = CGF.EmitRuntimeCallOrInvoke(MsgLookupFn, args);
851
John McCall3deb1ad2012-08-21 02:47:43 +0000852 imp->setMetadata(msgSendMDKind, node);
853 return imp.getInstruction();
854 }
855
John McCall7f416cc2015-09-08 08:05:57 +0000856 llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, Address ObjCSuper,
Craig Topper4f12f102014-03-12 06:41:41 +0000857 llvm::Value *cmd, MessageSendInfo &MSI) override {
Saleem Abdulrasoola088ad92016-07-17 22:27:41 +0000858 CGBuilderTy &Builder = CGF.Builder;
859 llvm::Value *lookupArgs[] = {
860 EnforceType(Builder, ObjCSuper.getPointer(), PtrToObjCSuperTy), cmd,
861 };
Eli Friedmanf24bd3b2013-07-26 00:53:29 +0000862
Saleem Abdulrasoola088ad92016-07-17 22:27:41 +0000863 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo))
864 return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFnSRet, lookupArgs);
865 else
866 return CGF.EmitNounwindRuntimeCall(MsgLookupSuperFn, lookupArgs);
867 }
John McCall3deb1ad2012-08-21 02:47:43 +0000868
Saleem Abdulrasoola088ad92016-07-17 22:27:41 +0000869 llvm::Value *GetClassNamed(CodeGenFunction &CGF, const std::string &Name,
870 bool isWeak) override {
John McCall775086e2012-07-12 02:07:58 +0000871 if (isWeak)
John McCall882987f2013-02-28 19:01:20 +0000872 return CGObjCGNU::GetClassNamed(CGF, Name, isWeak);
John McCall775086e2012-07-12 02:07:58 +0000873
874 EmitClassRef(Name);
John McCall775086e2012-07-12 02:07:58 +0000875 std::string SymbolName = "_OBJC_CLASS_" + Name;
John McCall775086e2012-07-12 02:07:58 +0000876 llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(SymbolName);
John McCall775086e2012-07-12 02:07:58 +0000877 if (!ClassSymbol)
878 ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false,
879 llvm::GlobalValue::ExternalLinkage,
Craig Topper8a13c412014-05-21 05:09:00 +0000880 nullptr, SymbolName);
John McCall775086e2012-07-12 02:07:58 +0000881 return ClassSymbol;
882 }
883
884public:
John McCall3deb1ad2012-08-21 02:47:43 +0000885 CGObjCObjFW(CodeGenModule &Mod): CGObjCGNU(Mod, 9, 3) {
886 // IMP objc_msg_lookup(id, SEL);
Craig Topper8a13c412014-05-21 05:09:00 +0000887 MsgLookupFn.init(&CGM, "objc_msg_lookup", IMPTy, IdTy, SelectorTy, nullptr);
Eli Friedmanf24bd3b2013-07-26 00:53:29 +0000888 MsgLookupFnSRet.init(&CGM, "objc_msg_lookup_stret", IMPTy, IdTy,
Craig Topper8a13c412014-05-21 05:09:00 +0000889 SelectorTy, nullptr);
John McCall3deb1ad2012-08-21 02:47:43 +0000890 // IMP objc_msg_lookup_super(struct objc_super*, SEL);
891 MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy,
Craig Topper8a13c412014-05-21 05:09:00 +0000892 PtrToObjCSuperTy, SelectorTy, nullptr);
Eli Friedmanf24bd3b2013-07-26 00:53:29 +0000893 MsgLookupSuperFnSRet.init(&CGM, "objc_msg_lookup_super_stret", IMPTy,
Craig Topper8a13c412014-05-21 05:09:00 +0000894 PtrToObjCSuperTy, SelectorTy, nullptr);
John McCall3deb1ad2012-08-21 02:47:43 +0000895 }
John McCall775086e2012-07-12 02:07:58 +0000896};
Chris Lattnerb7256cd2008-03-01 08:50:34 +0000897} // end anonymous namespace
898
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000899/// Emits a reference to a dummy variable which is emitted with each class.
900/// This ensures that a linker error will be generated when trying to link
901/// together modules where a referenced class is not defined.
Mike Stumpdd93a192009-07-31 21:31:32 +0000902void CGObjCGNU::EmitClassRef(const std::string &className) {
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000903 std::string symbolRef = "__objc_class_ref_" + className;
904 // Don't emit two copies of the same symbol
Mike Stumpdd93a192009-07-31 21:31:32 +0000905 if (TheModule.getGlobalVariable(symbolRef))
906 return;
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000907 std::string symbolName = "__objc_class_name_" + className;
908 llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(symbolName);
909 if (!ClassSymbol) {
Owen Andersonc10c8d32009-07-08 19:05:04 +0000910 ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false,
Craig Topper8a13c412014-05-21 05:09:00 +0000911 llvm::GlobalValue::ExternalLinkage,
912 nullptr, symbolName);
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000913 }
Owen Andersonc10c8d32009-07-08 19:05:04 +0000914 new llvm::GlobalVariable(TheModule, ClassSymbol->getType(), true,
Chris Lattnerc58e5692009-08-05 05:25:18 +0000915 llvm::GlobalValue::WeakAnyLinkage, ClassSymbol, symbolRef);
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +0000916}
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000917
Craig Topperbf3e3272014-08-30 16:55:52 +0000918static std::string SymbolNameForMethod( StringRef ClassName,
919 StringRef CategoryName, const Selector MethodName,
David Chisnalld7972f52011-03-23 16:36:54 +0000920 bool isClassMethod) {
921 std::string MethodNameColonStripped = MethodName.getAsString();
David Chisnall035ead22010-01-14 14:08:19 +0000922 std::replace(MethodNameColonStripped.begin(), MethodNameColonStripped.end(),
923 ':', '_');
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000924 return (Twine(isClassMethod ? "_c_" : "_i_") + ClassName + "_" +
David Chisnalld7972f52011-03-23 16:36:54 +0000925 CategoryName + "_" + MethodNameColonStripped).str();
David Chisnall0a24fd32010-05-08 20:58:05 +0000926}
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000927
David Chisnalld7972f52011-03-23 16:36:54 +0000928CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion,
Craig Topper8a13c412014-05-21 05:09:00 +0000929 unsigned protocolClassVersion)
John McCalla729c622012-02-17 03:33:10 +0000930 : CGObjCRuntime(cgm), TheModule(CGM.getModule()),
Craig Topper8a13c412014-05-21 05:09:00 +0000931 VMContext(cgm.getLLVMContext()), ClassPtrAlias(nullptr),
932 MetaClassPtrAlias(nullptr), RuntimeVersion(runtimeABIVersion),
933 ProtocolVersion(protocolClassVersion) {
David Chisnall01aa4672010-04-28 19:33:36 +0000934
935 msgSendMDKind = VMContext.getMDKindID("GNUObjCMessageSend");
936
David Chisnalld7972f52011-03-23 16:36:54 +0000937 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner8d3f4a42009-01-27 05:06:01 +0000938 IntTy = cast<llvm::IntegerType>(
David Chisnalld7972f52011-03-23 16:36:54 +0000939 Types.ConvertType(CGM.getContext().IntTy));
Chris Lattner8d3f4a42009-01-27 05:06:01 +0000940 LongTy = cast<llvm::IntegerType>(
David Chisnalld7972f52011-03-23 16:36:54 +0000941 Types.ConvertType(CGM.getContext().LongTy));
David Chisnall168b80f2010-12-26 22:13:16 +0000942 SizeTy = cast<llvm::IntegerType>(
David Chisnalld7972f52011-03-23 16:36:54 +0000943 Types.ConvertType(CGM.getContext().getSizeType()));
David Chisnall168b80f2010-12-26 22:13:16 +0000944 PtrDiffTy = cast<llvm::IntegerType>(
David Chisnalld7972f52011-03-23 16:36:54 +0000945 Types.ConvertType(CGM.getContext().getPointerDiffType()));
David Chisnall168b80f2010-12-26 22:13:16 +0000946 BoolTy = CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
Mike Stump11289f42009-09-09 15:08:12 +0000947
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000948 Int8Ty = llvm::Type::getInt8Ty(VMContext);
949 // C string type. Used in lots of places.
950 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
951
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000952 Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +0000953 Zeros[1] = Zeros[0];
Fariborz Jahanian2cde2032009-09-10 21:48:21 +0000954 NULLPtr = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Chris Lattner4bd55962008-03-30 23:03:07 +0000955 // Get the selector Type.
David Chisnall481e3a82010-01-23 02:40:42 +0000956 QualType selTy = CGM.getContext().getObjCSelType();
957 if (QualType() == selTy) {
958 SelectorTy = PtrToInt8Ty;
959 } else {
960 SelectorTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(selTy));
961 }
Chris Lattner8d3f4a42009-01-27 05:06:01 +0000962
Owen Anderson9793f0e2009-07-29 22:16:19 +0000963 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
Chris Lattner4bd55962008-03-30 23:03:07 +0000964 PtrTy = PtrToInt8Ty;
Mike Stump11289f42009-09-09 15:08:12 +0000965
David Chisnallcdd207e2011-10-04 15:35:30 +0000966 Int32Ty = llvm::Type::getInt32Ty(VMContext);
967 Int64Ty = llvm::Type::getInt64Ty(VMContext);
968
Rafael Espindola3cc5c2d2014-01-09 21:32:51 +0000969 IntPtrTy =
970 CGM.getDataLayout().getPointerSizeInBits() == 32 ? Int32Ty : Int64Ty;
David Chisnalle0dc7cb2011-10-08 08:54:36 +0000971
Chris Lattner4bd55962008-03-30 23:03:07 +0000972 // Object type
David Chisnall10d2ded2011-04-29 14:10:35 +0000973 QualType UnqualIdTy = CGM.getContext().getObjCIdType();
974 ASTIdTy = CanQualType();
975 if (UnqualIdTy != QualType()) {
976 ASTIdTy = CGM.getContext().getCanonicalType(UnqualIdTy);
David Chisnall481e3a82010-01-23 02:40:42 +0000977 IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
David Chisnall10d2ded2011-04-29 14:10:35 +0000978 } else {
979 IdTy = PtrToInt8Ty;
David Chisnall481e3a82010-01-23 02:40:42 +0000980 }
David Chisnall5bb4efd2010-02-03 15:59:02 +0000981 PtrToIdTy = llvm::PointerType::getUnqual(IdTy);
Mike Stump11289f42009-09-09 15:08:12 +0000982
Craig Topper8a13c412014-05-21 05:09:00 +0000983 ObjCSuperTy = llvm::StructType::get(IdTy, IdTy, nullptr);
David Chisnall76803412011-03-23 22:52:06 +0000984 PtrToObjCSuperTy = llvm::PointerType::getUnqual(ObjCSuperTy);
985
Chris Lattnera5f58b02011-07-09 17:41:47 +0000986 llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
David Chisnalld7972f52011-03-23 16:36:54 +0000987
988 // void objc_exception_throw(id);
Craig Topper8a13c412014-05-21 05:09:00 +0000989 ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy, nullptr);
990 ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy, nullptr);
David Chisnalld7972f52011-03-23 16:36:54 +0000991 // int objc_sync_enter(id);
Craig Topper8a13c412014-05-21 05:09:00 +0000992 SyncEnterFn.init(&CGM, "objc_sync_enter", IntTy, IdTy, nullptr);
David Chisnalld7972f52011-03-23 16:36:54 +0000993 // int objc_sync_exit(id);
Craig Topper8a13c412014-05-21 05:09:00 +0000994 SyncExitFn.init(&CGM, "objc_sync_exit", IntTy, IdTy, nullptr);
David Chisnalld7972f52011-03-23 16:36:54 +0000995
996 // void objc_enumerationMutation (id)
997 EnumerationMutationFn.init(&CGM, "objc_enumerationMutation", VoidTy,
Craig Topper8a13c412014-05-21 05:09:00 +0000998 IdTy, nullptr);
David Chisnalld7972f52011-03-23 16:36:54 +0000999
1000 // id objc_getProperty(id, SEL, ptrdiff_t, BOOL)
1001 GetPropertyFn.init(&CGM, "objc_getProperty", IdTy, IdTy, SelectorTy,
Craig Topper8a13c412014-05-21 05:09:00 +00001002 PtrDiffTy, BoolTy, nullptr);
David Chisnalld7972f52011-03-23 16:36:54 +00001003 // void objc_setProperty(id, SEL, ptrdiff_t, id, BOOL, BOOL)
1004 SetPropertyFn.init(&CGM, "objc_setProperty", VoidTy, IdTy, SelectorTy,
Craig Topper8a13c412014-05-21 05:09:00 +00001005 PtrDiffTy, IdTy, BoolTy, BoolTy, nullptr);
David Chisnalld7972f52011-03-23 16:36:54 +00001006 // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL)
1007 GetStructPropertyFn.init(&CGM, "objc_getPropertyStruct", VoidTy, PtrTy, PtrTy,
Craig Topper8a13c412014-05-21 05:09:00 +00001008 PtrDiffTy, BoolTy, BoolTy, nullptr);
David Chisnalld7972f52011-03-23 16:36:54 +00001009 // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL)
1010 SetStructPropertyFn.init(&CGM, "objc_setPropertyStruct", VoidTy, PtrTy, PtrTy,
Craig Topper8a13c412014-05-21 05:09:00 +00001011 PtrDiffTy, BoolTy, BoolTy, nullptr);
David Chisnalld7972f52011-03-23 16:36:54 +00001012
Chris Lattner4bd55962008-03-30 23:03:07 +00001013 // IMP type
Chris Lattnera5f58b02011-07-09 17:41:47 +00001014 llvm::Type *IMPArgs[] = { IdTy, SelectorTy };
David Chisnall76803412011-03-23 22:52:06 +00001015 IMPTy = llvm::PointerType::getUnqual(llvm::FunctionType::get(IdTy, IMPArgs,
1016 true));
David Chisnall5bb4efd2010-02-03 15:59:02 +00001017
David Blaikiebbafb8a2012-03-11 07:00:24 +00001018 const LangOptions &Opts = CGM.getLangOpts();
Douglas Gregor79a91412011-09-13 17:21:33 +00001019 if ((Opts.getGC() != LangOptions::NonGC) || Opts.ObjCAutoRefCount)
David Chisnalla918b882011-07-07 11:22:31 +00001020 RuntimeVersion = 10;
1021
David Chisnalld3858d62011-03-25 11:57:33 +00001022 // Don't bother initialising the GC stuff unless we're compiling in GC mode
Douglas Gregor79a91412011-09-13 17:21:33 +00001023 if (Opts.getGC() != LangOptions::NonGC) {
David Chisnall5c511772011-05-22 22:37:08 +00001024 // This is a bit of an hack. We should sort this out by having a proper
1025 // CGObjCGNUstep subclass for GC, but we may want to really support the old
1026 // ABI and GC added in ObjectiveC2.framework, so we fudge it a bit for now
David Chisnall5bb4efd2010-02-03 15:59:02 +00001027 // Get selectors needed in GC mode
1028 RetainSel = GetNullarySelector("retain", CGM.getContext());
1029 ReleaseSel = GetNullarySelector("release", CGM.getContext());
1030 AutoreleaseSel = GetNullarySelector("autorelease", CGM.getContext());
1031
1032 // Get functions needed in GC mode
1033
1034 // id objc_assign_ivar(id, id, ptrdiff_t);
David Chisnalld7972f52011-03-23 16:36:54 +00001035 IvarAssignFn.init(&CGM, "objc_assign_ivar", IdTy, IdTy, IdTy, PtrDiffTy,
Craig Topper8a13c412014-05-21 05:09:00 +00001036 nullptr);
David Chisnall5bb4efd2010-02-03 15:59:02 +00001037 // id objc_assign_strongCast (id, id*)
David Chisnalld7972f52011-03-23 16:36:54 +00001038 StrongCastAssignFn.init(&CGM, "objc_assign_strongCast", IdTy, IdTy,
Craig Topper8a13c412014-05-21 05:09:00 +00001039 PtrToIdTy, nullptr);
David Chisnall5bb4efd2010-02-03 15:59:02 +00001040 // id objc_assign_global(id, id*);
David Chisnalld7972f52011-03-23 16:36:54 +00001041 GlobalAssignFn.init(&CGM, "objc_assign_global", IdTy, IdTy, PtrToIdTy,
Craig Topper8a13c412014-05-21 05:09:00 +00001042 nullptr);
David Chisnall5bb4efd2010-02-03 15:59:02 +00001043 // id objc_assign_weak(id, id*);
Craig Topper8a13c412014-05-21 05:09:00 +00001044 WeakAssignFn.init(&CGM, "objc_assign_weak", IdTy, IdTy, PtrToIdTy, nullptr);
David Chisnall5bb4efd2010-02-03 15:59:02 +00001045 // id objc_read_weak(id*);
Craig Topper8a13c412014-05-21 05:09:00 +00001046 WeakReadFn.init(&CGM, "objc_read_weak", IdTy, PtrToIdTy, nullptr);
David Chisnall5bb4efd2010-02-03 15:59:02 +00001047 // void *objc_memmove_collectable(void*, void *, size_t);
David Chisnalld7972f52011-03-23 16:36:54 +00001048 MemMoveFn.init(&CGM, "objc_memmove_collectable", PtrTy, PtrTy, PtrTy,
Craig Topper8a13c412014-05-21 05:09:00 +00001049 SizeTy, nullptr);
David Chisnall5bb4efd2010-02-03 15:59:02 +00001050 }
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001051}
Mike Stumpdd93a192009-07-31 21:31:32 +00001052
John McCall882987f2013-02-28 19:01:20 +00001053llvm::Value *CGObjCGNU::GetClassNamed(CodeGenFunction &CGF,
Saleem Abdulrasoola088ad92016-07-17 22:27:41 +00001054 const std::string &Name, bool isWeak) {
John McCall7f416cc2015-09-08 08:05:57 +00001055 llvm::Constant *ClassName = MakeConstantString(Name);
David Chisnalldf349172010-01-08 00:14:31 +00001056 // With the incompatible ABI, this will need to be replaced with a direct
1057 // reference to the class symbol. For the compatible nonfragile ABI we are
1058 // still performing this lookup at run time but emitting the symbol for the
1059 // class externally so that we can make the switch later.
David Chisnall920e83b2011-06-29 13:16:41 +00001060 //
1061 // Libobjc2 contains an LLVM pass that replaces calls to objc_lookup_class
1062 // with memoized versions or with static references if it's safe to do so.
David Chisnall08d67332011-06-30 10:14:37 +00001063 if (!isWeak)
1064 EmitClassRef(Name);
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001065
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001066 llvm::Constant *ClassLookupFn =
Jay Foad5709f7c2011-07-29 13:56:53 +00001067 CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, PtrToInt8Ty, true),
Fariborz Jahanian3b636c12009-03-30 18:02:14 +00001068 "objc_lookup_class");
John McCall882987f2013-02-28 19:01:20 +00001069 return CGF.EmitNounwindRuntimeCall(ClassLookupFn, ClassName);
Chris Lattner4bd55962008-03-30 23:03:07 +00001070}
1071
David Chisnall920e83b2011-06-29 13:16:41 +00001072// This has to perform the lookup every time, since posing and related
1073// techniques can modify the name -> class mapping.
John McCall882987f2013-02-28 19:01:20 +00001074llvm::Value *CGObjCGNU::GetClass(CodeGenFunction &CGF,
David Chisnall920e83b2011-06-29 13:16:41 +00001075 const ObjCInterfaceDecl *OID) {
Saleem Abdulrasool7093e212016-07-17 22:27:44 +00001076 auto *Value =
1077 GetClassNamed(CGF, OID->getNameAsString(), OID->isWeakImported());
1078 if (CGM.getTriple().isOSBinFormatCOFF()) {
1079 if (auto *ClassSymbol = dyn_cast<llvm::GlobalVariable>(Value)) {
1080 auto DLLStorage = llvm::GlobalValue::DefaultStorageClass;
1081 if (OID->hasAttr<DLLExportAttr>())
1082 DLLStorage = llvm::GlobalValue::DLLExportStorageClass;
1083 else if (OID->hasAttr<DLLImportAttr>())
1084 DLLStorage = llvm::GlobalValue::DLLImportStorageClass;
1085 ClassSymbol->setDLLStorageClass(DLLStorage);
1086 }
1087 }
1088 return Value;
David Chisnall920e83b2011-06-29 13:16:41 +00001089}
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +00001090
John McCall882987f2013-02-28 19:01:20 +00001091llvm::Value *CGObjCGNU::EmitNSAutoreleasePoolClassRef(CodeGenFunction &CGF) {
Saleem Abdulrasool7093e212016-07-17 22:27:44 +00001092 auto *Value = GetClassNamed(CGF, "NSAutoreleasePool", false);
1093 if (CGM.getTriple().isOSBinFormatCOFF()) {
1094 if (auto *ClassSymbol = dyn_cast<llvm::GlobalVariable>(Value)) {
1095 IdentifierInfo &II = CGF.CGM.getContext().Idents.get("NSAutoreleasePool");
1096 TranslationUnitDecl *TUDecl = CGM.getContext().getTranslationUnitDecl();
1097 DeclContext *DC = TranslationUnitDecl::castToDeclContext(TUDecl);
1098
1099 const VarDecl *VD = nullptr;
1100 for (const auto &Result : DC->lookup(&II))
1101 if ((VD = dyn_cast<VarDecl>(Result)))
1102 break;
1103
1104 auto DLLStorage = llvm::GlobalValue::DefaultStorageClass;
1105 if (!VD || VD->hasAttr<DLLImportAttr>())
1106 DLLStorage = llvm::GlobalValue::DLLImportStorageClass;
1107 else if (VD->hasAttr<DLLExportAttr>())
1108 DLLStorage = llvm::GlobalValue::DLLExportStorageClass;
1109
1110 ClassSymbol->setDLLStorageClass(DLLStorage);
1111 }
1112 }
1113 return Value;
David Chisnall920e83b2011-06-29 13:16:41 +00001114}
1115
John McCall882987f2013-02-28 19:01:20 +00001116llvm::Value *CGObjCGNU::GetSelector(CodeGenFunction &CGF, Selector Sel,
John McCall7f416cc2015-09-08 08:05:57 +00001117 const std::string &TypeEncoding) {
Craig Topperfa159c12013-07-14 16:47:36 +00001118 SmallVectorImpl<TypedSelector> &Types = SelectorTable[Sel];
Craig Topper8a13c412014-05-21 05:09:00 +00001119 llvm::GlobalAlias *SelValue = nullptr;
David Chisnalld7972f52011-03-23 16:36:54 +00001120
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001121 for (SmallVectorImpl<TypedSelector>::iterator i = Types.begin(),
David Chisnalld7972f52011-03-23 16:36:54 +00001122 e = Types.end() ; i!=e ; i++) {
1123 if (i->first == TypeEncoding) {
1124 SelValue = i->second;
1125 break;
1126 }
1127 }
Craig Topper8a13c412014-05-21 05:09:00 +00001128 if (!SelValue) {
Rafael Espindola234405b2014-05-17 21:30:14 +00001129 SelValue = llvm::GlobalAlias::create(
David Blaikieaff29d32015-09-14 18:02:04 +00001130 SelectorTy->getElementType(), 0, llvm::GlobalValue::PrivateLinkage,
Rafael Espindola61722772014-05-17 19:58:16 +00001131 ".objc_selector_" + Sel.getAsString(), &TheModule);
Benjamin Kramer3204b152015-05-29 19:42:19 +00001132 Types.emplace_back(TypeEncoding, SelValue);
David Chisnalld7972f52011-03-23 16:36:54 +00001133 }
1134
David Chisnall76803412011-03-23 22:52:06 +00001135 return SelValue;
David Chisnalld7972f52011-03-23 16:36:54 +00001136}
1137
John McCall7f416cc2015-09-08 08:05:57 +00001138Address CGObjCGNU::GetAddrOfSelector(CodeGenFunction &CGF, Selector Sel) {
1139 llvm::Value *SelValue = GetSelector(CGF, Sel);
1140
1141 // Store it to a temporary. Does this satisfy the semantics of
1142 // GetAddrOfSelector? Hopefully.
1143 Address tmp = CGF.CreateTempAlloca(SelValue->getType(),
1144 CGF.getPointerAlign());
1145 CGF.Builder.CreateStore(SelValue, tmp);
1146 return tmp;
1147}
1148
1149llvm::Value *CGObjCGNU::GetSelector(CodeGenFunction &CGF, Selector Sel) {
1150 return GetSelector(CGF, Sel, std::string());
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001151}
1152
John McCall882987f2013-02-28 19:01:20 +00001153llvm::Value *CGObjCGNU::GetSelector(CodeGenFunction &CGF,
1154 const ObjCMethodDecl *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001155 std::string SelTypes;
1156 CGM.getContext().getObjCEncodingForMethodDecl(Method, SelTypes);
John McCall7f416cc2015-09-08 08:05:57 +00001157 return GetSelector(CGF, Method->getSelector(), SelTypes);
Chris Lattner6d522c02008-06-26 04:37:12 +00001158}
1159
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001160llvm::Constant *CGObjCGNU::GetEHType(QualType T) {
John McCallc31d8932012-11-14 09:08:34 +00001161 if (T->isObjCIdType() || T->isObjCQualifiedIdType()) {
1162 // With the old ABI, there was only one kind of catchall, which broke
1163 // foreign exceptions. With the new ABI, we use __objc_id_typeinfo as
1164 // a pointer indicating object catchalls, and NULL to indicate real
1165 // catchalls
1166 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
1167 return MakeConstantString("@id");
1168 } else {
Craig Topper8a13c412014-05-21 05:09:00 +00001169 return nullptr;
John McCallc31d8932012-11-14 09:08:34 +00001170 }
David Chisnalld3858d62011-03-25 11:57:33 +00001171 }
John McCallc31d8932012-11-14 09:08:34 +00001172
1173 // All other types should be Objective-C interface pointer types.
1174 const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>();
1175 assert(OPT && "Invalid @catch type.");
1176 const ObjCInterfaceDecl *IDecl = OPT->getObjectType()->getInterface();
1177 assert(IDecl && "Invalid @catch type.");
1178 return MakeConstantString(IDecl->getIdentifier()->getName());
1179}
1180
1181llvm::Constant *CGObjCGNUstep::GetEHType(QualType T) {
1182 if (!CGM.getLangOpts().CPlusPlus)
1183 return CGObjCGNU::GetEHType(T);
1184
David Chisnalle1d2584d2011-03-20 21:35:39 +00001185 // For Objective-C++, we want to provide the ability to catch both C++ and
1186 // Objective-C objects in the same function.
1187
1188 // There's a particular fixed type info for 'id'.
1189 if (T->isObjCIdType() ||
1190 T->isObjCQualifiedIdType()) {
1191 llvm::Constant *IDEHType =
1192 CGM.getModule().getGlobalVariable("__objc_id_type_info");
1193 if (!IDEHType)
1194 IDEHType =
1195 new llvm::GlobalVariable(CGM.getModule(), PtrToInt8Ty,
1196 false,
1197 llvm::GlobalValue::ExternalLinkage,
Craig Topper8a13c412014-05-21 05:09:00 +00001198 nullptr, "__objc_id_type_info");
David Chisnalle1d2584d2011-03-20 21:35:39 +00001199 return llvm::ConstantExpr::getBitCast(IDEHType, PtrToInt8Ty);
1200 }
1201
1202 const ObjCObjectPointerType *PT =
1203 T->getAs<ObjCObjectPointerType>();
1204 assert(PT && "Invalid @catch type.");
1205 const ObjCInterfaceType *IT = PT->getInterfaceType();
1206 assert(IT && "Invalid @catch type.");
1207 std::string className = IT->getDecl()->getIdentifier()->getName();
1208
1209 std::string typeinfoName = "__objc_eh_typeinfo_" + className;
1210
1211 // Return the existing typeinfo if it exists
1212 llvm::Constant *typeinfo = TheModule.getGlobalVariable(typeinfoName);
David Chisnalld6639342012-03-20 16:25:52 +00001213 if (typeinfo)
1214 return llvm::ConstantExpr::getBitCast(typeinfo, PtrToInt8Ty);
David Chisnalle1d2584d2011-03-20 21:35:39 +00001215
1216 // Otherwise create it.
1217
1218 // vtable for gnustep::libobjc::__objc_class_type_info
1219 // It's quite ugly hard-coding this. Ideally we'd generate it using the host
1220 // platform's name mangling.
1221 const char *vtableName = "_ZTVN7gnustep7libobjc22__objc_class_type_infoE";
David Blaikiee3b172a2015-04-02 18:55:21 +00001222 auto *Vtable = TheModule.getGlobalVariable(vtableName);
David Chisnalle1d2584d2011-03-20 21:35:39 +00001223 if (!Vtable) {
1224 Vtable = new llvm::GlobalVariable(TheModule, PtrToInt8Ty, true,
Craig Topper8a13c412014-05-21 05:09:00 +00001225 llvm::GlobalValue::ExternalLinkage,
1226 nullptr, vtableName);
David Chisnalle1d2584d2011-03-20 21:35:39 +00001227 }
1228 llvm::Constant *Two = llvm::ConstantInt::get(IntTy, 2);
David Blaikiee3b172a2015-04-02 18:55:21 +00001229 auto *BVtable = llvm::ConstantExpr::getBitCast(
1230 llvm::ConstantExpr::getGetElementPtr(Vtable->getValueType(), Vtable, Two),
1231 PtrToInt8Ty);
David Chisnalle1d2584d2011-03-20 21:35:39 +00001232
1233 llvm::Constant *typeName =
1234 ExportUniqueString(className, "__objc_eh_typename_");
1235
1236 std::vector<llvm::Constant*> fields;
David Blaikiee3b172a2015-04-02 18:55:21 +00001237 fields.push_back(BVtable);
David Chisnalle1d2584d2011-03-20 21:35:39 +00001238 fields.push_back(typeName);
1239 llvm::Constant *TI =
John McCall7f416cc2015-09-08 08:05:57 +00001240 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, nullptr),
1241 fields, CGM.getPointerAlign(),
1242 "__objc_eh_typeinfo_" + className,
David Chisnalle1d2584d2011-03-20 21:35:39 +00001243 llvm::GlobalValue::LinkOnceODRLinkage);
1244 return llvm::ConstantExpr::getBitCast(TI, PtrToInt8Ty);
John McCall2ca705e2010-07-24 00:37:23 +00001245}
1246
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001247/// Generate an NSConstantString object.
John McCall7f416cc2015-09-08 08:05:57 +00001248ConstantAddress CGObjCGNU::GenerateConstantString(const StringLiteral *SL) {
David Chisnall358e7512010-01-27 12:49:23 +00001249
Benjamin Kramer35b077e2010-08-17 12:54:38 +00001250 std::string Str = SL->getString().str();
John McCall7f416cc2015-09-08 08:05:57 +00001251 CharUnits Align = CGM.getPointerAlign();
David Chisnall481e3a82010-01-23 02:40:42 +00001252
David Chisnall358e7512010-01-27 12:49:23 +00001253 // Look for an existing one
1254 llvm::StringMap<llvm::Constant*>::iterator old = ObjCStrings.find(Str);
1255 if (old != ObjCStrings.end())
John McCall7f416cc2015-09-08 08:05:57 +00001256 return ConstantAddress(old->getValue(), Align);
David Chisnall358e7512010-01-27 12:49:23 +00001257
David Blaikiebbafb8a2012-03-11 07:00:24 +00001258 StringRef StringClass = CGM.getLangOpts().ObjCConstantStringClass;
David Chisnall207a6302012-01-04 12:02:13 +00001259
1260 if (StringClass.empty()) StringClass = "NXConstantString";
1261
1262 std::string Sym = "_OBJC_CLASS_";
1263 Sym += StringClass;
1264
1265 llvm::Constant *isa = TheModule.getNamedGlobal(Sym);
1266
1267 if (!isa)
1268 isa = new llvm::GlobalVariable(TheModule, IdTy, /* isConstant */false,
Craig Topper8a13c412014-05-21 05:09:00 +00001269 llvm::GlobalValue::ExternalWeakLinkage, nullptr, Sym);
David Chisnall207a6302012-01-04 12:02:13 +00001270 else if (isa->getType() != PtrToIdTy)
1271 isa = llvm::ConstantExpr::getBitCast(isa, PtrToIdTy);
1272
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001273 std::vector<llvm::Constant*> Ivars;
David Chisnall207a6302012-01-04 12:02:13 +00001274 Ivars.push_back(isa);
Chris Lattner091f6982008-06-21 21:44:18 +00001275 Ivars.push_back(MakeConstantString(Str));
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001276 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001277 llvm::Constant *ObjCStr = MakeGlobal(
Craig Topper8a13c412014-05-21 05:09:00 +00001278 llvm::StructType::get(PtrToIdTy, PtrToInt8Ty, IntTy, nullptr),
John McCall7f416cc2015-09-08 08:05:57 +00001279 Ivars, Align, ".objc_str");
David Chisnall358e7512010-01-27 12:49:23 +00001280 ObjCStr = llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty);
1281 ObjCStrings[Str] = ObjCStr;
1282 ConstantStrings.push_back(ObjCStr);
John McCall7f416cc2015-09-08 08:05:57 +00001283 return ConstantAddress(ObjCStr, Align);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001284}
1285
1286///Generates a message send where the super is the receiver. This is a message
1287///send to self with special delivery semantics indicating which class's method
1288///should be called.
David Chisnalld7972f52011-03-23 16:36:54 +00001289RValue
1290CGObjCGNU::GenerateMessageSendSuper(CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001291 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001292 QualType ResultType,
1293 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001294 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001295 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001296 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001297 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001298 const CallArgList &CallArgs,
1299 const ObjCMethodDecl *Method) {
David Chisnall8a42d192011-05-28 14:09:01 +00001300 CGBuilderTy &Builder = CGF.Builder;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001301 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
David Chisnall5bb4efd2010-02-03 15:59:02 +00001302 if (Sel == RetainSel || Sel == AutoreleaseSel) {
David Chisnall8a42d192011-05-28 14:09:01 +00001303 return RValue::get(EnforceType(Builder, Receiver,
1304 CGM.getTypes().ConvertType(ResultType)));
David Chisnall5bb4efd2010-02-03 15:59:02 +00001305 }
1306 if (Sel == ReleaseSel) {
Craig Topper8a13c412014-05-21 05:09:00 +00001307 return RValue::get(nullptr);
David Chisnall5bb4efd2010-02-03 15:59:02 +00001308 }
1309 }
David Chisnallea529a42010-05-01 12:37:16 +00001310
John McCall882987f2013-02-28 19:01:20 +00001311 llvm::Value *cmd = GetSelector(CGF, Sel);
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +00001312 CallArgList ActualArgs;
1313
Eli Friedman43dca6a2011-05-02 17:57:46 +00001314 ActualArgs.add(RValue::get(EnforceType(Builder, Receiver, IdTy)), ASTIdTy);
1315 ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType());
John McCall31168b02011-06-15 23:02:42 +00001316 ActualArgs.addFrom(CallArgs);
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +00001317
John McCalla729c622012-02-17 03:33:10 +00001318 MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +00001319
Craig Topper8a13c412014-05-21 05:09:00 +00001320 llvm::Value *ReceiverClass = nullptr;
Chris Lattnera02cb802009-05-08 15:39:58 +00001321 if (isCategoryImpl) {
Craig Topper8a13c412014-05-21 05:09:00 +00001322 llvm::Constant *classLookupFunction = nullptr;
Chris Lattnera02cb802009-05-08 15:39:58 +00001323 if (IsClassMessage) {
Owen Anderson9793f0e2009-07-29 22:16:19 +00001324 classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Jay Foad5709f7c2011-07-29 13:56:53 +00001325 IdTy, PtrTy, true), "objc_get_meta_class");
Chris Lattnera02cb802009-05-08 15:39:58 +00001326 } else {
Owen Anderson9793f0e2009-07-29 22:16:19 +00001327 classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Jay Foad5709f7c2011-07-29 13:56:53 +00001328 IdTy, PtrTy, true), "objc_get_class");
Daniel Dunbar566421c2009-05-04 15:31:17 +00001329 }
David Chisnallea529a42010-05-01 12:37:16 +00001330 ReceiverClass = Builder.CreateCall(classLookupFunction,
Chris Lattnera02cb802009-05-08 15:39:58 +00001331 MakeConstantString(Class->getNameAsString()));
Daniel Dunbar566421c2009-05-04 15:31:17 +00001332 } else {
Chris Lattnera02cb802009-05-08 15:39:58 +00001333 // Set up global aliases for the metaclass or class pointer if they do not
1334 // already exist. These will are forward-references which will be set to
Mike Stumpdd93a192009-07-31 21:31:32 +00001335 // pointers to the class and metaclass structure created for the runtime
1336 // load function. To send a message to super, we look up the value of the
Chris Lattnera02cb802009-05-08 15:39:58 +00001337 // super_class pointer from either the class or metaclass structure.
1338 if (IsClassMessage) {
1339 if (!MetaClassPtrAlias) {
Rafael Espindola234405b2014-05-17 21:30:14 +00001340 MetaClassPtrAlias = llvm::GlobalAlias::create(
David Blaikieaff29d32015-09-14 18:02:04 +00001341 IdTy->getElementType(), 0, llvm::GlobalValue::InternalLinkage,
Rafael Espindola61722772014-05-17 19:58:16 +00001342 ".objc_metaclass_ref" + Class->getNameAsString(), &TheModule);
Chris Lattnera02cb802009-05-08 15:39:58 +00001343 }
1344 ReceiverClass = MetaClassPtrAlias;
1345 } else {
1346 if (!ClassPtrAlias) {
Rafael Espindola234405b2014-05-17 21:30:14 +00001347 ClassPtrAlias = llvm::GlobalAlias::create(
David Blaikieaff29d32015-09-14 18:02:04 +00001348 IdTy->getElementType(), 0, llvm::GlobalValue::InternalLinkage,
Rafael Espindola61722772014-05-17 19:58:16 +00001349 ".objc_class_ref" + Class->getNameAsString(), &TheModule);
Chris Lattnera02cb802009-05-08 15:39:58 +00001350 }
1351 ReceiverClass = ClassPtrAlias;
Daniel Dunbar566421c2009-05-04 15:31:17 +00001352 }
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00001353 }
Daniel Dunbar566421c2009-05-04 15:31:17 +00001354 // Cast the pointer to a simplified version of the class structure
David Blaikie1ed728c2015-04-05 22:45:47 +00001355 llvm::Type *CastTy = llvm::StructType::get(IdTy, IdTy, nullptr);
David Chisnallea529a42010-05-01 12:37:16 +00001356 ReceiverClass = Builder.CreateBitCast(ReceiverClass,
David Blaikie1ed728c2015-04-05 22:45:47 +00001357 llvm::PointerType::getUnqual(CastTy));
Daniel Dunbar566421c2009-05-04 15:31:17 +00001358 // Get the superclass pointer
David Blaikie1ed728c2015-04-05 22:45:47 +00001359 ReceiverClass = Builder.CreateStructGEP(CastTy, ReceiverClass, 1);
Daniel Dunbar566421c2009-05-04 15:31:17 +00001360 // Load the superclass pointer
John McCall7f416cc2015-09-08 08:05:57 +00001361 ReceiverClass =
1362 Builder.CreateAlignedLoad(ReceiverClass, CGF.getPointerAlign());
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001363 // Construct the structure used to look up the IMP
Chris Lattner845511f2011-06-18 22:49:11 +00001364 llvm::StructType *ObjCSuperTy = llvm::StructType::get(
Craig Topper8a13c412014-05-21 05:09:00 +00001365 Receiver->getType(), IdTy, nullptr);
John McCall7f416cc2015-09-08 08:05:57 +00001366
1367 // FIXME: Is this really supposed to be a dynamic alloca?
1368 Address ObjCSuper = Address(Builder.CreateAlloca(ObjCSuperTy),
1369 CGF.getPointerAlign());
Fariborz Jahanianb73a23e2009-02-04 20:31:19 +00001370
David Blaikie2e804282015-04-05 22:47:07 +00001371 Builder.CreateStore(Receiver,
John McCall7f416cc2015-09-08 08:05:57 +00001372 Builder.CreateStructGEP(ObjCSuper, 0, CharUnits::Zero()));
David Blaikie2e804282015-04-05 22:47:07 +00001373 Builder.CreateStore(ReceiverClass,
John McCall7f416cc2015-09-08 08:05:57 +00001374 Builder.CreateStructGEP(ObjCSuper, 1, CGF.getPointerSize()));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001375
David Chisnall76803412011-03-23 22:52:06 +00001376 ObjCSuper = EnforceType(Builder, ObjCSuper, PtrToObjCSuperTy);
David Chisnall76803412011-03-23 22:52:06 +00001377
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001378 // Get the IMP
Eli Friedmanf24bd3b2013-07-26 00:53:29 +00001379 llvm::Value *imp = LookupIMPSuper(CGF, ObjCSuper, cmd, MSI);
John McCalla729c622012-02-17 03:33:10 +00001380 imp = EnforceType(Builder, imp, MSI.MessengerType);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001381
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001382 llvm::Metadata *impMD[] = {
David Chisnall9eecafa2010-05-01 11:15:56 +00001383 llvm::MDString::get(VMContext, Sel.getAsString()),
1384 llvm::MDString::get(VMContext, Class->getSuperClass()->getNameAsString()),
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001385 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1386 llvm::Type::getInt1Ty(VMContext), IsClassMessage))};
Jay Foadea324f12011-04-21 19:59:12 +00001387 llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD);
David Chisnall9eecafa2010-05-01 11:15:56 +00001388
David Chisnallff5f88c2010-05-02 13:41:58 +00001389 llvm::Instruction *call;
Samuel Antao798f11c2015-11-23 22:04:44 +00001390 RValue msgRet = CGF.EmitCall(MSI.CallInfo, imp, Return, ActualArgs,
1391 CGCalleeInfo(), &call);
David Chisnallff5f88c2010-05-02 13:41:58 +00001392 call->setMetadata(msgSendMDKind, node);
1393 return msgRet;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001394}
1395
Mike Stump11289f42009-09-09 15:08:12 +00001396/// Generate code for a message send expression.
David Chisnalld7972f52011-03-23 16:36:54 +00001397RValue
1398CGObjCGNU::GenerateMessageSend(CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001399 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001400 QualType ResultType,
1401 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001402 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001403 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001404 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001405 const ObjCMethodDecl *Method) {
David Chisnall8a42d192011-05-28 14:09:01 +00001406 CGBuilderTy &Builder = CGF.Builder;
1407
David Chisnall75afda62010-04-27 15:08:48 +00001408 // Strip out message sends to retain / release in GC mode
David Blaikiebbafb8a2012-03-11 07:00:24 +00001409 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
David Chisnall5bb4efd2010-02-03 15:59:02 +00001410 if (Sel == RetainSel || Sel == AutoreleaseSel) {
David Chisnall8a42d192011-05-28 14:09:01 +00001411 return RValue::get(EnforceType(Builder, Receiver,
1412 CGM.getTypes().ConvertType(ResultType)));
David Chisnall5bb4efd2010-02-03 15:59:02 +00001413 }
1414 if (Sel == ReleaseSel) {
Craig Topper8a13c412014-05-21 05:09:00 +00001415 return RValue::get(nullptr);
David Chisnall5bb4efd2010-02-03 15:59:02 +00001416 }
1417 }
David Chisnall75afda62010-04-27 15:08:48 +00001418
David Chisnall75afda62010-04-27 15:08:48 +00001419 // If the return type is something that goes in an integer register, the
1420 // runtime will handle 0 returns. For other cases, we fill in the 0 value
1421 // ourselves.
1422 //
1423 // The language spec says the result of this kind of message send is
1424 // undefined, but lots of people seem to have forgotten to read that
1425 // paragraph and insist on sending messages to nil that have structure
1426 // returns. With GCC, this generates a random return value (whatever happens
1427 // to be on the stack / in those registers at the time) on most platforms,
David Chisnall76803412011-03-23 22:52:06 +00001428 // and generates an illegal instruction trap on SPARC. With LLVM it corrupts
1429 // the stack.
1430 bool isPointerSizedReturn = (ResultType->isAnyPointerType() ||
1431 ResultType->isIntegralOrEnumerationType() || ResultType->isVoidType());
David Chisnall75afda62010-04-27 15:08:48 +00001432
Craig Topper8a13c412014-05-21 05:09:00 +00001433 llvm::BasicBlock *startBB = nullptr;
1434 llvm::BasicBlock *messageBB = nullptr;
1435 llvm::BasicBlock *continueBB = nullptr;
David Chisnall75afda62010-04-27 15:08:48 +00001436
1437 if (!isPointerSizedReturn) {
1438 startBB = Builder.GetInsertBlock();
1439 messageBB = CGF.createBasicBlock("msgSend");
David Chisnall29cefd12010-05-20 13:45:48 +00001440 continueBB = CGF.createBasicBlock("continue");
David Chisnall75afda62010-04-27 15:08:48 +00001441
1442 llvm::Value *isNil = Builder.CreateICmpEQ(Receiver,
1443 llvm::Constant::getNullValue(Receiver->getType()));
David Chisnall29cefd12010-05-20 13:45:48 +00001444 Builder.CreateCondBr(isNil, continueBB, messageBB);
David Chisnall75afda62010-04-27 15:08:48 +00001445 CGF.EmitBlock(messageBB);
1446 }
1447
David Chisnall9f57c292009-08-17 16:35:33 +00001448 IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001449 llvm::Value *cmd;
1450 if (Method)
John McCall882987f2013-02-28 19:01:20 +00001451 cmd = GetSelector(CGF, Method);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001452 else
John McCall882987f2013-02-28 19:01:20 +00001453 cmd = GetSelector(CGF, Sel);
David Chisnall76803412011-03-23 22:52:06 +00001454 cmd = EnforceType(Builder, cmd, SelectorTy);
1455 Receiver = EnforceType(Builder, Receiver, IdTy);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001456
Duncan P. N. Exon Smithfb494912014-12-09 18:39:32 +00001457 llvm::Metadata *impMD[] = {
1458 llvm::MDString::get(VMContext, Sel.getAsString()),
1459 llvm::MDString::get(VMContext, Class ? Class->getNameAsString() : ""),
1460 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
1461 llvm::Type::getInt1Ty(VMContext), Class != nullptr))};
Jay Foadea324f12011-04-21 19:59:12 +00001462 llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD);
David Chisnall76803412011-03-23 22:52:06 +00001463
David Chisnall76803412011-03-23 22:52:06 +00001464 CallArgList ActualArgs;
Eli Friedman43dca6a2011-05-02 17:57:46 +00001465 ActualArgs.add(RValue::get(Receiver), ASTIdTy);
1466 ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType());
John McCall31168b02011-06-15 23:02:42 +00001467 ActualArgs.addFrom(CallArgs);
John McCalla729c622012-02-17 03:33:10 +00001468
1469 MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
1470
David Chisnall8c93cf22011-10-24 14:07:03 +00001471 // Get the IMP to call
1472 llvm::Value *imp;
1473
1474 // If we have non-legacy dispatch specified, we try using the objc_msgSend()
1475 // functions. These are not supported on all platforms (or all runtimes on a
1476 // given platform), so we
1477 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
David Chisnall8c93cf22011-10-24 14:07:03 +00001478 case CodeGenOptions::Legacy:
Eli Friedmanf24bd3b2013-07-26 00:53:29 +00001479 imp = LookupIMP(CGF, Receiver, cmd, node, MSI);
David Chisnall8c93cf22011-10-24 14:07:03 +00001480 break;
1481 case CodeGenOptions::Mixed:
David Chisnall8c93cf22011-10-24 14:07:03 +00001482 case CodeGenOptions::NonLegacy:
David Chisnall0cc83e72011-10-28 17:55:06 +00001483 if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1484 imp = CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true),
1485 "objc_msgSend_fpret");
John McCalla729c622012-02-17 03:33:10 +00001486 } else if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
David Chisnall8c93cf22011-10-24 14:07:03 +00001487 // The actual types here don't matter - we're going to bitcast the
1488 // function anyway
1489 imp = CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true),
1490 "objc_msgSend_stret");
1491 } else {
1492 imp = CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true),
1493 "objc_msgSend");
1494 }
1495 }
1496
David Chisnall6aec31a2011-12-01 18:40:09 +00001497 // Reset the receiver in case the lookup modified it
1498 ActualArgs[0] = CallArg(RValue::get(Receiver), ASTIdTy, false);
David Chisnall8c93cf22011-10-24 14:07:03 +00001499
John McCalla729c622012-02-17 03:33:10 +00001500 imp = EnforceType(Builder, imp, MSI.MessengerType);
David Chisnallc0cf4222010-05-01 12:56:56 +00001501
David Chisnallff5f88c2010-05-02 13:41:58 +00001502 llvm::Instruction *call;
Samuel Antao798f11c2015-11-23 22:04:44 +00001503 RValue msgRet = CGF.EmitCall(MSI.CallInfo, imp, Return, ActualArgs,
1504 CGCalleeInfo(), &call);
David Chisnallff5f88c2010-05-02 13:41:58 +00001505 call->setMetadata(msgSendMDKind, node);
David Chisnall75afda62010-04-27 15:08:48 +00001506
David Chisnall29cefd12010-05-20 13:45:48 +00001507
David Chisnall75afda62010-04-27 15:08:48 +00001508 if (!isPointerSizedReturn) {
David Chisnall29cefd12010-05-20 13:45:48 +00001509 messageBB = CGF.Builder.GetInsertBlock();
1510 CGF.Builder.CreateBr(continueBB);
1511 CGF.EmitBlock(continueBB);
David Chisnall75afda62010-04-27 15:08:48 +00001512 if (msgRet.isScalar()) {
1513 llvm::Value *v = msgRet.getScalarVal();
Jay Foad20c0f022011-03-30 11:28:58 +00001514 llvm::PHINode *phi = Builder.CreatePHI(v->getType(), 2);
David Chisnall75afda62010-04-27 15:08:48 +00001515 phi->addIncoming(v, messageBB);
1516 phi->addIncoming(llvm::Constant::getNullValue(v->getType()), startBB);
1517 msgRet = RValue::get(phi);
1518 } else if (msgRet.isAggregate()) {
John McCall7f416cc2015-09-08 08:05:57 +00001519 Address v = msgRet.getAggregateAddress();
1520 llvm::PHINode *phi = Builder.CreatePHI(v.getType(), 2);
1521 llvm::Type *RetTy = v.getElementType();
1522 Address NullVal = CGF.CreateTempAlloca(RetTy, v.getAlignment(), "null");
1523 CGF.InitTempAlloca(NullVal, llvm::Constant::getNullValue(RetTy));
1524 phi->addIncoming(v.getPointer(), messageBB);
1525 phi->addIncoming(NullVal.getPointer(), startBB);
1526 msgRet = RValue::getAggregate(Address(phi, v.getAlignment()));
David Chisnall75afda62010-04-27 15:08:48 +00001527 } else /* isComplex() */ {
1528 std::pair<llvm::Value*,llvm::Value*> v = msgRet.getComplexVal();
Jay Foad20c0f022011-03-30 11:28:58 +00001529 llvm::PHINode *phi = Builder.CreatePHI(v.first->getType(), 2);
David Chisnall75afda62010-04-27 15:08:48 +00001530 phi->addIncoming(v.first, messageBB);
1531 phi->addIncoming(llvm::Constant::getNullValue(v.first->getType()),
1532 startBB);
Jay Foad20c0f022011-03-30 11:28:58 +00001533 llvm::PHINode *phi2 = Builder.CreatePHI(v.second->getType(), 2);
David Chisnall75afda62010-04-27 15:08:48 +00001534 phi2->addIncoming(v.second, messageBB);
1535 phi2->addIncoming(llvm::Constant::getNullValue(v.second->getType()),
1536 startBB);
1537 msgRet = RValue::getComplex(phi, phi2);
1538 }
1539 }
1540 return msgRet;
Chris Lattnerb7256cd2008-03-01 08:50:34 +00001541}
1542
Mike Stump11289f42009-09-09 15:08:12 +00001543/// Generates a MethodList. Used in construction of a objc_class and
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001544/// objc_category structures.
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00001545llvm::Constant *CGObjCGNU::
Craig Topperbf3e3272014-08-30 16:55:52 +00001546GenerateMethodList(StringRef ClassName,
1547 StringRef CategoryName,
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00001548 ArrayRef<Selector> MethodSels,
1549 ArrayRef<llvm::Constant *> MethodTypes,
1550 bool isClassMethodList) {
David Chisnall9f57c292009-08-17 16:35:33 +00001551 if (MethodSels.empty())
1552 return NULLPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001553 // Get the method structure type.
Chris Lattner845511f2011-06-18 22:49:11 +00001554 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001555 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
1556 PtrToInt8Ty, // Method types
David Chisnall76803412011-03-23 22:52:06 +00001557 IMPTy, //Method pointer
Craig Topper8a13c412014-05-21 05:09:00 +00001558 nullptr);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001559 std::vector<llvm::Constant*> Methods;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001560 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
David Chisnalld7972f52011-03-23 16:36:54 +00001561 llvm::Constant *Method =
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001562 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
David Chisnalld7972f52011-03-23 16:36:54 +00001563 MethodSels[i],
1564 isClassMethodList));
1565 assert(Method && "Can't generate metadata for method that doesn't exist");
1566 llvm::Constant *C = MakeConstantString(MethodSels[i].getAsString());
David Chisnalld7972f52011-03-23 16:36:54 +00001567 Method = llvm::ConstantExpr::getBitCast(Method,
David Chisnall76803412011-03-23 22:52:06 +00001568 IMPTy);
Benjamin Kramer30934732016-07-02 11:41:41 +00001569 Methods.push_back(
1570 llvm::ConstantStruct::get(ObjCMethodTy, {C, MethodTypes[i], Method}));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001571 }
1572
1573 // Array of method structures
Owen Anderson9793f0e2009-07-29 22:16:19 +00001574 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Fariborz Jahanian078cd522009-05-17 16:49:27 +00001575 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00001576 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattner882034d2008-06-26 04:52:29 +00001577 Methods);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001578
1579 // Structure containing list pointer, array and array count
Chris Lattner5ec04a52011-08-12 17:43:31 +00001580 llvm::StructType *ObjCMethodListTy = llvm::StructType::create(VMContext);
Chris Lattnera5f58b02011-07-09 17:41:47 +00001581 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(ObjCMethodListTy);
1582 ObjCMethodListTy->setBody(
Mike Stump11289f42009-09-09 15:08:12 +00001583 NextPtrTy,
1584 IntTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001585 ObjCMethodArrayTy,
Craig Topper8a13c412014-05-21 05:09:00 +00001586 nullptr);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001587
1588 Methods.clear();
Owen Anderson7ec07a52009-07-30 23:11:26 +00001589 Methods.push_back(llvm::ConstantPointerNull::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +00001590 llvm::PointerType::getUnqual(ObjCMethodListTy)));
David Chisnallcdd207e2011-10-04 15:35:30 +00001591 Methods.push_back(llvm::ConstantInt::get(Int32Ty, MethodTypes.size()));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001592 Methods.push_back(MethodArray);
Mike Stump11289f42009-09-09 15:08:12 +00001593
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001594 // Create an instance of the structure
John McCall7f416cc2015-09-08 08:05:57 +00001595 return MakeGlobal(ObjCMethodListTy, Methods, CGM.getPointerAlign(),
1596 ".objc_method_list");
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001597}
1598
1599/// Generates an IvarList. Used in construction of a objc_class.
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00001600llvm::Constant *CGObjCGNU::
1601GenerateIvarList(ArrayRef<llvm::Constant *> IvarNames,
1602 ArrayRef<llvm::Constant *> IvarTypes,
1603 ArrayRef<llvm::Constant *> IvarOffsets) {
David Chisnallb3b44ce2009-11-16 19:05:54 +00001604 if (IvarNames.size() == 0)
1605 return NULLPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001606 // Get the method structure type.
Chris Lattner845511f2011-06-18 22:49:11 +00001607 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001608 PtrToInt8Ty,
1609 PtrToInt8Ty,
1610 IntTy,
Craig Topper8a13c412014-05-21 05:09:00 +00001611 nullptr);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001612 std::vector<llvm::Constant*> Ivars;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001613 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
Benjamin Kramer30934732016-07-02 11:41:41 +00001614 Ivars.push_back(llvm::ConstantStruct::get(
1615 ObjCIvarTy, {IvarNames[i], IvarTypes[i], IvarOffsets[i]}));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001616 }
1617
1618 // Array of method structures
Owen Anderson9793f0e2009-07-29 22:16:19 +00001619 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001620 IvarNames.size());
1621
Benjamin Kramer30934732016-07-02 11:41:41 +00001622 llvm::Constant *Elements[] = {
1623 llvm::ConstantInt::get(IntTy, (int)IvarNames.size()),
1624 llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars)};
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001625 // Structure containing array and array count
Chris Lattner845511f2011-06-18 22:49:11 +00001626 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001627 ObjCIvarArrayTy,
Craig Topper8a13c412014-05-21 05:09:00 +00001628 nullptr);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001629
1630 // Create an instance of the structure
John McCall7f416cc2015-09-08 08:05:57 +00001631 return MakeGlobal(ObjCIvarListTy, Elements, CGM.getPointerAlign(),
1632 ".objc_ivar_list");
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001633}
1634
1635/// Generate a class structure
1636llvm::Constant *CGObjCGNU::GenerateClassStructure(
1637 llvm::Constant *MetaClass,
1638 llvm::Constant *SuperClass,
1639 unsigned info,
Chris Lattnerda35bc82008-06-26 04:47:04 +00001640 const char *Name,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001641 llvm::Constant *Version,
1642 llvm::Constant *InstanceSize,
1643 llvm::Constant *IVars,
1644 llvm::Constant *Methods,
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001645 llvm::Constant *Protocols,
1646 llvm::Constant *IvarOffsets,
David Chisnalld472c852010-04-28 14:29:56 +00001647 llvm::Constant *Properties,
David Chisnallcdd207e2011-10-04 15:35:30 +00001648 llvm::Constant *StrongIvarBitmap,
1649 llvm::Constant *WeakIvarBitmap,
David Chisnalld472c852010-04-28 14:29:56 +00001650 bool isMeta) {
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001651 // Set up the class structure
1652 // Note: Several of these are char*s when they should be ids. This is
1653 // because the runtime performs this translation on load.
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001654 //
1655 // Fields marked New ABI are part of the GNUstep runtime. We emit them
1656 // anyway; the classes will still work with the GNU runtime, they will just
1657 // be ignored.
Chris Lattner845511f2011-06-18 22:49:11 +00001658 llvm::StructType *ClassTy = llvm::StructType::get(
David Chisnall207a6302012-01-04 12:02:13 +00001659 PtrToInt8Ty, // isa
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001660 PtrToInt8Ty, // super_class
1661 PtrToInt8Ty, // name
1662 LongTy, // version
1663 LongTy, // info
1664 LongTy, // instance_size
1665 IVars->getType(), // ivars
1666 Methods->getType(), // methods
Mike Stump11289f42009-09-09 15:08:12 +00001667 // These are all filled in by the runtime, so we pretend
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001668 PtrTy, // dtable
1669 PtrTy, // subclass_list
1670 PtrTy, // sibling_class
1671 PtrTy, // protocols
1672 PtrTy, // gc_object_type
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001673 // New ABI:
1674 LongTy, // abi_version
1675 IvarOffsets->getType(), // ivar_offsets
1676 Properties->getType(), // properties
David Chisnalle89ac062011-10-25 10:12:21 +00001677 IntPtrTy, // strong_pointers
1678 IntPtrTy, // weak_pointers
Craig Topper8a13c412014-05-21 05:09:00 +00001679 nullptr);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001680 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001681 // Fill in the structure
1682 std::vector<llvm::Constant*> Elements;
Owen Andersonade90fd2009-07-29 18:54:39 +00001683 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001684 Elements.push_back(SuperClass);
Chris Lattnerda35bc82008-06-26 04:47:04 +00001685 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001686 Elements.push_back(Zero);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001687 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
David Chisnall055f0642011-02-21 23:47:40 +00001688 if (isMeta) {
Micah Villmowdd31ca12012-10-08 16:25:52 +00001689 llvm::DataLayout td(&TheModule);
Ken Dyck0fed10e2011-04-22 17:59:22 +00001690 Elements.push_back(
1691 llvm::ConstantInt::get(LongTy,
1692 td.getTypeSizeInBits(ClassTy) /
1693 CGM.getContext().getCharWidth()));
David Chisnall055f0642011-02-21 23:47:40 +00001694 } else
1695 Elements.push_back(InstanceSize);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001696 Elements.push_back(IVars);
1697 Elements.push_back(Methods);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001698 Elements.push_back(NULLPtr);
1699 Elements.push_back(NULLPtr);
1700 Elements.push_back(NULLPtr);
Owen Andersonade90fd2009-07-29 18:54:39 +00001701 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001702 Elements.push_back(NULLPtr);
David Chisnallcdd207e2011-10-04 15:35:30 +00001703 Elements.push_back(llvm::ConstantInt::get(LongTy, 1));
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001704 Elements.push_back(IvarOffsets);
1705 Elements.push_back(Properties);
David Chisnallcdd207e2011-10-04 15:35:30 +00001706 Elements.push_back(StrongIvarBitmap);
1707 Elements.push_back(WeakIvarBitmap);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001708 // Create an instance of the structure
David Chisnalldf349172010-01-08 00:14:31 +00001709 // This is now an externally visible symbol, so that we can speed up class
David Chisnall207a6302012-01-04 12:02:13 +00001710 // messages in the next ABI. We may already have some weak references to
1711 // this, so check and fix them properly.
1712 std::string ClassSym((isMeta ? "_OBJC_METACLASS_": "_OBJC_CLASS_") +
1713 std::string(Name));
1714 llvm::GlobalVariable *ClassRef = TheModule.getNamedGlobal(ClassSym);
John McCall7f416cc2015-09-08 08:05:57 +00001715 llvm::Constant *Class =
1716 MakeGlobal(ClassTy, Elements, CGM.getPointerAlign(), ClassSym,
1717 llvm::GlobalValue::ExternalLinkage);
David Chisnall207a6302012-01-04 12:02:13 +00001718 if (ClassRef) {
1719 ClassRef->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(Class,
1720 ClassRef->getType()));
1721 ClassRef->removeFromParent();
1722 Class->setName(ClassSym);
1723 }
1724 return Class;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001725}
1726
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00001727llvm::Constant *CGObjCGNU::
1728GenerateProtocolMethodList(ArrayRef<llvm::Constant *> MethodNames,
1729 ArrayRef<llvm::Constant *> MethodTypes) {
Mike Stump11289f42009-09-09 15:08:12 +00001730 // Get the method structure type.
Chris Lattner845511f2011-06-18 22:49:11 +00001731 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001732 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
1733 PtrToInt8Ty,
Craig Topper8a13c412014-05-21 05:09:00 +00001734 nullptr);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001735 std::vector<llvm::Constant*> Methods;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001736 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
Benjamin Kramer30934732016-07-02 11:41:41 +00001737 Methods.push_back(llvm::ConstantStruct::get(
1738 ObjCMethodDescTy, {MethodNames[i], MethodTypes[i]}));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001739 }
Owen Anderson9793f0e2009-07-29 22:16:19 +00001740 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001741 MethodNames.size());
Owen Anderson47034e12009-07-28 18:33:04 +00001742 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy,
Mike Stumpdd93a192009-07-31 21:31:32 +00001743 Methods);
Chris Lattner845511f2011-06-18 22:49:11 +00001744 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
Craig Topper8a13c412014-05-21 05:09:00 +00001745 IntTy, ObjCMethodArrayTy, nullptr);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001746 Methods.clear();
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001747 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001748 Methods.push_back(Array);
John McCall7f416cc2015-09-08 08:05:57 +00001749 return MakeGlobal(ObjCMethodDescListTy, Methods, CGM.getPointerAlign(),
1750 ".objc_method_list");
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001751}
Mike Stumpdd93a192009-07-31 21:31:32 +00001752
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001753// Create the protocol list structure used in classes, categories and so on
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00001754llvm::Constant *CGObjCGNU::GenerateProtocolList(ArrayRef<std::string>Protocols){
Owen Anderson9793f0e2009-07-29 22:16:19 +00001755 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001756 Protocols.size());
Chris Lattner845511f2011-06-18 22:49:11 +00001757 llvm::StructType *ProtocolListTy = llvm::StructType::get(
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001758 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
David Chisnall168b80f2010-12-26 22:13:16 +00001759 SizeTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001760 ProtocolArrayTy,
Craig Topper8a13c412014-05-21 05:09:00 +00001761 nullptr);
Mike Stump11289f42009-09-09 15:08:12 +00001762 std::vector<llvm::Constant*> Elements;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001763 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
1764 iter != endIter ; iter++) {
Craig Topper8a13c412014-05-21 05:09:00 +00001765 llvm::Constant *protocol = nullptr;
David Chisnallbc8bdea2009-11-20 14:50:59 +00001766 llvm::StringMap<llvm::Constant*>::iterator value =
1767 ExistingProtocols.find(*iter);
1768 if (value == ExistingProtocols.end()) {
Fariborz Jahanian89d23972009-03-31 18:27:22 +00001769 protocol = GenerateEmptyProtocol(*iter);
David Chisnallbc8bdea2009-11-20 14:50:59 +00001770 } else {
1771 protocol = value->getValue();
1772 }
Owen Andersonade90fd2009-07-29 18:54:39 +00001773 llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(protocol,
Owen Anderson170229f2009-07-14 23:10:40 +00001774 PtrToInt8Ty);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001775 Elements.push_back(Ptr);
1776 }
Owen Anderson47034e12009-07-28 18:33:04 +00001777 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001778 Elements);
1779 Elements.clear();
1780 Elements.push_back(NULLPtr);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001781 Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001782 Elements.push_back(ProtocolArray);
John McCall7f416cc2015-09-08 08:05:57 +00001783 return MakeGlobal(ProtocolListTy, Elements, CGM.getPointerAlign(),
1784 ".objc_protocol_list");
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001785}
1786
John McCall882987f2013-02-28 19:01:20 +00001787llvm::Value *CGObjCGNU::GenerateProtocolRef(CodeGenFunction &CGF,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001788 const ObjCProtocolDecl *PD) {
Fariborz Jahanian89d23972009-03-31 18:27:22 +00001789 llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()];
Chris Lattner2192fe52011-07-18 04:24:23 +00001790 llvm::Type *T =
Fariborz Jahanian89d23972009-03-31 18:27:22 +00001791 CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
John McCall882987f2013-02-28 19:01:20 +00001792 return CGF.Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
Fariborz Jahanian89d23972009-03-31 18:27:22 +00001793}
1794
1795llvm::Constant *CGObjCGNU::GenerateEmptyProtocol(
1796 const std::string &ProtocolName) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001797 SmallVector<std::string, 0> EmptyStringVector;
1798 SmallVector<llvm::Constant*, 0> EmptyConstantVector;
Fariborz Jahanian89d23972009-03-31 18:27:22 +00001799
1800 llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001801 llvm::Constant *MethodList =
Fariborz Jahanian89d23972009-03-31 18:27:22 +00001802 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
1803 // Protocols are objects containing lists of the methods implemented and
1804 // protocols adopted.
Chris Lattner845511f2011-06-18 22:49:11 +00001805 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
Fariborz Jahanian89d23972009-03-31 18:27:22 +00001806 PtrToInt8Ty,
1807 ProtocolList->getType(),
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001808 MethodList->getType(),
1809 MethodList->getType(),
1810 MethodList->getType(),
1811 MethodList->getType(),
Craig Topper8a13c412014-05-21 05:09:00 +00001812 nullptr);
Fariborz Jahanian89d23972009-03-31 18:27:22 +00001813 // The isa pointer must be set to a magic number so the runtime knows it's
1814 // the correct layout.
Benjamin Kramer30934732016-07-02 11:41:41 +00001815 llvm::Constant *Elements[] = {
1816 llvm::ConstantExpr::getIntToPtr(
1817 llvm::ConstantInt::get(Int32Ty, ProtocolVersion), IdTy),
1818 MakeConstantString(ProtocolName, ".objc_protocol_name"), ProtocolList,
1819 MethodList, MethodList, MethodList, MethodList};
John McCall7f416cc2015-09-08 08:05:57 +00001820 return MakeGlobal(ProtocolTy, Elements, CGM.getPointerAlign(),
1821 ".objc_protocol");
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001822}
1823
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001824void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
1825 ASTContext &Context = CGM.getContext();
Chris Lattner86d7d912008-11-24 03:54:41 +00001826 std::string ProtocolName = PD->getNameAsString();
Douglas Gregora715bff2012-01-01 19:51:50 +00001827
1828 // Use the protocol definition, if there is one.
1829 if (const ObjCProtocolDecl *Def = PD->getDefinition())
1830 PD = Def;
1831
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001832 SmallVector<std::string, 16> Protocols;
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001833 for (const auto *PI : PD->protocols())
1834 Protocols.push_back(PI->getNameAsString());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001835 SmallVector<llvm::Constant*, 16> InstanceMethodNames;
1836 SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
1837 SmallVector<llvm::Constant*, 16> OptionalInstanceMethodNames;
1838 SmallVector<llvm::Constant*, 16> OptionalInstanceMethodTypes;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001839 for (const auto *I : PD->instance_methods()) {
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001840 std::string TypeStr;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001841 Context.getObjCEncodingForMethodDecl(I, TypeStr);
1842 if (I->getImplementationControl() == ObjCMethodDecl::Optional) {
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001843 OptionalInstanceMethodNames.push_back(
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001844 MakeConstantString(I->getSelector().getAsString()));
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001845 OptionalInstanceMethodTypes.push_back(MakeConstantString(TypeStr));
David Chisnall12d81352012-08-23 12:17:21 +00001846 } else {
1847 InstanceMethodNames.push_back(
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001848 MakeConstantString(I->getSelector().getAsString()));
David Chisnall12d81352012-08-23 12:17:21 +00001849 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001850 }
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001851 }
1852 // Collect information about class methods:
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001853 SmallVector<llvm::Constant*, 16> ClassMethodNames;
1854 SmallVector<llvm::Constant*, 16> ClassMethodTypes;
1855 SmallVector<llvm::Constant*, 16> OptionalClassMethodNames;
1856 SmallVector<llvm::Constant*, 16> OptionalClassMethodTypes;
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001857 for (const auto *I : PD->class_methods()) {
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001858 std::string TypeStr;
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001859 Context.getObjCEncodingForMethodDecl(I,TypeStr);
1860 if (I->getImplementationControl() == ObjCMethodDecl::Optional) {
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001861 OptionalClassMethodNames.push_back(
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001862 MakeConstantString(I->getSelector().getAsString()));
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001863 OptionalClassMethodTypes.push_back(MakeConstantString(TypeStr));
David Chisnall12d81352012-08-23 12:17:21 +00001864 } else {
1865 ClassMethodNames.push_back(
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001866 MakeConstantString(I->getSelector().getAsString()));
David Chisnall12d81352012-08-23 12:17:21 +00001867 ClassMethodTypes.push_back(MakeConstantString(TypeStr));
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001868 }
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001869 }
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001870
1871 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
1872 llvm::Constant *InstanceMethodList =
1873 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
1874 llvm::Constant *ClassMethodList =
1875 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001876 llvm::Constant *OptionalInstanceMethodList =
1877 GenerateProtocolMethodList(OptionalInstanceMethodNames,
1878 OptionalInstanceMethodTypes);
1879 llvm::Constant *OptionalClassMethodList =
1880 GenerateProtocolMethodList(OptionalClassMethodNames,
1881 OptionalClassMethodTypes);
1882
1883 // Property metadata: name, attributes, isSynthesized, setter name, setter
1884 // types, getter name, getter types.
1885 // The isSynthesized value is always set to 0 in a protocol. It exists to
1886 // simplify the runtime library by allowing it to use the same data
1887 // structures for protocol metadata everywhere.
Chris Lattner845511f2011-06-18 22:49:11 +00001888 llvm::StructType *PropertyMetadataTy = llvm::StructType::get(
David Chisnallbeb80132013-02-28 13:59:29 +00001889 PtrToInt8Ty, Int8Ty, Int8Ty, Int8Ty, Int8Ty, PtrToInt8Ty,
Craig Topper8a13c412014-05-21 05:09:00 +00001890 PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty, nullptr);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001891 std::vector<llvm::Constant*> Properties;
1892 std::vector<llvm::Constant*> OptionalProperties;
1893
1894 // Add all of the property methods need adding to the method list and to the
1895 // property metadata list.
Manman Rena7a8b1f2016-01-26 18:05:23 +00001896 for (auto *property : PD->instance_properties()) {
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001897 std::vector<llvm::Constant*> Fields;
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001898
Craig Topper8a13c412014-05-21 05:09:00 +00001899 Fields.push_back(MakePropertyEncodingString(property, nullptr));
David Chisnallbeb80132013-02-28 13:59:29 +00001900 PushPropertyAttributes(Fields, property);
David Chisnalla5f59412012-10-16 15:11:55 +00001901
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001902 if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
1903 std::string TypeStr;
1904 Context.getObjCEncodingForMethodDecl(getter,TypeStr);
1905 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
1906 InstanceMethodTypes.push_back(TypeEncoding);
1907 Fields.push_back(MakeConstantString(getter->getSelector().getAsString()));
1908 Fields.push_back(TypeEncoding);
1909 } else {
1910 Fields.push_back(NULLPtr);
1911 Fields.push_back(NULLPtr);
1912 }
1913 if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
1914 std::string TypeStr;
1915 Context.getObjCEncodingForMethodDecl(setter,TypeStr);
1916 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
1917 InstanceMethodTypes.push_back(TypeEncoding);
1918 Fields.push_back(MakeConstantString(setter->getSelector().getAsString()));
1919 Fields.push_back(TypeEncoding);
1920 } else {
1921 Fields.push_back(NULLPtr);
1922 Fields.push_back(NULLPtr);
1923 }
1924 if (property->getPropertyImplementation() == ObjCPropertyDecl::Optional) {
1925 OptionalProperties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1926 } else {
1927 Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1928 }
1929 }
1930 llvm::Constant *PropertyArray = llvm::ConstantArray::get(
1931 llvm::ArrayType::get(PropertyMetadataTy, Properties.size()), Properties);
1932 llvm::Constant* PropertyListInitFields[] =
1933 {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray};
1934
1935 llvm::Constant *PropertyListInit =
Chris Lattnere64d7ba2011-06-20 04:01:35 +00001936 llvm::ConstantStruct::getAnon(PropertyListInitFields);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001937 llvm::Constant *PropertyList = new llvm::GlobalVariable(TheModule,
1938 PropertyListInit->getType(), false, llvm::GlobalValue::InternalLinkage,
1939 PropertyListInit, ".objc_property_list");
1940
1941 llvm::Constant *OptionalPropertyArray =
1942 llvm::ConstantArray::get(llvm::ArrayType::get(PropertyMetadataTy,
1943 OptionalProperties.size()) , OptionalProperties);
1944 llvm::Constant* OptionalPropertyListInitFields[] = {
1945 llvm::ConstantInt::get(IntTy, OptionalProperties.size()), NULLPtr,
1946 OptionalPropertyArray };
1947
1948 llvm::Constant *OptionalPropertyListInit =
Chris Lattnere64d7ba2011-06-20 04:01:35 +00001949 llvm::ConstantStruct::getAnon(OptionalPropertyListInitFields);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001950 llvm::Constant *OptionalPropertyList = new llvm::GlobalVariable(TheModule,
1951 OptionalPropertyListInit->getType(), false,
1952 llvm::GlobalValue::InternalLinkage, OptionalPropertyListInit,
1953 ".objc_property_list");
1954
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001955 // Protocols are objects containing lists of the methods implemented and
1956 // protocols adopted.
Chris Lattner845511f2011-06-18 22:49:11 +00001957 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001958 PtrToInt8Ty,
1959 ProtocolList->getType(),
1960 InstanceMethodList->getType(),
1961 ClassMethodList->getType(),
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001962 OptionalInstanceMethodList->getType(),
1963 OptionalClassMethodList->getType(),
1964 PropertyList->getType(),
1965 OptionalPropertyList->getType(),
Craig Topper8a13c412014-05-21 05:09:00 +00001966 nullptr);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001967 // The isa pointer must be set to a magic number so the runtime knows it's
1968 // the correct layout.
Benjamin Kramer30934732016-07-02 11:41:41 +00001969 llvm::Constant *Elements[] = {
1970 llvm::ConstantExpr::getIntToPtr(
1971 llvm::ConstantInt::get(Int32Ty, ProtocolVersion), IdTy),
1972 MakeConstantString(ProtocolName, ".objc_protocol_name"), ProtocolList,
1973 InstanceMethodList, ClassMethodList, OptionalInstanceMethodList,
1974 OptionalClassMethodList, PropertyList, OptionalPropertyList};
Mike Stump11289f42009-09-09 15:08:12 +00001975 ExistingProtocols[ProtocolName] =
Owen Andersonade90fd2009-07-29 18:54:39 +00001976 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
John McCall7f416cc2015-09-08 08:05:57 +00001977 CGM.getPointerAlign(), ".objc_protocol"), IdTy);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00001978}
Dmitri Gribenkob2aa9232012-11-15 14:28:07 +00001979void CGObjCGNU::GenerateProtocolHolderCategory() {
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001980 // Collect information about instance methods
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001981 SmallVector<Selector, 1> MethodSels;
1982 SmallVector<llvm::Constant*, 1> MethodTypes;
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001983
1984 std::vector<llvm::Constant*> Elements;
1985 const std::string ClassName = "__ObjC_Protocol_Holder_Ugly_Hack";
1986 const std::string CategoryName = "AnotherHack";
1987 Elements.push_back(MakeConstantString(CategoryName));
1988 Elements.push_back(MakeConstantString(ClassName));
1989 // Instance method list
1990 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1991 ClassName, CategoryName, MethodSels, MethodTypes, false), PtrTy));
1992 // Class method list
1993 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1994 ClassName, CategoryName, MethodSels, MethodTypes, true), PtrTy));
1995 // Protocol list
1996 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrTy,
1997 ExistingProtocols.size());
Chris Lattner845511f2011-06-18 22:49:11 +00001998 llvm::StructType *ProtocolListTy = llvm::StructType::get(
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00001999 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
David Chisnall168b80f2010-12-26 22:13:16 +00002000 SizeTy,
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00002001 ProtocolArrayTy,
Craig Topper8a13c412014-05-21 05:09:00 +00002002 nullptr);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00002003 std::vector<llvm::Constant*> ProtocolElements;
2004 for (llvm::StringMapIterator<llvm::Constant*> iter =
2005 ExistingProtocols.begin(), endIter = ExistingProtocols.end();
2006 iter != endIter ; iter++) {
2007 llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(iter->getValue(),
2008 PtrTy);
2009 ProtocolElements.push_back(Ptr);
2010 }
2011 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
2012 ProtocolElements);
2013 ProtocolElements.clear();
2014 ProtocolElements.push_back(NULLPtr);
2015 ProtocolElements.push_back(llvm::ConstantInt::get(LongTy,
2016 ExistingProtocols.size()));
2017 ProtocolElements.push_back(ProtocolArray);
2018 Elements.push_back(llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolListTy,
John McCall7f416cc2015-09-08 08:05:57 +00002019 ProtocolElements, CGM.getPointerAlign(),
2020 ".objc_protocol_list"), PtrTy));
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00002021 Categories.push_back(llvm::ConstantExpr::getBitCast(
Chris Lattner845511f2011-06-18 22:49:11 +00002022 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty,
John McCall7f416cc2015-09-08 08:05:57 +00002023 PtrTy, PtrTy, PtrTy, nullptr), Elements, CGM.getPointerAlign()),
2024 PtrTy));
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00002025}
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002026
David Chisnallcdd207e2011-10-04 15:35:30 +00002027/// Libobjc2 uses a bitfield representation where small(ish) bitfields are
2028/// stored in a 64-bit value with the low bit set to 1 and the remaining 63
2029/// bits set to their values, LSB first, while larger ones are stored in a
2030/// structure of this / form:
2031///
2032/// struct { int32_t length; int32_t values[length]; };
2033///
2034/// The values in the array are stored in host-endian format, with the least
2035/// significant bit being assumed to come first in the bitfield. Therefore, a
2036/// bitfield with the 64th bit set will be (int64_t)&{ 2, [0, 1<<31] }, while a
2037/// bitfield / with the 63rd bit set will be 1<<64.
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00002038llvm::Constant *CGObjCGNU::MakeBitField(ArrayRef<bool> bits) {
David Chisnallcdd207e2011-10-04 15:35:30 +00002039 int bitCount = bits.size();
Rafael Espindola3cc5c2d2014-01-09 21:32:51 +00002040 int ptrBits = CGM.getDataLayout().getPointerSizeInBits();
David Chisnalle89ac062011-10-25 10:12:21 +00002041 if (bitCount < ptrBits) {
David Chisnallcdd207e2011-10-04 15:35:30 +00002042 uint64_t val = 1;
2043 for (int i=0 ; i<bitCount ; ++i) {
Eli Friedman23526672011-10-08 01:03:47 +00002044 if (bits[i]) val |= 1ULL<<(i+1);
David Chisnallcdd207e2011-10-04 15:35:30 +00002045 }
David Chisnalle89ac062011-10-25 10:12:21 +00002046 return llvm::ConstantInt::get(IntPtrTy, val);
David Chisnallcdd207e2011-10-04 15:35:30 +00002047 }
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002048 SmallVector<llvm::Constant *, 8> values;
David Chisnallcdd207e2011-10-04 15:35:30 +00002049 int v=0;
2050 while (v < bitCount) {
2051 int32_t word = 0;
2052 for (int i=0 ; (i<32) && (v<bitCount) ; ++i) {
2053 if (bits[v]) word |= 1<<i;
2054 v++;
2055 }
2056 values.push_back(llvm::ConstantInt::get(Int32Ty, word));
2057 }
2058 llvm::ArrayType *arrayTy = llvm::ArrayType::get(Int32Ty, values.size());
2059 llvm::Constant *array = llvm::ConstantArray::get(arrayTy, values);
2060 llvm::Constant *fields[2] = {
2061 llvm::ConstantInt::get(Int32Ty, values.size()),
2062 array };
2063 llvm::Constant *GS = MakeGlobal(llvm::StructType::get(Int32Ty, arrayTy,
John McCall7f416cc2015-09-08 08:05:57 +00002064 nullptr), fields, CharUnits::fromQuantity(4));
David Chisnalle0dc7cb2011-10-08 08:54:36 +00002065 llvm::Constant *ptr = llvm::ConstantExpr::getPtrToInt(GS, IntPtrTy);
David Chisnalle0dc7cb2011-10-08 08:54:36 +00002066 return ptr;
David Chisnallcdd207e2011-10-04 15:35:30 +00002067}
2068
Daniel Dunbar92992502008-08-15 22:20:32 +00002069void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Chris Lattner86d7d912008-11-24 03:54:41 +00002070 std::string ClassName = OCD->getClassInterface()->getNameAsString();
2071 std::string CategoryName = OCD->getNameAsString();
Daniel Dunbar92992502008-08-15 22:20:32 +00002072 // Collect information about instance methods
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002073 SmallVector<Selector, 16> InstanceMethodSels;
2074 SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00002075 for (const auto *I : OCD->instance_methods()) {
2076 InstanceMethodSels.push_back(I->getSelector());
Daniel Dunbar92992502008-08-15 22:20:32 +00002077 std::string TypeStr;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00002078 CGM.getContext().getObjCEncodingForMethodDecl(I,TypeStr);
David Chisnall5778fce2009-08-31 16:41:57 +00002079 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar92992502008-08-15 22:20:32 +00002080 }
2081
2082 // Collect information about class methods
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002083 SmallVector<Selector, 16> ClassMethodSels;
2084 SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00002085 for (const auto *I : OCD->class_methods()) {
2086 ClassMethodSels.push_back(I->getSelector());
Daniel Dunbar92992502008-08-15 22:20:32 +00002087 std::string TypeStr;
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00002088 CGM.getContext().getObjCEncodingForMethodDecl(I,TypeStr);
David Chisnall5778fce2009-08-31 16:41:57 +00002089 ClassMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar92992502008-08-15 22:20:32 +00002090 }
2091
2092 // Collect the names of referenced protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002093 SmallVector<std::string, 16> Protocols;
David Chisnall2bfc50b2010-03-13 22:20:45 +00002094 const ObjCCategoryDecl *CatDecl = OCD->getCategoryDecl();
2095 const ObjCList<ObjCProtocolDecl> &Protos = CatDecl->getReferencedProtocols();
Daniel Dunbar92992502008-08-15 22:20:32 +00002096 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
2097 E = Protos.end(); I != E; ++I)
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002098 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar92992502008-08-15 22:20:32 +00002099
Benjamin Kramer30934732016-07-02 11:41:41 +00002100 llvm::Constant *Elements[] = {
2101 MakeConstantString(CategoryName), MakeConstantString(ClassName),
2102 // Instance method list
2103 llvm::ConstantExpr::getBitCast(
2104 GenerateMethodList(ClassName, CategoryName, InstanceMethodSels,
2105 InstanceMethodTypes, false),
2106 PtrTy),
2107 // Class method list
2108 llvm::ConstantExpr::getBitCast(GenerateMethodList(ClassName, CategoryName,
2109 ClassMethodSels,
2110 ClassMethodTypes, true),
2111 PtrTy),
2112 // Protocol list
2113 llvm::ConstantExpr::getBitCast(GenerateProtocolList(Protocols), PtrTy)};
Owen Andersonade90fd2009-07-29 18:54:39 +00002114 Categories.push_back(llvm::ConstantExpr::getBitCast(
Chris Lattner845511f2011-06-18 22:49:11 +00002115 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty,
John McCall7f416cc2015-09-08 08:05:57 +00002116 PtrTy, PtrTy, PtrTy, nullptr), Elements, CGM.getPointerAlign()),
2117 PtrTy));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002118}
Daniel Dunbar92992502008-08-15 22:20:32 +00002119
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00002120llvm::Constant *CGObjCGNU::GeneratePropertyList(const ObjCImplementationDecl *OID,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002121 SmallVectorImpl<Selector> &InstanceMethodSels,
2122 SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes) {
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00002123 ASTContext &Context = CGM.getContext();
David Chisnallbeb80132013-02-28 13:59:29 +00002124 // Property metadata: name, attributes, attributes2, padding1, padding2,
2125 // setter name, setter types, getter name, getter types.
Chris Lattner845511f2011-06-18 22:49:11 +00002126 llvm::StructType *PropertyMetadataTy = llvm::StructType::get(
David Chisnallbeb80132013-02-28 13:59:29 +00002127 PtrToInt8Ty, Int8Ty, Int8Ty, Int8Ty, Int8Ty, PtrToInt8Ty,
Craig Topper8a13c412014-05-21 05:09:00 +00002128 PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty, nullptr);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00002129 std::vector<llvm::Constant*> Properties;
2130
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00002131 // Add all of the property methods need adding to the method list and to the
2132 // property metadata list.
Aaron Ballmand85eff42014-03-14 15:02:45 +00002133 for (auto *propertyImpl : OID->property_impls()) {
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00002134 std::vector<llvm::Constant*> Fields;
Aaron Ballmand85eff42014-03-14 15:02:45 +00002135 ObjCPropertyDecl *property = propertyImpl->getPropertyDecl();
David Chisnall36c63202010-02-26 01:11:38 +00002136 bool isSynthesized = (propertyImpl->getPropertyImplementation() ==
2137 ObjCPropertyImplDecl::Synthesize);
David Chisnallbeb80132013-02-28 13:59:29 +00002138 bool isDynamic = (propertyImpl->getPropertyImplementation() ==
2139 ObjCPropertyImplDecl::Dynamic);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00002140
David Chisnalla5f59412012-10-16 15:11:55 +00002141 Fields.push_back(MakePropertyEncodingString(property, OID));
David Chisnallbeb80132013-02-28 13:59:29 +00002142 PushPropertyAttributes(Fields, property, isSynthesized, isDynamic);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00002143 if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00002144 std::string TypeStr;
2145 Context.getObjCEncodingForMethodDecl(getter,TypeStr);
2146 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
David Chisnall36c63202010-02-26 01:11:38 +00002147 if (isSynthesized) {
2148 InstanceMethodTypes.push_back(TypeEncoding);
2149 InstanceMethodSels.push_back(getter->getSelector());
2150 }
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00002151 Fields.push_back(MakeConstantString(getter->getSelector().getAsString()));
2152 Fields.push_back(TypeEncoding);
2153 } else {
2154 Fields.push_back(NULLPtr);
2155 Fields.push_back(NULLPtr);
2156 }
2157 if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00002158 std::string TypeStr;
2159 Context.getObjCEncodingForMethodDecl(setter,TypeStr);
2160 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
David Chisnall36c63202010-02-26 01:11:38 +00002161 if (isSynthesized) {
2162 InstanceMethodTypes.push_back(TypeEncoding);
2163 InstanceMethodSels.push_back(setter->getSelector());
2164 }
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00002165 Fields.push_back(MakeConstantString(setter->getSelector().getAsString()));
2166 Fields.push_back(TypeEncoding);
2167 } else {
2168 Fields.push_back(NULLPtr);
2169 Fields.push_back(NULLPtr);
2170 }
2171 Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
2172 }
2173 llvm::ArrayType *PropertyArrayTy =
2174 llvm::ArrayType::get(PropertyMetadataTy, Properties.size());
2175 llvm::Constant *PropertyArray = llvm::ConstantArray::get(PropertyArrayTy,
2176 Properties);
2177 llvm::Constant* PropertyListInitFields[] =
2178 {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray};
2179
2180 llvm::Constant *PropertyListInit =
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002181 llvm::ConstantStruct::getAnon(PropertyListInitFields);
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00002182 return new llvm::GlobalVariable(TheModule, PropertyListInit->getType(), false,
2183 llvm::GlobalValue::InternalLinkage, PropertyListInit,
2184 ".objc_property_list");
2185}
2186
David Chisnall92d436b2012-01-31 18:59:20 +00002187void CGObjCGNU::RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {
2188 // Get the class declaration for which the alias is specified.
2189 ObjCInterfaceDecl *ClassDecl =
2190 const_cast<ObjCInterfaceDecl *>(OAD->getClassInterface());
Benjamin Kramer3204b152015-05-29 19:42:19 +00002191 ClassAliases.emplace_back(ClassDecl->getNameAsString(),
2192 OAD->getNameAsString());
David Chisnall92d436b2012-01-31 18:59:20 +00002193}
2194
Daniel Dunbar92992502008-08-15 22:20:32 +00002195void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
2196 ASTContext &Context = CGM.getContext();
2197
2198 // Get the superclass name.
Mike Stump11289f42009-09-09 15:08:12 +00002199 const ObjCInterfaceDecl * SuperClassDecl =
Daniel Dunbar92992502008-08-15 22:20:32 +00002200 OID->getClassInterface()->getSuperClass();
Chris Lattner86d7d912008-11-24 03:54:41 +00002201 std::string SuperClassName;
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +00002202 if (SuperClassDecl) {
Chris Lattner86d7d912008-11-24 03:54:41 +00002203 SuperClassName = SuperClassDecl->getNameAsString();
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +00002204 EmitClassRef(SuperClassName);
2205 }
Daniel Dunbar92992502008-08-15 22:20:32 +00002206
2207 // Get the class name
Chris Lattner87bc3872009-04-01 02:00:48 +00002208 ObjCInterfaceDecl *ClassDecl =
Saleem Abdulrasoola088ad92016-07-17 22:27:41 +00002209 const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
Chris Lattner86d7d912008-11-24 03:54:41 +00002210 std::string ClassName = ClassDecl->getNameAsString();
Saleem Abdulrasoola088ad92016-07-17 22:27:41 +00002211
Chris Lattnerc7d2bfa2009-06-15 01:09:11 +00002212 // Emit the symbol that is used to generate linker errors if this class is
2213 // referenced in other modules but not declared.
Fariborz Jahanianbacbed92009-07-03 15:10:14 +00002214 std::string classSymbolName = "__objc_class_name_" + ClassName;
Saleem Abdulrasoola088ad92016-07-17 22:27:41 +00002215 if (auto *symbol = TheModule.getGlobalVariable(classSymbolName)) {
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002216 symbol->setInitializer(llvm::ConstantInt::get(LongTy, 0));
Fariborz Jahanianbacbed92009-07-03 15:10:14 +00002217 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002218 new llvm::GlobalVariable(TheModule, LongTy, false,
Saleem Abdulrasoola088ad92016-07-17 22:27:41 +00002219 llvm::GlobalValue::ExternalLinkage,
2220 llvm::ConstantInt::get(LongTy, 0),
2221 classSymbolName);
Fariborz Jahanianbacbed92009-07-03 15:10:14 +00002222 }
Mike Stump11289f42009-09-09 15:08:12 +00002223
Daniel Dunbar12119b92009-05-03 10:46:44 +00002224 // Get the size of instances.
Ken Dyckc8ae5502011-02-09 01:59:34 +00002225 int instanceSize =
2226 Context.getASTObjCImplementationLayout(OID).getSize().getQuantity();
Daniel Dunbar92992502008-08-15 22:20:32 +00002227
2228 // Collect information about instance variables.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002229 SmallVector<llvm::Constant*, 16> IvarNames;
2230 SmallVector<llvm::Constant*, 16> IvarTypes;
2231 SmallVector<llvm::Constant*, 16> IvarOffsets;
Mike Stump11289f42009-09-09 15:08:12 +00002232
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00002233 std::vector<llvm::Constant*> IvarOffsetValues;
David Chisnallcdd207e2011-10-04 15:35:30 +00002234 SmallVector<bool, 16> WeakIvars;
2235 SmallVector<bool, 16> StrongIvars;
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00002236
Mike Stump11289f42009-09-09 15:08:12 +00002237 int superInstanceSize = !SuperClassDecl ? 0 :
Ken Dyckc8ae5502011-02-09 01:59:34 +00002238 Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize().getQuantity();
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002239 // For non-fragile ivars, set the instance size to 0 - {the size of just this
2240 // class}. The runtime will then set this to the correct value on load.
Richard Smith9c6890a2012-11-01 22:30:59 +00002241 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002242 instanceSize = 0 - (instanceSize - superInstanceSize);
2243 }
David Chisnall18cf7372010-04-19 00:45:34 +00002244
Jordy Rosea91768e2011-07-22 02:08:32 +00002245 for (const ObjCIvarDecl *IVD = ClassDecl->all_declared_ivar_begin(); IVD;
2246 IVD = IVD->getNextIvar()) {
Daniel Dunbar92992502008-08-15 22:20:32 +00002247 // Store the name
David Chisnall18cf7372010-04-19 00:45:34 +00002248 IvarNames.push_back(MakeConstantString(IVD->getNameAsString()));
Daniel Dunbar92992502008-08-15 22:20:32 +00002249 // Get the type encoding for this ivar
2250 std::string TypeStr;
David Chisnall18cf7372010-04-19 00:45:34 +00002251 Context.getObjCEncodingForType(IVD->getType(), TypeStr);
David Chisnall5778fce2009-08-31 16:41:57 +00002252 IvarTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar92992502008-08-15 22:20:32 +00002253 // Get the offset
Eli Friedman8cbca202012-11-06 22:15:52 +00002254 uint64_t BaseOffset = ComputeIvarBaseOffset(CGM, OID, IVD);
David Chisnallcb1b7bf2009-11-17 19:32:15 +00002255 uint64_t Offset = BaseOffset;
Richard Smith9c6890a2012-11-01 22:30:59 +00002256 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00002257 Offset = BaseOffset - superInstanceSize;
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002258 }
David Chisnall1bfe6d32011-07-07 12:34:51 +00002259 llvm::Constant *OffsetValue = llvm::ConstantInt::get(IntTy, Offset);
2260 // Create the direct offset value
2261 std::string OffsetName = "__objc_ivar_offset_value_" + ClassName +"." +
2262 IVD->getNameAsString();
2263 llvm::GlobalVariable *OffsetVar = TheModule.getGlobalVariable(OffsetName);
2264 if (OffsetVar) {
2265 OffsetVar->setInitializer(OffsetValue);
2266 // If this is the real definition, change its linkage type so that
2267 // different modules will use this one, rather than their private
2268 // copy.
2269 OffsetVar->setLinkage(llvm::GlobalValue::ExternalLinkage);
2270 } else
2271 OffsetVar = new llvm::GlobalVariable(TheModule, IntTy,
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00002272 false, llvm::GlobalValue::ExternalLinkage,
David Chisnall1bfe6d32011-07-07 12:34:51 +00002273 OffsetValue,
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00002274 "__objc_ivar_offset_value_" + ClassName +"." +
David Chisnall1bfe6d32011-07-07 12:34:51 +00002275 IVD->getNameAsString());
2276 IvarOffsets.push_back(OffsetValue);
2277 IvarOffsetValues.push_back(OffsetVar);
David Chisnallcdd207e2011-10-04 15:35:30 +00002278 Qualifiers::ObjCLifetime lt = IVD->getType().getQualifiers().getObjCLifetime();
2279 switch (lt) {
2280 case Qualifiers::OCL_Strong:
2281 StrongIvars.push_back(true);
2282 WeakIvars.push_back(false);
2283 break;
2284 case Qualifiers::OCL_Weak:
2285 StrongIvars.push_back(false);
2286 WeakIvars.push_back(true);
2287 break;
2288 default:
2289 StrongIvars.push_back(false);
2290 WeakIvars.push_back(false);
2291 }
Daniel Dunbar92992502008-08-15 22:20:32 +00002292 }
David Chisnallcdd207e2011-10-04 15:35:30 +00002293 llvm::Constant *StrongIvarBitmap = MakeBitField(StrongIvars);
2294 llvm::Constant *WeakIvarBitmap = MakeBitField(WeakIvars);
David Chisnalld7972f52011-03-23 16:36:54 +00002295 llvm::GlobalVariable *IvarOffsetArray =
John McCall7f416cc2015-09-08 08:05:57 +00002296 MakeGlobalArray(PtrToIntTy, IvarOffsetValues, CGM.getPointerAlign(),
2297 ".ivar.offsets");
David Chisnalld7972f52011-03-23 16:36:54 +00002298
Daniel Dunbar92992502008-08-15 22:20:32 +00002299 // Collect information about instance methods
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002300 SmallVector<Selector, 16> InstanceMethodSels;
2301 SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00002302 for (const auto *I : OID->instance_methods()) {
2303 InstanceMethodSels.push_back(I->getSelector());
Daniel Dunbar92992502008-08-15 22:20:32 +00002304 std::string TypeStr;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00002305 Context.getObjCEncodingForMethodDecl(I,TypeStr);
David Chisnall5778fce2009-08-31 16:41:57 +00002306 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar92992502008-08-15 22:20:32 +00002307 }
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00002308
2309 llvm::Constant *Properties = GeneratePropertyList(OID, InstanceMethodSels,
2310 InstanceMethodTypes);
2311
Daniel Dunbar92992502008-08-15 22:20:32 +00002312 // Collect information about class methods
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002313 SmallVector<Selector, 16> ClassMethodSels;
2314 SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00002315 for (const auto *I : OID->class_methods()) {
2316 ClassMethodSels.push_back(I->getSelector());
Daniel Dunbar92992502008-08-15 22:20:32 +00002317 std::string TypeStr;
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00002318 Context.getObjCEncodingForMethodDecl(I,TypeStr);
David Chisnall5778fce2009-08-31 16:41:57 +00002319 ClassMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar92992502008-08-15 22:20:32 +00002320 }
2321 // Collect the names of referenced protocols
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002322 SmallVector<std::string, 16> Protocols;
Aaron Ballmana49c5062014-03-13 20:29:09 +00002323 for (const auto *I : ClassDecl->protocols())
2324 Protocols.push_back(I->getNameAsString());
Daniel Dunbar92992502008-08-15 22:20:32 +00002325
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002326 // Get the superclass pointer.
2327 llvm::Constant *SuperClass;
Chris Lattner86d7d912008-11-24 03:54:41 +00002328 if (!SuperClassName.empty()) {
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002329 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
2330 } else {
Owen Anderson7ec07a52009-07-30 23:11:26 +00002331 SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002332 }
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002333 // Empty vector used to construct empty method lists
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002334 SmallVector<llvm::Constant*, 1> empty;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002335 // Generate the method and instance variable lists
2336 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnerbf231a62008-06-26 05:08:00 +00002337 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002338 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnerbf231a62008-06-26 05:08:00 +00002339 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002340 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
2341 IvarOffsets);
Mike Stump11289f42009-09-09 15:08:12 +00002342 // Irrespective of whether we are compiling for a fragile or non-fragile ABI,
David Chisnall5778fce2009-08-31 16:41:57 +00002343 // we emit a symbol containing the offset for each ivar in the class. This
2344 // allows code compiled for the non-Fragile ABI to inherit from code compiled
2345 // for the legacy ABI, without causing problems. The converse is also
2346 // possible, but causes all ivar accesses to be fragile.
David Chisnalle8431a72010-11-03 16:12:44 +00002347
David Chisnall5778fce2009-08-31 16:41:57 +00002348 // Offset pointer for getting at the correct field in the ivar list when
2349 // setting up the alias. These are: The base address for the global, the
2350 // ivar array (second field), the ivar in this list (set for each ivar), and
2351 // the offset (third field in ivar structure)
David Chisnallcdd207e2011-10-04 15:35:30 +00002352 llvm::Type *IndexTy = Int32Ty;
David Chisnall5778fce2009-08-31 16:41:57 +00002353 llvm::Constant *offsetPointerIndexes[] = {Zeros[0],
Craig Topper8a13c412014-05-21 05:09:00 +00002354 llvm::ConstantInt::get(IndexTy, 1), nullptr,
David Chisnall5778fce2009-08-31 16:41:57 +00002355 llvm::ConstantInt::get(IndexTy, 2) };
2356
Jordy Rosea91768e2011-07-22 02:08:32 +00002357 unsigned ivarIndex = 0;
2358 for (const ObjCIvarDecl *IVD = ClassDecl->all_declared_ivar_begin(); IVD;
2359 IVD = IVD->getNextIvar()) {
David Chisnall5778fce2009-08-31 16:41:57 +00002360 const std::string Name = "__objc_ivar_offset_" + ClassName + '.'
David Chisnalle8431a72010-11-03 16:12:44 +00002361 + IVD->getNameAsString();
Jordy Rosea91768e2011-07-22 02:08:32 +00002362 offsetPointerIndexes[2] = llvm::ConstantInt::get(IndexTy, ivarIndex);
David Chisnall5778fce2009-08-31 16:41:57 +00002363 // Get the correct ivar field
2364 llvm::Constant *offsetValue = llvm::ConstantExpr::getGetElementPtr(
David Blaikiee3b172a2015-04-02 18:55:21 +00002365 cast<llvm::GlobalVariable>(IvarList)->getValueType(), IvarList,
2366 offsetPointerIndexes);
David Chisnalle8431a72010-11-03 16:12:44 +00002367 // Get the existing variable, if one exists.
David Chisnall5778fce2009-08-31 16:41:57 +00002368 llvm::GlobalVariable *offset = TheModule.getNamedGlobal(Name);
2369 if (offset) {
Ted Kremenek669669f2012-04-04 00:55:25 +00002370 offset->setInitializer(offsetValue);
2371 // If this is the real definition, change its linkage type so that
2372 // different modules will use this one, rather than their private
2373 // copy.
2374 offset->setLinkage(llvm::GlobalValue::ExternalLinkage);
David Chisnall5778fce2009-08-31 16:41:57 +00002375 } else {
Ted Kremenek669669f2012-04-04 00:55:25 +00002376 // Add a new alias if there isn't one already.
2377 offset = new llvm::GlobalVariable(TheModule, offsetValue->getType(),
2378 false, llvm::GlobalValue::ExternalLinkage, offsetValue, Name);
2379 (void) offset; // Silence dead store warning.
David Chisnall5778fce2009-08-31 16:41:57 +00002380 }
Jordy Rosea91768e2011-07-22 02:08:32 +00002381 ++ivarIndex;
David Chisnall5778fce2009-08-31 16:41:57 +00002382 }
David Chisnalle89ac062011-10-25 10:12:21 +00002383 llvm::Constant *ZeroPtr = llvm::ConstantInt::get(IntPtrTy, 0);
Saleem Abdulrasoola088ad92016-07-17 22:27:41 +00002384
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002385 //Generate metaclass for class methods
Saleem Abdulrasoola088ad92016-07-17 22:27:41 +00002386 llvm::Constant *MetaClassStruct = GenerateClassStructure(
2387 NULLPtr, NULLPtr, 0x12L, ClassName.c_str(), nullptr, Zeros[0],
2388 GenerateIvarList(empty, empty, empty), ClassMethodList, NULLPtr, NULLPtr,
2389 NULLPtr, ZeroPtr, ZeroPtr, true);
Saleem Abdulrasool7093e212016-07-17 22:27:44 +00002390 if (CGM.getTriple().isOSBinFormatCOFF()) {
2391 auto Storage = llvm::GlobalValue::DefaultStorageClass;
2392 if (OID->getClassInterface()->hasAttr<DLLImportAttr>())
2393 Storage = llvm::GlobalValue::DLLImportStorageClass;
2394 else if (OID->getClassInterface()->hasAttr<DLLExportAttr>())
2395 Storage = llvm::GlobalValue::DLLExportStorageClass;
2396 cast<llvm::GlobalValue>(MetaClassStruct)->setDLLStorageClass(Storage);
2397 }
Daniel Dunbar566421c2009-05-04 15:31:17 +00002398
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002399 // Generate the class structure
Saleem Abdulrasoola088ad92016-07-17 22:27:41 +00002400 llvm::Constant *ClassStruct = GenerateClassStructure(
2401 MetaClassStruct, SuperClass, 0x11L, ClassName.c_str(), nullptr,
2402 llvm::ConstantInt::get(LongTy, instanceSize), IvarList, MethodList,
2403 GenerateProtocolList(Protocols), IvarOffsetArray, Properties,
2404 StrongIvarBitmap, WeakIvarBitmap);
Saleem Abdulrasool7093e212016-07-17 22:27:44 +00002405 if (CGM.getTriple().isOSBinFormatCOFF()) {
2406 auto Storage = llvm::GlobalValue::DefaultStorageClass;
2407 if (OID->getClassInterface()->hasAttr<DLLImportAttr>())
2408 Storage = llvm::GlobalValue::DLLImportStorageClass;
2409 else if (OID->getClassInterface()->hasAttr<DLLExportAttr>())
2410 Storage = llvm::GlobalValue::DLLExportStorageClass;
2411 cast<llvm::GlobalValue>(ClassStruct)->setDLLStorageClass(Storage);
2412 }
Daniel Dunbar566421c2009-05-04 15:31:17 +00002413
2414 // Resolve the class aliases, if they exist.
2415 if (ClassPtrAlias) {
David Chisnall82f755c2010-11-09 11:21:43 +00002416 ClassPtrAlias->replaceAllUsesWith(
Owen Andersonade90fd2009-07-29 18:54:39 +00002417 llvm::ConstantExpr::getBitCast(ClassStruct, IdTy));
David Chisnall82f755c2010-11-09 11:21:43 +00002418 ClassPtrAlias->eraseFromParent();
Craig Topper8a13c412014-05-21 05:09:00 +00002419 ClassPtrAlias = nullptr;
Daniel Dunbar566421c2009-05-04 15:31:17 +00002420 }
2421 if (MetaClassPtrAlias) {
David Chisnall82f755c2010-11-09 11:21:43 +00002422 MetaClassPtrAlias->replaceAllUsesWith(
Owen Andersonade90fd2009-07-29 18:54:39 +00002423 llvm::ConstantExpr::getBitCast(MetaClassStruct, IdTy));
David Chisnall82f755c2010-11-09 11:21:43 +00002424 MetaClassPtrAlias->eraseFromParent();
Craig Topper8a13c412014-05-21 05:09:00 +00002425 MetaClassPtrAlias = nullptr;
Daniel Dunbar566421c2009-05-04 15:31:17 +00002426 }
2427
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002428 // Add class structure to list to be added to the symtab later
Owen Andersonade90fd2009-07-29 18:54:39 +00002429 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002430 Classes.push_back(ClassStruct);
2431}
2432
Mike Stump11289f42009-09-09 15:08:12 +00002433llvm::Function *CGObjCGNU::ModuleInitFunction() {
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002434 // Only emit an ObjC load function if no Objective-C stuff has been called
2435 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
David Chisnalld7972f52011-03-23 16:36:54 +00002436 ExistingProtocols.empty() && SelectorTable.empty())
Craig Topper8a13c412014-05-21 05:09:00 +00002437 return nullptr;
Eli Friedman412c6682008-06-01 16:00:02 +00002438
Fariborz Jahanian2cde2032009-09-10 21:48:21 +00002439 // Add all referenced protocols to a category.
2440 GenerateProtocolHolderCategory();
2441
Chris Lattner2192fe52011-07-18 04:24:23 +00002442 llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
Chris Lattner8d3f4a42009-01-27 05:06:01 +00002443 SelectorTy->getElementType());
Jay Foad7c57be32011-07-11 09:56:20 +00002444 llvm::Type *SelStructPtrTy = SelectorTy;
Craig Topper8a13c412014-05-21 05:09:00 +00002445 if (!SelStructTy) {
2446 SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, nullptr);
Owen Anderson9793f0e2009-07-29 22:16:19 +00002447 SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
Chris Lattner8d3f4a42009-01-27 05:06:01 +00002448 }
2449
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002450 std::vector<llvm::Constant*> Elements;
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00002451 llvm::Constant *Statics = NULLPtr;
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002452 // Generate statics list:
Alexander Kornienko6ee521c2015-01-23 15:36:10 +00002453 if (!ConstantStrings.empty()) {
Owen Anderson9793f0e2009-07-29 22:16:19 +00002454 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00002455 ConstantStrings.size() + 1);
2456 ConstantStrings.push_back(NULLPtr);
David Chisnall5778fce2009-08-31 16:41:57 +00002457
David Blaikiebbafb8a2012-03-11 07:00:24 +00002458 StringRef StringClass = CGM.getLangOpts().ObjCConstantStringClass;
David Chisnalld7972f52011-03-23 16:36:54 +00002459
Daniel Dunbar75fa84e2009-11-29 02:38:47 +00002460 if (StringClass.empty()) StringClass = "NXConstantString";
David Chisnalld7972f52011-03-23 16:36:54 +00002461
David Chisnall5778fce2009-08-31 16:41:57 +00002462 Elements.push_back(MakeConstantString(StringClass,
2463 ".objc_static_class_name"));
Owen Anderson47034e12009-07-28 18:33:04 +00002464 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy,
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00002465 ConstantStrings));
Mike Stump11289f42009-09-09 15:08:12 +00002466 llvm::StructType *StaticsListTy =
Craig Topper8a13c412014-05-21 05:09:00 +00002467 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, nullptr);
Owen Anderson170229f2009-07-14 23:10:40 +00002468 llvm::Type *StaticsListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00002469 llvm::PointerType::getUnqual(StaticsListTy);
John McCall7f416cc2015-09-08 08:05:57 +00002470 Statics = MakeGlobal(StaticsListTy, Elements, CGM.getPointerAlign(),
2471 ".objc_statics");
Mike Stump11289f42009-09-09 15:08:12 +00002472 llvm::ArrayType *StaticsListArrayTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00002473 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00002474 Elements.clear();
2475 Elements.push_back(Statics);
Owen Anderson0b75f232009-07-31 20:28:54 +00002476 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
John McCall7f416cc2015-09-08 08:05:57 +00002477 Statics = MakeGlobal(StaticsListArrayTy, Elements,
2478 CGM.getPointerAlign(), ".objc_statics_ptr");
Owen Andersonade90fd2009-07-29 18:54:39 +00002479 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
Chris Lattnerc06ce0f2009-04-25 23:19:45 +00002480 }
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002481 // Array of classes, categories, and constant objects
Owen Anderson9793f0e2009-07-29 22:16:19 +00002482 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002483 Classes.size() + Categories.size() + 2);
Chris Lattner845511f2011-06-18 22:49:11 +00002484 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy,
Owen Anderson41a75022009-08-13 21:57:51 +00002485 llvm::Type::getInt16Ty(VMContext),
2486 llvm::Type::getInt16Ty(VMContext),
Craig Topper8a13c412014-05-21 05:09:00 +00002487 ClassListTy, nullptr);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002488
2489 Elements.clear();
2490 // Pointer to an array of selectors used in this module.
2491 std::vector<llvm::Constant*> Selectors;
David Chisnalld7972f52011-03-23 16:36:54 +00002492 std::vector<llvm::GlobalAlias*> SelectorAliases;
2493 for (SelectorMap::iterator iter = SelectorTable.begin(),
2494 iterEnd = SelectorTable.end(); iter != iterEnd ; ++iter) {
2495
2496 std::string SelNameStr = iter->first.getAsString();
2497 llvm::Constant *SelName = ExportUniqueString(SelNameStr, ".objc_sel_name");
2498
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002499 SmallVectorImpl<TypedSelector> &Types = iter->second;
2500 for (SmallVectorImpl<TypedSelector>::iterator i = Types.begin(),
David Chisnalld7972f52011-03-23 16:36:54 +00002501 e = Types.end() ; i!=e ; i++) {
2502
2503 llvm::Constant *SelectorTypeEncoding = NULLPtr;
2504 if (!i->first.empty())
2505 SelectorTypeEncoding = MakeConstantString(i->first, ".objc_sel_types");
2506
2507 Elements.push_back(SelName);
2508 Elements.push_back(SelectorTypeEncoding);
2509 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
2510 Elements.clear();
2511
2512 // Store the selector alias for later replacement
2513 SelectorAliases.push_back(i->second);
2514 }
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002515 }
David Chisnalld7972f52011-03-23 16:36:54 +00002516 unsigned SelectorCount = Selectors.size();
2517 // NULL-terminate the selector list. This should not actually be required,
2518 // because the selector list has a length field. Unfortunately, the GCC
2519 // runtime decides to ignore the length field and expects a NULL terminator,
2520 // and GCC cooperates with this by always setting the length to 0.
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002521 Elements.push_back(NULLPtr);
2522 Elements.push_back(NULLPtr);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002523 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002524 Elements.clear();
David Chisnalld7972f52011-03-23 16:36:54 +00002525
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002526 // Number of static selectors
David Chisnalld7972f52011-03-23 16:36:54 +00002527 Elements.push_back(llvm::ConstantInt::get(LongTy, SelectorCount));
David Blaikiee3b172a2015-04-02 18:55:21 +00002528 llvm::GlobalVariable *SelectorList =
John McCall7f416cc2015-09-08 08:05:57 +00002529 MakeGlobalArray(SelStructTy, Selectors, CGM.getPointerAlign(),
2530 ".objc_selector_list");
Mike Stump11289f42009-09-09 15:08:12 +00002531 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
Chris Lattner8d3f4a42009-01-27 05:06:01 +00002532 SelStructPtrTy));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002533
2534 // Now that all of the static selectors exist, create pointers to them.
David Chisnalld7972f52011-03-23 16:36:54 +00002535 for (unsigned int i=0 ; i<SelectorCount ; i++) {
2536
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002537 llvm::Constant *Idxs[] = {Zeros[0],
David Chisnallcdd207e2011-10-04 15:35:30 +00002538 llvm::ConstantInt::get(Int32Ty, i), Zeros[0]};
David Chisnalld7972f52011-03-23 16:36:54 +00002539 // FIXME: We're generating redundant loads and stores here!
David Blaikiee3b172a2015-04-02 18:55:21 +00002540 llvm::Constant *SelPtr = llvm::ConstantExpr::getGetElementPtr(
2541 SelectorList->getValueType(), SelectorList, makeArrayRef(Idxs, 2));
Chris Lattner8d3f4a42009-01-27 05:06:01 +00002542 // If selectors are defined as an opaque type, cast the pointer to this
2543 // type.
David Chisnall76803412011-03-23 22:52:06 +00002544 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr, SelectorTy);
David Chisnalld7972f52011-03-23 16:36:54 +00002545 SelectorAliases[i]->replaceAllUsesWith(SelPtr);
2546 SelectorAliases[i]->eraseFromParent();
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002547 }
David Chisnalld7972f52011-03-23 16:36:54 +00002548
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002549 // Number of classes defined.
Mike Stump11289f42009-09-09 15:08:12 +00002550 Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002551 Classes.size()));
2552 // Number of categories defined
Mike Stump11289f42009-09-09 15:08:12 +00002553 Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002554 Categories.size()));
2555 // Create an array of classes, then categories, then static object instances
2556 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
2557 // NULL-terminated list of static object instances (mainly constant strings)
2558 Classes.push_back(Statics);
2559 Classes.push_back(NULLPtr);
Owen Anderson47034e12009-07-28 18:33:04 +00002560 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002561 Elements.push_back(ClassList);
Mike Stump11289f42009-09-09 15:08:12 +00002562 // Construct the symbol table
John McCall7f416cc2015-09-08 08:05:57 +00002563 llvm::Constant *SymTab =
2564 MakeGlobal(SymTabTy, Elements, CGM.getPointerAlign());
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002565
2566 // The symbol table is contained in a module which has some version-checking
2567 // constants
Chris Lattner845511f2011-06-18 22:49:11 +00002568 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
David Chisnall5c511772011-05-22 22:37:08 +00002569 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy),
Craig Topper8a13c412014-05-21 05:09:00 +00002570 (RuntimeVersion >= 10) ? IntTy : nullptr, nullptr);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002571 Elements.clear();
David Chisnalld7972f52011-03-23 16:36:54 +00002572 // Runtime version, used for ABI compatibility checking.
2573 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
Fariborz Jahanianc2d56182009-04-01 19:49:42 +00002574 // sizeof(ModuleTy)
Micah Villmowdd31ca12012-10-08 16:25:52 +00002575 llvm::DataLayout td(&TheModule);
Ken Dyck0fed10e2011-04-22 17:59:22 +00002576 Elements.push_back(
2577 llvm::ConstantInt::get(LongTy,
2578 td.getTypeSizeInBits(ModuleTy) /
2579 CGM.getContext().getCharWidth()));
David Chisnalld7972f52011-03-23 16:36:54 +00002580
2581 // The path to the source file where this module was declared
2582 SourceManager &SM = CGM.getContext().getSourceManager();
2583 const FileEntry *mainFile = SM.getFileEntryForID(SM.getMainFileID());
2584 std::string path =
2585 std::string(mainFile->getDir()->getName()) + '/' + mainFile->getName();
2586 Elements.push_back(MakeConstantString(path, ".objc_source_file_name"));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002587 Elements.push_back(SymTab);
David Chisnall5c511772011-05-22 22:37:08 +00002588
David Chisnalla918b882011-07-07 11:22:31 +00002589 if (RuntimeVersion >= 10)
David Blaikiebbafb8a2012-03-11 07:00:24 +00002590 switch (CGM.getLangOpts().getGC()) {
David Chisnalla918b882011-07-07 11:22:31 +00002591 case LangOptions::GCOnly:
David Chisnall5c511772011-05-22 22:37:08 +00002592 Elements.push_back(llvm::ConstantInt::get(IntTy, 2));
David Chisnall5c511772011-05-22 22:37:08 +00002593 break;
David Chisnalla918b882011-07-07 11:22:31 +00002594 case LangOptions::NonGC:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002595 if (CGM.getLangOpts().ObjCAutoRefCount)
David Chisnalla918b882011-07-07 11:22:31 +00002596 Elements.push_back(llvm::ConstantInt::get(IntTy, 1));
2597 else
2598 Elements.push_back(llvm::ConstantInt::get(IntTy, 0));
2599 break;
2600 case LangOptions::HybridGC:
2601 Elements.push_back(llvm::ConstantInt::get(IntTy, 1));
2602 break;
2603 }
David Chisnall5c511772011-05-22 22:37:08 +00002604
John McCall7f416cc2015-09-08 08:05:57 +00002605 llvm::Value *Module = MakeGlobal(ModuleTy, Elements, CGM.getPointerAlign());
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002606
2607 // Create the load function calling the runtime entry point with the module
2608 // structure
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002609 llvm::Function * LoadFunction = llvm::Function::Create(
Owen Anderson41a75022009-08-13 21:57:51 +00002610 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false),
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002611 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
2612 &TheModule);
Owen Anderson41a75022009-08-13 21:57:51 +00002613 llvm::BasicBlock *EntryBB =
2614 llvm::BasicBlock::Create(VMContext, "entry", LoadFunction);
John McCall7f416cc2015-09-08 08:05:57 +00002615 CGBuilderTy Builder(CGM, VMContext);
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002616 Builder.SetInsertPoint(EntryBB);
Fariborz Jahanian3b636c12009-03-30 18:02:14 +00002617
Benjamin Kramerdf1fb132011-05-28 14:26:31 +00002618 llvm::FunctionType *FT =
Jay Foad5709f7c2011-07-29 13:56:53 +00002619 llvm::FunctionType::get(Builder.getVoidTy(),
2620 llvm::PointerType::getUnqual(ModuleTy), true);
Benjamin Kramerdf1fb132011-05-28 14:26:31 +00002621 llvm::Value *Register = CGM.CreateRuntimeFunction(FT, "__objc_exec_class");
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002622 Builder.CreateCall(Register, Module);
David Chisnall92d436b2012-01-31 18:59:20 +00002623
David Chisnallaf066bbb2012-02-01 19:16:56 +00002624 if (!ClassAliases.empty()) {
David Chisnall92d436b2012-01-31 18:59:20 +00002625 llvm::Type *ArgTypes[2] = {PtrTy, PtrToInt8Ty};
2626 llvm::FunctionType *RegisterAliasTy =
2627 llvm::FunctionType::get(Builder.getVoidTy(),
2628 ArgTypes, false);
2629 llvm::Function *RegisterAlias = llvm::Function::Create(
2630 RegisterAliasTy,
2631 llvm::GlobalValue::ExternalWeakLinkage, "class_registerAlias_np",
2632 &TheModule);
2633 llvm::BasicBlock *AliasBB =
2634 llvm::BasicBlock::Create(VMContext, "alias", LoadFunction);
2635 llvm::BasicBlock *NoAliasBB =
2636 llvm::BasicBlock::Create(VMContext, "no_alias", LoadFunction);
2637
2638 // Branch based on whether the runtime provided class_registerAlias_np()
2639 llvm::Value *HasRegisterAlias = Builder.CreateICmpNE(RegisterAlias,
2640 llvm::Constant::getNullValue(RegisterAlias->getType()));
2641 Builder.CreateCondBr(HasRegisterAlias, AliasBB, NoAliasBB);
2642
Alp Tokerf6a24ce2013-12-05 16:25:25 +00002643 // The true branch (has alias registration function):
David Chisnall92d436b2012-01-31 18:59:20 +00002644 Builder.SetInsertPoint(AliasBB);
2645 // Emit alias registration calls:
2646 for (std::vector<ClassAliasPair>::iterator iter = ClassAliases.begin();
2647 iter != ClassAliases.end(); ++iter) {
2648 llvm::Constant *TheClass =
2649 TheModule.getGlobalVariable(("_OBJC_CLASS_" + iter->first).c_str(),
2650 true);
Craig Topper8a13c412014-05-21 05:09:00 +00002651 if (TheClass) {
David Chisnall92d436b2012-01-31 18:59:20 +00002652 TheClass = llvm::ConstantExpr::getBitCast(TheClass, PtrTy);
David Blaikie43f9bb72015-05-18 22:14:03 +00002653 Builder.CreateCall(RegisterAlias,
2654 {TheClass, MakeConstantString(iter->second)});
David Chisnall92d436b2012-01-31 18:59:20 +00002655 }
2656 }
2657 // Jump to end:
2658 Builder.CreateBr(NoAliasBB);
2659
2660 // Missing alias registration function, just return from the function:
2661 Builder.SetInsertPoint(NoAliasBB);
2662 }
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002663 Builder.CreateRetVoid();
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002664
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002665 return LoadFunction;
2666}
Daniel Dunbar92992502008-08-15 22:20:32 +00002667
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002668llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
Mike Stump11289f42009-09-09 15:08:12 +00002669 const ObjCContainerDecl *CD) {
2670 const ObjCCategoryImplDecl *OCD =
Steve Naroff11b387f2009-01-08 19:41:02 +00002671 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002672 StringRef CategoryName = OCD ? OCD->getName() : "";
2673 StringRef ClassName = CD->getName();
David Chisnalld7972f52011-03-23 16:36:54 +00002674 Selector MethodName = OMD->getSelector();
Douglas Gregorffca3a22009-01-09 17:18:27 +00002675 bool isClassMethod = !OMD->isInstanceMethod();
Daniel Dunbar92992502008-08-15 22:20:32 +00002676
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002677 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2192fe52011-07-18 04:24:23 +00002678 llvm::FunctionType *MethodTy =
John McCalla729c622012-02-17 03:33:10 +00002679 Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD));
Anton Korobeynikov1200aca2008-06-01 14:13:53 +00002680 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
2681 MethodName, isClassMethod);
2682
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00002683 llvm::Function *Method
Mike Stump11289f42009-09-09 15:08:12 +00002684 = llvm::Function::Create(MethodTy,
2685 llvm::GlobalValue::InternalLinkage,
2686 FunctionName,
2687 &TheModule);
Chris Lattner4bd55962008-03-30 23:03:07 +00002688 return Method;
2689}
2690
David Chisnall3fe89562011-05-23 22:33:28 +00002691llvm::Constant *CGObjCGNU::GetPropertyGetFunction() {
David Chisnalld7972f52011-03-23 16:36:54 +00002692 return GetPropertyFn;
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002693}
2694
David Chisnall3fe89562011-05-23 22:33:28 +00002695llvm::Constant *CGObjCGNU::GetPropertySetFunction() {
David Chisnalld7972f52011-03-23 16:36:54 +00002696 return SetPropertyFn;
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002697}
2698
Ted Kremeneke65b0862012-03-06 20:05:56 +00002699llvm::Constant *CGObjCGNU::GetOptimizedPropertySetFunction(bool atomic,
2700 bool copy) {
Craig Topper8a13c412014-05-21 05:09:00 +00002701 return nullptr;
Ted Kremeneke65b0862012-03-06 20:05:56 +00002702}
2703
David Chisnall3fe89562011-05-23 22:33:28 +00002704llvm::Constant *CGObjCGNU::GetGetStructFunction() {
David Chisnalld7972f52011-03-23 16:36:54 +00002705 return GetStructPropertyFn;
David Chisnall168b80f2010-12-26 22:13:16 +00002706}
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +00002707
David Chisnall3fe89562011-05-23 22:33:28 +00002708llvm::Constant *CGObjCGNU::GetSetStructFunction() {
David Chisnalld7972f52011-03-23 16:36:54 +00002709 return SetStructPropertyFn;
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002710}
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +00002711
David Chisnall0d75e062012-12-17 18:54:24 +00002712llvm::Constant *CGObjCGNU::GetCppAtomicObjectGetFunction() {
Craig Topper8a13c412014-05-21 05:09:00 +00002713 return nullptr;
David Chisnall0d75e062012-12-17 18:54:24 +00002714}
Eugene Zelenko0a4f3f42016-02-10 19:11:58 +00002715
David Chisnall0d75e062012-12-17 18:54:24 +00002716llvm::Constant *CGObjCGNU::GetCppAtomicObjectSetFunction() {
Craig Topper8a13c412014-05-21 05:09:00 +00002717 return nullptr;
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00002718}
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002719
Daniel Dunbarc46a0792009-07-24 07:40:24 +00002720llvm::Constant *CGObjCGNU::EnumerationMutationFunction() {
David Chisnalld7972f52011-03-23 16:36:54 +00002721 return EnumerationMutationFn;
Anders Carlsson3f35a262008-08-31 04:05:03 +00002722}
2723
David Chisnalld7972f52011-03-23 16:36:54 +00002724void CGObjCGNU::EmitSynchronizedStmt(CodeGenFunction &CGF,
John McCallbd309292010-07-06 01:34:17 +00002725 const ObjCAtSynchronizedStmt &S) {
David Chisnalld3858d62011-03-25 11:57:33 +00002726 EmitAtSynchronizedStmt(CGF, S, SyncEnterFn, SyncExitFn);
John McCallbd309292010-07-06 01:34:17 +00002727}
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00002728
David Chisnall3a509cd2009-12-24 02:26:34 +00002729
David Chisnalld7972f52011-03-23 16:36:54 +00002730void CGObjCGNU::EmitTryStmt(CodeGenFunction &CGF,
John McCallbd309292010-07-06 01:34:17 +00002731 const ObjCAtTryStmt &S) {
2732 // Unlike the Apple non-fragile runtimes, which also uses
2733 // unwind-based zero cost exceptions, the GNU Objective C runtime's
2734 // EH support isn't a veneer over C++ EH. Instead, exception
David Chisnall9a837be2012-11-07 16:50:40 +00002735 // objects are created by objc_exception_throw and destroyed by
John McCallbd309292010-07-06 01:34:17 +00002736 // the personality function; this avoids the need for bracketing
2737 // catch handlers with calls to __blah_begin_catch/__blah_end_catch
2738 // (or even _Unwind_DeleteException), but probably doesn't
2739 // interoperate very well with foreign exceptions.
David Chisnalld3858d62011-03-25 11:57:33 +00002740 //
David Chisnalle1d2584d2011-03-20 21:35:39 +00002741 // In Objective-C++ mode, we actually emit something equivalent to the C++
David Chisnalld3858d62011-03-25 11:57:33 +00002742 // exception handler.
2743 EmitTryCatchStmt(CGF, S, EnterCatchFn, ExitCatchFn, ExceptionReThrowFn);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00002744}
2745
David Chisnalld7972f52011-03-23 16:36:54 +00002746void CGObjCGNU::EmitThrowStmt(CodeGenFunction &CGF,
Fariborz Jahanian1eab0522013-01-10 19:02:56 +00002747 const ObjCAtThrowStmt &S,
2748 bool ClearInsertionPoint) {
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00002749 llvm::Value *ExceptionAsObject;
2750
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00002751 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00002752 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Fariborz Jahanian078cd522009-05-17 16:49:27 +00002753 ExceptionAsObject = Exception;
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00002754 } else {
Mike Stump11289f42009-09-09 15:08:12 +00002755 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Chris Lattnerb6e9eb62009-05-08 00:11:50 +00002756 "Unexpected rethrow outside @catch block.");
2757 ExceptionAsObject = CGF.ObjCEHValueStack.back();
2758 }
Benjamin Kramer76399eb2011-09-27 21:06:10 +00002759 ExceptionAsObject = CGF.Builder.CreateBitCast(ExceptionAsObject, IdTy);
David Chisnall9a837be2012-11-07 16:50:40 +00002760 llvm::CallSite Throw =
John McCall882987f2013-02-28 19:01:20 +00002761 CGF.EmitRuntimeCallOrInvoke(ExceptionThrowFn, ExceptionAsObject);
David Chisnall9a837be2012-11-07 16:50:40 +00002762 Throw.setDoesNotReturn();
Eli Friedmandc009da2012-08-10 21:26:17 +00002763 CGF.Builder.CreateUnreachable();
Fariborz Jahanian1eab0522013-01-10 19:02:56 +00002764 if (ClearInsertionPoint)
2765 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00002766}
2767
David Chisnalld7972f52011-03-23 16:36:54 +00002768llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +00002769 Address AddrWeakObj) {
John McCall882987f2013-02-28 19:01:20 +00002770 CGBuilderTy &B = CGF.Builder;
David Chisnallfcb37e92011-05-30 12:00:26 +00002771 AddrWeakObj = EnforceType(B, AddrWeakObj, PtrToIdTy);
John McCall7f416cc2015-09-08 08:05:57 +00002772 return B.CreateCall(WeakReadFn.getType(), WeakReadFn,
2773 AddrWeakObj.getPointer());
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00002774}
2775
David Chisnalld7972f52011-03-23 16:36:54 +00002776void CGObjCGNU::EmitObjCWeakAssign(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +00002777 llvm::Value *src, Address dst) {
John McCall882987f2013-02-28 19:01:20 +00002778 CGBuilderTy &B = CGF.Builder;
David Chisnall5bb4efd2010-02-03 15:59:02 +00002779 src = EnforceType(B, src, IdTy);
2780 dst = EnforceType(B, dst, PtrToIdTy);
John McCall7f416cc2015-09-08 08:05:57 +00002781 B.CreateCall(WeakAssignFn.getType(), WeakAssignFn,
2782 {src, dst.getPointer()});
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00002783}
2784
David Chisnalld7972f52011-03-23 16:36:54 +00002785void CGObjCGNU::EmitObjCGlobalAssign(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +00002786 llvm::Value *src, Address dst,
Fariborz Jahanian217af242010-07-20 20:30:03 +00002787 bool threadlocal) {
John McCall882987f2013-02-28 19:01:20 +00002788 CGBuilderTy &B = CGF.Builder;
David Chisnall5bb4efd2010-02-03 15:59:02 +00002789 src = EnforceType(B, src, IdTy);
2790 dst = EnforceType(B, dst, PtrToIdTy);
David Blaikie43f9bb72015-05-18 22:14:03 +00002791 // FIXME. Add threadloca assign API
2792 assert(!threadlocal && "EmitObjCGlobalAssign - Threal Local API NYI");
John McCall7f416cc2015-09-08 08:05:57 +00002793 B.CreateCall(GlobalAssignFn.getType(), GlobalAssignFn,
2794 {src, dst.getPointer()});
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00002795}
2796
David Chisnalld7972f52011-03-23 16:36:54 +00002797void CGObjCGNU::EmitObjCIvarAssign(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +00002798 llvm::Value *src, Address dst,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00002799 llvm::Value *ivarOffset) {
John McCall882987f2013-02-28 19:01:20 +00002800 CGBuilderTy &B = CGF.Builder;
David Chisnall5bb4efd2010-02-03 15:59:02 +00002801 src = EnforceType(B, src, IdTy);
David Chisnalle4e5c0f2011-05-25 20:33:17 +00002802 dst = EnforceType(B, dst, IdTy);
John McCall7f416cc2015-09-08 08:05:57 +00002803 B.CreateCall(IvarAssignFn.getType(), IvarAssignFn,
2804 {src, dst.getPointer(), ivarOffset});
Fariborz Jahaniane881b532008-11-20 19:23:36 +00002805}
2806
David Chisnalld7972f52011-03-23 16:36:54 +00002807void CGObjCGNU::EmitObjCStrongCastAssign(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +00002808 llvm::Value *src, Address dst) {
John McCall882987f2013-02-28 19:01:20 +00002809 CGBuilderTy &B = CGF.Builder;
David Chisnall5bb4efd2010-02-03 15:59:02 +00002810 src = EnforceType(B, src, IdTy);
2811 dst = EnforceType(B, dst, PtrToIdTy);
John McCall7f416cc2015-09-08 08:05:57 +00002812 B.CreateCall(StrongCastAssignFn.getType(), StrongCastAssignFn,
2813 {src, dst.getPointer()});
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00002814}
2815
David Chisnalld7972f52011-03-23 16:36:54 +00002816void CGObjCGNU::EmitGCMemmoveCollectable(CodeGenFunction &CGF,
John McCall7f416cc2015-09-08 08:05:57 +00002817 Address DestPtr,
2818 Address SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00002819 llvm::Value *Size) {
John McCall882987f2013-02-28 19:01:20 +00002820 CGBuilderTy &B = CGF.Builder;
David Chisnall7441d882011-05-28 14:23:43 +00002821 DestPtr = EnforceType(B, DestPtr, PtrTy);
2822 SrcPtr = EnforceType(B, SrcPtr, PtrTy);
David Chisnall5bb4efd2010-02-03 15:59:02 +00002823
John McCall7f416cc2015-09-08 08:05:57 +00002824 B.CreateCall(MemMoveFn.getType(), MemMoveFn,
2825 {DestPtr.getPointer(), SrcPtr.getPointer(), Size});
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002826}
2827
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002828llvm::GlobalVariable *CGObjCGNU::ObjCIvarOffsetVariable(
2829 const ObjCInterfaceDecl *ID,
2830 const ObjCIvarDecl *Ivar) {
2831 const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString()
2832 + '.' + Ivar->getNameAsString();
2833 // Emit the variable and initialize it with what we think the correct value
2834 // is. This allows code compiled with non-fragile ivars to work correctly
2835 // when linked against code which isn't (most of the time).
David Chisnall5778fce2009-08-31 16:41:57 +00002836 llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name);
2837 if (!IvarOffsetPointer) {
David Chisnalle8431a72010-11-03 16:12:44 +00002838 // This will cause a run-time crash if we accidentally use it. A value of
2839 // 0 would seem more sensible, but will silently overwrite the isa pointer
2840 // causing a great deal of confusion.
2841 uint64_t Offset = -1;
2842 // We can't call ComputeIvarBaseOffset() here if we have the
2843 // implementation, because it will create an invalid ASTRecordLayout object
2844 // that we are then stuck with forever, so we only initialize the ivar
2845 // offset variable with a guess if we only have the interface. The
2846 // initializer will be reset later anyway, when we are generating the class
2847 // description.
2848 if (!CGM.getContext().getObjCImplementation(
Dan Gohman145f3f12010-04-19 16:39:44 +00002849 const_cast<ObjCInterfaceDecl *>(ID)))
Eli Friedman8cbca202012-11-06 22:15:52 +00002850 Offset = ComputeIvarBaseOffset(CGM, ID, Ivar);
David Chisnall44ec5552010-04-19 01:37:25 +00002851
David Chisnalle0dc7cb2011-10-08 08:54:36 +00002852 llvm::ConstantInt *OffsetGuess = llvm::ConstantInt::get(Int32Ty, Offset,
Richard Trieue4f31802011-09-21 02:46:06 +00002853 /*isSigned*/true);
David Chisnall5778fce2009-08-31 16:41:57 +00002854 // Don't emit the guess in non-PIC code because the linker will not be able
2855 // to replace it with the real version for a library. In non-PIC code you
2856 // must compile with the fragile ABI if you want to use ivars from a
Mike Stump11289f42009-09-09 15:08:12 +00002857 // GCC-compiled class.
Rafael Espindolac9d336e2016-06-23 15:07:32 +00002858 if (CGM.getLangOpts().PICLevel) {
David Chisnall5778fce2009-08-31 16:41:57 +00002859 llvm::GlobalVariable *IvarOffsetGV = new llvm::GlobalVariable(TheModule,
David Chisnallcdd207e2011-10-04 15:35:30 +00002860 Int32Ty, false,
David Chisnall5778fce2009-08-31 16:41:57 +00002861 llvm::GlobalValue::PrivateLinkage, OffsetGuess, Name+".guess");
2862 IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
2863 IvarOffsetGV->getType(), false, llvm::GlobalValue::LinkOnceAnyLinkage,
2864 IvarOffsetGV, Name);
2865 } else {
2866 IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
Benjamin Kramerabd5b902009-10-13 10:07:13 +00002867 llvm::Type::getInt32PtrTy(VMContext), false,
Craig Topper8a13c412014-05-21 05:09:00 +00002868 llvm::GlobalValue::ExternalLinkage, nullptr, Name);
David Chisnall5778fce2009-08-31 16:41:57 +00002869 }
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002870 }
David Chisnall5778fce2009-08-31 16:41:57 +00002871 return IvarOffsetPointer;
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002872}
2873
David Chisnalld7972f52011-03-23 16:36:54 +00002874LValue CGObjCGNU::EmitObjCValueForIvar(CodeGenFunction &CGF,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00002875 QualType ObjectTy,
2876 llvm::Value *BaseValue,
2877 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00002878 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00002879 const ObjCInterfaceDecl *ID =
2880 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00002881 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
2882 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00002883}
Mike Stumpdd93a192009-07-31 21:31:32 +00002884
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002885static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context,
2886 const ObjCInterfaceDecl *OID,
2887 const ObjCIvarDecl *OIVD) {
Jordy Rosea91768e2011-07-22 02:08:32 +00002888 for (const ObjCIvarDecl *next = OID->all_declared_ivar_begin(); next;
2889 next = next->getNextIvar()) {
2890 if (OIVD == next)
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002891 return OID;
2892 }
Mike Stump11289f42009-09-09 15:08:12 +00002893
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002894 // Otherwise check in the super class.
2895 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
2896 return FindIvarInterface(Context, Super, OIVD);
Mike Stump11289f42009-09-09 15:08:12 +00002897
Craig Topper8a13c412014-05-21 05:09:00 +00002898 return nullptr;
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002899}
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00002900
David Chisnalld7972f52011-03-23 16:36:54 +00002901llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00002902 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00002903 const ObjCIvarDecl *Ivar) {
John McCall5fb5df92012-06-20 06:18:46 +00002904 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002905 Interface = FindIvarInterface(CGM.getContext(), Interface, Ivar);
Saleem Abdulrasool7093e212016-07-17 22:27:44 +00002906
2907 // The MSVC linker cannot have a single global defined as LinkOnceAnyLinkage
2908 // and ExternalLinkage, so create a reference to the ivar global and rely on
2909 // the definition being created as part of GenerateClass.
2910 if (RuntimeVersion < 10 ||
2911 CGF.CGM.getTarget().getTriple().isKnownWindowsMSVCEnvironment())
David Chisnall1bfe6d32011-07-07 12:34:51 +00002912 return CGF.Builder.CreateZExtOrBitCast(
John McCall7f416cc2015-09-08 08:05:57 +00002913 CGF.Builder.CreateDefaultAlignedLoad(CGF.Builder.CreateAlignedLoad(
2914 ObjCIvarOffsetVariable(Interface, Ivar),
2915 CGF.getPointerAlign(), "ivar")),
David Chisnall1bfe6d32011-07-07 12:34:51 +00002916 PtrDiffTy);
2917 std::string name = "__objc_ivar_offset_value_" +
2918 Interface->getNameAsString() +"." + Ivar->getNameAsString();
John McCall7f416cc2015-09-08 08:05:57 +00002919 CharUnits Align = CGM.getIntAlign();
David Chisnall1bfe6d32011-07-07 12:34:51 +00002920 llvm::Value *Offset = TheModule.getGlobalVariable(name);
John McCall7f416cc2015-09-08 08:05:57 +00002921 if (!Offset) {
2922 auto GV = new llvm::GlobalVariable(TheModule, IntTy,
David Chisnall28dc7f92011-08-01 17:36:53 +00002923 false, llvm::GlobalValue::LinkOnceAnyLinkage,
2924 llvm::Constant::getNullValue(IntTy), name);
John McCall7f416cc2015-09-08 08:05:57 +00002925 GV->setAlignment(Align.getQuantity());
2926 Offset = GV;
2927 }
2928 Offset = CGF.Builder.CreateAlignedLoad(Offset, Align);
David Chisnalla79b4692012-04-06 15:39:12 +00002929 if (Offset->getType() != PtrDiffTy)
2930 Offset = CGF.Builder.CreateZExtOrBitCast(Offset, PtrDiffTy);
2931 return Offset;
Fariborz Jahaniand20a03f2009-05-20 18:41:51 +00002932 }
Eli Friedman8cbca202012-11-06 22:15:52 +00002933 uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar);
2934 return llvm::ConstantInt::get(PtrDiffTy, Offset, /*isSigned*/true);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00002935}
2936
David Chisnalld7972f52011-03-23 16:36:54 +00002937CGObjCRuntime *
2938clang::CodeGen::CreateGNUObjCRuntime(CodeGenModule &CGM) {
John McCall5fb5df92012-06-20 06:18:46 +00002939 switch (CGM.getLangOpts().ObjCRuntime.getKind()) {
David Chisnallb601c962012-07-03 20:49:52 +00002940 case ObjCRuntime::GNUstep:
David Chisnalld7972f52011-03-23 16:36:54 +00002941 return new CGObjCGNUstep(CGM);
John McCall5fb5df92012-06-20 06:18:46 +00002942
David Chisnallb601c962012-07-03 20:49:52 +00002943 case ObjCRuntime::GCC:
John McCall5fb5df92012-06-20 06:18:46 +00002944 return new CGObjCGCC(CGM);
2945
John McCall775086e2012-07-12 02:07:58 +00002946 case ObjCRuntime::ObjFW:
2947 return new CGObjCObjFW(CGM);
2948
John McCall5fb5df92012-06-20 06:18:46 +00002949 case ObjCRuntime::FragileMacOSX:
2950 case ObjCRuntime::MacOSX:
2951 case ObjCRuntime::iOS:
Tim Northover756447a2015-10-30 16:30:36 +00002952 case ObjCRuntime::WatchOS:
John McCall5fb5df92012-06-20 06:18:46 +00002953 llvm_unreachable("these runtimes are not GNU runtimes");
2954 }
2955 llvm_unreachable("bad runtime");
Chris Lattnerb7256cd2008-03-01 08:50:34 +00002956}