blob: 9993f448f7b04d98723e939d9fac86ac89b07568 [file] [log] [blame]
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001//===------- CGObjCGNU.cpp - Emit LLVM Code from ASTs for a Module --------===//
Chris Lattner0f984262008-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 Lattnerfc8f0e12011-04-15 05:22:18 +000010// This provides Objective-C code generation targeting the GNU runtime. The
Anton Korobeynikov20ff3102008-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 Lattner0f984262008-03-01 08:50:34 +000014//
15//===----------------------------------------------------------------------===//
16
17#include "CGObjCRuntime.h"
Chris Lattnerdce14062008-06-26 04:19:03 +000018#include "CodeGenModule.h"
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000019#include "CodeGenFunction.h"
John McCall36f893c2011-01-28 11:13:47 +000020#include "CGCleanup.h"
Chris Lattnerdce14062008-06-26 04:19:03 +000021#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000022#include "clang/AST/Decl.h"
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000025#include "clang/AST/StmtObjC.h"
David Chisnall9f6614e2011-03-23 16:36:54 +000026#include "clang/Basic/SourceManager.h"
27#include "clang/Basic/FileManager.h"
Chris Lattner5dc08672009-05-08 00:11:50 +000028
29#include "llvm/Intrinsics.h"
Chris Lattner0f984262008-03-01 08:50:34 +000030#include "llvm/Module.h"
David Chisnallc6cd5fd2010-04-28 19:33:36 +000031#include "llvm/LLVMContext.h"
Chris Lattner0f984262008-03-01 08:50:34 +000032#include "llvm/ADT/SmallVector.h"
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000033#include "llvm/ADT/StringMap.h"
David Chisnall80558d22011-03-20 21:35:39 +000034#include "llvm/Support/CallSite.h"
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000035#include "llvm/Support/Compiler.h"
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000036#include "llvm/Target/TargetData.h"
Chris Lattner5dc08672009-05-08 00:11:50 +000037
Chris Lattner5f9e2722011-07-23 10:55:15 +000038#include <cstdarg>
Chris Lattnere160c9b2009-01-27 05:06:01 +000039
40
Chris Lattnerdce14062008-06-26 04:19:03 +000041using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000042using namespace CodeGen;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000043
Chris Lattner0f984262008-03-01 08:50:34 +000044
Chris Lattner0f984262008-03-01 08:50:34 +000045namespace {
David Chisnall81a65f52011-03-26 11:48:37 +000046/// Class that lazily initialises the runtime function. Avoids inserting the
47/// types and the function declaration into a module if they're not used, and
48/// avoids constructing the type more than once if it's used more than once.
David Chisnall9f6614e2011-03-23 16:36:54 +000049class LazyRuntimeFunction {
50 CodeGenModule *CGM;
Chris Lattner9cbe4f02011-07-09 17:41:47 +000051 std::vector<llvm::Type*> ArgTys;
David Chisnall9f6614e2011-03-23 16:36:54 +000052 const char *FunctionName;
David Chisnall789ecde2011-05-23 22:33:28 +000053 llvm::Constant *Function;
David Chisnall9f6614e2011-03-23 16:36:54 +000054 public:
David Chisnall81a65f52011-03-26 11:48:37 +000055 /// Constructor leaves this class uninitialized, because it is intended to
56 /// be used as a field in another class and not all of the types that are
57 /// used as arguments will necessarily be available at construction time.
David Chisnall9f6614e2011-03-23 16:36:54 +000058 LazyRuntimeFunction() : CGM(0), FunctionName(0), Function(0) {}
59
David Chisnall81a65f52011-03-26 11:48:37 +000060 /// Initialises the lazy function with the name, return type, and the types
61 /// of the arguments.
David Chisnall9f6614e2011-03-23 16:36:54 +000062 END_WITH_NULL
63 void init(CodeGenModule *Mod, const char *name,
Chris Lattner9cbe4f02011-07-09 17:41:47 +000064 llvm::Type *RetTy, ...) {
David Chisnall9f6614e2011-03-23 16:36:54 +000065 CGM =Mod;
66 FunctionName = name;
67 Function = 0;
David Chisnall9735ca62011-03-25 11:57:33 +000068 ArgTys.clear();
David Chisnall9f6614e2011-03-23 16:36:54 +000069 va_list Args;
70 va_start(Args, RetTy);
Chris Lattner9cbe4f02011-07-09 17:41:47 +000071 while (llvm::Type *ArgTy = va_arg(Args, llvm::Type*))
David Chisnall9f6614e2011-03-23 16:36:54 +000072 ArgTys.push_back(ArgTy);
73 va_end(Args);
74 // Push the return type on at the end so we can pop it off easily
75 ArgTys.push_back(RetTy);
76 }
David Chisnall81a65f52011-03-26 11:48:37 +000077 /// Overloaded cast operator, allows the class to be implicitly cast to an
78 /// LLVM constant.
David Chisnall789ecde2011-05-23 22:33:28 +000079 operator llvm::Constant*() {
David Chisnall9f6614e2011-03-23 16:36:54 +000080 if (!Function) {
David Chisnall9735ca62011-03-25 11:57:33 +000081 if (0 == FunctionName) return 0;
82 // We put the return type on the end of the vector, so pop it back off
Chris Lattner2acc6e32011-07-18 04:24:23 +000083 llvm::Type *RetTy = ArgTys.back();
David Chisnall9f6614e2011-03-23 16:36:54 +000084 ArgTys.pop_back();
85 llvm::FunctionType *FTy = llvm::FunctionType::get(RetTy, ArgTys, false);
86 Function =
David Chisnall789ecde2011-05-23 22:33:28 +000087 cast<llvm::Constant>(CGM->CreateRuntimeFunction(FTy, FunctionName));
David Chisnall9735ca62011-03-25 11:57:33 +000088 // We won't need to use the types again, so we may as well clean up the
89 // vector now
David Chisnall9f6614e2011-03-23 16:36:54 +000090 ArgTys.resize(0);
91 }
92 return Function;
93 }
David Chisnall789ecde2011-05-23 22:33:28 +000094 operator llvm::Function*() {
David Chisnall5f0bcc42011-05-23 23:15:11 +000095 return cast<llvm::Function>((llvm::Constant*)*this);
David Chisnall789ecde2011-05-23 22:33:28 +000096 }
David Chisnall5f0bcc42011-05-23 23:15:11 +000097
David Chisnall9f6614e2011-03-23 16:36:54 +000098};
99
100
David Chisnall81a65f52011-03-26 11:48:37 +0000101/// GNU Objective-C runtime code generation. This class implements the parts of
John McCallf7226fb2012-07-12 02:07:58 +0000102/// Objective-C support that are specific to the GNU family of runtimes (GCC,
103/// GNUstep and ObjFW).
David Chisnall9f6614e2011-03-23 16:36:54 +0000104class CGObjCGNU : public CGObjCRuntime {
David Chisnallc7ef4622011-03-23 22:52:06 +0000105protected:
David Chisnall81a65f52011-03-26 11:48:37 +0000106 /// The LLVM module into which output is inserted
Chris Lattner0f984262008-03-01 08:50:34 +0000107 llvm::Module &TheModule;
David Chisnall81a65f52011-03-26 11:48:37 +0000108 /// strut objc_super. Used for sending messages to super. This structure
109 /// contains the receiver (object) and the expected class.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000110 llvm::StructType *ObjCSuperTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000111 /// struct objc_super*. The type of the argument to the superclass message
112 /// lookup functions.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000113 llvm::PointerType *PtrToObjCSuperTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000114 /// LLVM type for selectors. Opaque pointer (i8*) unless a header declaring
115 /// SEL is included in a header somewhere, in which case it will be whatever
116 /// type is declared in that header, most likely {i8*, i8*}.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000117 llvm::PointerType *SelectorTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000118 /// LLVM i8 type. Cached here to avoid repeatedly getting it in all of the
119 /// places where it's used
Chris Lattner2acc6e32011-07-18 04:24:23 +0000120 llvm::IntegerType *Int8Ty;
David Chisnall81a65f52011-03-26 11:48:37 +0000121 /// Pointer to i8 - LLVM type of char*, for all of the places where the
122 /// runtime needs to deal with C strings.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000123 llvm::PointerType *PtrToInt8Ty;
David Chisnall81a65f52011-03-26 11:48:37 +0000124 /// Instance Method Pointer type. This is a pointer to a function that takes,
125 /// at a minimum, an object and a selector, and is the generic type for
126 /// Objective-C methods. Due to differences between variadic / non-variadic
127 /// calling conventions, it must always be cast to the correct type before
128 /// actually being used.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000129 llvm::PointerType *IMPTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000130 /// Type of an untyped Objective-C object. Clang treats id as a built-in type
131 /// when compiling Objective-C code, so this may be an opaque pointer (i8*),
132 /// but if the runtime header declaring it is included then it may be a
133 /// pointer to a structure.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000134 llvm::PointerType *IdTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000135 /// Pointer to a pointer to an Objective-C object. Used in the new ABI
136 /// message lookup function and some GC-related functions.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000137 llvm::PointerType *PtrToIdTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000138 /// The clang type of id. Used when using the clang CGCall infrastructure to
139 /// call Objective-C methods.
John McCallead608a2010-02-26 00:48:12 +0000140 CanQualType ASTIdTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000141 /// LLVM type for C int type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000142 llvm::IntegerType *IntTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000143 /// LLVM type for an opaque pointer. This is identical to PtrToInt8Ty, but is
144 /// used in the code to document the difference between i8* meaning a pointer
145 /// to a C string and i8* meaning a pointer to some opaque type.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000146 llvm::PointerType *PtrTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000147 /// LLVM type for C long type. The runtime uses this in a lot of places where
148 /// it should be using intptr_t, but we can't fix this without breaking
149 /// compatibility with GCC...
Jay Foadef6de3d2011-07-11 09:56:20 +0000150 llvm::IntegerType *LongTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000151 /// LLVM type for C size_t. Used in various runtime data structures.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000152 llvm::IntegerType *SizeTy;
David Chisnall49de5282011-10-08 08:54:36 +0000153 /// LLVM type for C intptr_t.
154 llvm::IntegerType *IntPtrTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000155 /// LLVM type for C ptrdiff_t. Mainly used in property accessor functions.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000156 llvm::IntegerType *PtrDiffTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000157 /// LLVM type for C int*. Used for GCC-ABI-compatible non-fragile instance
158 /// variables.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000159 llvm::PointerType *PtrToIntTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000160 /// LLVM type for Objective-C BOOL type.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000161 llvm::Type *BoolTy;
David Chisnall917b28b2011-10-04 15:35:30 +0000162 /// 32-bit integer type, to save us needing to look it up every time it's used.
163 llvm::IntegerType *Int32Ty;
164 /// 64-bit integer type, to save us needing to look it up every time it's used.
165 llvm::IntegerType *Int64Ty;
David Chisnall81a65f52011-03-26 11:48:37 +0000166 /// Metadata kind used to tie method lookups to message sends. The GNUstep
167 /// runtime provides some LLVM passes that can use this to do things like
168 /// automatic IMP caching and speculative inlining.
David Chisnallc7ef4622011-03-23 22:52:06 +0000169 unsigned msgSendMDKind;
David Chisnall81a65f52011-03-26 11:48:37 +0000170 /// Helper function that generates a constant string and returns a pointer to
171 /// the start of the string. The result of this function can be used anywhere
172 /// where the C code specifies const char*.
David Chisnall9735ca62011-03-25 11:57:33 +0000173 llvm::Constant *MakeConstantString(const std::string &Str,
174 const std::string &Name="") {
175 llvm::Constant *ConstStr = CGM.GetAddrOfConstantCString(Str, Name.c_str());
Jay Foada5c04342011-07-21 14:31:17 +0000176 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros);
David Chisnall9735ca62011-03-25 11:57:33 +0000177 }
David Chisnall81a65f52011-03-26 11:48:37 +0000178 /// Emits a linkonce_odr string, whose name is the prefix followed by the
179 /// string value. This allows the linker to combine the strings between
180 /// different modules. Used for EH typeinfo names, selector strings, and a
181 /// few other things.
David Chisnall9735ca62011-03-25 11:57:33 +0000182 llvm::Constant *ExportUniqueString(const std::string &Str,
183 const std::string prefix) {
184 std::string name = prefix + Str;
185 llvm::Constant *ConstStr = TheModule.getGlobalVariable(name);
186 if (!ConstStr) {
Chris Lattner94010692012-02-05 02:30:40 +0000187 llvm::Constant *value = llvm::ConstantDataArray::getString(VMContext,Str);
David Chisnall9735ca62011-03-25 11:57:33 +0000188 ConstStr = new llvm::GlobalVariable(TheModule, value->getType(), true,
189 llvm::GlobalValue::LinkOnceODRLinkage, value, prefix + Str);
190 }
Jay Foada5c04342011-07-21 14:31:17 +0000191 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros);
David Chisnall9735ca62011-03-25 11:57:33 +0000192 }
David Chisnall81a65f52011-03-26 11:48:37 +0000193 /// Generates a global structure, initialized by the elements in the vector.
194 /// The element types must match the types of the structure elements in the
195 /// first argument.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000196 llvm::GlobalVariable *MakeGlobal(llvm::StructType *Ty,
David Chisnall917b28b2011-10-04 15:35:30 +0000197 llvm::ArrayRef<llvm::Constant*> V,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000198 StringRef Name="",
David Chisnall9735ca62011-03-25 11:57:33 +0000199 llvm::GlobalValue::LinkageTypes linkage
200 =llvm::GlobalValue::InternalLinkage) {
201 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
202 return new llvm::GlobalVariable(TheModule, Ty, false,
203 linkage, C, Name);
204 }
David Chisnall81a65f52011-03-26 11:48:37 +0000205 /// Generates a global array. The vector must contain the same number of
206 /// elements that the array type declares, of the type specified as the array
207 /// element type.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000208 llvm::GlobalVariable *MakeGlobal(llvm::ArrayType *Ty,
David Chisnall917b28b2011-10-04 15:35:30 +0000209 llvm::ArrayRef<llvm::Constant*> V,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000210 StringRef Name="",
David Chisnall9735ca62011-03-25 11:57:33 +0000211 llvm::GlobalValue::LinkageTypes linkage
212 =llvm::GlobalValue::InternalLinkage) {
213 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
214 return new llvm::GlobalVariable(TheModule, Ty, false,
215 linkage, C, Name);
216 }
David Chisnall81a65f52011-03-26 11:48:37 +0000217 /// Generates a global array, inferring the array type from the specified
218 /// element type and the size of the initialiser.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000219 llvm::GlobalVariable *MakeGlobalArray(llvm::Type *Ty,
David Chisnall917b28b2011-10-04 15:35:30 +0000220 llvm::ArrayRef<llvm::Constant*> V,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000221 StringRef Name="",
David Chisnall9735ca62011-03-25 11:57:33 +0000222 llvm::GlobalValue::LinkageTypes linkage
223 =llvm::GlobalValue::InternalLinkage) {
224 llvm::ArrayType *ArrayTy = llvm::ArrayType::get(Ty, V.size());
225 return MakeGlobal(ArrayTy, V, Name, linkage);
226 }
David Chisnall81a65f52011-03-26 11:48:37 +0000227 /// Ensures that the value has the required type, by inserting a bitcast if
228 /// required. This function lets us avoid inserting bitcasts that are
229 /// redundant.
Chris Lattner2acc6e32011-07-18 04:24:23 +0000230 llvm::Value* EnforceType(CGBuilderTy B, llvm::Value *V, llvm::Type *Ty){
David Chisnallc7ef4622011-03-23 22:52:06 +0000231 if (V->getType() == Ty) return V;
232 return B.CreateBitCast(V, Ty);
233 }
234 // Some zeros used for GEPs in lots of places.
235 llvm::Constant *Zeros[2];
David Chisnall81a65f52011-03-26 11:48:37 +0000236 /// Null pointer value. Mainly used as a terminator in various arrays.
David Chisnallc7ef4622011-03-23 22:52:06 +0000237 llvm::Constant *NULLPtr;
David Chisnall81a65f52011-03-26 11:48:37 +0000238 /// LLVM context.
David Chisnallc7ef4622011-03-23 22:52:06 +0000239 llvm::LLVMContext &VMContext;
240private:
David Chisnall81a65f52011-03-26 11:48:37 +0000241 /// Placeholder for the class. Lots of things refer to the class before we've
242 /// actually emitted it. We use this alias as a placeholder, and then replace
243 /// it with a pointer to the class structure before finally emitting the
244 /// module.
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000245 llvm::GlobalAlias *ClassPtrAlias;
David Chisnall81a65f52011-03-26 11:48:37 +0000246 /// Placeholder for the metaclass. Lots of things refer to the class before
247 /// we've / actually emitted it. We use this alias as a placeholder, and then
248 /// replace / it with a pointer to the metaclass structure before finally
249 /// emitting the / module.
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000250 llvm::GlobalAlias *MetaClassPtrAlias;
David Chisnall81a65f52011-03-26 11:48:37 +0000251 /// All of the classes that have been generated for this compilation units.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000252 std::vector<llvm::Constant*> Classes;
David Chisnall81a65f52011-03-26 11:48:37 +0000253 /// All of the categories that have been generated for this compilation units.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000254 std::vector<llvm::Constant*> Categories;
David Chisnall81a65f52011-03-26 11:48:37 +0000255 /// All of the Objective-C constant strings that have been generated for this
256 /// compilation units.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000257 std::vector<llvm::Constant*> ConstantStrings;
David Chisnall81a65f52011-03-26 11:48:37 +0000258 /// Map from string values to Objective-C constant strings in the output.
259 /// Used to prevent emitting Objective-C strings more than once. This should
260 /// not be required at all - CodeGenModule should manage this list.
David Chisnall48272a02010-01-27 12:49:23 +0000261 llvm::StringMap<llvm::Constant*> ObjCStrings;
David Chisnall81a65f52011-03-26 11:48:37 +0000262 /// All of the protocols that have been declared.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000263 llvm::StringMap<llvm::Constant*> ExistingProtocols;
David Chisnall81a65f52011-03-26 11:48:37 +0000264 /// For each variant of a selector, we store the type encoding and a
265 /// placeholder value. For an untyped selector, the type will be the empty
266 /// string. Selector references are all done via the module's selector table,
267 /// so we create an alias as a placeholder and then replace it with the real
268 /// value later.
David Chisnall9f6614e2011-03-23 16:36:54 +0000269 typedef std::pair<std::string, llvm::GlobalAlias*> TypedSelector;
David Chisnall81a65f52011-03-26 11:48:37 +0000270 /// Type of the selector map. This is roughly equivalent to the structure
271 /// used in the GNUstep runtime, which maintains a list of all of the valid
272 /// types for a selector in a table.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000273 typedef llvm::DenseMap<Selector, SmallVector<TypedSelector, 2> >
David Chisnall9f6614e2011-03-23 16:36:54 +0000274 SelectorMap;
David Chisnall81a65f52011-03-26 11:48:37 +0000275 /// A map from selectors to selector types. This allows us to emit all
276 /// selectors of the same name and type together.
David Chisnall9f6614e2011-03-23 16:36:54 +0000277 SelectorMap SelectorTable;
278
David Chisnall81a65f52011-03-26 11:48:37 +0000279 /// Selectors related to memory management. When compiling in GC mode, we
280 /// omit these.
David Chisnallef6e0f32010-02-03 15:59:02 +0000281 Selector RetainSel, ReleaseSel, AutoreleaseSel;
David Chisnall81a65f52011-03-26 11:48:37 +0000282 /// Runtime functions used for memory management in GC mode. Note that clang
283 /// supports code generation for calling these functions, but neither GNU
284 /// runtime actually supports this API properly yet.
David Chisnall9f6614e2011-03-23 16:36:54 +0000285 LazyRuntimeFunction IvarAssignFn, StrongCastAssignFn, MemMoveFn, WeakReadFn,
286 WeakAssignFn, GlobalAssignFn;
David Chisnall9f6614e2011-03-23 16:36:54 +0000287
David Chisnall29254f42012-01-31 18:59:20 +0000288 typedef std::pair<std::string, std::string> ClassAliasPair;
289 /// All classes that have aliases set for them.
290 std::vector<ClassAliasPair> ClassAliases;
291
David Chisnall9735ca62011-03-25 11:57:33 +0000292protected:
David Chisnall81a65f52011-03-26 11:48:37 +0000293 /// Function used for throwing Objective-C exceptions.
David Chisnall9f6614e2011-03-23 16:36:54 +0000294 LazyRuntimeFunction ExceptionThrowFn;
James Dennett809d1be2012-06-13 22:07:09 +0000295 /// Function used for rethrowing exceptions, used at the end of \@finally or
296 /// \@synchronize blocks.
David Chisnall9735ca62011-03-25 11:57:33 +0000297 LazyRuntimeFunction ExceptionReThrowFn;
David Chisnall81a65f52011-03-26 11:48:37 +0000298 /// Function called when entering a catch function. This is required for
299 /// differentiating Objective-C exceptions and foreign exceptions.
David Chisnall9735ca62011-03-25 11:57:33 +0000300 LazyRuntimeFunction EnterCatchFn;
David Chisnall81a65f52011-03-26 11:48:37 +0000301 /// Function called when exiting from a catch block. Used to do exception
302 /// cleanup.
David Chisnall9735ca62011-03-25 11:57:33 +0000303 LazyRuntimeFunction ExitCatchFn;
James Dennett809d1be2012-06-13 22:07:09 +0000304 /// Function called when entering an \@synchronize block. Acquires the lock.
David Chisnall9f6614e2011-03-23 16:36:54 +0000305 LazyRuntimeFunction SyncEnterFn;
James Dennett809d1be2012-06-13 22:07:09 +0000306 /// Function called when exiting an \@synchronize block. Releases the lock.
David Chisnall9f6614e2011-03-23 16:36:54 +0000307 LazyRuntimeFunction SyncExitFn;
308
David Chisnall9735ca62011-03-25 11:57:33 +0000309private:
310
David Chisnall81a65f52011-03-26 11:48:37 +0000311 /// Function called if fast enumeration detects that the collection is
312 /// modified during the update.
David Chisnall9f6614e2011-03-23 16:36:54 +0000313 LazyRuntimeFunction EnumerationMutationFn;
David Chisnall81a65f52011-03-26 11:48:37 +0000314 /// Function for implementing synthesized property getters that return an
315 /// object.
David Chisnall9f6614e2011-03-23 16:36:54 +0000316 LazyRuntimeFunction GetPropertyFn;
David Chisnall81a65f52011-03-26 11:48:37 +0000317 /// Function for implementing synthesized property setters that return an
318 /// object.
David Chisnall9f6614e2011-03-23 16:36:54 +0000319 LazyRuntimeFunction SetPropertyFn;
David Chisnall81a65f52011-03-26 11:48:37 +0000320 /// Function used for non-object declared property getters.
David Chisnall9f6614e2011-03-23 16:36:54 +0000321 LazyRuntimeFunction GetStructPropertyFn;
David Chisnall81a65f52011-03-26 11:48:37 +0000322 /// Function used for non-object declared property setters.
David Chisnall9f6614e2011-03-23 16:36:54 +0000323 LazyRuntimeFunction SetStructPropertyFn;
324
David Chisnall81a65f52011-03-26 11:48:37 +0000325 /// The version of the runtime that this class targets. Must match the
326 /// version in the runtime.
David Chisnalla2120032011-05-22 22:37:08 +0000327 int RuntimeVersion;
David Chisnall81a65f52011-03-26 11:48:37 +0000328 /// The version of the protocol class. Used to differentiate between ObjC1
329 /// and ObjC2 protocols. Objective-C 1 protocols can not contain optional
330 /// components and can not contain declared properties. We always emit
331 /// Objective-C 2 property structures, but we have to pretend that they're
332 /// Objective-C 1 property structures when targeting the GCC runtime or it
333 /// will abort.
David Chisnall9f6614e2011-03-23 16:36:54 +0000334 const int ProtocolVersion;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000335private:
David Chisnall81a65f52011-03-26 11:48:37 +0000336 /// Generates an instance variable list structure. This is a structure
337 /// containing a size and an array of structures containing instance variable
338 /// metadata. This is used purely for introspection in the fragile ABI. In
339 /// the non-fragile ABI, it's used for instance variable fixup.
Bill Wendling795b1002012-02-22 09:30:11 +0000340 llvm::Constant *GenerateIvarList(ArrayRef<llvm::Constant *> IvarNames,
341 ArrayRef<llvm::Constant *> IvarTypes,
342 ArrayRef<llvm::Constant *> IvarOffsets);
David Chisnall81a65f52011-03-26 11:48:37 +0000343 /// Generates a method list structure. This is a structure containing a size
344 /// and an array of structures containing method metadata.
345 ///
346 /// This structure is used by both classes and categories, and contains a next
347 /// pointer allowing them to be chained together in a linked list.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000348 llvm::Constant *GenerateMethodList(const StringRef &ClassName,
349 const StringRef &CategoryName,
Bill Wendling795b1002012-02-22 09:30:11 +0000350 ArrayRef<Selector> MethodSels,
351 ArrayRef<llvm::Constant *> MethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000352 bool isClassMethodList);
James Dennett809d1be2012-06-13 22:07:09 +0000353 /// Emits an empty protocol. This is used for \@protocol() where no protocol
David Chisnall81a65f52011-03-26 11:48:37 +0000354 /// is found. The runtime will (hopefully) fix up the pointer to refer to the
355 /// real protocol.
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000356 llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName);
David Chisnall81a65f52011-03-26 11:48:37 +0000357 /// Generates a list of property metadata structures. This follows the same
358 /// pattern as method and instance variable metadata lists.
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000359 llvm::Constant *GeneratePropertyList(const ObjCImplementationDecl *OID,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000360 SmallVectorImpl<Selector> &InstanceMethodSels,
361 SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes);
David Chisnall81a65f52011-03-26 11:48:37 +0000362 /// Generates a list of referenced protocols. Classes, categories, and
363 /// protocols all use this structure.
Bill Wendling795b1002012-02-22 09:30:11 +0000364 llvm::Constant *GenerateProtocolList(ArrayRef<std::string> Protocols);
David Chisnall81a65f52011-03-26 11:48:37 +0000365 /// To ensure that all protocols are seen by the runtime, we add a category on
366 /// a class defined in the runtime, declaring no methods, but adopting the
367 /// protocols. This is a horribly ugly hack, but it allows us to collect all
368 /// of the protocols without changing the ABI.
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000369 void GenerateProtocolHolderCategory(void);
David Chisnall81a65f52011-03-26 11:48:37 +0000370 /// Generates a class structure.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000371 llvm::Constant *GenerateClassStructure(
372 llvm::Constant *MetaClass,
373 llvm::Constant *SuperClass,
374 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000375 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000376 llvm::Constant *Version,
377 llvm::Constant *InstanceSize,
378 llvm::Constant *IVars,
379 llvm::Constant *Methods,
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000380 llvm::Constant *Protocols,
381 llvm::Constant *IvarOffsets,
David Chisnall8c757f92010-04-28 14:29:56 +0000382 llvm::Constant *Properties,
David Chisnall917b28b2011-10-04 15:35:30 +0000383 llvm::Constant *StrongIvarBitmap,
384 llvm::Constant *WeakIvarBitmap,
David Chisnall8c757f92010-04-28 14:29:56 +0000385 bool isMeta=false);
David Chisnall81a65f52011-03-26 11:48:37 +0000386 /// Generates a method list. This is used by protocols to define the required
387 /// and optional methods.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000388 llvm::Constant *GenerateProtocolMethodList(
Bill Wendling795b1002012-02-22 09:30:11 +0000389 ArrayRef<llvm::Constant *> MethodNames,
390 ArrayRef<llvm::Constant *> MethodTypes);
David Chisnall81a65f52011-03-26 11:48:37 +0000391 /// Returns a selector with the specified type encoding. An empty string is
392 /// used to return an untyped selector (with the types field set to NULL).
David Chisnall9f6614e2011-03-23 16:36:54 +0000393 llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
394 const std::string &TypeEncoding, bool lval);
David Chisnall81a65f52011-03-26 11:48:37 +0000395 /// Returns the variable used to store the offset of an instance variable.
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +0000396 llvm::GlobalVariable *ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
397 const ObjCIvarDecl *Ivar);
David Chisnall81a65f52011-03-26 11:48:37 +0000398 /// Emits a reference to a class. This allows the linker to object if there
399 /// is no class of the matching name.
John McCallf7226fb2012-07-12 02:07:58 +0000400protected:
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000401 void EmitClassRef(const std::string &className);
David Chisnallc7aed3b2011-06-29 13:16:41 +0000402 /// Emits a pointer to the named class
John McCallf7226fb2012-07-12 02:07:58 +0000403 virtual llvm::Value *GetClassNamed(CGBuilderTy &Builder,
404 const std::string &Name, bool isWeak);
David Chisnall81a65f52011-03-26 11:48:37 +0000405 /// Looks up the method for sending a message to the specified object. This
406 /// mechanism differs between the GCC and GNU runtimes, so this method must be
407 /// overridden in subclasses.
David Chisnallc7ef4622011-03-23 22:52:06 +0000408 virtual llvm::Value *LookupIMP(CodeGenFunction &CGF,
409 llvm::Value *&Receiver,
410 llvm::Value *cmd,
411 llvm::MDNode *node) = 0;
David Chisnall917b28b2011-10-04 15:35:30 +0000412 /// Looks up the method for sending a message to a superclass. This
413 /// mechanism differs between the GCC and GNU runtimes, so this method must
414 /// be overridden in subclasses.
David Chisnallc7ef4622011-03-23 22:52:06 +0000415 virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF,
416 llvm::Value *ObjCSuper,
417 llvm::Value *cmd) = 0;
David Chisnall917b28b2011-10-04 15:35:30 +0000418 /// Libobjc2 uses a bitfield representation where small(ish) bitfields are
419 /// stored in a 64-bit value with the low bit set to 1 and the remaining 63
420 /// bits set to their values, LSB first, while larger ones are stored in a
421 /// structure of this / form:
422 ///
423 /// struct { int32_t length; int32_t values[length]; };
424 ///
425 /// The values in the array are stored in host-endian format, with the least
426 /// significant bit being assumed to come first in the bitfield. Therefore,
427 /// a bitfield with the 64th bit set will be (int64_t)&{ 2, [0, 1<<31] },
428 /// while a bitfield / with the 63rd bit set will be 1<<64.
Bill Wendling795b1002012-02-22 09:30:11 +0000429 llvm::Constant *MakeBitField(ArrayRef<bool> bits);
Chris Lattner0f984262008-03-01 08:50:34 +0000430public:
David Chisnall9f6614e2011-03-23 16:36:54 +0000431 CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion,
432 unsigned protocolClassVersion);
433
David Chisnall0d13f6f2010-01-23 02:40:42 +0000434 virtual llvm::Constant *GenerateConstantString(const StringLiteral *);
David Chisnall9f6614e2011-03-23 16:36:54 +0000435
436 virtual RValue
437 GenerateMessageSend(CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +0000438 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000439 QualType ResultType,
440 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000441 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000442 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000443 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000444 const ObjCMethodDecl *Method);
David Chisnall9f6614e2011-03-23 16:36:54 +0000445 virtual RValue
446 GenerateMessageSendSuper(CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +0000447 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000448 QualType ResultType,
449 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000450 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000451 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000452 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000453 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000454 const CallArgList &CallArgs,
455 const ObjCMethodDecl *Method);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000456 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000457 const ObjCInterfaceDecl *OID);
Fariborz Jahanian03b29602010-06-17 19:56:20 +0000458 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
459 bool lval = false);
Daniel Dunbar6d5a1c22010-02-03 20:11:42 +0000460 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
461 *Method);
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +0000462 virtual llvm::Constant *GetEHType(QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000463
464 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000465 const ObjCContainerDecl *CD);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000466 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
467 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
David Chisnall29254f42012-01-31 18:59:20 +0000468 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000469 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000470 const ObjCProtocolDecl *PD);
471 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000472 virtual llvm::Function *ModuleInitFunction();
David Chisnall789ecde2011-05-23 22:33:28 +0000473 virtual llvm::Constant *GetPropertyGetFunction();
474 virtual llvm::Constant *GetPropertySetFunction();
Ted Kremenekebcb57a2012-03-06 20:05:56 +0000475 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
476 bool copy);
David Chisnall789ecde2011-05-23 22:33:28 +0000477 virtual llvm::Constant *GetSetStructFunction();
Fariborz Jahaniane3173022012-01-06 18:07:23 +0000478 virtual llvm::Constant *GetCppAtomicObjectFunction();
David Chisnall789ecde2011-05-23 22:33:28 +0000479 virtual llvm::Constant *GetGetStructFunction();
Daniel Dunbar309a4362009-07-24 07:40:24 +0000480 virtual llvm::Constant *EnumerationMutationFunction();
Mike Stump1eb44332009-09-09 15:08:12 +0000481
David Chisnall9f6614e2011-03-23 16:36:54 +0000482 virtual void EmitTryStmt(CodeGenFunction &CGF,
John McCallf1549f62010-07-06 01:34:17 +0000483 const ObjCAtTryStmt &S);
David Chisnall9f6614e2011-03-23 16:36:54 +0000484 virtual void EmitSynchronizedStmt(CodeGenFunction &CGF,
John McCallf1549f62010-07-06 01:34:17 +0000485 const ObjCAtSynchronizedStmt &S);
David Chisnall9f6614e2011-03-23 16:36:54 +0000486 virtual void EmitThrowStmt(CodeGenFunction &CGF,
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000487 const ObjCAtThrowStmt &S);
David Chisnall9f6614e2011-03-23 16:36:54 +0000488 virtual llvm::Value * EmitObjCWeakRead(CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000489 llvm::Value *AddrWeakObj);
David Chisnall9f6614e2011-03-23 16:36:54 +0000490 virtual void EmitObjCWeakAssign(CodeGenFunction &CGF,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000491 llvm::Value *src, llvm::Value *dst);
David Chisnall9f6614e2011-03-23 16:36:54 +0000492 virtual void EmitObjCGlobalAssign(CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000493 llvm::Value *src, llvm::Value *dest,
494 bool threadlocal=false);
David Chisnall9f6614e2011-03-23 16:36:54 +0000495 virtual void EmitObjCIvarAssign(CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000496 llvm::Value *src, llvm::Value *dest,
497 llvm::Value *ivarOffset);
David Chisnall9f6614e2011-03-23 16:36:54 +0000498 virtual void EmitObjCStrongCastAssign(CodeGenFunction &CGF,
Fariborz Jahanian58626502008-11-19 00:59:10 +0000499 llvm::Value *src, llvm::Value *dest);
David Chisnall9f6614e2011-03-23 16:36:54 +0000500 virtual void EmitGCMemmoveCollectable(CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +0000501 llvm::Value *DestPtr,
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000502 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +0000503 llvm::Value *Size);
David Chisnall9f6614e2011-03-23 16:36:54 +0000504 virtual LValue EmitObjCValueForIvar(CodeGenFunction &CGF,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000505 QualType ObjectTy,
506 llvm::Value *BaseValue,
507 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000508 unsigned CVRQualifiers);
David Chisnall9f6614e2011-03-23 16:36:54 +0000509 virtual llvm::Value *EmitIvarOffset(CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +0000510 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000511 const ObjCIvarDecl *Ivar);
David Chisnallc7aed3b2011-06-29 13:16:41 +0000512 virtual llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
David Chisnall9f6614e2011-03-23 16:36:54 +0000513 virtual llvm::Constant *BuildGCBlockLayout(CodeGenModule &CGM,
John McCall6b5a61b2011-02-07 10:33:21 +0000514 const CGBlockInfo &blockInfo) {
Fariborz Jahanian89ecd412010-08-04 16:57:49 +0000515 return NULLPtr;
516 }
Fariborz Jahanian6f40e222011-05-17 22:21:16 +0000517
518 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
519 return 0;
520 }
Chris Lattner0f984262008-03-01 08:50:34 +0000521};
David Chisnall81a65f52011-03-26 11:48:37 +0000522/// Class representing the legacy GCC Objective-C ABI. This is the default when
523/// -fobjc-nonfragile-abi is not specified.
524///
525/// The GCC ABI target actually generates code that is approximately compatible
526/// with the new GNUstep runtime ABI, but refrains from using any features that
527/// would not work with the GCC runtime. For example, clang always generates
528/// the extended form of the class structure, and the extra fields are simply
529/// ignored by GCC libobjc.
David Chisnall9f6614e2011-03-23 16:36:54 +0000530class CGObjCGCC : public CGObjCGNU {
David Chisnall81a65f52011-03-26 11:48:37 +0000531 /// The GCC ABI message lookup function. Returns an IMP pointing to the
532 /// method implementation for this message.
David Chisnallc7ef4622011-03-23 22:52:06 +0000533 LazyRuntimeFunction MsgLookupFn;
David Chisnall81a65f52011-03-26 11:48:37 +0000534 /// The GCC ABI superclass message lookup function. Takes a pointer to a
535 /// structure describing the receiver and the class, and a selector as
536 /// arguments. Returns the IMP for the corresponding method.
David Chisnallc7ef4622011-03-23 22:52:06 +0000537 LazyRuntimeFunction MsgLookupSuperFn;
538protected:
539 virtual llvm::Value *LookupIMP(CodeGenFunction &CGF,
540 llvm::Value *&Receiver,
541 llvm::Value *cmd,
542 llvm::MDNode *node) {
543 CGBuilderTy &Builder = CGF.Builder;
David Chisnall6f3887e2011-10-28 17:55:06 +0000544 llvm::Value *args[] = {
David Chisnallc7ef4622011-03-23 22:52:06 +0000545 EnforceType(Builder, Receiver, IdTy),
David Chisnall6f3887e2011-10-28 17:55:06 +0000546 EnforceType(Builder, cmd, SelectorTy) };
547 llvm::CallSite imp = CGF.EmitCallOrInvoke(MsgLookupFn, args);
548 imp->setMetadata(msgSendMDKind, node);
549 return imp.getInstruction();
David Chisnallc7ef4622011-03-23 22:52:06 +0000550 }
551 virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF,
552 llvm::Value *ObjCSuper,
553 llvm::Value *cmd) {
554 CGBuilderTy &Builder = CGF.Builder;
555 llvm::Value *lookupArgs[] = {EnforceType(Builder, ObjCSuper,
556 PtrToObjCSuperTy), cmd};
Jay Foad4c7d9f12011-07-15 08:37:34 +0000557 return Builder.CreateCall(MsgLookupSuperFn, lookupArgs);
David Chisnallc7ef4622011-03-23 22:52:06 +0000558 }
David Chisnall9f6614e2011-03-23 16:36:54 +0000559 public:
David Chisnallc7ef4622011-03-23 22:52:06 +0000560 CGObjCGCC(CodeGenModule &Mod) : CGObjCGNU(Mod, 8, 2) {
561 // IMP objc_msg_lookup(id, SEL);
562 MsgLookupFn.init(&CGM, "objc_msg_lookup", IMPTy, IdTy, SelectorTy, NULL);
563 // IMP objc_msg_lookup_super(struct objc_super*, SEL);
564 MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy,
565 PtrToObjCSuperTy, SelectorTy, NULL);
566 }
David Chisnall9f6614e2011-03-23 16:36:54 +0000567};
David Chisnall81a65f52011-03-26 11:48:37 +0000568/// Class used when targeting the new GNUstep runtime ABI.
David Chisnall9f6614e2011-03-23 16:36:54 +0000569class CGObjCGNUstep : public CGObjCGNU {
David Chisnall81a65f52011-03-26 11:48:37 +0000570 /// The slot lookup function. Returns a pointer to a cacheable structure
571 /// that contains (among other things) the IMP.
David Chisnallc7ef4622011-03-23 22:52:06 +0000572 LazyRuntimeFunction SlotLookupFn;
David Chisnall81a65f52011-03-26 11:48:37 +0000573 /// The GNUstep ABI superclass message lookup function. Takes a pointer to
574 /// a structure describing the receiver and the class, and a selector as
575 /// arguments. Returns the slot for the corresponding method. Superclass
576 /// message lookup rarely changes, so this is a good caching opportunity.
David Chisnallc7ef4622011-03-23 22:52:06 +0000577 LazyRuntimeFunction SlotLookupSuperFn;
David Chisnall81a65f52011-03-26 11:48:37 +0000578 /// Type of an slot structure pointer. This is returned by the various
579 /// lookup functions.
David Chisnallc7ef4622011-03-23 22:52:06 +0000580 llvm::Type *SlotTy;
581 protected:
582 virtual llvm::Value *LookupIMP(CodeGenFunction &CGF,
583 llvm::Value *&Receiver,
584 llvm::Value *cmd,
585 llvm::MDNode *node) {
586 CGBuilderTy &Builder = CGF.Builder;
587 llvm::Function *LookupFn = SlotLookupFn;
588
589 // Store the receiver on the stack so that we can reload it later
590 llvm::Value *ReceiverPtr = CGF.CreateTempAlloca(Receiver->getType());
591 Builder.CreateStore(Receiver, ReceiverPtr);
592
593 llvm::Value *self;
594
595 if (isa<ObjCMethodDecl>(CGF.CurCodeDecl)) {
596 self = CGF.LoadObjCSelf();
597 } else {
598 self = llvm::ConstantPointerNull::get(IdTy);
599 }
600
601 // The lookup function is guaranteed not to capture the receiver pointer.
602 LookupFn->setDoesNotCapture(1);
603
David Chisnall6f3887e2011-10-28 17:55:06 +0000604 llvm::Value *args[] = {
David Chisnallc7ef4622011-03-23 22:52:06 +0000605 EnforceType(Builder, ReceiverPtr, PtrToIdTy),
606 EnforceType(Builder, cmd, SelectorTy),
David Chisnall6f3887e2011-10-28 17:55:06 +0000607 EnforceType(Builder, self, IdTy) };
608 llvm::CallSite slot = CGF.EmitCallOrInvoke(LookupFn, args);
609 slot.setOnlyReadsMemory();
David Chisnallc7ef4622011-03-23 22:52:06 +0000610 slot->setMetadata(msgSendMDKind, node);
611
612 // Load the imp from the slot
David Chisnall6f3887e2011-10-28 17:55:06 +0000613 llvm::Value *imp =
614 Builder.CreateLoad(Builder.CreateStructGEP(slot.getInstruction(), 4));
David Chisnallc7ef4622011-03-23 22:52:06 +0000615
616 // The lookup function may have changed the receiver, so make sure we use
617 // the new one.
618 Receiver = Builder.CreateLoad(ReceiverPtr, true);
619 return imp;
620 }
621 virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF,
622 llvm::Value *ObjCSuper,
623 llvm::Value *cmd) {
624 CGBuilderTy &Builder = CGF.Builder;
625 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
626
Jay Foad4c7d9f12011-07-15 08:37:34 +0000627 llvm::CallInst *slot = Builder.CreateCall(SlotLookupSuperFn, lookupArgs);
David Chisnallc7ef4622011-03-23 22:52:06 +0000628 slot->setOnlyReadsMemory();
629
630 return Builder.CreateLoad(Builder.CreateStructGEP(slot, 4));
631 }
David Chisnall9f6614e2011-03-23 16:36:54 +0000632 public:
David Chisnallc7ef4622011-03-23 22:52:06 +0000633 CGObjCGNUstep(CodeGenModule &Mod) : CGObjCGNU(Mod, 9, 3) {
Chris Lattner7650d952011-06-18 22:49:11 +0000634 llvm::StructType *SlotStructTy = llvm::StructType::get(PtrTy,
David Chisnallc7ef4622011-03-23 22:52:06 +0000635 PtrTy, PtrTy, IntTy, IMPTy, NULL);
636 SlotTy = llvm::PointerType::getUnqual(SlotStructTy);
637 // Slot_t objc_msg_lookup_sender(id *receiver, SEL selector, id sender);
638 SlotLookupFn.init(&CGM, "objc_msg_lookup_sender", SlotTy, PtrToIdTy,
639 SelectorTy, IdTy, NULL);
640 // Slot_t objc_msg_lookup_super(struct objc_super*, SEL);
641 SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
642 PtrToObjCSuperTy, SelectorTy, NULL);
David Chisnall9735ca62011-03-25 11:57:33 +0000643 // If we're in ObjC++ mode, then we want to make
David Blaikie4e4d0842012-03-11 07:00:24 +0000644 if (CGM.getLangOpts().CPlusPlus) {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000645 llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
David Chisnall9735ca62011-03-25 11:57:33 +0000646 // void *__cxa_begin_catch(void *e)
647 EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy, NULL);
648 // void __cxa_end_catch(void)
David Chisnall4bd5d092011-08-08 17:26:06 +0000649 ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy, NULL);
David Chisnall9735ca62011-03-25 11:57:33 +0000650 // void _Unwind_Resume_or_Rethrow(void*)
David Chisnall978d4152011-04-05 17:15:18 +0000651 ExceptionReThrowFn.init(&CGM, "_Unwind_Resume_or_Rethrow", VoidTy, PtrTy, NULL);
David Chisnall9735ca62011-03-25 11:57:33 +0000652 }
David Chisnallc7ef4622011-03-23 22:52:06 +0000653 }
David Chisnall9f6614e2011-03-23 16:36:54 +0000654};
655
John McCallf7226fb2012-07-12 02:07:58 +0000656/// Class used when targeting the ObjFW runtime.
657class CGObjCObjFW: public CGObjCGCC {
658 virtual llvm::Value *GetClassNamed(CGBuilderTy &Builder,
659 const std::string &Name, bool isWeak) {
660 if (isWeak)
661 return CGObjCGNU::GetClassNamed(Builder, Name, isWeak);
662
663 EmitClassRef(Name);
664
665 std::string SymbolName = "_OBJC_CLASS_" + Name;
666
667 llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(SymbolName);
668
669 if (!ClassSymbol)
670 ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false,
671 llvm::GlobalValue::ExternalLinkage,
672 0, SymbolName);
673
674 return ClassSymbol;
675 }
676
677public:
678 CGObjCObjFW(CodeGenModule &Mod): CGObjCGCC(Mod) {}
679};
Chris Lattner0f984262008-03-01 08:50:34 +0000680} // end anonymous namespace
681
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000682
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000683/// Emits a reference to a dummy variable which is emitted with each class.
684/// This ensures that a linker error will be generated when trying to link
685/// together modules where a referenced class is not defined.
Mike Stumpbb1c8602009-07-31 21:31:32 +0000686void CGObjCGNU::EmitClassRef(const std::string &className) {
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000687 std::string symbolRef = "__objc_class_ref_" + className;
688 // Don't emit two copies of the same symbol
Mike Stumpbb1c8602009-07-31 21:31:32 +0000689 if (TheModule.getGlobalVariable(symbolRef))
690 return;
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000691 std::string symbolName = "__objc_class_name_" + className;
692 llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(symbolName);
693 if (!ClassSymbol) {
Owen Anderson1c431b32009-07-08 19:05:04 +0000694 ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false,
695 llvm::GlobalValue::ExternalLinkage, 0, symbolName);
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000696 }
Owen Anderson1c431b32009-07-08 19:05:04 +0000697 new llvm::GlobalVariable(TheModule, ClassSymbol->getType(), true,
Chris Lattnerf35271b2009-08-05 05:25:18 +0000698 llvm::GlobalValue::WeakAnyLinkage, ClassSymbol, symbolRef);
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000699}
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000700
Chris Lattner5f9e2722011-07-23 10:55:15 +0000701static std::string SymbolNameForMethod(const StringRef &ClassName,
702 const StringRef &CategoryName, const Selector MethodName,
David Chisnall9f6614e2011-03-23 16:36:54 +0000703 bool isClassMethod) {
704 std::string MethodNameColonStripped = MethodName.getAsString();
David Chisnalld3467362010-01-14 14:08:19 +0000705 std::replace(MethodNameColonStripped.begin(), MethodNameColonStripped.end(),
706 ':', '_');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000707 return (Twine(isClassMethod ? "_c_" : "_i_") + ClassName + "_" +
David Chisnall9f6614e2011-03-23 16:36:54 +0000708 CategoryName + "_" + MethodNameColonStripped).str();
David Chisnall87935a82010-05-08 20:58:05 +0000709}
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000710
David Chisnall9f6614e2011-03-23 16:36:54 +0000711CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion,
712 unsigned protocolClassVersion)
John McCallde5d3c72012-02-17 03:33:10 +0000713 : CGObjCRuntime(cgm), TheModule(CGM.getModule()),
714 VMContext(cgm.getLLVMContext()), ClassPtrAlias(0), MetaClassPtrAlias(0),
715 RuntimeVersion(runtimeABIVersion), ProtocolVersion(protocolClassVersion) {
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000716
717 msgSendMDKind = VMContext.getMDKindID("GNUObjCMessageSend");
718
David Chisnall9f6614e2011-03-23 16:36:54 +0000719 CodeGenTypes &Types = CGM.getTypes();
Chris Lattnere160c9b2009-01-27 05:06:01 +0000720 IntTy = cast<llvm::IntegerType>(
David Chisnall9f6614e2011-03-23 16:36:54 +0000721 Types.ConvertType(CGM.getContext().IntTy));
Chris Lattnere160c9b2009-01-27 05:06:01 +0000722 LongTy = cast<llvm::IntegerType>(
David Chisnall9f6614e2011-03-23 16:36:54 +0000723 Types.ConvertType(CGM.getContext().LongTy));
David Chisnall8fac25d2010-12-26 22:13:16 +0000724 SizeTy = cast<llvm::IntegerType>(
David Chisnall9f6614e2011-03-23 16:36:54 +0000725 Types.ConvertType(CGM.getContext().getSizeType()));
David Chisnall8fac25d2010-12-26 22:13:16 +0000726 PtrDiffTy = cast<llvm::IntegerType>(
David Chisnall9f6614e2011-03-23 16:36:54 +0000727 Types.ConvertType(CGM.getContext().getPointerDiffType()));
David Chisnall8fac25d2010-12-26 22:13:16 +0000728 BoolTy = CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000730 Int8Ty = llvm::Type::getInt8Ty(VMContext);
731 // C string type. Used in lots of places.
732 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
733
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000734 Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000735 Zeros[1] = Zeros[0];
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000736 NULLPtr = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Chris Lattner391d77a2008-03-30 23:03:07 +0000737 // Get the selector Type.
David Chisnall0d13f6f2010-01-23 02:40:42 +0000738 QualType selTy = CGM.getContext().getObjCSelType();
739 if (QualType() == selTy) {
740 SelectorTy = PtrToInt8Ty;
741 } else {
742 SelectorTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(selTy));
743 }
Chris Lattnere160c9b2009-01-27 05:06:01 +0000744
Owen Anderson96e0fc72009-07-29 22:16:19 +0000745 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
Chris Lattner391d77a2008-03-30 23:03:07 +0000746 PtrTy = PtrToInt8Ty;
Mike Stump1eb44332009-09-09 15:08:12 +0000747
David Chisnall917b28b2011-10-04 15:35:30 +0000748 Int32Ty = llvm::Type::getInt32Ty(VMContext);
749 Int64Ty = llvm::Type::getInt64Ty(VMContext);
750
David Chisnall49de5282011-10-08 08:54:36 +0000751 IntPtrTy =
752 TheModule.getPointerSize() == llvm::Module::Pointer32 ? Int32Ty : Int64Ty;
753
Chris Lattner391d77a2008-03-30 23:03:07 +0000754 // Object type
David Chisnall7bcf6c32011-04-29 14:10:35 +0000755 QualType UnqualIdTy = CGM.getContext().getObjCIdType();
756 ASTIdTy = CanQualType();
757 if (UnqualIdTy != QualType()) {
758 ASTIdTy = CGM.getContext().getCanonicalType(UnqualIdTy);
David Chisnall0d13f6f2010-01-23 02:40:42 +0000759 IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
David Chisnall7bcf6c32011-04-29 14:10:35 +0000760 } else {
761 IdTy = PtrToInt8Ty;
David Chisnall0d13f6f2010-01-23 02:40:42 +0000762 }
David Chisnallef6e0f32010-02-03 15:59:02 +0000763 PtrToIdTy = llvm::PointerType::getUnqual(IdTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Chris Lattner7650d952011-06-18 22:49:11 +0000765 ObjCSuperTy = llvm::StructType::get(IdTy, IdTy, NULL);
David Chisnallc7ef4622011-03-23 22:52:06 +0000766 PtrToObjCSuperTy = llvm::PointerType::getUnqual(ObjCSuperTy);
767
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000768 llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
David Chisnall9f6614e2011-03-23 16:36:54 +0000769
770 // void objc_exception_throw(id);
771 ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy, NULL);
David Chisnall9735ca62011-03-25 11:57:33 +0000772 ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy, NULL);
David Chisnall9f6614e2011-03-23 16:36:54 +0000773 // int objc_sync_enter(id);
774 SyncEnterFn.init(&CGM, "objc_sync_enter", IntTy, IdTy, NULL);
775 // int objc_sync_exit(id);
776 SyncExitFn.init(&CGM, "objc_sync_exit", IntTy, IdTy, NULL);
777
778 // void objc_enumerationMutation (id)
779 EnumerationMutationFn.init(&CGM, "objc_enumerationMutation", VoidTy,
780 IdTy, NULL);
781
782 // id objc_getProperty(id, SEL, ptrdiff_t, BOOL)
783 GetPropertyFn.init(&CGM, "objc_getProperty", IdTy, IdTy, SelectorTy,
784 PtrDiffTy, BoolTy, NULL);
785 // void objc_setProperty(id, SEL, ptrdiff_t, id, BOOL, BOOL)
786 SetPropertyFn.init(&CGM, "objc_setProperty", VoidTy, IdTy, SelectorTy,
787 PtrDiffTy, IdTy, BoolTy, BoolTy, NULL);
788 // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL)
789 GetStructPropertyFn.init(&CGM, "objc_getPropertyStruct", VoidTy, PtrTy, PtrTy,
790 PtrDiffTy, BoolTy, BoolTy, NULL);
791 // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL)
792 SetStructPropertyFn.init(&CGM, "objc_setPropertyStruct", VoidTy, PtrTy, PtrTy,
793 PtrDiffTy, BoolTy, BoolTy, NULL);
794
Chris Lattner391d77a2008-03-30 23:03:07 +0000795 // IMP type
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000796 llvm::Type *IMPArgs[] = { IdTy, SelectorTy };
David Chisnallc7ef4622011-03-23 22:52:06 +0000797 IMPTy = llvm::PointerType::getUnqual(llvm::FunctionType::get(IdTy, IMPArgs,
798 true));
David Chisnallef6e0f32010-02-03 15:59:02 +0000799
David Blaikie4e4d0842012-03-11 07:00:24 +0000800 const LangOptions &Opts = CGM.getLangOpts();
Douglas Gregore289d812011-09-13 17:21:33 +0000801 if ((Opts.getGC() != LangOptions::NonGC) || Opts.ObjCAutoRefCount)
David Chisnallf0748852011-07-07 11:22:31 +0000802 RuntimeVersion = 10;
803
David Chisnall9735ca62011-03-25 11:57:33 +0000804 // Don't bother initialising the GC stuff unless we're compiling in GC mode
Douglas Gregore289d812011-09-13 17:21:33 +0000805 if (Opts.getGC() != LangOptions::NonGC) {
David Chisnalla2120032011-05-22 22:37:08 +0000806 // This is a bit of an hack. We should sort this out by having a proper
807 // CGObjCGNUstep subclass for GC, but we may want to really support the old
808 // ABI and GC added in ObjectiveC2.framework, so we fudge it a bit for now
David Chisnallef6e0f32010-02-03 15:59:02 +0000809 // Get selectors needed in GC mode
810 RetainSel = GetNullarySelector("retain", CGM.getContext());
811 ReleaseSel = GetNullarySelector("release", CGM.getContext());
812 AutoreleaseSel = GetNullarySelector("autorelease", CGM.getContext());
813
814 // Get functions needed in GC mode
815
816 // id objc_assign_ivar(id, id, ptrdiff_t);
David Chisnall9f6614e2011-03-23 16:36:54 +0000817 IvarAssignFn.init(&CGM, "objc_assign_ivar", IdTy, IdTy, IdTy, PtrDiffTy,
818 NULL);
David Chisnallef6e0f32010-02-03 15:59:02 +0000819 // id objc_assign_strongCast (id, id*)
David Chisnall9f6614e2011-03-23 16:36:54 +0000820 StrongCastAssignFn.init(&CGM, "objc_assign_strongCast", IdTy, IdTy,
821 PtrToIdTy, NULL);
David Chisnallef6e0f32010-02-03 15:59:02 +0000822 // id objc_assign_global(id, id*);
David Chisnall9f6614e2011-03-23 16:36:54 +0000823 GlobalAssignFn.init(&CGM, "objc_assign_global", IdTy, IdTy, PtrToIdTy,
824 NULL);
David Chisnallef6e0f32010-02-03 15:59:02 +0000825 // id objc_assign_weak(id, id*);
David Chisnall9f6614e2011-03-23 16:36:54 +0000826 WeakAssignFn.init(&CGM, "objc_assign_weak", IdTy, IdTy, PtrToIdTy, NULL);
David Chisnallef6e0f32010-02-03 15:59:02 +0000827 // id objc_read_weak(id*);
David Chisnall9f6614e2011-03-23 16:36:54 +0000828 WeakReadFn.init(&CGM, "objc_read_weak", IdTy, PtrToIdTy, NULL);
David Chisnallef6e0f32010-02-03 15:59:02 +0000829 // void *objc_memmove_collectable(void*, void *, size_t);
David Chisnall9f6614e2011-03-23 16:36:54 +0000830 MemMoveFn.init(&CGM, "objc_memmove_collectable", PtrTy, PtrTy, PtrTy,
831 SizeTy, NULL);
David Chisnallef6e0f32010-02-03 15:59:02 +0000832 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000833}
Mike Stumpbb1c8602009-07-31 21:31:32 +0000834
David Chisnallc7aed3b2011-06-29 13:16:41 +0000835llvm::Value *CGObjCGNU::GetClassNamed(CGBuilderTy &Builder,
David Chisnalld3fc7292011-06-30 10:14:37 +0000836 const std::string &Name,
837 bool isWeak) {
David Chisnallc7aed3b2011-06-29 13:16:41 +0000838 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(Name);
David Chisnall41d63ed2010-01-08 00:14:31 +0000839 // With the incompatible ABI, this will need to be replaced with a direct
840 // reference to the class symbol. For the compatible nonfragile ABI we are
841 // still performing this lookup at run time but emitting the symbol for the
842 // class externally so that we can make the switch later.
David Chisnallc7aed3b2011-06-29 13:16:41 +0000843 //
844 // Libobjc2 contains an LLVM pass that replaces calls to objc_lookup_class
845 // with memoized versions or with static references if it's safe to do so.
David Chisnalld3fc7292011-06-30 10:14:37 +0000846 if (!isWeak)
847 EmitClassRef(Name);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000848 ClassName = Builder.CreateStructGEP(ClassName, 0);
849
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000850 llvm::Constant *ClassLookupFn =
Jay Foadda549e82011-07-29 13:56:53 +0000851 CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, PtrToInt8Ty, true),
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000852 "objc_lookup_class");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000853 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner391d77a2008-03-30 23:03:07 +0000854}
855
David Chisnallc7aed3b2011-06-29 13:16:41 +0000856// This has to perform the lookup every time, since posing and related
857// techniques can modify the name -> class mapping.
858llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
859 const ObjCInterfaceDecl *OID) {
David Chisnalld3fc7292011-06-30 10:14:37 +0000860 return GetClassNamed(Builder, OID->getNameAsString(), OID->isWeakImported());
David Chisnallc7aed3b2011-06-29 13:16:41 +0000861}
862llvm::Value *CGObjCGNU::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
David Chisnalld3fc7292011-06-30 10:14:37 +0000863 return GetClassNamed(Builder, "NSAutoreleasePool", false);
David Chisnallc7aed3b2011-06-29 13:16:41 +0000864}
865
Fariborz Jahanian03b29602010-06-17 19:56:20 +0000866llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel,
David Chisnall9f6614e2011-03-23 16:36:54 +0000867 const std::string &TypeEncoding, bool lval) {
868
Chris Lattner5f9e2722011-07-23 10:55:15 +0000869 SmallVector<TypedSelector, 2> &Types = SelectorTable[Sel];
David Chisnall9f6614e2011-03-23 16:36:54 +0000870 llvm::GlobalAlias *SelValue = 0;
871
872
Chris Lattner5f9e2722011-07-23 10:55:15 +0000873 for (SmallVectorImpl<TypedSelector>::iterator i = Types.begin(),
David Chisnall9f6614e2011-03-23 16:36:54 +0000874 e = Types.end() ; i!=e ; i++) {
875 if (i->first == TypeEncoding) {
876 SelValue = i->second;
877 break;
878 }
879 }
880 if (0 == SelValue) {
David Chisnallc7ef4622011-03-23 22:52:06 +0000881 SelValue = new llvm::GlobalAlias(SelectorTy,
David Chisnall9f6614e2011-03-23 16:36:54 +0000882 llvm::GlobalValue::PrivateLinkage,
883 ".objc_selector_"+Sel.getAsString(), NULL,
884 &TheModule);
885 Types.push_back(TypedSelector(TypeEncoding, SelValue));
886 }
887
David Chisnallc7ef4622011-03-23 22:52:06 +0000888 if (lval) {
889 llvm::Value *tmp = Builder.CreateAlloca(SelValue->getType());
890 Builder.CreateStore(SelValue, tmp);
891 return tmp;
892 }
893 return SelValue;
David Chisnall9f6614e2011-03-23 16:36:54 +0000894}
895
896llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel,
897 bool lval) {
898 return GetSelector(Builder, Sel, std::string(), lval);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000899}
900
Daniel Dunbar6d5a1c22010-02-03 20:11:42 +0000901llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000902 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000903 std::string SelTypes;
904 CGM.getContext().getObjCEncodingForMethodDecl(Method, SelTypes);
David Chisnall9f6614e2011-03-23 16:36:54 +0000905 return GetSelector(Builder, Method->getSelector(), SelTypes, false);
Chris Lattner8e67b632008-06-26 04:37:12 +0000906}
907
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +0000908llvm::Constant *CGObjCGNU::GetEHType(QualType T) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000909 if (!CGM.getLangOpts().CPlusPlus) {
David Chisnall9735ca62011-03-25 11:57:33 +0000910 if (T->isObjCIdType()
911 || T->isObjCQualifiedIdType()) {
912 // With the old ABI, there was only one kind of catchall, which broke
913 // foreign exceptions. With the new ABI, we use __objc_id_typeinfo as
914 // a pointer indicating object catchalls, and NULL to indicate real
915 // catchalls
John McCall260611a2012-06-20 06:18:46 +0000916 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
David Chisnall9735ca62011-03-25 11:57:33 +0000917 return MakeConstantString("@id");
918 } else {
919 return 0;
920 }
921 }
922
923 // All other types should be Objective-C interface pointer types.
924 const ObjCObjectPointerType *OPT =
925 T->getAs<ObjCObjectPointerType>();
926 assert(OPT && "Invalid @catch type.");
927 const ObjCInterfaceDecl *IDecl =
928 OPT->getObjectType()->getInterface();
929 assert(IDecl && "Invalid @catch type.");
930 return MakeConstantString(IDecl->getIdentifier()->getName());
931 }
David Chisnall80558d22011-03-20 21:35:39 +0000932 // For Objective-C++, we want to provide the ability to catch both C++ and
933 // Objective-C objects in the same function.
934
935 // There's a particular fixed type info for 'id'.
936 if (T->isObjCIdType() ||
937 T->isObjCQualifiedIdType()) {
938 llvm::Constant *IDEHType =
939 CGM.getModule().getGlobalVariable("__objc_id_type_info");
940 if (!IDEHType)
941 IDEHType =
942 new llvm::GlobalVariable(CGM.getModule(), PtrToInt8Ty,
943 false,
944 llvm::GlobalValue::ExternalLinkage,
945 0, "__objc_id_type_info");
946 return llvm::ConstantExpr::getBitCast(IDEHType, PtrToInt8Ty);
947 }
948
949 const ObjCObjectPointerType *PT =
950 T->getAs<ObjCObjectPointerType>();
951 assert(PT && "Invalid @catch type.");
952 const ObjCInterfaceType *IT = PT->getInterfaceType();
953 assert(IT && "Invalid @catch type.");
954 std::string className = IT->getDecl()->getIdentifier()->getName();
955
956 std::string typeinfoName = "__objc_eh_typeinfo_" + className;
957
958 // Return the existing typeinfo if it exists
959 llvm::Constant *typeinfo = TheModule.getGlobalVariable(typeinfoName);
David Chisnallacd76fe2012-03-20 16:25:52 +0000960 if (typeinfo)
961 return llvm::ConstantExpr::getBitCast(typeinfo, PtrToInt8Ty);
David Chisnall80558d22011-03-20 21:35:39 +0000962
963 // Otherwise create it.
964
965 // vtable for gnustep::libobjc::__objc_class_type_info
966 // It's quite ugly hard-coding this. Ideally we'd generate it using the host
967 // platform's name mangling.
968 const char *vtableName = "_ZTVN7gnustep7libobjc22__objc_class_type_infoE";
969 llvm::Constant *Vtable = TheModule.getGlobalVariable(vtableName);
970 if (!Vtable) {
971 Vtable = new llvm::GlobalVariable(TheModule, PtrToInt8Ty, true,
972 llvm::GlobalValue::ExternalLinkage, 0, vtableName);
973 }
974 llvm::Constant *Two = llvm::ConstantInt::get(IntTy, 2);
Jay Foada5c04342011-07-21 14:31:17 +0000975 Vtable = llvm::ConstantExpr::getGetElementPtr(Vtable, Two);
David Chisnall80558d22011-03-20 21:35:39 +0000976 Vtable = llvm::ConstantExpr::getBitCast(Vtable, PtrToInt8Ty);
977
978 llvm::Constant *typeName =
979 ExportUniqueString(className, "__objc_eh_typename_");
980
981 std::vector<llvm::Constant*> fields;
982 fields.push_back(Vtable);
983 fields.push_back(typeName);
984 llvm::Constant *TI =
Chris Lattner7650d952011-06-18 22:49:11 +0000985 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty,
David Chisnall80558d22011-03-20 21:35:39 +0000986 NULL), fields, "__objc_eh_typeinfo_" + className,
987 llvm::GlobalValue::LinkOnceODRLinkage);
988 return llvm::ConstantExpr::getBitCast(TI, PtrToInt8Ty);
John McCall5a180392010-07-24 00:37:23 +0000989}
990
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000991/// Generate an NSConstantString object.
David Chisnall0d13f6f2010-01-23 02:40:42 +0000992llvm::Constant *CGObjCGNU::GenerateConstantString(const StringLiteral *SL) {
David Chisnall48272a02010-01-27 12:49:23 +0000993
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000994 std::string Str = SL->getString().str();
David Chisnall0d13f6f2010-01-23 02:40:42 +0000995
David Chisnall48272a02010-01-27 12:49:23 +0000996 // Look for an existing one
997 llvm::StringMap<llvm::Constant*>::iterator old = ObjCStrings.find(Str);
998 if (old != ObjCStrings.end())
999 return old->getValue();
1000
David Blaikie4e4d0842012-03-11 07:00:24 +00001001 StringRef StringClass = CGM.getLangOpts().ObjCConstantStringClass;
David Chisnall13df6f62012-01-04 12:02:13 +00001002
1003 if (StringClass.empty()) StringClass = "NXConstantString";
1004
1005 std::string Sym = "_OBJC_CLASS_";
1006 Sym += StringClass;
1007
1008 llvm::Constant *isa = TheModule.getNamedGlobal(Sym);
1009
1010 if (!isa)
1011 isa = new llvm::GlobalVariable(TheModule, IdTy, /* isConstant */false,
1012 llvm::GlobalValue::ExternalWeakLinkage, 0, Sym);
1013 else if (isa->getType() != PtrToIdTy)
1014 isa = llvm::ConstantExpr::getBitCast(isa, PtrToIdTy);
1015
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001016 std::vector<llvm::Constant*> Ivars;
David Chisnall13df6f62012-01-04 12:02:13 +00001017 Ivars.push_back(isa);
Chris Lattner13fd7e52008-06-21 21:44:18 +00001018 Ivars.push_back(MakeConstantString(Str));
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001019 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001020 llvm::Constant *ObjCStr = MakeGlobal(
David Chisnall13df6f62012-01-04 12:02:13 +00001021 llvm::StructType::get(PtrToIdTy, PtrToInt8Ty, IntTy, NULL),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001022 Ivars, ".objc_str");
David Chisnall48272a02010-01-27 12:49:23 +00001023 ObjCStr = llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty);
1024 ObjCStrings[Str] = ObjCStr;
1025 ConstantStrings.push_back(ObjCStr);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001026 return ObjCStr;
1027}
1028
1029///Generates a message send where the super is the receiver. This is a message
1030///send to self with special delivery semantics indicating which class's method
1031///should be called.
David Chisnall9f6614e2011-03-23 16:36:54 +00001032RValue
1033CGObjCGNU::GenerateMessageSendSuper(CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001034 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001035 QualType ResultType,
1036 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001037 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001038 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001039 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001040 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001041 const CallArgList &CallArgs,
1042 const ObjCMethodDecl *Method) {
David Chisnall0bbe0cf2011-05-28 14:09:01 +00001043 CGBuilderTy &Builder = CGF.Builder;
David Blaikie4e4d0842012-03-11 07:00:24 +00001044 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
David Chisnallef6e0f32010-02-03 15:59:02 +00001045 if (Sel == RetainSel || Sel == AutoreleaseSel) {
David Chisnall0bbe0cf2011-05-28 14:09:01 +00001046 return RValue::get(EnforceType(Builder, Receiver,
1047 CGM.getTypes().ConvertType(ResultType)));
David Chisnallef6e0f32010-02-03 15:59:02 +00001048 }
1049 if (Sel == ReleaseSel) {
1050 return RValue::get(0);
1051 }
1052 }
David Chisnalldb831942010-05-01 12:37:16 +00001053
David Chisnalldb831942010-05-01 12:37:16 +00001054 llvm::Value *cmd = GetSelector(Builder, Sel);
1055
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +00001056
1057 CallArgList ActualArgs;
1058
Eli Friedman04c9a492011-05-02 17:57:46 +00001059 ActualArgs.add(RValue::get(EnforceType(Builder, Receiver, IdTy)), ASTIdTy);
1060 ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType());
John McCallf85e1932011-06-15 23:02:42 +00001061 ActualArgs.addFrom(CallArgs);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +00001062
John McCallde5d3c72012-02-17 03:33:10 +00001063 MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +00001064
Daniel Dunbar5efccb12009-05-04 15:31:17 +00001065 llvm::Value *ReceiverClass = 0;
Chris Lattner48e6e7e2009-05-08 15:39:58 +00001066 if (isCategoryImpl) {
1067 llvm::Constant *classLookupFunction = 0;
Chris Lattner48e6e7e2009-05-08 15:39:58 +00001068 if (IsClassMessage) {
Owen Anderson96e0fc72009-07-29 22:16:19 +00001069 classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Jay Foadda549e82011-07-29 13:56:53 +00001070 IdTy, PtrTy, true), "objc_get_meta_class");
Chris Lattner48e6e7e2009-05-08 15:39:58 +00001071 } else {
Owen Anderson96e0fc72009-07-29 22:16:19 +00001072 classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Jay Foadda549e82011-07-29 13:56:53 +00001073 IdTy, PtrTy, true), "objc_get_class");
Daniel Dunbar5efccb12009-05-04 15:31:17 +00001074 }
David Chisnalldb831942010-05-01 12:37:16 +00001075 ReceiverClass = Builder.CreateCall(classLookupFunction,
Chris Lattner48e6e7e2009-05-08 15:39:58 +00001076 MakeConstantString(Class->getNameAsString()));
Daniel Dunbar5efccb12009-05-04 15:31:17 +00001077 } else {
Chris Lattner48e6e7e2009-05-08 15:39:58 +00001078 // Set up global aliases for the metaclass or class pointer if they do not
1079 // already exist. These will are forward-references which will be set to
Mike Stumpbb1c8602009-07-31 21:31:32 +00001080 // pointers to the class and metaclass structure created for the runtime
1081 // load function. To send a message to super, we look up the value of the
Chris Lattner48e6e7e2009-05-08 15:39:58 +00001082 // super_class pointer from either the class or metaclass structure.
1083 if (IsClassMessage) {
1084 if (!MetaClassPtrAlias) {
1085 MetaClassPtrAlias = new llvm::GlobalAlias(IdTy,
1086 llvm::GlobalValue::InternalLinkage, ".objc_metaclass_ref" +
1087 Class->getNameAsString(), NULL, &TheModule);
1088 }
1089 ReceiverClass = MetaClassPtrAlias;
1090 } else {
1091 if (!ClassPtrAlias) {
1092 ClassPtrAlias = new llvm::GlobalAlias(IdTy,
1093 llvm::GlobalValue::InternalLinkage, ".objc_class_ref" +
1094 Class->getNameAsString(), NULL, &TheModule);
1095 }
1096 ReceiverClass = ClassPtrAlias;
Daniel Dunbar5efccb12009-05-04 15:31:17 +00001097 }
Chris Lattner71238f62009-04-25 23:19:45 +00001098 }
Daniel Dunbar5efccb12009-05-04 15:31:17 +00001099 // Cast the pointer to a simplified version of the class structure
David Chisnalldb831942010-05-01 12:37:16 +00001100 ReceiverClass = Builder.CreateBitCast(ReceiverClass,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001101 llvm::PointerType::getUnqual(
Chris Lattner7650d952011-06-18 22:49:11 +00001102 llvm::StructType::get(IdTy, IdTy, NULL)));
Daniel Dunbar5efccb12009-05-04 15:31:17 +00001103 // Get the superclass pointer
David Chisnalldb831942010-05-01 12:37:16 +00001104 ReceiverClass = Builder.CreateStructGEP(ReceiverClass, 1);
Daniel Dunbar5efccb12009-05-04 15:31:17 +00001105 // Load the superclass pointer
David Chisnalldb831942010-05-01 12:37:16 +00001106 ReceiverClass = Builder.CreateLoad(ReceiverClass);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001107 // Construct the structure used to look up the IMP
Chris Lattner7650d952011-06-18 22:49:11 +00001108 llvm::StructType *ObjCSuperTy = llvm::StructType::get(
Owen Anderson47a434f2009-08-05 23:18:46 +00001109 Receiver->getType(), IdTy, NULL);
David Chisnalldb831942010-05-01 12:37:16 +00001110 llvm::Value *ObjCSuper = Builder.CreateAlloca(ObjCSuperTy);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +00001111
David Chisnalldb831942010-05-01 12:37:16 +00001112 Builder.CreateStore(Receiver, Builder.CreateStructGEP(ObjCSuper, 0));
1113 Builder.CreateStore(ReceiverClass, Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001114
David Chisnallc7ef4622011-03-23 22:52:06 +00001115 ObjCSuper = EnforceType(Builder, ObjCSuper, PtrToObjCSuperTy);
David Chisnallc7ef4622011-03-23 22:52:06 +00001116
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001117 // Get the IMP
David Chisnallc7ef4622011-03-23 22:52:06 +00001118 llvm::Value *imp = LookupIMPSuper(CGF, ObjCSuper, cmd);
John McCallde5d3c72012-02-17 03:33:10 +00001119 imp = EnforceType(Builder, imp, MSI.MessengerType);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001120
David Chisnalldd5c98f2010-05-01 11:15:56 +00001121 llvm::Value *impMD[] = {
1122 llvm::MDString::get(VMContext, Sel.getAsString()),
1123 llvm::MDString::get(VMContext, Class->getSuperClass()->getNameAsString()),
1124 llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsClassMessage)
1125 };
Jay Foad6f141652011-04-21 19:59:12 +00001126 llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD);
David Chisnalldd5c98f2010-05-01 11:15:56 +00001127
David Chisnall4b02afc2010-05-02 13:41:58 +00001128 llvm::Instruction *call;
John McCallde5d3c72012-02-17 03:33:10 +00001129 RValue msgRet = CGF.EmitCall(MSI.CallInfo, imp, Return, ActualArgs, 0, &call);
David Chisnall4b02afc2010-05-02 13:41:58 +00001130 call->setMetadata(msgSendMDKind, node);
1131 return msgRet;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001132}
1133
Mike Stump1eb44332009-09-09 15:08:12 +00001134/// Generate code for a message send expression.
David Chisnall9f6614e2011-03-23 16:36:54 +00001135RValue
1136CGObjCGNU::GenerateMessageSend(CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001137 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001138 QualType ResultType,
1139 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001140 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001141 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001142 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001143 const ObjCMethodDecl *Method) {
David Chisnall0bbe0cf2011-05-28 14:09:01 +00001144 CGBuilderTy &Builder = CGF.Builder;
1145
David Chisnall664b7c72010-04-27 15:08:48 +00001146 // Strip out message sends to retain / release in GC mode
David Blaikie4e4d0842012-03-11 07:00:24 +00001147 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
David Chisnallef6e0f32010-02-03 15:59:02 +00001148 if (Sel == RetainSel || Sel == AutoreleaseSel) {
David Chisnall0bbe0cf2011-05-28 14:09:01 +00001149 return RValue::get(EnforceType(Builder, Receiver,
1150 CGM.getTypes().ConvertType(ResultType)));
David Chisnallef6e0f32010-02-03 15:59:02 +00001151 }
1152 if (Sel == ReleaseSel) {
1153 return RValue::get(0);
1154 }
1155 }
David Chisnall664b7c72010-04-27 15:08:48 +00001156
David Chisnall664b7c72010-04-27 15:08:48 +00001157 // If the return type is something that goes in an integer register, the
1158 // runtime will handle 0 returns. For other cases, we fill in the 0 value
1159 // ourselves.
1160 //
1161 // The language spec says the result of this kind of message send is
1162 // undefined, but lots of people seem to have forgotten to read that
1163 // paragraph and insist on sending messages to nil that have structure
1164 // returns. With GCC, this generates a random return value (whatever happens
1165 // to be on the stack / in those registers at the time) on most platforms,
David Chisnallc7ef4622011-03-23 22:52:06 +00001166 // and generates an illegal instruction trap on SPARC. With LLVM it corrupts
1167 // the stack.
1168 bool isPointerSizedReturn = (ResultType->isAnyPointerType() ||
1169 ResultType->isIntegralOrEnumerationType() || ResultType->isVoidType());
David Chisnall664b7c72010-04-27 15:08:48 +00001170
1171 llvm::BasicBlock *startBB = 0;
1172 llvm::BasicBlock *messageBB = 0;
David Chisnalla54da052010-05-20 13:45:48 +00001173 llvm::BasicBlock *continueBB = 0;
David Chisnall664b7c72010-04-27 15:08:48 +00001174
1175 if (!isPointerSizedReturn) {
1176 startBB = Builder.GetInsertBlock();
1177 messageBB = CGF.createBasicBlock("msgSend");
David Chisnalla54da052010-05-20 13:45:48 +00001178 continueBB = CGF.createBasicBlock("continue");
David Chisnall664b7c72010-04-27 15:08:48 +00001179
1180 llvm::Value *isNil = Builder.CreateICmpEQ(Receiver,
1181 llvm::Constant::getNullValue(Receiver->getType()));
David Chisnalla54da052010-05-20 13:45:48 +00001182 Builder.CreateCondBr(isNil, continueBB, messageBB);
David Chisnall664b7c72010-04-27 15:08:48 +00001183 CGF.EmitBlock(messageBB);
1184 }
1185
David Chisnall0f436562009-08-17 16:35:33 +00001186 IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001187 llvm::Value *cmd;
1188 if (Method)
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001189 cmd = GetSelector(Builder, Method);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001190 else
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001191 cmd = GetSelector(Builder, Sel);
David Chisnallc7ef4622011-03-23 22:52:06 +00001192 cmd = EnforceType(Builder, cmd, SelectorTy);
1193 Receiver = EnforceType(Builder, Receiver, IdTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001194
David Chisnallc7ef4622011-03-23 22:52:06 +00001195 llvm::Value *impMD[] = {
1196 llvm::MDString::get(VMContext, Sel.getAsString()),
1197 llvm::MDString::get(VMContext, Class ? Class->getNameAsString() :""),
1198 llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), Class!=0)
1199 };
Jay Foad6f141652011-04-21 19:59:12 +00001200 llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD);
David Chisnallc7ef4622011-03-23 22:52:06 +00001201
David Chisnallc7ef4622011-03-23 22:52:06 +00001202 CallArgList ActualArgs;
Eli Friedman04c9a492011-05-02 17:57:46 +00001203 ActualArgs.add(RValue::get(Receiver), ASTIdTy);
1204 ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType());
John McCallf85e1932011-06-15 23:02:42 +00001205 ActualArgs.addFrom(CallArgs);
John McCallde5d3c72012-02-17 03:33:10 +00001206
1207 MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
1208
David Chisnall89c30042011-10-24 14:07:03 +00001209 // Get the IMP to call
1210 llvm::Value *imp;
1211
1212 // If we have non-legacy dispatch specified, we try using the objc_msgSend()
1213 // functions. These are not supported on all platforms (or all runtimes on a
1214 // given platform), so we
1215 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
David Chisnall89c30042011-10-24 14:07:03 +00001216 case CodeGenOptions::Legacy:
David Chisnall89c30042011-10-24 14:07:03 +00001217 imp = LookupIMP(CGF, Receiver, cmd, node);
1218 break;
1219 case CodeGenOptions::Mixed:
David Chisnall89c30042011-10-24 14:07:03 +00001220 case CodeGenOptions::NonLegacy:
David Chisnall6f3887e2011-10-28 17:55:06 +00001221 if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1222 imp = CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true),
1223 "objc_msgSend_fpret");
John McCallde5d3c72012-02-17 03:33:10 +00001224 } else if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
David Chisnall89c30042011-10-24 14:07:03 +00001225 // The actual types here don't matter - we're going to bitcast the
1226 // function anyway
1227 imp = CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true),
1228 "objc_msgSend_stret");
1229 } else {
1230 imp = CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true),
1231 "objc_msgSend");
1232 }
1233 }
1234
David Chisnall403bc3f2011-12-01 18:40:09 +00001235 // Reset the receiver in case the lookup modified it
1236 ActualArgs[0] = CallArg(RValue::get(Receiver), ASTIdTy, false);
David Chisnall89c30042011-10-24 14:07:03 +00001237
John McCallde5d3c72012-02-17 03:33:10 +00001238 imp = EnforceType(Builder, imp, MSI.MessengerType);
David Chisnall63e742b2010-05-01 12:56:56 +00001239
David Chisnall4b02afc2010-05-02 13:41:58 +00001240 llvm::Instruction *call;
John McCallde5d3c72012-02-17 03:33:10 +00001241 RValue msgRet = CGF.EmitCall(MSI.CallInfo, imp, Return, ActualArgs,
David Chisnall4b02afc2010-05-02 13:41:58 +00001242 0, &call);
1243 call->setMetadata(msgSendMDKind, node);
David Chisnall664b7c72010-04-27 15:08:48 +00001244
David Chisnalla54da052010-05-20 13:45:48 +00001245
David Chisnall664b7c72010-04-27 15:08:48 +00001246 if (!isPointerSizedReturn) {
David Chisnalla54da052010-05-20 13:45:48 +00001247 messageBB = CGF.Builder.GetInsertBlock();
1248 CGF.Builder.CreateBr(continueBB);
1249 CGF.EmitBlock(continueBB);
David Chisnall664b7c72010-04-27 15:08:48 +00001250 if (msgRet.isScalar()) {
1251 llvm::Value *v = msgRet.getScalarVal();
Jay Foadbbf3bac2011-03-30 11:28:58 +00001252 llvm::PHINode *phi = Builder.CreatePHI(v->getType(), 2);
David Chisnall664b7c72010-04-27 15:08:48 +00001253 phi->addIncoming(v, messageBB);
1254 phi->addIncoming(llvm::Constant::getNullValue(v->getType()), startBB);
1255 msgRet = RValue::get(phi);
1256 } else if (msgRet.isAggregate()) {
1257 llvm::Value *v = msgRet.getAggregateAddr();
Jay Foadbbf3bac2011-03-30 11:28:58 +00001258 llvm::PHINode *phi = Builder.CreatePHI(v->getType(), 2);
Chris Lattner2acc6e32011-07-18 04:24:23 +00001259 llvm::PointerType *RetTy = cast<llvm::PointerType>(v->getType());
David Chisnall866163b2010-04-30 13:36:12 +00001260 llvm::AllocaInst *NullVal =
1261 CGF.CreateTempAlloca(RetTy->getElementType(), "null");
David Chisnall664b7c72010-04-27 15:08:48 +00001262 CGF.InitTempAlloca(NullVal,
1263 llvm::Constant::getNullValue(RetTy->getElementType()));
1264 phi->addIncoming(v, messageBB);
1265 phi->addIncoming(NullVal, startBB);
1266 msgRet = RValue::getAggregate(phi);
1267 } else /* isComplex() */ {
1268 std::pair<llvm::Value*,llvm::Value*> v = msgRet.getComplexVal();
Jay Foadbbf3bac2011-03-30 11:28:58 +00001269 llvm::PHINode *phi = Builder.CreatePHI(v.first->getType(), 2);
David Chisnall664b7c72010-04-27 15:08:48 +00001270 phi->addIncoming(v.first, messageBB);
1271 phi->addIncoming(llvm::Constant::getNullValue(v.first->getType()),
1272 startBB);
Jay Foadbbf3bac2011-03-30 11:28:58 +00001273 llvm::PHINode *phi2 = Builder.CreatePHI(v.second->getType(), 2);
David Chisnall664b7c72010-04-27 15:08:48 +00001274 phi2->addIncoming(v.second, messageBB);
1275 phi2->addIncoming(llvm::Constant::getNullValue(v.second->getType()),
1276 startBB);
1277 msgRet = RValue::getComplex(phi, phi2);
1278 }
1279 }
1280 return msgRet;
Chris Lattner0f984262008-03-01 08:50:34 +00001281}
1282
Mike Stump1eb44332009-09-09 15:08:12 +00001283/// Generates a MethodList. Used in construction of a objc_class and
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001284/// objc_category structures.
Bill Wendling795b1002012-02-22 09:30:11 +00001285llvm::Constant *CGObjCGNU::
1286GenerateMethodList(const StringRef &ClassName,
1287 const StringRef &CategoryName,
1288 ArrayRef<Selector> MethodSels,
1289 ArrayRef<llvm::Constant *> MethodTypes,
1290 bool isClassMethodList) {
David Chisnall0f436562009-08-17 16:35:33 +00001291 if (MethodSels.empty())
1292 return NULLPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001293 // Get the method structure type.
Chris Lattner7650d952011-06-18 22:49:11 +00001294 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001295 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
1296 PtrToInt8Ty, // Method types
David Chisnallc7ef4622011-03-23 22:52:06 +00001297 IMPTy, //Method pointer
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001298 NULL);
1299 std::vector<llvm::Constant*> Methods;
1300 std::vector<llvm::Constant*> Elements;
1301 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
1302 Elements.clear();
David Chisnall9f6614e2011-03-23 16:36:54 +00001303 llvm::Constant *Method =
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001304 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
David Chisnall9f6614e2011-03-23 16:36:54 +00001305 MethodSels[i],
1306 isClassMethodList));
1307 assert(Method && "Can't generate metadata for method that doesn't exist");
1308 llvm::Constant *C = MakeConstantString(MethodSels[i].getAsString());
1309 Elements.push_back(C);
1310 Elements.push_back(MethodTypes[i]);
1311 Method = llvm::ConstantExpr::getBitCast(Method,
David Chisnallc7ef4622011-03-23 22:52:06 +00001312 IMPTy);
David Chisnall9f6614e2011-03-23 16:36:54 +00001313 Elements.push_back(Method);
1314 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001315 }
1316
1317 // Array of method structures
Owen Anderson96e0fc72009-07-29 22:16:19 +00001318 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Fariborz Jahanian1e64a952009-05-17 16:49:27 +00001319 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00001320 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerfba67632008-06-26 04:52:29 +00001321 Methods);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001322
1323 // Structure containing list pointer, array and array count
Chris Lattnerc1c20112011-08-12 17:43:31 +00001324 llvm::StructType *ObjCMethodListTy = llvm::StructType::create(VMContext);
Chris Lattner9cbe4f02011-07-09 17:41:47 +00001325 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(ObjCMethodListTy);
1326 ObjCMethodListTy->setBody(
Mike Stump1eb44332009-09-09 15:08:12 +00001327 NextPtrTy,
1328 IntTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001329 ObjCMethodArrayTy,
1330 NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001331
1332 Methods.clear();
Owen Anderson03e20502009-07-30 23:11:26 +00001333 Methods.push_back(llvm::ConstantPointerNull::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00001334 llvm::PointerType::getUnqual(ObjCMethodListTy)));
David Chisnall917b28b2011-10-04 15:35:30 +00001335 Methods.push_back(llvm::ConstantInt::get(Int32Ty, MethodTypes.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001336 Methods.push_back(MethodArray);
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001338 // Create an instance of the structure
1339 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
1340}
1341
1342/// Generates an IvarList. Used in construction of a objc_class.
Bill Wendling795b1002012-02-22 09:30:11 +00001343llvm::Constant *CGObjCGNU::
1344GenerateIvarList(ArrayRef<llvm::Constant *> IvarNames,
1345 ArrayRef<llvm::Constant *> IvarTypes,
1346 ArrayRef<llvm::Constant *> IvarOffsets) {
David Chisnall18044632009-11-16 19:05:54 +00001347 if (IvarNames.size() == 0)
1348 return NULLPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001349 // Get the method structure type.
Chris Lattner7650d952011-06-18 22:49:11 +00001350 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001351 PtrToInt8Ty,
1352 PtrToInt8Ty,
1353 IntTy,
1354 NULL);
1355 std::vector<llvm::Constant*> Ivars;
1356 std::vector<llvm::Constant*> Elements;
1357 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
1358 Elements.clear();
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001359 Elements.push_back(IvarNames[i]);
1360 Elements.push_back(IvarTypes[i]);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001361 Elements.push_back(IvarOffsets[i]);
Owen Anderson08e25242009-07-27 22:29:56 +00001362 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001363 }
1364
1365 // Array of method structures
Owen Anderson96e0fc72009-07-29 22:16:19 +00001366 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001367 IvarNames.size());
1368
Mike Stump1eb44332009-09-09 15:08:12 +00001369
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001370 Elements.clear();
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001371 Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
Owen Anderson7db6d832009-07-28 18:33:04 +00001372 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001373 // Structure containing array and array count
Chris Lattner7650d952011-06-18 22:49:11 +00001374 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001375 ObjCIvarArrayTy,
1376 NULL);
1377
1378 // Create an instance of the structure
1379 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
1380}
1381
1382/// Generate a class structure
1383llvm::Constant *CGObjCGNU::GenerateClassStructure(
1384 llvm::Constant *MetaClass,
1385 llvm::Constant *SuperClass,
1386 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +00001387 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001388 llvm::Constant *Version,
1389 llvm::Constant *InstanceSize,
1390 llvm::Constant *IVars,
1391 llvm::Constant *Methods,
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001392 llvm::Constant *Protocols,
1393 llvm::Constant *IvarOffsets,
David Chisnall8c757f92010-04-28 14:29:56 +00001394 llvm::Constant *Properties,
David Chisnall917b28b2011-10-04 15:35:30 +00001395 llvm::Constant *StrongIvarBitmap,
1396 llvm::Constant *WeakIvarBitmap,
David Chisnall8c757f92010-04-28 14:29:56 +00001397 bool isMeta) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001398 // Set up the class structure
1399 // Note: Several of these are char*s when they should be ids. This is
1400 // because the runtime performs this translation on load.
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001401 //
1402 // Fields marked New ABI are part of the GNUstep runtime. We emit them
1403 // anyway; the classes will still work with the GNU runtime, they will just
1404 // be ignored.
Chris Lattner7650d952011-06-18 22:49:11 +00001405 llvm::StructType *ClassTy = llvm::StructType::get(
David Chisnall13df6f62012-01-04 12:02:13 +00001406 PtrToInt8Ty, // isa
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001407 PtrToInt8Ty, // super_class
1408 PtrToInt8Ty, // name
1409 LongTy, // version
1410 LongTy, // info
1411 LongTy, // instance_size
1412 IVars->getType(), // ivars
1413 Methods->getType(), // methods
Mike Stump1eb44332009-09-09 15:08:12 +00001414 // These are all filled in by the runtime, so we pretend
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001415 PtrTy, // dtable
1416 PtrTy, // subclass_list
1417 PtrTy, // sibling_class
1418 PtrTy, // protocols
1419 PtrTy, // gc_object_type
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001420 // New ABI:
1421 LongTy, // abi_version
1422 IvarOffsets->getType(), // ivar_offsets
1423 Properties->getType(), // properties
David Chisnall9d06ba82011-10-25 10:12:21 +00001424 IntPtrTy, // strong_pointers
1425 IntPtrTy, // weak_pointers
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001426 NULL);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001427 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001428 // Fill in the structure
1429 std::vector<llvm::Constant*> Elements;
Owen Anderson3c4972d2009-07-29 18:54:39 +00001430 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001431 Elements.push_back(SuperClass);
Chris Lattnerd002cc62008-06-26 04:47:04 +00001432 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001433 Elements.push_back(Zero);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001434 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
David Chisnall05f3a502011-02-21 23:47:40 +00001435 if (isMeta) {
1436 llvm::TargetData td(&TheModule);
Ken Dycke0afc892011-04-22 17:59:22 +00001437 Elements.push_back(
1438 llvm::ConstantInt::get(LongTy,
1439 td.getTypeSizeInBits(ClassTy) /
1440 CGM.getContext().getCharWidth()));
David Chisnall05f3a502011-02-21 23:47:40 +00001441 } else
1442 Elements.push_back(InstanceSize);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001443 Elements.push_back(IVars);
1444 Elements.push_back(Methods);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001445 Elements.push_back(NULLPtr);
1446 Elements.push_back(NULLPtr);
1447 Elements.push_back(NULLPtr);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001448 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001449 Elements.push_back(NULLPtr);
David Chisnall917b28b2011-10-04 15:35:30 +00001450 Elements.push_back(llvm::ConstantInt::get(LongTy, 1));
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001451 Elements.push_back(IvarOffsets);
1452 Elements.push_back(Properties);
David Chisnall917b28b2011-10-04 15:35:30 +00001453 Elements.push_back(StrongIvarBitmap);
1454 Elements.push_back(WeakIvarBitmap);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001455 // Create an instance of the structure
David Chisnall41d63ed2010-01-08 00:14:31 +00001456 // This is now an externally visible symbol, so that we can speed up class
David Chisnall13df6f62012-01-04 12:02:13 +00001457 // messages in the next ABI. We may already have some weak references to
1458 // this, so check and fix them properly.
1459 std::string ClassSym((isMeta ? "_OBJC_METACLASS_": "_OBJC_CLASS_") +
1460 std::string(Name));
1461 llvm::GlobalVariable *ClassRef = TheModule.getNamedGlobal(ClassSym);
1462 llvm::Constant *Class = MakeGlobal(ClassTy, Elements, ClassSym,
1463 llvm::GlobalValue::ExternalLinkage);
1464 if (ClassRef) {
1465 ClassRef->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(Class,
1466 ClassRef->getType()));
1467 ClassRef->removeFromParent();
1468 Class->setName(ClassSym);
1469 }
1470 return Class;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001471}
1472
Bill Wendling795b1002012-02-22 09:30:11 +00001473llvm::Constant *CGObjCGNU::
1474GenerateProtocolMethodList(ArrayRef<llvm::Constant *> MethodNames,
1475 ArrayRef<llvm::Constant *> MethodTypes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001476 // Get the method structure type.
Chris Lattner7650d952011-06-18 22:49:11 +00001477 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001478 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
1479 PtrToInt8Ty,
1480 NULL);
1481 std::vector<llvm::Constant*> Methods;
1482 std::vector<llvm::Constant*> Elements;
1483 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
1484 Elements.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001485 Elements.push_back(MethodNames[i]);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001486 Elements.push_back(MethodTypes[i]);
Owen Anderson08e25242009-07-27 22:29:56 +00001487 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001488 }
Owen Anderson96e0fc72009-07-29 22:16:19 +00001489 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001490 MethodNames.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00001491 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy,
Mike Stumpbb1c8602009-07-31 21:31:32 +00001492 Methods);
Chris Lattner7650d952011-06-18 22:49:11 +00001493 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001494 IntTy, ObjCMethodArrayTy, NULL);
1495 Methods.clear();
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001496 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001497 Methods.push_back(Array);
1498 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
1499}
Mike Stumpbb1c8602009-07-31 21:31:32 +00001500
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001501// Create the protocol list structure used in classes, categories and so on
Bill Wendling795b1002012-02-22 09:30:11 +00001502llvm::Constant *CGObjCGNU::GenerateProtocolList(ArrayRef<std::string>Protocols){
Owen Anderson96e0fc72009-07-29 22:16:19 +00001503 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001504 Protocols.size());
Chris Lattner7650d952011-06-18 22:49:11 +00001505 llvm::StructType *ProtocolListTy = llvm::StructType::get(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001506 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
David Chisnall8fac25d2010-12-26 22:13:16 +00001507 SizeTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001508 ProtocolArrayTy,
1509 NULL);
Mike Stump1eb44332009-09-09 15:08:12 +00001510 std::vector<llvm::Constant*> Elements;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001511 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
1512 iter != endIter ; iter++) {
David Chisnallff80fab2009-11-20 14:50:59 +00001513 llvm::Constant *protocol = 0;
1514 llvm::StringMap<llvm::Constant*>::iterator value =
1515 ExistingProtocols.find(*iter);
1516 if (value == ExistingProtocols.end()) {
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +00001517 protocol = GenerateEmptyProtocol(*iter);
David Chisnallff80fab2009-11-20 14:50:59 +00001518 } else {
1519 protocol = value->getValue();
1520 }
Owen Anderson3c4972d2009-07-29 18:54:39 +00001521 llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(protocol,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001522 PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001523 Elements.push_back(Ptr);
1524 }
Owen Anderson7db6d832009-07-28 18:33:04 +00001525 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001526 Elements);
1527 Elements.clear();
1528 Elements.push_back(NULLPtr);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001529 Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001530 Elements.push_back(ProtocolArray);
1531 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
1532}
1533
Mike Stump1eb44332009-09-09 15:08:12 +00001534llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001535 const ObjCProtocolDecl *PD) {
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +00001536 llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()];
Chris Lattner2acc6e32011-07-18 04:24:23 +00001537 llvm::Type *T =
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +00001538 CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00001539 return Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +00001540}
1541
1542llvm::Constant *CGObjCGNU::GenerateEmptyProtocol(
1543 const std::string &ProtocolName) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001544 SmallVector<std::string, 0> EmptyStringVector;
1545 SmallVector<llvm::Constant*, 0> EmptyConstantVector;
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +00001546
1547 llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001548 llvm::Constant *MethodList =
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +00001549 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
1550 // Protocols are objects containing lists of the methods implemented and
1551 // protocols adopted.
Chris Lattner7650d952011-06-18 22:49:11 +00001552 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +00001553 PtrToInt8Ty,
1554 ProtocolList->getType(),
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001555 MethodList->getType(),
1556 MethodList->getType(),
1557 MethodList->getType(),
1558 MethodList->getType(),
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +00001559 NULL);
Mike Stump1eb44332009-09-09 15:08:12 +00001560 std::vector<llvm::Constant*> Elements;
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +00001561 // The isa pointer must be set to a magic number so the runtime knows it's
1562 // the correct layout.
Owen Anderson3c4972d2009-07-29 18:54:39 +00001563 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
David Chisnall917b28b2011-10-04 15:35:30 +00001564 llvm::ConstantInt::get(Int32Ty, ProtocolVersion), IdTy));
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +00001565 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
1566 Elements.push_back(ProtocolList);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001567 Elements.push_back(MethodList);
1568 Elements.push_back(MethodList);
1569 Elements.push_back(MethodList);
1570 Elements.push_back(MethodList);
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +00001571 return MakeGlobal(ProtocolTy, Elements, ".objc_protocol");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001572}
1573
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001574void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
1575 ASTContext &Context = CGM.getContext();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001576 std::string ProtocolName = PD->getNameAsString();
Douglas Gregor1d784b22012-01-01 19:51:50 +00001577
1578 // Use the protocol definition, if there is one.
1579 if (const ObjCProtocolDecl *Def = PD->getDefinition())
1580 PD = Def;
1581
Chris Lattner5f9e2722011-07-23 10:55:15 +00001582 SmallVector<std::string, 16> Protocols;
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001583 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
1584 E = PD->protocol_end(); PI != E; ++PI)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001585 Protocols.push_back((*PI)->getNameAsString());
Chris Lattner5f9e2722011-07-23 10:55:15 +00001586 SmallVector<llvm::Constant*, 16> InstanceMethodNames;
1587 SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
1588 SmallVector<llvm::Constant*, 16> OptionalInstanceMethodNames;
1589 SmallVector<llvm::Constant*, 16> OptionalInstanceMethodTypes;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001590 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
1591 E = PD->instmeth_end(); iter != E; iter++) {
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001592 std::string TypeStr;
1593 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001594 if ((*iter)->getImplementationControl() == ObjCMethodDecl::Optional) {
1595 InstanceMethodNames.push_back(
1596 MakeConstantString((*iter)->getSelector().getAsString()));
1597 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
1598 } else {
1599 OptionalInstanceMethodNames.push_back(
1600 MakeConstantString((*iter)->getSelector().getAsString()));
1601 OptionalInstanceMethodTypes.push_back(MakeConstantString(TypeStr));
1602 }
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001603 }
1604 // Collect information about class methods:
Chris Lattner5f9e2722011-07-23 10:55:15 +00001605 SmallVector<llvm::Constant*, 16> ClassMethodNames;
1606 SmallVector<llvm::Constant*, 16> ClassMethodTypes;
1607 SmallVector<llvm::Constant*, 16> OptionalClassMethodNames;
1608 SmallVector<llvm::Constant*, 16> OptionalClassMethodTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00001609 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001610 iter = PD->classmeth_begin(), endIter = PD->classmeth_end();
1611 iter != endIter ; iter++) {
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001612 std::string TypeStr;
1613 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001614 if ((*iter)->getImplementationControl() == ObjCMethodDecl::Optional) {
1615 ClassMethodNames.push_back(
1616 MakeConstantString((*iter)->getSelector().getAsString()));
1617 ClassMethodTypes.push_back(MakeConstantString(TypeStr));
1618 } else {
1619 OptionalClassMethodNames.push_back(
1620 MakeConstantString((*iter)->getSelector().getAsString()));
1621 OptionalClassMethodTypes.push_back(MakeConstantString(TypeStr));
1622 }
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001623 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001624
1625 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
1626 llvm::Constant *InstanceMethodList =
1627 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
1628 llvm::Constant *ClassMethodList =
1629 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001630 llvm::Constant *OptionalInstanceMethodList =
1631 GenerateProtocolMethodList(OptionalInstanceMethodNames,
1632 OptionalInstanceMethodTypes);
1633 llvm::Constant *OptionalClassMethodList =
1634 GenerateProtocolMethodList(OptionalClassMethodNames,
1635 OptionalClassMethodTypes);
1636
1637 // Property metadata: name, attributes, isSynthesized, setter name, setter
1638 // types, getter name, getter types.
1639 // The isSynthesized value is always set to 0 in a protocol. It exists to
1640 // simplify the runtime library by allowing it to use the same data
1641 // structures for protocol metadata everywhere.
Chris Lattner7650d952011-06-18 22:49:11 +00001642 llvm::StructType *PropertyMetadataTy = llvm::StructType::get(
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001643 PtrToInt8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty,
1644 PtrToInt8Ty, NULL);
1645 std::vector<llvm::Constant*> Properties;
1646 std::vector<llvm::Constant*> OptionalProperties;
1647
1648 // Add all of the property methods need adding to the method list and to the
1649 // property metadata list.
1650 for (ObjCContainerDecl::prop_iterator
1651 iter = PD->prop_begin(), endIter = PD->prop_end();
1652 iter != endIter ; iter++) {
1653 std::vector<llvm::Constant*> Fields;
David Blaikie581deb32012-06-06 20:45:41 +00001654 ObjCPropertyDecl *property = *iter;
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001655
1656 Fields.push_back(MakeConstantString(property->getNameAsString()));
1657 Fields.push_back(llvm::ConstantInt::get(Int8Ty,
1658 property->getPropertyAttributes()));
1659 Fields.push_back(llvm::ConstantInt::get(Int8Ty, 0));
1660 if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
1661 std::string TypeStr;
1662 Context.getObjCEncodingForMethodDecl(getter,TypeStr);
1663 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
1664 InstanceMethodTypes.push_back(TypeEncoding);
1665 Fields.push_back(MakeConstantString(getter->getSelector().getAsString()));
1666 Fields.push_back(TypeEncoding);
1667 } else {
1668 Fields.push_back(NULLPtr);
1669 Fields.push_back(NULLPtr);
1670 }
1671 if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
1672 std::string TypeStr;
1673 Context.getObjCEncodingForMethodDecl(setter,TypeStr);
1674 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
1675 InstanceMethodTypes.push_back(TypeEncoding);
1676 Fields.push_back(MakeConstantString(setter->getSelector().getAsString()));
1677 Fields.push_back(TypeEncoding);
1678 } else {
1679 Fields.push_back(NULLPtr);
1680 Fields.push_back(NULLPtr);
1681 }
1682 if (property->getPropertyImplementation() == ObjCPropertyDecl::Optional) {
1683 OptionalProperties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1684 } else {
1685 Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1686 }
1687 }
1688 llvm::Constant *PropertyArray = llvm::ConstantArray::get(
1689 llvm::ArrayType::get(PropertyMetadataTy, Properties.size()), Properties);
1690 llvm::Constant* PropertyListInitFields[] =
1691 {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray};
1692
1693 llvm::Constant *PropertyListInit =
Chris Lattnerc5cbb902011-06-20 04:01:35 +00001694 llvm::ConstantStruct::getAnon(PropertyListInitFields);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001695 llvm::Constant *PropertyList = new llvm::GlobalVariable(TheModule,
1696 PropertyListInit->getType(), false, llvm::GlobalValue::InternalLinkage,
1697 PropertyListInit, ".objc_property_list");
1698
1699 llvm::Constant *OptionalPropertyArray =
1700 llvm::ConstantArray::get(llvm::ArrayType::get(PropertyMetadataTy,
1701 OptionalProperties.size()) , OptionalProperties);
1702 llvm::Constant* OptionalPropertyListInitFields[] = {
1703 llvm::ConstantInt::get(IntTy, OptionalProperties.size()), NULLPtr,
1704 OptionalPropertyArray };
1705
1706 llvm::Constant *OptionalPropertyListInit =
Chris Lattnerc5cbb902011-06-20 04:01:35 +00001707 llvm::ConstantStruct::getAnon(OptionalPropertyListInitFields);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001708 llvm::Constant *OptionalPropertyList = new llvm::GlobalVariable(TheModule,
1709 OptionalPropertyListInit->getType(), false,
1710 llvm::GlobalValue::InternalLinkage, OptionalPropertyListInit,
1711 ".objc_property_list");
1712
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001713 // Protocols are objects containing lists of the methods implemented and
1714 // protocols adopted.
Chris Lattner7650d952011-06-18 22:49:11 +00001715 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001716 PtrToInt8Ty,
1717 ProtocolList->getType(),
1718 InstanceMethodList->getType(),
1719 ClassMethodList->getType(),
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001720 OptionalInstanceMethodList->getType(),
1721 OptionalClassMethodList->getType(),
1722 PropertyList->getType(),
1723 OptionalPropertyList->getType(),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001724 NULL);
Mike Stump1eb44332009-09-09 15:08:12 +00001725 std::vector<llvm::Constant*> Elements;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001726 // The isa pointer must be set to a magic number so the runtime knows it's
1727 // the correct layout.
Owen Anderson3c4972d2009-07-29 18:54:39 +00001728 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
David Chisnall917b28b2011-10-04 15:35:30 +00001729 llvm::ConstantInt::get(Int32Ty, ProtocolVersion), IdTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001730 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
1731 Elements.push_back(ProtocolList);
1732 Elements.push_back(InstanceMethodList);
1733 Elements.push_back(ClassMethodList);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001734 Elements.push_back(OptionalInstanceMethodList);
1735 Elements.push_back(OptionalClassMethodList);
1736 Elements.push_back(PropertyList);
1737 Elements.push_back(OptionalPropertyList);
Mike Stump1eb44332009-09-09 15:08:12 +00001738 ExistingProtocols[ProtocolName] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00001739 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001740 ".objc_protocol"), IdTy);
1741}
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001742void CGObjCGNU::GenerateProtocolHolderCategory(void) {
1743 // Collect information about instance methods
Chris Lattner5f9e2722011-07-23 10:55:15 +00001744 SmallVector<Selector, 1> MethodSels;
1745 SmallVector<llvm::Constant*, 1> MethodTypes;
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001746
1747 std::vector<llvm::Constant*> Elements;
1748 const std::string ClassName = "__ObjC_Protocol_Holder_Ugly_Hack";
1749 const std::string CategoryName = "AnotherHack";
1750 Elements.push_back(MakeConstantString(CategoryName));
1751 Elements.push_back(MakeConstantString(ClassName));
1752 // Instance method list
1753 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1754 ClassName, CategoryName, MethodSels, MethodTypes, false), PtrTy));
1755 // Class method list
1756 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1757 ClassName, CategoryName, MethodSels, MethodTypes, true), PtrTy));
1758 // Protocol list
1759 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrTy,
1760 ExistingProtocols.size());
Chris Lattner7650d952011-06-18 22:49:11 +00001761 llvm::StructType *ProtocolListTy = llvm::StructType::get(
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001762 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
David Chisnall8fac25d2010-12-26 22:13:16 +00001763 SizeTy,
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001764 ProtocolArrayTy,
1765 NULL);
1766 std::vector<llvm::Constant*> ProtocolElements;
1767 for (llvm::StringMapIterator<llvm::Constant*> iter =
1768 ExistingProtocols.begin(), endIter = ExistingProtocols.end();
1769 iter != endIter ; iter++) {
1770 llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(iter->getValue(),
1771 PtrTy);
1772 ProtocolElements.push_back(Ptr);
1773 }
1774 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
1775 ProtocolElements);
1776 ProtocolElements.clear();
1777 ProtocolElements.push_back(NULLPtr);
1778 ProtocolElements.push_back(llvm::ConstantInt::get(LongTy,
1779 ExistingProtocols.size()));
1780 ProtocolElements.push_back(ProtocolArray);
1781 Elements.push_back(llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolListTy,
1782 ProtocolElements, ".objc_protocol_list"), PtrTy));
1783 Categories.push_back(llvm::ConstantExpr::getBitCast(
Chris Lattner7650d952011-06-18 22:49:11 +00001784 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty,
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001785 PtrTy, PtrTy, PtrTy, NULL), Elements), PtrTy));
1786}
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001787
David Chisnall917b28b2011-10-04 15:35:30 +00001788/// Libobjc2 uses a bitfield representation where small(ish) bitfields are
1789/// stored in a 64-bit value with the low bit set to 1 and the remaining 63
1790/// bits set to their values, LSB first, while larger ones are stored in a
1791/// structure of this / form:
1792///
1793/// struct { int32_t length; int32_t values[length]; };
1794///
1795/// The values in the array are stored in host-endian format, with the least
1796/// significant bit being assumed to come first in the bitfield. Therefore, a
1797/// bitfield with the 64th bit set will be (int64_t)&{ 2, [0, 1<<31] }, while a
1798/// bitfield / with the 63rd bit set will be 1<<64.
Bill Wendling795b1002012-02-22 09:30:11 +00001799llvm::Constant *CGObjCGNU::MakeBitField(ArrayRef<bool> bits) {
David Chisnall917b28b2011-10-04 15:35:30 +00001800 int bitCount = bits.size();
David Chisnall9d06ba82011-10-25 10:12:21 +00001801 int ptrBits =
1802 (TheModule.getPointerSize() == llvm::Module::Pointer32) ? 32 : 64;
1803 if (bitCount < ptrBits) {
David Chisnall917b28b2011-10-04 15:35:30 +00001804 uint64_t val = 1;
1805 for (int i=0 ; i<bitCount ; ++i) {
Eli Friedmane3c944a2011-10-08 01:03:47 +00001806 if (bits[i]) val |= 1ULL<<(i+1);
David Chisnall917b28b2011-10-04 15:35:30 +00001807 }
David Chisnall9d06ba82011-10-25 10:12:21 +00001808 return llvm::ConstantInt::get(IntPtrTy, val);
David Chisnall917b28b2011-10-04 15:35:30 +00001809 }
1810 llvm::SmallVector<llvm::Constant*, 8> values;
1811 int v=0;
1812 while (v < bitCount) {
1813 int32_t word = 0;
1814 for (int i=0 ; (i<32) && (v<bitCount) ; ++i) {
1815 if (bits[v]) word |= 1<<i;
1816 v++;
1817 }
1818 values.push_back(llvm::ConstantInt::get(Int32Ty, word));
1819 }
1820 llvm::ArrayType *arrayTy = llvm::ArrayType::get(Int32Ty, values.size());
1821 llvm::Constant *array = llvm::ConstantArray::get(arrayTy, values);
1822 llvm::Constant *fields[2] = {
1823 llvm::ConstantInt::get(Int32Ty, values.size()),
1824 array };
1825 llvm::Constant *GS = MakeGlobal(llvm::StructType::get(Int32Ty, arrayTy,
1826 NULL), fields);
David Chisnall49de5282011-10-08 08:54:36 +00001827 llvm::Constant *ptr = llvm::ConstantExpr::getPtrToInt(GS, IntPtrTy);
David Chisnall49de5282011-10-08 08:54:36 +00001828 return ptr;
David Chisnall917b28b2011-10-04 15:35:30 +00001829}
1830
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001831void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Chris Lattner8ec03f52008-11-24 03:54:41 +00001832 std::string ClassName = OCD->getClassInterface()->getNameAsString();
1833 std::string CategoryName = OCD->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001834 // Collect information about instance methods
Chris Lattner5f9e2722011-07-23 10:55:15 +00001835 SmallVector<Selector, 16> InstanceMethodSels;
1836 SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001837 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001838 iter = OCD->instmeth_begin(), endIter = OCD->instmeth_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00001839 iter != endIter ; iter++) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001840 InstanceMethodSels.push_back((*iter)->getSelector());
1841 std::string TypeStr;
1842 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001843 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001844 }
1845
1846 // Collect information about class methods
Chris Lattner5f9e2722011-07-23 10:55:15 +00001847 SmallVector<Selector, 16> ClassMethodSels;
1848 SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00001849 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001850 iter = OCD->classmeth_begin(), endIter = OCD->classmeth_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00001851 iter != endIter ; iter++) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001852 ClassMethodSels.push_back((*iter)->getSelector());
1853 std::string TypeStr;
1854 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001855 ClassMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001856 }
1857
1858 // Collect the names of referenced protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00001859 SmallVector<std::string, 16> Protocols;
David Chisnallad9e06d2010-03-13 22:20:45 +00001860 const ObjCCategoryDecl *CatDecl = OCD->getCategoryDecl();
1861 const ObjCList<ObjCProtocolDecl> &Protos = CatDecl->getReferencedProtocols();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001862 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
1863 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001864 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001865
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001866 std::vector<llvm::Constant*> Elements;
1867 Elements.push_back(MakeConstantString(CategoryName));
1868 Elements.push_back(MakeConstantString(ClassName));
Mike Stump1eb44332009-09-09 15:08:12 +00001869 // Instance method list
Owen Anderson3c4972d2009-07-29 18:54:39 +00001870 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +00001871 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001872 false), PtrTy));
1873 // Class method list
Owen Anderson3c4972d2009-07-29 18:54:39 +00001874 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +00001875 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001876 PtrTy));
1877 // Protocol list
Owen Anderson3c4972d2009-07-29 18:54:39 +00001878 Elements.push_back(llvm::ConstantExpr::getBitCast(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001879 GenerateProtocolList(Protocols), PtrTy));
Owen Anderson3c4972d2009-07-29 18:54:39 +00001880 Categories.push_back(llvm::ConstantExpr::getBitCast(
Chris Lattner7650d952011-06-18 22:49:11 +00001881 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty,
Owen Anderson47a434f2009-08-05 23:18:46 +00001882 PtrTy, PtrTy, PtrTy, NULL), Elements), PtrTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001883}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001884
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001885llvm::Constant *CGObjCGNU::GeneratePropertyList(const ObjCImplementationDecl *OID,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001886 SmallVectorImpl<Selector> &InstanceMethodSels,
1887 SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes) {
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001888 ASTContext &Context = CGM.getContext();
1889 //
1890 // Property metadata: name, attributes, isSynthesized, setter name, setter
1891 // types, getter name, getter types.
Chris Lattner7650d952011-06-18 22:49:11 +00001892 llvm::StructType *PropertyMetadataTy = llvm::StructType::get(
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001893 PtrToInt8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty,
1894 PtrToInt8Ty, NULL);
1895 std::vector<llvm::Constant*> Properties;
1896
1897
1898 // Add all of the property methods need adding to the method list and to the
1899 // property metadata list.
1900 for (ObjCImplDecl::propimpl_iterator
1901 iter = OID->propimpl_begin(), endIter = OID->propimpl_end();
1902 iter != endIter ; iter++) {
1903 std::vector<llvm::Constant*> Fields;
David Blaikie262bc182012-04-30 02:36:29 +00001904 ObjCPropertyDecl *property = iter->getPropertyDecl();
David Blaikie581deb32012-06-06 20:45:41 +00001905 ObjCPropertyImplDecl *propertyImpl = *iter;
David Chisnall42ba04a2010-02-26 01:11:38 +00001906 bool isSynthesized = (propertyImpl->getPropertyImplementation() ==
1907 ObjCPropertyImplDecl::Synthesize);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001908
1909 Fields.push_back(MakeConstantString(property->getNameAsString()));
1910 Fields.push_back(llvm::ConstantInt::get(Int8Ty,
1911 property->getPropertyAttributes()));
David Chisnall42ba04a2010-02-26 01:11:38 +00001912 Fields.push_back(llvm::ConstantInt::get(Int8Ty, isSynthesized));
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001913 if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001914 std::string TypeStr;
1915 Context.getObjCEncodingForMethodDecl(getter,TypeStr);
1916 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
David Chisnall42ba04a2010-02-26 01:11:38 +00001917 if (isSynthesized) {
1918 InstanceMethodTypes.push_back(TypeEncoding);
1919 InstanceMethodSels.push_back(getter->getSelector());
1920 }
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001921 Fields.push_back(MakeConstantString(getter->getSelector().getAsString()));
1922 Fields.push_back(TypeEncoding);
1923 } else {
1924 Fields.push_back(NULLPtr);
1925 Fields.push_back(NULLPtr);
1926 }
1927 if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001928 std::string TypeStr;
1929 Context.getObjCEncodingForMethodDecl(setter,TypeStr);
1930 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
David Chisnall42ba04a2010-02-26 01:11:38 +00001931 if (isSynthesized) {
1932 InstanceMethodTypes.push_back(TypeEncoding);
1933 InstanceMethodSels.push_back(setter->getSelector());
1934 }
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001935 Fields.push_back(MakeConstantString(setter->getSelector().getAsString()));
1936 Fields.push_back(TypeEncoding);
1937 } else {
1938 Fields.push_back(NULLPtr);
1939 Fields.push_back(NULLPtr);
1940 }
1941 Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1942 }
1943 llvm::ArrayType *PropertyArrayTy =
1944 llvm::ArrayType::get(PropertyMetadataTy, Properties.size());
1945 llvm::Constant *PropertyArray = llvm::ConstantArray::get(PropertyArrayTy,
1946 Properties);
1947 llvm::Constant* PropertyListInitFields[] =
1948 {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray};
1949
1950 llvm::Constant *PropertyListInit =
Chris Lattnerc5cbb902011-06-20 04:01:35 +00001951 llvm::ConstantStruct::getAnon(PropertyListInitFields);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001952 return new llvm::GlobalVariable(TheModule, PropertyListInit->getType(), false,
1953 llvm::GlobalValue::InternalLinkage, PropertyListInit,
1954 ".objc_property_list");
1955}
1956
David Chisnall29254f42012-01-31 18:59:20 +00001957void CGObjCGNU::RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {
1958 // Get the class declaration for which the alias is specified.
1959 ObjCInterfaceDecl *ClassDecl =
1960 const_cast<ObjCInterfaceDecl *>(OAD->getClassInterface());
1961 std::string ClassName = ClassDecl->getNameAsString();
1962 std::string AliasName = OAD->getNameAsString();
1963 ClassAliases.push_back(ClassAliasPair(ClassName,AliasName));
1964}
1965
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001966void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
1967 ASTContext &Context = CGM.getContext();
1968
1969 // Get the superclass name.
Mike Stump1eb44332009-09-09 15:08:12 +00001970 const ObjCInterfaceDecl * SuperClassDecl =
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001971 OID->getClassInterface()->getSuperClass();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001972 std::string SuperClassName;
Chris Lattner2a8e4e12009-06-15 01:09:11 +00001973 if (SuperClassDecl) {
Chris Lattner8ec03f52008-11-24 03:54:41 +00001974 SuperClassName = SuperClassDecl->getNameAsString();
Chris Lattner2a8e4e12009-06-15 01:09:11 +00001975 EmitClassRef(SuperClassName);
1976 }
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001977
1978 // Get the class name
Chris Lattner09dc6662009-04-01 02:00:48 +00001979 ObjCInterfaceDecl *ClassDecl =
1980 const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001981 std::string ClassName = ClassDecl->getNameAsString();
Chris Lattner2a8e4e12009-06-15 01:09:11 +00001982 // Emit the symbol that is used to generate linker errors if this class is
1983 // referenced in other modules but not declared.
Fariborz Jahanianc51db232009-07-03 15:10:14 +00001984 std::string classSymbolName = "__objc_class_name_" + ClassName;
Mike Stump1eb44332009-09-09 15:08:12 +00001985 if (llvm::GlobalVariable *symbol =
Fariborz Jahanianc51db232009-07-03 15:10:14 +00001986 TheModule.getGlobalVariable(classSymbolName)) {
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001987 symbol->setInitializer(llvm::ConstantInt::get(LongTy, 0));
Fariborz Jahanianc51db232009-07-03 15:10:14 +00001988 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00001989 new llvm::GlobalVariable(TheModule, LongTy, false,
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001990 llvm::GlobalValue::ExternalLinkage, llvm::ConstantInt::get(LongTy, 0),
Owen Anderson1c431b32009-07-08 19:05:04 +00001991 classSymbolName);
Fariborz Jahanianc51db232009-07-03 15:10:14 +00001992 }
Mike Stump1eb44332009-09-09 15:08:12 +00001993
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00001994 // Get the size of instances.
Ken Dyck5f022d82011-02-09 01:59:34 +00001995 int instanceSize =
1996 Context.getASTObjCImplementationLayout(OID).getSize().getQuantity();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001997
1998 // Collect information about instance variables.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001999 SmallVector<llvm::Constant*, 16> IvarNames;
2000 SmallVector<llvm::Constant*, 16> IvarTypes;
2001 SmallVector<llvm::Constant*, 16> IvarOffsets;
Mike Stump1eb44332009-09-09 15:08:12 +00002002
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00002003 std::vector<llvm::Constant*> IvarOffsetValues;
David Chisnall917b28b2011-10-04 15:35:30 +00002004 SmallVector<bool, 16> WeakIvars;
2005 SmallVector<bool, 16> StrongIvars;
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00002006
Mike Stump1eb44332009-09-09 15:08:12 +00002007 int superInstanceSize = !SuperClassDecl ? 0 :
Ken Dyck5f022d82011-02-09 01:59:34 +00002008 Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize().getQuantity();
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002009 // For non-fragile ivars, set the instance size to 0 - {the size of just this
2010 // class}. The runtime will then set this to the correct value on load.
John McCall260611a2012-06-20 06:18:46 +00002011 if (CGM.getContext().getLangOpts().ObjCRuntime.isNonFragile()) {
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002012 instanceSize = 0 - (instanceSize - superInstanceSize);
2013 }
David Chisnall7f63cb02010-04-19 00:45:34 +00002014
Jordy Rosedb8264e2011-07-22 02:08:32 +00002015 for (const ObjCIvarDecl *IVD = ClassDecl->all_declared_ivar_begin(); IVD;
2016 IVD = IVD->getNextIvar()) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002017 // Store the name
David Chisnall7f63cb02010-04-19 00:45:34 +00002018 IvarNames.push_back(MakeConstantString(IVD->getNameAsString()));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002019 // Get the type encoding for this ivar
2020 std::string TypeStr;
David Chisnall7f63cb02010-04-19 00:45:34 +00002021 Context.getObjCEncodingForType(IVD->getType(), TypeStr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002022 IvarTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002023 // Get the offset
David Chisnalld901da52010-04-19 01:37:25 +00002024 uint64_t BaseOffset = ComputeIvarBaseOffset(CGM, OID, IVD);
David Chisnallaecbf242009-11-17 19:32:15 +00002025 uint64_t Offset = BaseOffset;
John McCall260611a2012-06-20 06:18:46 +00002026 if (CGM.getContext().getLangOpts().ObjCRuntime.isNonFragile()) {
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00002027 Offset = BaseOffset - superInstanceSize;
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002028 }
David Chisnall63ff7032011-07-07 12:34:51 +00002029 llvm::Constant *OffsetValue = llvm::ConstantInt::get(IntTy, Offset);
2030 // Create the direct offset value
2031 std::string OffsetName = "__objc_ivar_offset_value_" + ClassName +"." +
2032 IVD->getNameAsString();
2033 llvm::GlobalVariable *OffsetVar = TheModule.getGlobalVariable(OffsetName);
2034 if (OffsetVar) {
2035 OffsetVar->setInitializer(OffsetValue);
2036 // If this is the real definition, change its linkage type so that
2037 // different modules will use this one, rather than their private
2038 // copy.
2039 OffsetVar->setLinkage(llvm::GlobalValue::ExternalLinkage);
2040 } else
2041 OffsetVar = new llvm::GlobalVariable(TheModule, IntTy,
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00002042 false, llvm::GlobalValue::ExternalLinkage,
David Chisnall63ff7032011-07-07 12:34:51 +00002043 OffsetValue,
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00002044 "__objc_ivar_offset_value_" + ClassName +"." +
David Chisnall63ff7032011-07-07 12:34:51 +00002045 IVD->getNameAsString());
2046 IvarOffsets.push_back(OffsetValue);
2047 IvarOffsetValues.push_back(OffsetVar);
David Chisnall917b28b2011-10-04 15:35:30 +00002048 Qualifiers::ObjCLifetime lt = IVD->getType().getQualifiers().getObjCLifetime();
2049 switch (lt) {
2050 case Qualifiers::OCL_Strong:
2051 StrongIvars.push_back(true);
2052 WeakIvars.push_back(false);
2053 break;
2054 case Qualifiers::OCL_Weak:
2055 StrongIvars.push_back(false);
2056 WeakIvars.push_back(true);
2057 break;
2058 default:
2059 StrongIvars.push_back(false);
2060 WeakIvars.push_back(false);
2061 }
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002062 }
David Chisnall917b28b2011-10-04 15:35:30 +00002063 llvm::Constant *StrongIvarBitmap = MakeBitField(StrongIvars);
2064 llvm::Constant *WeakIvarBitmap = MakeBitField(WeakIvars);
David Chisnall9f6614e2011-03-23 16:36:54 +00002065 llvm::GlobalVariable *IvarOffsetArray =
2066 MakeGlobalArray(PtrToIntTy, IvarOffsetValues, ".ivar.offsets");
2067
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002068
2069 // Collect information about instance methods
Chris Lattner5f9e2722011-07-23 10:55:15 +00002070 SmallVector<Selector, 16> InstanceMethodSels;
2071 SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00002072 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002073 iter = OID->instmeth_begin(), endIter = OID->instmeth_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00002074 iter != endIter ; iter++) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002075 InstanceMethodSels.push_back((*iter)->getSelector());
2076 std::string TypeStr;
2077 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002078 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002079 }
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00002080
2081 llvm::Constant *Properties = GeneratePropertyList(OID, InstanceMethodSels,
2082 InstanceMethodTypes);
2083
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002084
2085 // Collect information about class methods
Chris Lattner5f9e2722011-07-23 10:55:15 +00002086 SmallVector<Selector, 16> ClassMethodSels;
2087 SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregor653f1b12009-04-23 01:02:12 +00002088 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002089 iter = OID->classmeth_begin(), endIter = OID->classmeth_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00002090 iter != endIter ; iter++) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002091 ClassMethodSels.push_back((*iter)->getSelector());
2092 std::string TypeStr;
2093 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002094 ClassMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002095 }
2096 // Collect the names of referenced protocols
Chris Lattner5f9e2722011-07-23 10:55:15 +00002097 SmallVector<std::string, 16> Protocols;
Argyrios Kyrtzidisa5f44412012-03-13 01:09:41 +00002098 for (ObjCInterfaceDecl::protocol_iterator
2099 I = ClassDecl->protocol_begin(),
2100 E = ClassDecl->protocol_end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002101 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002102
2103
2104
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002105 // Get the superclass pointer.
2106 llvm::Constant *SuperClass;
Chris Lattner8ec03f52008-11-24 03:54:41 +00002107 if (!SuperClassName.empty()) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002108 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
2109 } else {
Owen Anderson03e20502009-07-30 23:11:26 +00002110 SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002111 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002112 // Empty vector used to construct empty method lists
Chris Lattner5f9e2722011-07-23 10:55:15 +00002113 SmallVector<llvm::Constant*, 1> empty;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002114 // Generate the method and instance variable lists
2115 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +00002116 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002117 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +00002118 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002119 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
2120 IvarOffsets);
Mike Stump1eb44332009-09-09 15:08:12 +00002121 // Irrespective of whether we are compiling for a fragile or non-fragile ABI,
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002122 // we emit a symbol containing the offset for each ivar in the class. This
2123 // allows code compiled for the non-Fragile ABI to inherit from code compiled
2124 // for the legacy ABI, without causing problems. The converse is also
2125 // possible, but causes all ivar accesses to be fragile.
David Chisnalle0d98762010-11-03 16:12:44 +00002126
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002127 // Offset pointer for getting at the correct field in the ivar list when
2128 // setting up the alias. These are: The base address for the global, the
2129 // ivar array (second field), the ivar in this list (set for each ivar), and
2130 // the offset (third field in ivar structure)
David Chisnall917b28b2011-10-04 15:35:30 +00002131 llvm::Type *IndexTy = Int32Ty;
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002132 llvm::Constant *offsetPointerIndexes[] = {Zeros[0],
Mike Stump1eb44332009-09-09 15:08:12 +00002133 llvm::ConstantInt::get(IndexTy, 1), 0,
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002134 llvm::ConstantInt::get(IndexTy, 2) };
2135
Jordy Rosedb8264e2011-07-22 02:08:32 +00002136 unsigned ivarIndex = 0;
2137 for (const ObjCIvarDecl *IVD = ClassDecl->all_declared_ivar_begin(); IVD;
2138 IVD = IVD->getNextIvar()) {
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002139 const std::string Name = "__objc_ivar_offset_" + ClassName + '.'
David Chisnalle0d98762010-11-03 16:12:44 +00002140 + IVD->getNameAsString();
Jordy Rosedb8264e2011-07-22 02:08:32 +00002141 offsetPointerIndexes[2] = llvm::ConstantInt::get(IndexTy, ivarIndex);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002142 // Get the correct ivar field
2143 llvm::Constant *offsetValue = llvm::ConstantExpr::getGetElementPtr(
Jay Foada5c04342011-07-21 14:31:17 +00002144 IvarList, offsetPointerIndexes);
David Chisnalle0d98762010-11-03 16:12:44 +00002145 // Get the existing variable, if one exists.
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002146 llvm::GlobalVariable *offset = TheModule.getNamedGlobal(Name);
2147 if (offset) {
Ted Kremenek74a1a1f2012-04-04 00:55:25 +00002148 offset->setInitializer(offsetValue);
2149 // If this is the real definition, change its linkage type so that
2150 // different modules will use this one, rather than their private
2151 // copy.
2152 offset->setLinkage(llvm::GlobalValue::ExternalLinkage);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002153 } else {
Ted Kremenek74a1a1f2012-04-04 00:55:25 +00002154 // Add a new alias if there isn't one already.
2155 offset = new llvm::GlobalVariable(TheModule, offsetValue->getType(),
2156 false, llvm::GlobalValue::ExternalLinkage, offsetValue, Name);
2157 (void) offset; // Silence dead store warning.
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002158 }
Jordy Rosedb8264e2011-07-22 02:08:32 +00002159 ++ivarIndex;
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002160 }
David Chisnall9d06ba82011-10-25 10:12:21 +00002161 llvm::Constant *ZeroPtr = llvm::ConstantInt::get(IntPtrTy, 0);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002162 //Generate metaclass for class methods
2163 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
David Chisnall18044632009-11-16 19:05:54 +00002164 NULLPtr, 0x12L, ClassName.c_str(), 0, Zeros[0], GenerateIvarList(
David Chisnall917b28b2011-10-04 15:35:30 +00002165 empty, empty, empty), ClassMethodList, NULLPtr,
David Chisnall9d06ba82011-10-25 10:12:21 +00002166 NULLPtr, NULLPtr, ZeroPtr, ZeroPtr, true);
Daniel Dunbar5efccb12009-05-04 15:31:17 +00002167
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002168 // Generate the class structure
Chris Lattner8ec03f52008-11-24 03:54:41 +00002169 llvm::Constant *ClassStruct =
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00002170 GenerateClassStructure(MetaClassStruct, SuperClass, 0x11L,
Chris Lattner8ec03f52008-11-24 03:54:41 +00002171 ClassName.c_str(), 0,
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002172 llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00002173 MethodList, GenerateProtocolList(Protocols), IvarOffsetArray,
David Chisnall917b28b2011-10-04 15:35:30 +00002174 Properties, StrongIvarBitmap, WeakIvarBitmap);
Daniel Dunbar5efccb12009-05-04 15:31:17 +00002175
2176 // Resolve the class aliases, if they exist.
2177 if (ClassPtrAlias) {
David Chisnall0b9c22b2010-11-09 11:21:43 +00002178 ClassPtrAlias->replaceAllUsesWith(
Owen Anderson3c4972d2009-07-29 18:54:39 +00002179 llvm::ConstantExpr::getBitCast(ClassStruct, IdTy));
David Chisnall0b9c22b2010-11-09 11:21:43 +00002180 ClassPtrAlias->eraseFromParent();
Daniel Dunbar5efccb12009-05-04 15:31:17 +00002181 ClassPtrAlias = 0;
2182 }
2183 if (MetaClassPtrAlias) {
David Chisnall0b9c22b2010-11-09 11:21:43 +00002184 MetaClassPtrAlias->replaceAllUsesWith(
Owen Anderson3c4972d2009-07-29 18:54:39 +00002185 llvm::ConstantExpr::getBitCast(MetaClassStruct, IdTy));
David Chisnall0b9c22b2010-11-09 11:21:43 +00002186 MetaClassPtrAlias->eraseFromParent();
Daniel Dunbar5efccb12009-05-04 15:31:17 +00002187 MetaClassPtrAlias = 0;
2188 }
2189
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002190 // Add class structure to list to be added to the symtab later
Owen Anderson3c4972d2009-07-29 18:54:39 +00002191 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002192 Classes.push_back(ClassStruct);
2193}
2194
Fariborz Jahanianc38e9af2009-06-23 21:47:46 +00002195
Mike Stump1eb44332009-09-09 15:08:12 +00002196llvm::Function *CGObjCGNU::ModuleInitFunction() {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002197 // Only emit an ObjC load function if no Objective-C stuff has been called
2198 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
David Chisnall9f6614e2011-03-23 16:36:54 +00002199 ExistingProtocols.empty() && SelectorTable.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002200 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +00002201
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00002202 // Add all referenced protocols to a category.
2203 GenerateProtocolHolderCategory();
2204
Chris Lattner2acc6e32011-07-18 04:24:23 +00002205 llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
Chris Lattnere160c9b2009-01-27 05:06:01 +00002206 SelectorTy->getElementType());
Jay Foadef6de3d2011-07-11 09:56:20 +00002207 llvm::Type *SelStructPtrTy = SelectorTy;
Chris Lattnere160c9b2009-01-27 05:06:01 +00002208 if (SelStructTy == 0) {
Chris Lattner7650d952011-06-18 22:49:11 +00002209 SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00002210 SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
Chris Lattnere160c9b2009-01-27 05:06:01 +00002211 }
2212
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002213 std::vector<llvm::Constant*> Elements;
Chris Lattner71238f62009-04-25 23:19:45 +00002214 llvm::Constant *Statics = NULLPtr;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002215 // Generate statics list:
Chris Lattner71238f62009-04-25 23:19:45 +00002216 if (ConstantStrings.size()) {
Owen Anderson96e0fc72009-07-29 22:16:19 +00002217 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
Chris Lattner71238f62009-04-25 23:19:45 +00002218 ConstantStrings.size() + 1);
2219 ConstantStrings.push_back(NULLPtr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002220
David Blaikie4e4d0842012-03-11 07:00:24 +00002221 StringRef StringClass = CGM.getLangOpts().ObjCConstantStringClass;
David Chisnall9f6614e2011-03-23 16:36:54 +00002222
Daniel Dunbar1b096952009-11-29 02:38:47 +00002223 if (StringClass.empty()) StringClass = "NXConstantString";
David Chisnall9f6614e2011-03-23 16:36:54 +00002224
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002225 Elements.push_back(MakeConstantString(StringClass,
2226 ".objc_static_class_name"));
Owen Anderson7db6d832009-07-28 18:33:04 +00002227 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy,
Chris Lattner71238f62009-04-25 23:19:45 +00002228 ConstantStrings));
Mike Stump1eb44332009-09-09 15:08:12 +00002229 llvm::StructType *StaticsListTy =
Chris Lattner7650d952011-06-18 22:49:11 +00002230 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002231 llvm::Type *StaticsListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002232 llvm::PointerType::getUnqual(StaticsListTy);
Chris Lattner71238f62009-04-25 23:19:45 +00002233 Statics = MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Mike Stump1eb44332009-09-09 15:08:12 +00002234 llvm::ArrayType *StaticsListArrayTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002235 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner71238f62009-04-25 23:19:45 +00002236 Elements.clear();
2237 Elements.push_back(Statics);
Owen Andersonc9c88b42009-07-31 20:28:54 +00002238 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner71238f62009-04-25 23:19:45 +00002239 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Owen Anderson3c4972d2009-07-29 18:54:39 +00002240 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
Chris Lattner71238f62009-04-25 23:19:45 +00002241 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002242 // Array of classes, categories, and constant objects
Owen Anderson96e0fc72009-07-29 22:16:19 +00002243 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002244 Classes.size() + Categories.size() + 2);
Chris Lattner7650d952011-06-18 22:49:11 +00002245 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy,
Owen Anderson0032b272009-08-13 21:57:51 +00002246 llvm::Type::getInt16Ty(VMContext),
2247 llvm::Type::getInt16Ty(VMContext),
Chris Lattner630404b2008-06-26 04:10:42 +00002248 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002249
2250 Elements.clear();
2251 // Pointer to an array of selectors used in this module.
2252 std::vector<llvm::Constant*> Selectors;
David Chisnall9f6614e2011-03-23 16:36:54 +00002253 std::vector<llvm::GlobalAlias*> SelectorAliases;
2254 for (SelectorMap::iterator iter = SelectorTable.begin(),
2255 iterEnd = SelectorTable.end(); iter != iterEnd ; ++iter) {
2256
2257 std::string SelNameStr = iter->first.getAsString();
2258 llvm::Constant *SelName = ExportUniqueString(SelNameStr, ".objc_sel_name");
2259
Chris Lattner5f9e2722011-07-23 10:55:15 +00002260 SmallVectorImpl<TypedSelector> &Types = iter->second;
2261 for (SmallVectorImpl<TypedSelector>::iterator i = Types.begin(),
David Chisnall9f6614e2011-03-23 16:36:54 +00002262 e = Types.end() ; i!=e ; i++) {
2263
2264 llvm::Constant *SelectorTypeEncoding = NULLPtr;
2265 if (!i->first.empty())
2266 SelectorTypeEncoding = MakeConstantString(i->first, ".objc_sel_types");
2267
2268 Elements.push_back(SelName);
2269 Elements.push_back(SelectorTypeEncoding);
2270 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
2271 Elements.clear();
2272
2273 // Store the selector alias for later replacement
2274 SelectorAliases.push_back(i->second);
2275 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002276 }
David Chisnall9f6614e2011-03-23 16:36:54 +00002277 unsigned SelectorCount = Selectors.size();
2278 // NULL-terminate the selector list. This should not actually be required,
2279 // because the selector list has a length field. Unfortunately, the GCC
2280 // runtime decides to ignore the length field and expects a NULL terminator,
2281 // and GCC cooperates with this by always setting the length to 0.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002282 Elements.push_back(NULLPtr);
2283 Elements.push_back(NULLPtr);
Owen Anderson08e25242009-07-27 22:29:56 +00002284 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002285 Elements.clear();
David Chisnall9f6614e2011-03-23 16:36:54 +00002286
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002287 // Number of static selectors
David Chisnall9f6614e2011-03-23 16:36:54 +00002288 Elements.push_back(llvm::ConstantInt::get(LongTy, SelectorCount));
2289 llvm::Constant *SelectorList = MakeGlobalArray(SelStructTy, Selectors,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002290 ".objc_selector_list");
Mike Stump1eb44332009-09-09 15:08:12 +00002291 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
Chris Lattnere160c9b2009-01-27 05:06:01 +00002292 SelStructPtrTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002293
2294 // Now that all of the static selectors exist, create pointers to them.
David Chisnall9f6614e2011-03-23 16:36:54 +00002295 for (unsigned int i=0 ; i<SelectorCount ; i++) {
2296
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002297 llvm::Constant *Idxs[] = {Zeros[0],
David Chisnall917b28b2011-10-04 15:35:30 +00002298 llvm::ConstantInt::get(Int32Ty, i), Zeros[0]};
David Chisnall9f6614e2011-03-23 16:36:54 +00002299 // FIXME: We're generating redundant loads and stores here!
David Chisnallc7ef4622011-03-23 22:52:06 +00002300 llvm::Constant *SelPtr = llvm::ConstantExpr::getGetElementPtr(SelectorList,
Jay Foada5c04342011-07-21 14:31:17 +00002301 makeArrayRef(Idxs, 2));
Chris Lattnere160c9b2009-01-27 05:06:01 +00002302 // If selectors are defined as an opaque type, cast the pointer to this
2303 // type.
David Chisnallc7ef4622011-03-23 22:52:06 +00002304 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr, SelectorTy);
David Chisnall9f6614e2011-03-23 16:36:54 +00002305 SelectorAliases[i]->replaceAllUsesWith(SelPtr);
2306 SelectorAliases[i]->eraseFromParent();
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002307 }
David Chisnall9f6614e2011-03-23 16:36:54 +00002308
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002309 // Number of classes defined.
Mike Stump1eb44332009-09-09 15:08:12 +00002310 Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002311 Classes.size()));
2312 // Number of categories defined
Mike Stump1eb44332009-09-09 15:08:12 +00002313 Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002314 Categories.size()));
2315 // Create an array of classes, then categories, then static object instances
2316 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
2317 // NULL-terminated list of static object instances (mainly constant strings)
2318 Classes.push_back(Statics);
2319 Classes.push_back(NULLPtr);
Owen Anderson7db6d832009-07-28 18:33:04 +00002320 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002321 Elements.push_back(ClassList);
Mike Stump1eb44332009-09-09 15:08:12 +00002322 // Construct the symbol table
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002323 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
2324
2325 // The symbol table is contained in a module which has some version-checking
2326 // constants
Chris Lattner7650d952011-06-18 22:49:11 +00002327 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
David Chisnalla2120032011-05-22 22:37:08 +00002328 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy),
David Chisnallf0748852011-07-07 11:22:31 +00002329 (RuntimeVersion >= 10) ? IntTy : NULL, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002330 Elements.clear();
David Chisnall9f6614e2011-03-23 16:36:54 +00002331 // Runtime version, used for ABI compatibility checking.
2332 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
Fariborz Jahanian91a0b512009-04-01 19:49:42 +00002333 // sizeof(ModuleTy)
Benjamin Kramer74a8bbf2010-02-09 19:31:24 +00002334 llvm::TargetData td(&TheModule);
Ken Dycke0afc892011-04-22 17:59:22 +00002335 Elements.push_back(
2336 llvm::ConstantInt::get(LongTy,
2337 td.getTypeSizeInBits(ModuleTy) /
2338 CGM.getContext().getCharWidth()));
David Chisnall9f6614e2011-03-23 16:36:54 +00002339
2340 // The path to the source file where this module was declared
2341 SourceManager &SM = CGM.getContext().getSourceManager();
2342 const FileEntry *mainFile = SM.getFileEntryForID(SM.getMainFileID());
2343 std::string path =
2344 std::string(mainFile->getDir()->getName()) + '/' + mainFile->getName();
2345 Elements.push_back(MakeConstantString(path, ".objc_source_file_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002346 Elements.push_back(SymTab);
David Chisnalla2120032011-05-22 22:37:08 +00002347
David Chisnallf0748852011-07-07 11:22:31 +00002348 if (RuntimeVersion >= 10)
David Blaikie4e4d0842012-03-11 07:00:24 +00002349 switch (CGM.getLangOpts().getGC()) {
David Chisnallf0748852011-07-07 11:22:31 +00002350 case LangOptions::GCOnly:
David Chisnalla2120032011-05-22 22:37:08 +00002351 Elements.push_back(llvm::ConstantInt::get(IntTy, 2));
David Chisnalla2120032011-05-22 22:37:08 +00002352 break;
David Chisnallf0748852011-07-07 11:22:31 +00002353 case LangOptions::NonGC:
David Blaikie4e4d0842012-03-11 07:00:24 +00002354 if (CGM.getLangOpts().ObjCAutoRefCount)
David Chisnallf0748852011-07-07 11:22:31 +00002355 Elements.push_back(llvm::ConstantInt::get(IntTy, 1));
2356 else
2357 Elements.push_back(llvm::ConstantInt::get(IntTy, 0));
2358 break;
2359 case LangOptions::HybridGC:
2360 Elements.push_back(llvm::ConstantInt::get(IntTy, 1));
2361 break;
2362 }
David Chisnalla2120032011-05-22 22:37:08 +00002363
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002364 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
2365
2366 // Create the load function calling the runtime entry point with the module
2367 // structure
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002368 llvm::Function * LoadFunction = llvm::Function::Create(
Owen Anderson0032b272009-08-13 21:57:51 +00002369 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002370 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
2371 &TheModule);
Owen Anderson0032b272009-08-13 21:57:51 +00002372 llvm::BasicBlock *EntryBB =
2373 llvm::BasicBlock::Create(VMContext, "entry", LoadFunction);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002374 CGBuilderTy Builder(VMContext);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002375 Builder.SetInsertPoint(EntryBB);
Fariborz Jahanian26c82942009-03-30 18:02:14 +00002376
Benjamin Kramer95d318c2011-05-28 14:26:31 +00002377 llvm::FunctionType *FT =
Jay Foadda549e82011-07-29 13:56:53 +00002378 llvm::FunctionType::get(Builder.getVoidTy(),
2379 llvm::PointerType::getUnqual(ModuleTy), true);
Benjamin Kramer95d318c2011-05-28 14:26:31 +00002380 llvm::Value *Register = CGM.CreateRuntimeFunction(FT, "__objc_exec_class");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002381 Builder.CreateCall(Register, Module);
David Chisnall29254f42012-01-31 18:59:20 +00002382
David Chisnalldccaa232012-02-01 19:16:56 +00002383 if (!ClassAliases.empty()) {
David Chisnall29254f42012-01-31 18:59:20 +00002384 llvm::Type *ArgTypes[2] = {PtrTy, PtrToInt8Ty};
2385 llvm::FunctionType *RegisterAliasTy =
2386 llvm::FunctionType::get(Builder.getVoidTy(),
2387 ArgTypes, false);
2388 llvm::Function *RegisterAlias = llvm::Function::Create(
2389 RegisterAliasTy,
2390 llvm::GlobalValue::ExternalWeakLinkage, "class_registerAlias_np",
2391 &TheModule);
2392 llvm::BasicBlock *AliasBB =
2393 llvm::BasicBlock::Create(VMContext, "alias", LoadFunction);
2394 llvm::BasicBlock *NoAliasBB =
2395 llvm::BasicBlock::Create(VMContext, "no_alias", LoadFunction);
2396
2397 // Branch based on whether the runtime provided class_registerAlias_np()
2398 llvm::Value *HasRegisterAlias = Builder.CreateICmpNE(RegisterAlias,
2399 llvm::Constant::getNullValue(RegisterAlias->getType()));
2400 Builder.CreateCondBr(HasRegisterAlias, AliasBB, NoAliasBB);
2401
2402 // The true branch (has alias registration fucntion):
2403 Builder.SetInsertPoint(AliasBB);
2404 // Emit alias registration calls:
2405 for (std::vector<ClassAliasPair>::iterator iter = ClassAliases.begin();
2406 iter != ClassAliases.end(); ++iter) {
2407 llvm::Constant *TheClass =
2408 TheModule.getGlobalVariable(("_OBJC_CLASS_" + iter->first).c_str(),
2409 true);
2410 if (0 != TheClass) {
2411 TheClass = llvm::ConstantExpr::getBitCast(TheClass, PtrTy);
2412 Builder.CreateCall2(RegisterAlias, TheClass,
2413 MakeConstantString(iter->second));
2414 }
2415 }
2416 // Jump to end:
2417 Builder.CreateBr(NoAliasBB);
2418
2419 // Missing alias registration function, just return from the function:
2420 Builder.SetInsertPoint(NoAliasBB);
2421 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002422 Builder.CreateRetVoid();
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002423
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002424 return LoadFunction;
2425}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002426
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002427llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
Mike Stump1eb44332009-09-09 15:08:12 +00002428 const ObjCContainerDecl *CD) {
2429 const ObjCCategoryImplDecl *OCD =
Steve Naroff3e0a5402009-01-08 19:41:02 +00002430 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002431 StringRef CategoryName = OCD ? OCD->getName() : "";
2432 StringRef ClassName = CD->getName();
David Chisnall9f6614e2011-03-23 16:36:54 +00002433 Selector MethodName = OMD->getSelector();
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002434 bool isClassMethod = !OMD->isInstanceMethod();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002435
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002436 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2acc6e32011-07-18 04:24:23 +00002437 llvm::FunctionType *MethodTy =
John McCallde5d3c72012-02-17 03:33:10 +00002438 Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002439 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
2440 MethodName, isClassMethod);
2441
Daniel Dunbard6c93d72009-09-17 04:01:22 +00002442 llvm::Function *Method
Mike Stump1eb44332009-09-09 15:08:12 +00002443 = llvm::Function::Create(MethodTy,
2444 llvm::GlobalValue::InternalLinkage,
2445 FunctionName,
2446 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +00002447 return Method;
2448}
2449
David Chisnall789ecde2011-05-23 22:33:28 +00002450llvm::Constant *CGObjCGNU::GetPropertyGetFunction() {
David Chisnall9f6614e2011-03-23 16:36:54 +00002451 return GetPropertyFn;
Daniel Dunbar49f66022008-09-24 03:38:44 +00002452}
2453
David Chisnall789ecde2011-05-23 22:33:28 +00002454llvm::Constant *CGObjCGNU::GetPropertySetFunction() {
David Chisnall9f6614e2011-03-23 16:36:54 +00002455 return SetPropertyFn;
Daniel Dunbar49f66022008-09-24 03:38:44 +00002456}
2457
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002458llvm::Constant *CGObjCGNU::GetOptimizedPropertySetFunction(bool atomic,
2459 bool copy) {
2460 return 0;
2461}
2462
David Chisnall789ecde2011-05-23 22:33:28 +00002463llvm::Constant *CGObjCGNU::GetGetStructFunction() {
David Chisnall9f6614e2011-03-23 16:36:54 +00002464 return GetStructPropertyFn;
David Chisnall8fac25d2010-12-26 22:13:16 +00002465}
David Chisnall789ecde2011-05-23 22:33:28 +00002466llvm::Constant *CGObjCGNU::GetSetStructFunction() {
David Chisnall9f6614e2011-03-23 16:36:54 +00002467 return SetStructPropertyFn;
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002468}
Fariborz Jahaniane3173022012-01-06 18:07:23 +00002469llvm::Constant *CGObjCGNU::GetCppAtomicObjectFunction() {
2470 return 0;
2471}
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002472
Daniel Dunbar309a4362009-07-24 07:40:24 +00002473llvm::Constant *CGObjCGNU::EnumerationMutationFunction() {
David Chisnall9f6614e2011-03-23 16:36:54 +00002474 return EnumerationMutationFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002475}
2476
David Chisnall9f6614e2011-03-23 16:36:54 +00002477void CGObjCGNU::EmitSynchronizedStmt(CodeGenFunction &CGF,
John McCallf1549f62010-07-06 01:34:17 +00002478 const ObjCAtSynchronizedStmt &S) {
David Chisnall9735ca62011-03-25 11:57:33 +00002479 EmitAtSynchronizedStmt(CGF, S, SyncEnterFn, SyncExitFn);
John McCallf1549f62010-07-06 01:34:17 +00002480}
Chris Lattner5dc08672009-05-08 00:11:50 +00002481
David Chisnall0faa5162009-12-24 02:26:34 +00002482
David Chisnall9f6614e2011-03-23 16:36:54 +00002483void CGObjCGNU::EmitTryStmt(CodeGenFunction &CGF,
John McCallf1549f62010-07-06 01:34:17 +00002484 const ObjCAtTryStmt &S) {
2485 // Unlike the Apple non-fragile runtimes, which also uses
2486 // unwind-based zero cost exceptions, the GNU Objective C runtime's
2487 // EH support isn't a veneer over C++ EH. Instead, exception
2488 // objects are created by __objc_exception_throw and destroyed by
2489 // the personality function; this avoids the need for bracketing
2490 // catch handlers with calls to __blah_begin_catch/__blah_end_catch
2491 // (or even _Unwind_DeleteException), but probably doesn't
2492 // interoperate very well with foreign exceptions.
David Chisnall9735ca62011-03-25 11:57:33 +00002493 //
David Chisnall80558d22011-03-20 21:35:39 +00002494 // In Objective-C++ mode, we actually emit something equivalent to the C++
David Chisnall9735ca62011-03-25 11:57:33 +00002495 // exception handler.
2496 EmitTryCatchStmt(CGF, S, EnterCatchFn, ExitCatchFn, ExceptionReThrowFn);
2497 return ;
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002498}
2499
David Chisnall9f6614e2011-03-23 16:36:54 +00002500void CGObjCGNU::EmitThrowStmt(CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +00002501 const ObjCAtThrowStmt &S) {
Chris Lattner5dc08672009-05-08 00:11:50 +00002502 llvm::Value *ExceptionAsObject;
2503
Chris Lattner5dc08672009-05-08 00:11:50 +00002504 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00002505 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Fariborz Jahanian1e64a952009-05-17 16:49:27 +00002506 ExceptionAsObject = Exception;
Chris Lattner5dc08672009-05-08 00:11:50 +00002507 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00002508 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Chris Lattner5dc08672009-05-08 00:11:50 +00002509 "Unexpected rethrow outside @catch block.");
2510 ExceptionAsObject = CGF.ObjCEHValueStack.back();
2511 }
Benjamin Kramer578faa82011-09-27 21:06:10 +00002512 ExceptionAsObject = CGF.Builder.CreateBitCast(ExceptionAsObject, IdTy);
Mike Stump1eb44332009-09-09 15:08:12 +00002513
Chris Lattner5dc08672009-05-08 00:11:50 +00002514 // Note: This may have to be an invoke, if we want to support constructs like:
2515 // @try {
2516 // @throw(obj);
2517 // }
2518 // @catch(id) ...
2519 //
2520 // This is effectively turning @throw into an incredibly-expensive goto, but
2521 // it may happen as a result of inlining followed by missed optimizations, or
2522 // as a result of stupidity.
2523 llvm::BasicBlock *UnwindBB = CGF.getInvokeDest();
2524 if (!UnwindBB) {
David Chisnall9f6614e2011-03-23 16:36:54 +00002525 CGF.Builder.CreateCall(ExceptionThrowFn, ExceptionAsObject);
Chris Lattner5dc08672009-05-08 00:11:50 +00002526 CGF.Builder.CreateUnreachable();
2527 } else {
Jay Foad4c7d9f12011-07-15 08:37:34 +00002528 CGF.Builder.CreateInvoke(ExceptionThrowFn, UnwindBB, UnwindBB,
2529 ExceptionAsObject);
Chris Lattner5dc08672009-05-08 00:11:50 +00002530 }
2531 // Clear the insertion point to indicate we are in unreachable code.
2532 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002533}
2534
David Chisnall9f6614e2011-03-23 16:36:54 +00002535llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00002536 llvm::Value *AddrWeakObj) {
David Chisnallef6e0f32010-02-03 15:59:02 +00002537 CGBuilderTy B = CGF.Builder;
David Chisnall31fc0c12011-05-30 12:00:26 +00002538 AddrWeakObj = EnforceType(B, AddrWeakObj, PtrToIdTy);
David Chisnallef6e0f32010-02-03 15:59:02 +00002539 return B.CreateCall(WeakReadFn, AddrWeakObj);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002540}
2541
David Chisnall9f6614e2011-03-23 16:36:54 +00002542void CGObjCGNU::EmitObjCWeakAssign(CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00002543 llvm::Value *src, llvm::Value *dst) {
David Chisnallef6e0f32010-02-03 15:59:02 +00002544 CGBuilderTy B = CGF.Builder;
2545 src = EnforceType(B, src, IdTy);
2546 dst = EnforceType(B, dst, PtrToIdTy);
2547 B.CreateCall2(WeakAssignFn, src, dst);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002548}
2549
David Chisnall9f6614e2011-03-23 16:36:54 +00002550void CGObjCGNU::EmitObjCGlobalAssign(CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00002551 llvm::Value *src, llvm::Value *dst,
2552 bool threadlocal) {
David Chisnallef6e0f32010-02-03 15:59:02 +00002553 CGBuilderTy B = CGF.Builder;
2554 src = EnforceType(B, src, IdTy);
2555 dst = EnforceType(B, dst, PtrToIdTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00002556 if (!threadlocal)
2557 B.CreateCall2(GlobalAssignFn, src, dst);
2558 else
2559 // FIXME. Add threadloca assign API
David Blaikieb219cfc2011-09-23 05:06:16 +00002560 llvm_unreachable("EmitObjCGlobalAssign - Threal Local API NYI");
Fariborz Jahanian58626502008-11-19 00:59:10 +00002561}
2562
David Chisnall9f6614e2011-03-23 16:36:54 +00002563void CGObjCGNU::EmitObjCIvarAssign(CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00002564 llvm::Value *src, llvm::Value *dst,
2565 llvm::Value *ivarOffset) {
David Chisnallef6e0f32010-02-03 15:59:02 +00002566 CGBuilderTy B = CGF.Builder;
2567 src = EnforceType(B, src, IdTy);
David Chisnallb44eda32011-05-25 20:33:17 +00002568 dst = EnforceType(B, dst, IdTy);
David Chisnallef6e0f32010-02-03 15:59:02 +00002569 B.CreateCall3(IvarAssignFn, src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002570}
2571
David Chisnall9f6614e2011-03-23 16:36:54 +00002572void CGObjCGNU::EmitObjCStrongCastAssign(CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00002573 llvm::Value *src, llvm::Value *dst) {
David Chisnallef6e0f32010-02-03 15:59:02 +00002574 CGBuilderTy B = CGF.Builder;
2575 src = EnforceType(B, src, IdTy);
2576 dst = EnforceType(B, dst, PtrToIdTy);
2577 B.CreateCall2(StrongCastAssignFn, src, dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002578}
2579
David Chisnall9f6614e2011-03-23 16:36:54 +00002580void CGObjCGNU::EmitGCMemmoveCollectable(CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00002581 llvm::Value *DestPtr,
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002582 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00002583 llvm::Value *Size) {
David Chisnallef6e0f32010-02-03 15:59:02 +00002584 CGBuilderTy B = CGF.Builder;
David Chisnall68e5e132011-05-28 14:23:43 +00002585 DestPtr = EnforceType(B, DestPtr, PtrTy);
2586 SrcPtr = EnforceType(B, SrcPtr, PtrTy);
David Chisnallef6e0f32010-02-03 15:59:02 +00002587
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00002588 B.CreateCall3(MemMoveFn, DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002589}
2590
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002591llvm::GlobalVariable *CGObjCGNU::ObjCIvarOffsetVariable(
2592 const ObjCInterfaceDecl *ID,
2593 const ObjCIvarDecl *Ivar) {
2594 const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString()
2595 + '.' + Ivar->getNameAsString();
2596 // Emit the variable and initialize it with what we think the correct value
2597 // is. This allows code compiled with non-fragile ivars to work correctly
2598 // when linked against code which isn't (most of the time).
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002599 llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name);
2600 if (!IvarOffsetPointer) {
David Chisnalle0d98762010-11-03 16:12:44 +00002601 // This will cause a run-time crash if we accidentally use it. A value of
2602 // 0 would seem more sensible, but will silently overwrite the isa pointer
2603 // causing a great deal of confusion.
2604 uint64_t Offset = -1;
2605 // We can't call ComputeIvarBaseOffset() here if we have the
2606 // implementation, because it will create an invalid ASTRecordLayout object
2607 // that we are then stuck with forever, so we only initialize the ivar
2608 // offset variable with a guess if we only have the interface. The
2609 // initializer will be reset later anyway, when we are generating the class
2610 // description.
2611 if (!CGM.getContext().getObjCImplementation(
Dan Gohmancb421fa2010-04-19 16:39:44 +00002612 const_cast<ObjCInterfaceDecl *>(ID)))
David Chisnalld901da52010-04-19 01:37:25 +00002613 Offset = ComputeIvarBaseOffset(CGM, ID, Ivar);
2614
David Chisnall49de5282011-10-08 08:54:36 +00002615 llvm::ConstantInt *OffsetGuess = llvm::ConstantInt::get(Int32Ty, Offset,
Richard Trieu243f1082011-09-21 02:46:06 +00002616 /*isSigned*/true);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002617 // Don't emit the guess in non-PIC code because the linker will not be able
2618 // to replace it with the real version for a library. In non-PIC code you
2619 // must compile with the fragile ABI if you want to use ivars from a
Mike Stump1eb44332009-09-09 15:08:12 +00002620 // GCC-compiled class.
Chandler Carruth5e219cf2012-04-08 16:40:35 +00002621 if (CGM.getLangOpts().PICLevel || CGM.getLangOpts().PIELevel) {
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002622 llvm::GlobalVariable *IvarOffsetGV = new llvm::GlobalVariable(TheModule,
David Chisnall917b28b2011-10-04 15:35:30 +00002623 Int32Ty, false,
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002624 llvm::GlobalValue::PrivateLinkage, OffsetGuess, Name+".guess");
2625 IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
2626 IvarOffsetGV->getType(), false, llvm::GlobalValue::LinkOnceAnyLinkage,
2627 IvarOffsetGV, Name);
2628 } else {
2629 IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00002630 llvm::Type::getInt32PtrTy(VMContext), false,
2631 llvm::GlobalValue::ExternalLinkage, 0, Name);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002632 }
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002633 }
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002634 return IvarOffsetPointer;
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002635}
2636
David Chisnall9f6614e2011-03-23 16:36:54 +00002637LValue CGObjCGNU::EmitObjCValueForIvar(CodeGenFunction &CGF,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002638 QualType ObjectTy,
2639 llvm::Value *BaseValue,
2640 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002641 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00002642 const ObjCInterfaceDecl *ID =
2643 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00002644 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
2645 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002646}
Mike Stumpbb1c8602009-07-31 21:31:32 +00002647
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002648static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context,
2649 const ObjCInterfaceDecl *OID,
2650 const ObjCIvarDecl *OIVD) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002651 for (const ObjCIvarDecl *next = OID->all_declared_ivar_begin(); next;
2652 next = next->getNextIvar()) {
2653 if (OIVD == next)
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002654 return OID;
2655 }
Mike Stump1eb44332009-09-09 15:08:12 +00002656
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002657 // Otherwise check in the super class.
2658 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
2659 return FindIvarInterface(Context, Super, OIVD);
Mike Stump1eb44332009-09-09 15:08:12 +00002660
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002661 return 0;
2662}
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002663
David Chisnall9f6614e2011-03-23 16:36:54 +00002664llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00002665 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002666 const ObjCIvarDecl *Ivar) {
John McCall260611a2012-06-20 06:18:46 +00002667 if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) {
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002668 Interface = FindIvarInterface(CGM.getContext(), Interface, Ivar);
David Chisnall63ff7032011-07-07 12:34:51 +00002669 if (RuntimeVersion < 10)
2670 return CGF.Builder.CreateZExtOrBitCast(
2671 CGF.Builder.CreateLoad(CGF.Builder.CreateLoad(
2672 ObjCIvarOffsetVariable(Interface, Ivar), false, "ivar")),
2673 PtrDiffTy);
2674 std::string name = "__objc_ivar_offset_value_" +
2675 Interface->getNameAsString() +"." + Ivar->getNameAsString();
2676 llvm::Value *Offset = TheModule.getGlobalVariable(name);
2677 if (!Offset)
2678 Offset = new llvm::GlobalVariable(TheModule, IntTy,
David Chisnall3fc81d32011-08-01 17:36:53 +00002679 false, llvm::GlobalValue::LinkOnceAnyLinkage,
2680 llvm::Constant::getNullValue(IntTy), name);
David Chisnall66148452012-04-06 15:39:12 +00002681 Offset = CGF.Builder.CreateLoad(Offset);
2682 if (Offset->getType() != PtrDiffTy)
2683 Offset = CGF.Builder.CreateZExtOrBitCast(Offset, PtrDiffTy);
2684 return Offset;
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002685 }
Daniel Dunbar97776872009-04-22 07:32:20 +00002686 uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar);
Richard Trieu243f1082011-09-21 02:46:06 +00002687 return llvm::ConstantInt::get(PtrDiffTy, Offset, /*isSigned*/true);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002688}
2689
David Chisnall9f6614e2011-03-23 16:36:54 +00002690CGObjCRuntime *
2691clang::CodeGen::CreateGNUObjCRuntime(CodeGenModule &CGM) {
John McCall260611a2012-06-20 06:18:46 +00002692 switch (CGM.getLangOpts().ObjCRuntime.getKind()) {
David Chisnall11d3f4c2012-07-03 20:49:52 +00002693 case ObjCRuntime::GNUstep:
David Chisnall9f6614e2011-03-23 16:36:54 +00002694 return new CGObjCGNUstep(CGM);
John McCall260611a2012-06-20 06:18:46 +00002695
David Chisnall11d3f4c2012-07-03 20:49:52 +00002696 case ObjCRuntime::GCC:
John McCall260611a2012-06-20 06:18:46 +00002697 return new CGObjCGCC(CGM);
2698
John McCallf7226fb2012-07-12 02:07:58 +00002699 case ObjCRuntime::ObjFW:
2700 return new CGObjCObjFW(CGM);
2701
John McCall260611a2012-06-20 06:18:46 +00002702 case ObjCRuntime::FragileMacOSX:
2703 case ObjCRuntime::MacOSX:
2704 case ObjCRuntime::iOS:
2705 llvm_unreachable("these runtimes are not GNU runtimes");
2706 }
2707 llvm_unreachable("bad runtime");
Chris Lattner0f984262008-03-01 08:50:34 +00002708}