blob: ce8123d016390b8d120d712ef5fd0f2bb90e7cae [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 Lattner5dc08672009-05-08 00:11:50 +000021
Chris Lattnerdce14062008-06-26 04:19:03 +000022#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000023#include "clang/AST/Decl.h"
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000024#include "clang/AST/DeclObjC.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000025#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000026#include "clang/AST/StmtObjC.h"
David Chisnall9f6614e2011-03-23 16:36:54 +000027#include "clang/Basic/SourceManager.h"
28#include "clang/Basic/FileManager.h"
Chris Lattner5dc08672009-05-08 00:11:50 +000029
30#include "llvm/Intrinsics.h"
Chris Lattner0f984262008-03-01 08:50:34 +000031#include "llvm/Module.h"
David Chisnallc6cd5fd2010-04-28 19:33:36 +000032#include "llvm/LLVMContext.h"
Chris Lattner0f984262008-03-01 08:50:34 +000033#include "llvm/ADT/SmallVector.h"
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000034#include "llvm/ADT/StringMap.h"
David Chisnall80558d22011-03-20 21:35:39 +000035#include "llvm/Support/CallSite.h"
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000036#include "llvm/Support/Compiler.h"
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000037#include "llvm/Target/TargetData.h"
Chris Lattner5dc08672009-05-08 00:11:50 +000038
David Chisnall9f6614e2011-03-23 16:36:54 +000039#include <stdarg.h>
Chris Lattnere160c9b2009-01-27 05:06:01 +000040
41
Chris Lattnerdce14062008-06-26 04:19:03 +000042using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000043using namespace CodeGen;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000044using llvm::dyn_cast;
45
Chris Lattner0f984262008-03-01 08:50:34 +000046
Chris Lattner0f984262008-03-01 08:50:34 +000047namespace {
David Chisnall81a65f52011-03-26 11:48:37 +000048/// Class that lazily initialises the runtime function. Avoids inserting the
49/// types and the function declaration into a module if they're not used, and
50/// avoids constructing the type more than once if it's used more than once.
David Chisnall9f6614e2011-03-23 16:36:54 +000051class LazyRuntimeFunction {
52 CodeGenModule *CGM;
53 std::vector<const llvm::Type*> ArgTys;
54 const char *FunctionName;
David Chisnall789ecde2011-05-23 22:33:28 +000055 llvm::Constant *Function;
David Chisnall9f6614e2011-03-23 16:36:54 +000056 public:
David Chisnall81a65f52011-03-26 11:48:37 +000057 /// Constructor leaves this class uninitialized, because it is intended to
58 /// be used as a field in another class and not all of the types that are
59 /// used as arguments will necessarily be available at construction time.
David Chisnall9f6614e2011-03-23 16:36:54 +000060 LazyRuntimeFunction() : CGM(0), FunctionName(0), Function(0) {}
61
David Chisnall81a65f52011-03-26 11:48:37 +000062 /// Initialises the lazy function with the name, return type, and the types
63 /// of the arguments.
David Chisnall9f6614e2011-03-23 16:36:54 +000064 END_WITH_NULL
65 void init(CodeGenModule *Mod, const char *name,
66 const llvm::Type *RetTy, ...) {
67 CGM =Mod;
68 FunctionName = name;
69 Function = 0;
David Chisnall9735ca62011-03-25 11:57:33 +000070 ArgTys.clear();
David Chisnall9f6614e2011-03-23 16:36:54 +000071 va_list Args;
72 va_start(Args, RetTy);
73 while (const llvm::Type *ArgTy = va_arg(Args, const llvm::Type*))
74 ArgTys.push_back(ArgTy);
75 va_end(Args);
76 // Push the return type on at the end so we can pop it off easily
77 ArgTys.push_back(RetTy);
78 }
David Chisnall81a65f52011-03-26 11:48:37 +000079 /// Overloaded cast operator, allows the class to be implicitly cast to an
80 /// LLVM constant.
David Chisnall789ecde2011-05-23 22:33:28 +000081 operator llvm::Constant*() {
David Chisnall9f6614e2011-03-23 16:36:54 +000082 if (!Function) {
David Chisnall9735ca62011-03-25 11:57:33 +000083 if (0 == FunctionName) return 0;
84 // We put the return type on the end of the vector, so pop it back off
David Chisnall9f6614e2011-03-23 16:36:54 +000085 const llvm::Type *RetTy = ArgTys.back();
86 ArgTys.pop_back();
87 llvm::FunctionType *FTy = llvm::FunctionType::get(RetTy, ArgTys, false);
88 Function =
David Chisnall789ecde2011-05-23 22:33:28 +000089 cast<llvm::Constant>(CGM->CreateRuntimeFunction(FTy, FunctionName));
David Chisnall9735ca62011-03-25 11:57:33 +000090 // We won't need to use the types again, so we may as well clean up the
91 // vector now
David Chisnall9f6614e2011-03-23 16:36:54 +000092 ArgTys.resize(0);
93 }
94 return Function;
95 }
David Chisnall789ecde2011-05-23 22:33:28 +000096 operator llvm::Function*() {
David Chisnall5f0bcc42011-05-23 23:15:11 +000097 return cast<llvm::Function>((llvm::Constant*)*this);
David Chisnall789ecde2011-05-23 22:33:28 +000098 }
David Chisnall5f0bcc42011-05-23 23:15:11 +000099
David Chisnall9f6614e2011-03-23 16:36:54 +0000100};
101
102
David Chisnall81a65f52011-03-26 11:48:37 +0000103/// GNU Objective-C runtime code generation. This class implements the parts of
104/// Objective-C support that are specific to the GNU family of runtimes (GCC and
105/// GNUstep).
David Chisnall9f6614e2011-03-23 16:36:54 +0000106class CGObjCGNU : public CGObjCRuntime {
David Chisnallc7ef4622011-03-23 22:52:06 +0000107protected:
David Chisnall81a65f52011-03-26 11:48:37 +0000108 /// The module that is using this class
David Chisnall9f6614e2011-03-23 16:36:54 +0000109 CodeGenModule &CGM;
David Chisnall81a65f52011-03-26 11:48:37 +0000110 /// The LLVM module into which output is inserted
Chris Lattner0f984262008-03-01 08:50:34 +0000111 llvm::Module &TheModule;
David Chisnall81a65f52011-03-26 11:48:37 +0000112 /// strut objc_super. Used for sending messages to super. This structure
113 /// contains the receiver (object) and the expected class.
David Chisnallc7ef4622011-03-23 22:52:06 +0000114 const llvm::StructType *ObjCSuperTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000115 /// struct objc_super*. The type of the argument to the superclass message
116 /// lookup functions.
David Chisnallc7ef4622011-03-23 22:52:06 +0000117 const llvm::PointerType *PtrToObjCSuperTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000118 /// LLVM type for selectors. Opaque pointer (i8*) unless a header declaring
119 /// SEL is included in a header somewhere, in which case it will be whatever
120 /// type is declared in that header, most likely {i8*, i8*}.
Chris Lattnere160c9b2009-01-27 05:06:01 +0000121 const llvm::PointerType *SelectorTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000122 /// LLVM i8 type. Cached here to avoid repeatedly getting it in all of the
123 /// places where it's used
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000124 const llvm::IntegerType *Int8Ty;
David Chisnall81a65f52011-03-26 11:48:37 +0000125 /// Pointer to i8 - LLVM type of char*, for all of the places where the
126 /// runtime needs to deal with C strings.
Chris Lattnere160c9b2009-01-27 05:06:01 +0000127 const llvm::PointerType *PtrToInt8Ty;
David Chisnall81a65f52011-03-26 11:48:37 +0000128 /// Instance Method Pointer type. This is a pointer to a function that takes,
129 /// at a minimum, an object and a selector, and is the generic type for
130 /// Objective-C methods. Due to differences between variadic / non-variadic
131 /// calling conventions, it must always be cast to the correct type before
132 /// actually being used.
David Chisnallc7ef4622011-03-23 22:52:06 +0000133 const llvm::PointerType *IMPTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000134 /// Type of an untyped Objective-C object. Clang treats id as a built-in type
135 /// when compiling Objective-C code, so this may be an opaque pointer (i8*),
136 /// but if the runtime header declaring it is included then it may be a
137 /// pointer to a structure.
Chris Lattnere160c9b2009-01-27 05:06:01 +0000138 const llvm::PointerType *IdTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000139 /// Pointer to a pointer to an Objective-C object. Used in the new ABI
140 /// message lookup function and some GC-related functions.
David Chisnallef6e0f32010-02-03 15:59:02 +0000141 const llvm::PointerType *PtrToIdTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000142 /// The clang type of id. Used when using the clang CGCall infrastructure to
143 /// call Objective-C methods.
John McCallead608a2010-02-26 00:48:12 +0000144 CanQualType ASTIdTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000145 /// LLVM type for C int type.
Chris Lattnere160c9b2009-01-27 05:06:01 +0000146 const llvm::IntegerType *IntTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000147 /// LLVM type for an opaque pointer. This is identical to PtrToInt8Ty, but is
148 /// used in the code to document the difference between i8* meaning a pointer
149 /// to a C string and i8* meaning a pointer to some opaque type.
Chris Lattnere160c9b2009-01-27 05:06:01 +0000150 const llvm::PointerType *PtrTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000151 /// LLVM type for C long type. The runtime uses this in a lot of places where
152 /// it should be using intptr_t, but we can't fix this without breaking
153 /// compatibility with GCC...
Chris Lattnere160c9b2009-01-27 05:06:01 +0000154 const llvm::IntegerType *LongTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000155 /// LLVM type for C size_t. Used in various runtime data structures.
David Chisnall8fac25d2010-12-26 22:13:16 +0000156 const llvm::IntegerType *SizeTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000157 /// LLVM type for C ptrdiff_t. Mainly used in property accessor functions.
David Chisnall8fac25d2010-12-26 22:13:16 +0000158 const llvm::IntegerType *PtrDiffTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000159 /// LLVM type for C int*. Used for GCC-ABI-compatible non-fragile instance
160 /// variables.
Chris Lattnere160c9b2009-01-27 05:06:01 +0000161 const llvm::PointerType *PtrToIntTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000162 /// LLVM type for Objective-C BOOL type.
David Chisnall8fac25d2010-12-26 22:13:16 +0000163 const llvm::Type *BoolTy;
David Chisnall81a65f52011-03-26 11:48:37 +0000164 /// Metadata kind used to tie method lookups to message sends. The GNUstep
165 /// runtime provides some LLVM passes that can use this to do things like
166 /// automatic IMP caching and speculative inlining.
David Chisnallc7ef4622011-03-23 22:52:06 +0000167 unsigned msgSendMDKind;
David Chisnall81a65f52011-03-26 11:48:37 +0000168 /// Helper function that generates a constant string and returns a pointer to
169 /// the start of the string. The result of this function can be used anywhere
170 /// where the C code specifies const char*.
David Chisnall9735ca62011-03-25 11:57:33 +0000171 llvm::Constant *MakeConstantString(const std::string &Str,
172 const std::string &Name="") {
173 llvm::Constant *ConstStr = CGM.GetAddrOfConstantCString(Str, Name.c_str());
174 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
175 }
David Chisnall81a65f52011-03-26 11:48:37 +0000176 /// Emits a linkonce_odr string, whose name is the prefix followed by the
177 /// string value. This allows the linker to combine the strings between
178 /// different modules. Used for EH typeinfo names, selector strings, and a
179 /// few other things.
David Chisnall9735ca62011-03-25 11:57:33 +0000180 llvm::Constant *ExportUniqueString(const std::string &Str,
181 const std::string prefix) {
182 std::string name = prefix + Str;
183 llvm::Constant *ConstStr = TheModule.getGlobalVariable(name);
184 if (!ConstStr) {
185 llvm::Constant *value = llvm::ConstantArray::get(VMContext, Str, true);
186 ConstStr = new llvm::GlobalVariable(TheModule, value->getType(), true,
187 llvm::GlobalValue::LinkOnceODRLinkage, value, prefix + Str);
188 }
189 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
190 }
David Chisnall81a65f52011-03-26 11:48:37 +0000191 /// Generates a global structure, initialized by the elements in the vector.
192 /// The element types must match the types of the structure elements in the
193 /// first argument.
David Chisnallc7ef4622011-03-23 22:52:06 +0000194 llvm::GlobalVariable *MakeGlobal(const llvm::StructType *Ty,
David Chisnall9735ca62011-03-25 11:57:33 +0000195 std::vector<llvm::Constant*> &V,
196 llvm::StringRef Name="",
197 llvm::GlobalValue::LinkageTypes linkage
198 =llvm::GlobalValue::InternalLinkage) {
199 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
200 return new llvm::GlobalVariable(TheModule, Ty, false,
201 linkage, C, Name);
202 }
David Chisnall81a65f52011-03-26 11:48:37 +0000203 /// Generates a global array. The vector must contain the same number of
204 /// elements that the array type declares, of the type specified as the array
205 /// element type.
David Chisnallc7ef4622011-03-23 22:52:06 +0000206 llvm::GlobalVariable *MakeGlobal(const llvm::ArrayType *Ty,
David Chisnall9735ca62011-03-25 11:57:33 +0000207 std::vector<llvm::Constant*> &V,
208 llvm::StringRef Name="",
209 llvm::GlobalValue::LinkageTypes linkage
210 =llvm::GlobalValue::InternalLinkage) {
211 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
212 return new llvm::GlobalVariable(TheModule, Ty, false,
213 linkage, C, Name);
214 }
David Chisnall81a65f52011-03-26 11:48:37 +0000215 /// Generates a global array, inferring the array type from the specified
216 /// element type and the size of the initialiser.
David Chisnallc7ef4622011-03-23 22:52:06 +0000217 llvm::GlobalVariable *MakeGlobalArray(const llvm::Type *Ty,
David Chisnall9735ca62011-03-25 11:57:33 +0000218 std::vector<llvm::Constant*> &V,
219 llvm::StringRef Name="",
220 llvm::GlobalValue::LinkageTypes linkage
221 =llvm::GlobalValue::InternalLinkage) {
222 llvm::ArrayType *ArrayTy = llvm::ArrayType::get(Ty, V.size());
223 return MakeGlobal(ArrayTy, V, Name, linkage);
224 }
David Chisnall81a65f52011-03-26 11:48:37 +0000225 /// Ensures that the value has the required type, by inserting a bitcast if
226 /// required. This function lets us avoid inserting bitcasts that are
227 /// redundant.
David Chisnallc7ef4622011-03-23 22:52:06 +0000228 llvm::Value* EnforceType(CGBuilderTy B, llvm::Value *V, const llvm::Type *Ty){
229 if (V->getType() == Ty) return V;
230 return B.CreateBitCast(V, Ty);
231 }
232 // Some zeros used for GEPs in lots of places.
233 llvm::Constant *Zeros[2];
David Chisnall81a65f52011-03-26 11:48:37 +0000234 /// Null pointer value. Mainly used as a terminator in various arrays.
David Chisnallc7ef4622011-03-23 22:52:06 +0000235 llvm::Constant *NULLPtr;
David Chisnall81a65f52011-03-26 11:48:37 +0000236 /// LLVM context.
David Chisnallc7ef4622011-03-23 22:52:06 +0000237 llvm::LLVMContext &VMContext;
238private:
David Chisnall81a65f52011-03-26 11:48:37 +0000239 /// Placeholder for the class. Lots of things refer to the class before we've
240 /// actually emitted it. We use this alias as a placeholder, and then replace
241 /// it with a pointer to the class structure before finally emitting the
242 /// module.
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000243 llvm::GlobalAlias *ClassPtrAlias;
David Chisnall81a65f52011-03-26 11:48:37 +0000244 /// Placeholder for the metaclass. Lots of things refer to the class before
245 /// we've / actually emitted it. We use this alias as a placeholder, and then
246 /// replace / it with a pointer to the metaclass structure before finally
247 /// emitting the / module.
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000248 llvm::GlobalAlias *MetaClassPtrAlias;
David Chisnall81a65f52011-03-26 11:48:37 +0000249 /// All of the classes that have been generated for this compilation units.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000250 std::vector<llvm::Constant*> Classes;
David Chisnall81a65f52011-03-26 11:48:37 +0000251 /// All of the categories that have been generated for this compilation units.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000252 std::vector<llvm::Constant*> Categories;
David Chisnall81a65f52011-03-26 11:48:37 +0000253 /// All of the Objective-C constant strings that have been generated for this
254 /// compilation units.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000255 std::vector<llvm::Constant*> ConstantStrings;
David Chisnall81a65f52011-03-26 11:48:37 +0000256 /// Map from string values to Objective-C constant strings in the output.
257 /// Used to prevent emitting Objective-C strings more than once. This should
258 /// not be required at all - CodeGenModule should manage this list.
David Chisnall48272a02010-01-27 12:49:23 +0000259 llvm::StringMap<llvm::Constant*> ObjCStrings;
David Chisnall81a65f52011-03-26 11:48:37 +0000260 /// All of the protocols that have been declared.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000261 llvm::StringMap<llvm::Constant*> ExistingProtocols;
David Chisnall81a65f52011-03-26 11:48:37 +0000262 /// For each variant of a selector, we store the type encoding and a
263 /// placeholder value. For an untyped selector, the type will be the empty
264 /// string. Selector references are all done via the module's selector table,
265 /// so we create an alias as a placeholder and then replace it with the real
266 /// value later.
David Chisnall9f6614e2011-03-23 16:36:54 +0000267 typedef std::pair<std::string, llvm::GlobalAlias*> TypedSelector;
David Chisnall81a65f52011-03-26 11:48:37 +0000268 /// Type of the selector map. This is roughly equivalent to the structure
269 /// used in the GNUstep runtime, which maintains a list of all of the valid
270 /// types for a selector in a table.
David Chisnall9f6614e2011-03-23 16:36:54 +0000271 typedef llvm::DenseMap<Selector, llvm::SmallVector<TypedSelector, 2> >
272 SelectorMap;
David Chisnall81a65f52011-03-26 11:48:37 +0000273 /// A map from selectors to selector types. This allows us to emit all
274 /// selectors of the same name and type together.
David Chisnall9f6614e2011-03-23 16:36:54 +0000275 SelectorMap SelectorTable;
276
David Chisnall81a65f52011-03-26 11:48:37 +0000277 /// Selectors related to memory management. When compiling in GC mode, we
278 /// omit these.
David Chisnallef6e0f32010-02-03 15:59:02 +0000279 Selector RetainSel, ReleaseSel, AutoreleaseSel;
David Chisnall81a65f52011-03-26 11:48:37 +0000280 /// Runtime functions used for memory management in GC mode. Note that clang
281 /// supports code generation for calling these functions, but neither GNU
282 /// runtime actually supports this API properly yet.
David Chisnall9f6614e2011-03-23 16:36:54 +0000283 LazyRuntimeFunction IvarAssignFn, StrongCastAssignFn, MemMoveFn, WeakReadFn,
284 WeakAssignFn, GlobalAssignFn;
David Chisnall9f6614e2011-03-23 16:36:54 +0000285
David Chisnall9735ca62011-03-25 11:57:33 +0000286protected:
David Chisnall81a65f52011-03-26 11:48:37 +0000287 /// Function used for throwing Objective-C exceptions.
David Chisnall9f6614e2011-03-23 16:36:54 +0000288 LazyRuntimeFunction ExceptionThrowFn;
David Chisnall81a65f52011-03-26 11:48:37 +0000289 /// Function used for rethrowing exceptions, used at the end of @finally or
290 /// @synchronize blocks.
David Chisnall9735ca62011-03-25 11:57:33 +0000291 LazyRuntimeFunction ExceptionReThrowFn;
David Chisnall81a65f52011-03-26 11:48:37 +0000292 /// Function called when entering a catch function. This is required for
293 /// differentiating Objective-C exceptions and foreign exceptions.
David Chisnall9735ca62011-03-25 11:57:33 +0000294 LazyRuntimeFunction EnterCatchFn;
David Chisnall81a65f52011-03-26 11:48:37 +0000295 /// Function called when exiting from a catch block. Used to do exception
296 /// cleanup.
David Chisnall9735ca62011-03-25 11:57:33 +0000297 LazyRuntimeFunction ExitCatchFn;
David Chisnall81a65f52011-03-26 11:48:37 +0000298 /// Function called when entering an @synchronize block. Acquires the lock.
David Chisnall9f6614e2011-03-23 16:36:54 +0000299 LazyRuntimeFunction SyncEnterFn;
David Chisnall81a65f52011-03-26 11:48:37 +0000300 /// Function called when exiting an @synchronize block. Releases the lock.
David Chisnall9f6614e2011-03-23 16:36:54 +0000301 LazyRuntimeFunction SyncExitFn;
302
David Chisnall9735ca62011-03-25 11:57:33 +0000303private:
304
David Chisnall81a65f52011-03-26 11:48:37 +0000305 /// Function called if fast enumeration detects that the collection is
306 /// modified during the update.
David Chisnall9f6614e2011-03-23 16:36:54 +0000307 LazyRuntimeFunction EnumerationMutationFn;
David Chisnall81a65f52011-03-26 11:48:37 +0000308 /// Function for implementing synthesized property getters that return an
309 /// object.
David Chisnall9f6614e2011-03-23 16:36:54 +0000310 LazyRuntimeFunction GetPropertyFn;
David Chisnall81a65f52011-03-26 11:48:37 +0000311 /// Function for implementing synthesized property setters that return an
312 /// object.
David Chisnall9f6614e2011-03-23 16:36:54 +0000313 LazyRuntimeFunction SetPropertyFn;
David Chisnall81a65f52011-03-26 11:48:37 +0000314 /// Function used for non-object declared property getters.
David Chisnall9f6614e2011-03-23 16:36:54 +0000315 LazyRuntimeFunction GetStructPropertyFn;
David Chisnall81a65f52011-03-26 11:48:37 +0000316 /// Function used for non-object declared property setters.
David Chisnall9f6614e2011-03-23 16:36:54 +0000317 LazyRuntimeFunction SetStructPropertyFn;
318
David Chisnall81a65f52011-03-26 11:48:37 +0000319 /// The version of the runtime that this class targets. Must match the
320 /// version in the runtime.
David Chisnalla2120032011-05-22 22:37:08 +0000321 int RuntimeVersion;
David Chisnall81a65f52011-03-26 11:48:37 +0000322 /// The version of the protocol class. Used to differentiate between ObjC1
323 /// and ObjC2 protocols. Objective-C 1 protocols can not contain optional
324 /// components and can not contain declared properties. We always emit
325 /// Objective-C 2 property structures, but we have to pretend that they're
326 /// Objective-C 1 property structures when targeting the GCC runtime or it
327 /// will abort.
David Chisnall9f6614e2011-03-23 16:36:54 +0000328 const int ProtocolVersion;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000329private:
David Chisnall81a65f52011-03-26 11:48:37 +0000330 /// Generates an instance variable list structure. This is a structure
331 /// containing a size and an array of structures containing instance variable
332 /// metadata. This is used purely for introspection in the fragile ABI. In
333 /// the non-fragile ABI, it's used for instance variable fixup.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000334 llvm::Constant *GenerateIvarList(
335 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
336 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
337 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets);
David Chisnall81a65f52011-03-26 11:48:37 +0000338 /// Generates a method list structure. This is a structure containing a size
339 /// and an array of structures containing method metadata.
340 ///
341 /// This structure is used by both classes and categories, and contains a next
342 /// pointer allowing them to be chained together in a linked list.
David Chisnall9f6614e2011-03-23 16:36:54 +0000343 llvm::Constant *GenerateMethodList(const llvm::StringRef &ClassName,
344 const llvm::StringRef &CategoryName,
Mike Stump1eb44332009-09-09 15:08:12 +0000345 const llvm::SmallVectorImpl<Selector> &MethodSels,
346 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000347 bool isClassMethodList);
David Chisnall81a65f52011-03-26 11:48:37 +0000348 /// Emits an empty protocol. This is used for @protocol() where no protocol
349 /// is found. The runtime will (hopefully) fix up the pointer to refer to the
350 /// real protocol.
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000351 llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName);
David Chisnall81a65f52011-03-26 11:48:37 +0000352 /// Generates a list of property metadata structures. This follows the same
353 /// pattern as method and instance variable metadata lists.
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000354 llvm::Constant *GeneratePropertyList(const ObjCImplementationDecl *OID,
355 llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
356 llvm::SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes);
David Chisnall81a65f52011-03-26 11:48:37 +0000357 /// Generates a list of referenced protocols. Classes, categories, and
358 /// protocols all use this structure.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000359 llvm::Constant *GenerateProtocolList(
360 const llvm::SmallVectorImpl<std::string> &Protocols);
David Chisnall81a65f52011-03-26 11:48:37 +0000361 /// To ensure that all protocols are seen by the runtime, we add a category on
362 /// a class defined in the runtime, declaring no methods, but adopting the
363 /// protocols. This is a horribly ugly hack, but it allows us to collect all
364 /// of the protocols without changing the ABI.
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000365 void GenerateProtocolHolderCategory(void);
David Chisnall81a65f52011-03-26 11:48:37 +0000366 /// Generates a class structure.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000367 llvm::Constant *GenerateClassStructure(
368 llvm::Constant *MetaClass,
369 llvm::Constant *SuperClass,
370 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000371 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000372 llvm::Constant *Version,
373 llvm::Constant *InstanceSize,
374 llvm::Constant *IVars,
375 llvm::Constant *Methods,
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000376 llvm::Constant *Protocols,
377 llvm::Constant *IvarOffsets,
David Chisnall8c757f92010-04-28 14:29:56 +0000378 llvm::Constant *Properties,
379 bool isMeta=false);
David Chisnall81a65f52011-03-26 11:48:37 +0000380 /// Generates a method list. This is used by protocols to define the required
381 /// and optional methods.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000382 llvm::Constant *GenerateProtocolMethodList(
383 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
384 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes);
David Chisnall81a65f52011-03-26 11:48:37 +0000385 /// Returns a selector with the specified type encoding. An empty string is
386 /// used to return an untyped selector (with the types field set to NULL).
David Chisnall9f6614e2011-03-23 16:36:54 +0000387 llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
388 const std::string &TypeEncoding, bool lval);
David Chisnall81a65f52011-03-26 11:48:37 +0000389 /// Returns the variable used to store the offset of an instance variable.
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +0000390 llvm::GlobalVariable *ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
391 const ObjCIvarDecl *Ivar);
David Chisnall81a65f52011-03-26 11:48:37 +0000392 /// Emits a reference to a class. This allows the linker to object if there
393 /// is no class of the matching name.
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000394 void EmitClassRef(const std::string &className);
David Chisnallc7ef4622011-03-23 22:52:06 +0000395protected:
David Chisnall81a65f52011-03-26 11:48:37 +0000396 /// Looks up the method for sending a message to the specified object. This
397 /// mechanism differs between the GCC and GNU runtimes, so this method must be
398 /// overridden in subclasses.
David Chisnallc7ef4622011-03-23 22:52:06 +0000399 virtual llvm::Value *LookupIMP(CodeGenFunction &CGF,
400 llvm::Value *&Receiver,
401 llvm::Value *cmd,
402 llvm::MDNode *node) = 0;
David Chisnall81a65f52011-03-26 11:48:37 +0000403 /// Looks up the method for sending a message to a superclass. This mechanism
404 /// differs between the GCC and GNU runtimes, so this method must be
405 /// overridden in subclasses.
David Chisnallc7ef4622011-03-23 22:52:06 +0000406 virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF,
407 llvm::Value *ObjCSuper,
408 llvm::Value *cmd) = 0;
Chris Lattner0f984262008-03-01 08:50:34 +0000409public:
David Chisnall9f6614e2011-03-23 16:36:54 +0000410 CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion,
411 unsigned protocolClassVersion);
412
David Chisnall0d13f6f2010-01-23 02:40:42 +0000413 virtual llvm::Constant *GenerateConstantString(const StringLiteral *);
David Chisnall9f6614e2011-03-23 16:36:54 +0000414
415 virtual RValue
416 GenerateMessageSend(CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +0000417 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000418 QualType ResultType,
419 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000420 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000421 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000422 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000423 const ObjCMethodDecl *Method);
David Chisnall9f6614e2011-03-23 16:36:54 +0000424 virtual RValue
425 GenerateMessageSendSuper(CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +0000426 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000427 QualType ResultType,
428 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000429 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000430 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000431 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000432 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000433 const CallArgList &CallArgs,
434 const ObjCMethodDecl *Method);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000435 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000436 const ObjCInterfaceDecl *OID);
Fariborz Jahanian03b29602010-06-17 19:56:20 +0000437 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
438 bool lval = false);
Daniel Dunbar6d5a1c22010-02-03 20:11:42 +0000439 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
440 *Method);
John McCall5a180392010-07-24 00:37:23 +0000441 virtual llvm::Constant *GetEHType(QualType T);
Mike Stump1eb44332009-09-09 15:08:12 +0000442
443 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000444 const ObjCContainerDecl *CD);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000445 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
446 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000447 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000448 const ObjCProtocolDecl *PD);
449 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000450 virtual llvm::Function *ModuleInitFunction();
David Chisnall789ecde2011-05-23 22:33:28 +0000451 virtual llvm::Constant *GetPropertyGetFunction();
452 virtual llvm::Constant *GetPropertySetFunction();
453 virtual llvm::Constant *GetSetStructFunction();
454 virtual llvm::Constant *GetGetStructFunction();
Daniel Dunbar309a4362009-07-24 07:40:24 +0000455 virtual llvm::Constant *EnumerationMutationFunction();
Mike Stump1eb44332009-09-09 15:08:12 +0000456
David Chisnall9f6614e2011-03-23 16:36:54 +0000457 virtual void EmitTryStmt(CodeGenFunction &CGF,
John McCallf1549f62010-07-06 01:34:17 +0000458 const ObjCAtTryStmt &S);
David Chisnall9f6614e2011-03-23 16:36:54 +0000459 virtual void EmitSynchronizedStmt(CodeGenFunction &CGF,
John McCallf1549f62010-07-06 01:34:17 +0000460 const ObjCAtSynchronizedStmt &S);
David Chisnall9f6614e2011-03-23 16:36:54 +0000461 virtual void EmitThrowStmt(CodeGenFunction &CGF,
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000462 const ObjCAtThrowStmt &S);
David Chisnall9f6614e2011-03-23 16:36:54 +0000463 virtual llvm::Value * EmitObjCWeakRead(CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000464 llvm::Value *AddrWeakObj);
David Chisnall9f6614e2011-03-23 16:36:54 +0000465 virtual void EmitObjCWeakAssign(CodeGenFunction &CGF,
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000466 llvm::Value *src, llvm::Value *dst);
David Chisnall9f6614e2011-03-23 16:36:54 +0000467 virtual void EmitObjCGlobalAssign(CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000468 llvm::Value *src, llvm::Value *dest,
469 bool threadlocal=false);
David Chisnall9f6614e2011-03-23 16:36:54 +0000470 virtual void EmitObjCIvarAssign(CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000471 llvm::Value *src, llvm::Value *dest,
472 llvm::Value *ivarOffset);
David Chisnall9f6614e2011-03-23 16:36:54 +0000473 virtual void EmitObjCStrongCastAssign(CodeGenFunction &CGF,
Fariborz Jahanian58626502008-11-19 00:59:10 +0000474 llvm::Value *src, llvm::Value *dest);
David Chisnall9f6614e2011-03-23 16:36:54 +0000475 virtual void EmitGCMemmoveCollectable(CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +0000476 llvm::Value *DestPtr,
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000477 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +0000478 llvm::Value *Size);
David Chisnall9f6614e2011-03-23 16:36:54 +0000479 virtual LValue EmitObjCValueForIvar(CodeGenFunction &CGF,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000480 QualType ObjectTy,
481 llvm::Value *BaseValue,
482 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000483 unsigned CVRQualifiers);
David Chisnall9f6614e2011-03-23 16:36:54 +0000484 virtual llvm::Value *EmitIvarOffset(CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +0000485 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000486 const ObjCIvarDecl *Ivar);
David Chisnall9f6614e2011-03-23 16:36:54 +0000487 virtual llvm::Constant *BuildGCBlockLayout(CodeGenModule &CGM,
John McCall6b5a61b2011-02-07 10:33:21 +0000488 const CGBlockInfo &blockInfo) {
Fariborz Jahanian89ecd412010-08-04 16:57:49 +0000489 return NULLPtr;
490 }
Fariborz Jahanian6f40e222011-05-17 22:21:16 +0000491
492 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
493 return 0;
494 }
Chris Lattner0f984262008-03-01 08:50:34 +0000495};
David Chisnall81a65f52011-03-26 11:48:37 +0000496/// Class representing the legacy GCC Objective-C ABI. This is the default when
497/// -fobjc-nonfragile-abi is not specified.
498///
499/// The GCC ABI target actually generates code that is approximately compatible
500/// with the new GNUstep runtime ABI, but refrains from using any features that
501/// would not work with the GCC runtime. For example, clang always generates
502/// the extended form of the class structure, and the extra fields are simply
503/// ignored by GCC libobjc.
David Chisnall9f6614e2011-03-23 16:36:54 +0000504class CGObjCGCC : public CGObjCGNU {
David Chisnall81a65f52011-03-26 11:48:37 +0000505 /// The GCC ABI message lookup function. Returns an IMP pointing to the
506 /// method implementation for this message.
David Chisnallc7ef4622011-03-23 22:52:06 +0000507 LazyRuntimeFunction MsgLookupFn;
David Chisnall81a65f52011-03-26 11:48:37 +0000508 /// The GCC ABI superclass message lookup function. Takes a pointer to a
509 /// structure describing the receiver and the class, and a selector as
510 /// arguments. Returns the IMP for the corresponding method.
David Chisnallc7ef4622011-03-23 22:52:06 +0000511 LazyRuntimeFunction MsgLookupSuperFn;
512protected:
513 virtual llvm::Value *LookupIMP(CodeGenFunction &CGF,
514 llvm::Value *&Receiver,
515 llvm::Value *cmd,
516 llvm::MDNode *node) {
517 CGBuilderTy &Builder = CGF.Builder;
518 llvm::Value *imp = Builder.CreateCall2(MsgLookupFn,
519 EnforceType(Builder, Receiver, IdTy),
520 EnforceType(Builder, cmd, SelectorTy));
521 cast<llvm::CallInst>(imp)->setMetadata(msgSendMDKind, node);
522 return imp;
523 }
524 virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF,
525 llvm::Value *ObjCSuper,
526 llvm::Value *cmd) {
527 CGBuilderTy &Builder = CGF.Builder;
528 llvm::Value *lookupArgs[] = {EnforceType(Builder, ObjCSuper,
529 PtrToObjCSuperTy), cmd};
530 return Builder.CreateCall(MsgLookupSuperFn, lookupArgs, lookupArgs+2);
531 }
David Chisnall9f6614e2011-03-23 16:36:54 +0000532 public:
David Chisnallc7ef4622011-03-23 22:52:06 +0000533 CGObjCGCC(CodeGenModule &Mod) : CGObjCGNU(Mod, 8, 2) {
534 // IMP objc_msg_lookup(id, SEL);
535 MsgLookupFn.init(&CGM, "objc_msg_lookup", IMPTy, IdTy, SelectorTy, NULL);
536 // IMP objc_msg_lookup_super(struct objc_super*, SEL);
537 MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy,
538 PtrToObjCSuperTy, SelectorTy, NULL);
539 }
David Chisnall9f6614e2011-03-23 16:36:54 +0000540};
David Chisnall81a65f52011-03-26 11:48:37 +0000541/// Class used when targeting the new GNUstep runtime ABI.
David Chisnall9f6614e2011-03-23 16:36:54 +0000542class CGObjCGNUstep : public CGObjCGNU {
David Chisnall81a65f52011-03-26 11:48:37 +0000543 /// The slot lookup function. Returns a pointer to a cacheable structure
544 /// that contains (among other things) the IMP.
David Chisnallc7ef4622011-03-23 22:52:06 +0000545 LazyRuntimeFunction SlotLookupFn;
David Chisnall81a65f52011-03-26 11:48:37 +0000546 /// The GNUstep ABI superclass message lookup function. Takes a pointer to
547 /// a structure describing the receiver and the class, and a selector as
548 /// arguments. Returns the slot for the corresponding method. Superclass
549 /// message lookup rarely changes, so this is a good caching opportunity.
David Chisnallc7ef4622011-03-23 22:52:06 +0000550 LazyRuntimeFunction SlotLookupSuperFn;
David Chisnall81a65f52011-03-26 11:48:37 +0000551 /// Type of an slot structure pointer. This is returned by the various
552 /// lookup functions.
David Chisnallc7ef4622011-03-23 22:52:06 +0000553 llvm::Type *SlotTy;
554 protected:
555 virtual llvm::Value *LookupIMP(CodeGenFunction &CGF,
556 llvm::Value *&Receiver,
557 llvm::Value *cmd,
558 llvm::MDNode *node) {
559 CGBuilderTy &Builder = CGF.Builder;
560 llvm::Function *LookupFn = SlotLookupFn;
561
562 // Store the receiver on the stack so that we can reload it later
563 llvm::Value *ReceiverPtr = CGF.CreateTempAlloca(Receiver->getType());
564 Builder.CreateStore(Receiver, ReceiverPtr);
565
566 llvm::Value *self;
567
568 if (isa<ObjCMethodDecl>(CGF.CurCodeDecl)) {
569 self = CGF.LoadObjCSelf();
570 } else {
571 self = llvm::ConstantPointerNull::get(IdTy);
572 }
573
574 // The lookup function is guaranteed not to capture the receiver pointer.
575 LookupFn->setDoesNotCapture(1);
576
577 llvm::CallInst *slot =
578 Builder.CreateCall3(LookupFn,
579 EnforceType(Builder, ReceiverPtr, PtrToIdTy),
580 EnforceType(Builder, cmd, SelectorTy),
581 EnforceType(Builder, self, IdTy));
582 slot->setOnlyReadsMemory();
583 slot->setMetadata(msgSendMDKind, node);
584
585 // Load the imp from the slot
586 llvm::Value *imp = Builder.CreateLoad(Builder.CreateStructGEP(slot, 4));
587
588 // The lookup function may have changed the receiver, so make sure we use
589 // the new one.
590 Receiver = Builder.CreateLoad(ReceiverPtr, true);
591 return imp;
592 }
593 virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF,
594 llvm::Value *ObjCSuper,
595 llvm::Value *cmd) {
596 CGBuilderTy &Builder = CGF.Builder;
597 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
598
599 llvm::CallInst *slot = Builder.CreateCall(SlotLookupSuperFn, lookupArgs,
600 lookupArgs+2);
601 slot->setOnlyReadsMemory();
602
603 return Builder.CreateLoad(Builder.CreateStructGEP(slot, 4));
604 }
David Chisnall9f6614e2011-03-23 16:36:54 +0000605 public:
David Chisnallc7ef4622011-03-23 22:52:06 +0000606 CGObjCGNUstep(CodeGenModule &Mod) : CGObjCGNU(Mod, 9, 3) {
Chris Lattner7650d952011-06-18 22:49:11 +0000607 llvm::StructType *SlotStructTy = llvm::StructType::get(PtrTy,
David Chisnallc7ef4622011-03-23 22:52:06 +0000608 PtrTy, PtrTy, IntTy, IMPTy, NULL);
609 SlotTy = llvm::PointerType::getUnqual(SlotStructTy);
610 // Slot_t objc_msg_lookup_sender(id *receiver, SEL selector, id sender);
611 SlotLookupFn.init(&CGM, "objc_msg_lookup_sender", SlotTy, PtrToIdTy,
612 SelectorTy, IdTy, NULL);
613 // Slot_t objc_msg_lookup_super(struct objc_super*, SEL);
614 SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy,
615 PtrToObjCSuperTy, SelectorTy, NULL);
David Chisnall9735ca62011-03-25 11:57:33 +0000616 // If we're in ObjC++ mode, then we want to make
617 if (CGM.getLangOptions().CPlusPlus) {
618 const llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
619 // void *__cxa_begin_catch(void *e)
620 EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy, NULL);
621 // void __cxa_end_catch(void)
622 EnterCatchFn.init(&CGM, "__cxa_end_catch", VoidTy, NULL);
623 // void _Unwind_Resume_or_Rethrow(void*)
David Chisnall978d4152011-04-05 17:15:18 +0000624 ExceptionReThrowFn.init(&CGM, "_Unwind_Resume_or_Rethrow", VoidTy, PtrTy, NULL);
David Chisnall9735ca62011-03-25 11:57:33 +0000625 }
David Chisnallc7ef4622011-03-23 22:52:06 +0000626 }
David Chisnall9f6614e2011-03-23 16:36:54 +0000627};
628
Chris Lattner0f984262008-03-01 08:50:34 +0000629} // end anonymous namespace
630
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000631
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000632/// Emits a reference to a dummy variable which is emitted with each class.
633/// This ensures that a linker error will be generated when trying to link
634/// together modules where a referenced class is not defined.
Mike Stumpbb1c8602009-07-31 21:31:32 +0000635void CGObjCGNU::EmitClassRef(const std::string &className) {
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000636 std::string symbolRef = "__objc_class_ref_" + className;
637 // Don't emit two copies of the same symbol
Mike Stumpbb1c8602009-07-31 21:31:32 +0000638 if (TheModule.getGlobalVariable(symbolRef))
639 return;
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000640 std::string symbolName = "__objc_class_name_" + className;
641 llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(symbolName);
642 if (!ClassSymbol) {
Owen Anderson1c431b32009-07-08 19:05:04 +0000643 ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false,
644 llvm::GlobalValue::ExternalLinkage, 0, symbolName);
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000645 }
Owen Anderson1c431b32009-07-08 19:05:04 +0000646 new llvm::GlobalVariable(TheModule, ClassSymbol->getType(), true,
Chris Lattnerf35271b2009-08-05 05:25:18 +0000647 llvm::GlobalValue::WeakAnyLinkage, ClassSymbol, symbolRef);
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000648}
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000649
David Chisnall9f6614e2011-03-23 16:36:54 +0000650static std::string SymbolNameForMethod(const llvm::StringRef &ClassName,
651 const llvm::StringRef &CategoryName, const Selector MethodName,
652 bool isClassMethod) {
653 std::string MethodNameColonStripped = MethodName.getAsString();
David Chisnalld3467362010-01-14 14:08:19 +0000654 std::replace(MethodNameColonStripped.begin(), MethodNameColonStripped.end(),
655 ':', '_');
David Chisnall9f6614e2011-03-23 16:36:54 +0000656 return (llvm::Twine(isClassMethod ? "_c_" : "_i_") + ClassName + "_" +
657 CategoryName + "_" + MethodNameColonStripped).str();
David Chisnall87935a82010-05-08 20:58:05 +0000658}
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000659
David Chisnall9f6614e2011-03-23 16:36:54 +0000660CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion,
661 unsigned protocolClassVersion)
David Chisnallc7ef4622011-03-23 22:52:06 +0000662 : CGM(cgm), TheModule(CGM.getModule()), VMContext(cgm.getLLVMContext()),
663 ClassPtrAlias(0), MetaClassPtrAlias(0), RuntimeVersion(runtimeABIVersion),
664 ProtocolVersion(protocolClassVersion) {
David Chisnallc6cd5fd2010-04-28 19:33:36 +0000665
666 msgSendMDKind = VMContext.getMDKindID("GNUObjCMessageSend");
667
David Chisnall9f6614e2011-03-23 16:36:54 +0000668 CodeGenTypes &Types = CGM.getTypes();
Chris Lattnere160c9b2009-01-27 05:06:01 +0000669 IntTy = cast<llvm::IntegerType>(
David Chisnall9f6614e2011-03-23 16:36:54 +0000670 Types.ConvertType(CGM.getContext().IntTy));
Chris Lattnere160c9b2009-01-27 05:06:01 +0000671 LongTy = cast<llvm::IntegerType>(
David Chisnall9f6614e2011-03-23 16:36:54 +0000672 Types.ConvertType(CGM.getContext().LongTy));
David Chisnall8fac25d2010-12-26 22:13:16 +0000673 SizeTy = cast<llvm::IntegerType>(
David Chisnall9f6614e2011-03-23 16:36:54 +0000674 Types.ConvertType(CGM.getContext().getSizeType()));
David Chisnall8fac25d2010-12-26 22:13:16 +0000675 PtrDiffTy = cast<llvm::IntegerType>(
David Chisnall9f6614e2011-03-23 16:36:54 +0000676 Types.ConvertType(CGM.getContext().getPointerDiffType()));
David Chisnall8fac25d2010-12-26 22:13:16 +0000677 BoolTy = CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000679 Int8Ty = llvm::Type::getInt8Ty(VMContext);
680 // C string type. Used in lots of places.
681 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
682
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000683 Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000684 Zeros[1] = Zeros[0];
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +0000685 NULLPtr = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Chris Lattner391d77a2008-03-30 23:03:07 +0000686 // Get the selector Type.
David Chisnall0d13f6f2010-01-23 02:40:42 +0000687 QualType selTy = CGM.getContext().getObjCSelType();
688 if (QualType() == selTy) {
689 SelectorTy = PtrToInt8Ty;
690 } else {
691 SelectorTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(selTy));
692 }
Chris Lattnere160c9b2009-01-27 05:06:01 +0000693
Owen Anderson96e0fc72009-07-29 22:16:19 +0000694 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
Chris Lattner391d77a2008-03-30 23:03:07 +0000695 PtrTy = PtrToInt8Ty;
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Chris Lattner391d77a2008-03-30 23:03:07 +0000697 // Object type
David Chisnall7bcf6c32011-04-29 14:10:35 +0000698 QualType UnqualIdTy = CGM.getContext().getObjCIdType();
699 ASTIdTy = CanQualType();
700 if (UnqualIdTy != QualType()) {
701 ASTIdTy = CGM.getContext().getCanonicalType(UnqualIdTy);
David Chisnall0d13f6f2010-01-23 02:40:42 +0000702 IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
David Chisnall7bcf6c32011-04-29 14:10:35 +0000703 } else {
704 IdTy = PtrToInt8Ty;
David Chisnall0d13f6f2010-01-23 02:40:42 +0000705 }
David Chisnallef6e0f32010-02-03 15:59:02 +0000706 PtrToIdTy = llvm::PointerType::getUnqual(IdTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Chris Lattner7650d952011-06-18 22:49:11 +0000708 ObjCSuperTy = llvm::StructType::get(IdTy, IdTy, NULL);
David Chisnallc7ef4622011-03-23 22:52:06 +0000709 PtrToObjCSuperTy = llvm::PointerType::getUnqual(ObjCSuperTy);
710
David Chisnall9f6614e2011-03-23 16:36:54 +0000711 const llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext);
712
713 // void objc_exception_throw(id);
714 ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy, NULL);
David Chisnall9735ca62011-03-25 11:57:33 +0000715 ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy, NULL);
David Chisnall9f6614e2011-03-23 16:36:54 +0000716 // int objc_sync_enter(id);
717 SyncEnterFn.init(&CGM, "objc_sync_enter", IntTy, IdTy, NULL);
718 // int objc_sync_exit(id);
719 SyncExitFn.init(&CGM, "objc_sync_exit", IntTy, IdTy, NULL);
720
721 // void objc_enumerationMutation (id)
722 EnumerationMutationFn.init(&CGM, "objc_enumerationMutation", VoidTy,
723 IdTy, NULL);
724
725 // id objc_getProperty(id, SEL, ptrdiff_t, BOOL)
726 GetPropertyFn.init(&CGM, "objc_getProperty", IdTy, IdTy, SelectorTy,
727 PtrDiffTy, BoolTy, NULL);
728 // void objc_setProperty(id, SEL, ptrdiff_t, id, BOOL, BOOL)
729 SetPropertyFn.init(&CGM, "objc_setProperty", VoidTy, IdTy, SelectorTy,
730 PtrDiffTy, IdTy, BoolTy, BoolTy, NULL);
731 // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL)
732 GetStructPropertyFn.init(&CGM, "objc_getPropertyStruct", VoidTy, PtrTy, PtrTy,
733 PtrDiffTy, BoolTy, BoolTy, NULL);
734 // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL)
735 SetStructPropertyFn.init(&CGM, "objc_setPropertyStruct", VoidTy, PtrTy, PtrTy,
736 PtrDiffTy, BoolTy, BoolTy, NULL);
737
Chris Lattner391d77a2008-03-30 23:03:07 +0000738 // IMP type
Benjamin Kramer95d318c2011-05-28 14:26:31 +0000739 const llvm::Type *IMPArgs[] = { IdTy, SelectorTy };
David Chisnallc7ef4622011-03-23 22:52:06 +0000740 IMPTy = llvm::PointerType::getUnqual(llvm::FunctionType::get(IdTy, IMPArgs,
741 true));
David Chisnallef6e0f32010-02-03 15:59:02 +0000742
David Chisnall9735ca62011-03-25 11:57:33 +0000743 // Don't bother initialising the GC stuff unless we're compiling in GC mode
David Chisnallef6e0f32010-02-03 15:59:02 +0000744 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) {
David Chisnalla2120032011-05-22 22:37:08 +0000745 // This is a bit of an hack. We should sort this out by having a proper
746 // CGObjCGNUstep subclass for GC, but we may want to really support the old
747 // ABI and GC added in ObjectiveC2.framework, so we fudge it a bit for now
748 RuntimeVersion = 10;
David Chisnallef6e0f32010-02-03 15:59:02 +0000749 // Get selectors needed in GC mode
750 RetainSel = GetNullarySelector("retain", CGM.getContext());
751 ReleaseSel = GetNullarySelector("release", CGM.getContext());
752 AutoreleaseSel = GetNullarySelector("autorelease", CGM.getContext());
753
754 // Get functions needed in GC mode
755
756 // id objc_assign_ivar(id, id, ptrdiff_t);
David Chisnall9f6614e2011-03-23 16:36:54 +0000757 IvarAssignFn.init(&CGM, "objc_assign_ivar", IdTy, IdTy, IdTy, PtrDiffTy,
758 NULL);
David Chisnallef6e0f32010-02-03 15:59:02 +0000759 // id objc_assign_strongCast (id, id*)
David Chisnall9f6614e2011-03-23 16:36:54 +0000760 StrongCastAssignFn.init(&CGM, "objc_assign_strongCast", IdTy, IdTy,
761 PtrToIdTy, NULL);
David Chisnallef6e0f32010-02-03 15:59:02 +0000762 // id objc_assign_global(id, id*);
David Chisnall9f6614e2011-03-23 16:36:54 +0000763 GlobalAssignFn.init(&CGM, "objc_assign_global", IdTy, IdTy, PtrToIdTy,
764 NULL);
David Chisnallef6e0f32010-02-03 15:59:02 +0000765 // id objc_assign_weak(id, id*);
David Chisnall9f6614e2011-03-23 16:36:54 +0000766 WeakAssignFn.init(&CGM, "objc_assign_weak", IdTy, IdTy, PtrToIdTy, NULL);
David Chisnallef6e0f32010-02-03 15:59:02 +0000767 // id objc_read_weak(id*);
David Chisnall9f6614e2011-03-23 16:36:54 +0000768 WeakReadFn.init(&CGM, "objc_read_weak", IdTy, PtrToIdTy, NULL);
David Chisnallef6e0f32010-02-03 15:59:02 +0000769 // void *objc_memmove_collectable(void*, void *, size_t);
David Chisnall9f6614e2011-03-23 16:36:54 +0000770 MemMoveFn.init(&CGM, "objc_memmove_collectable", PtrTy, PtrTy, PtrTy,
771 SizeTy, NULL);
David Chisnallef6e0f32010-02-03 15:59:02 +0000772 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000773}
Mike Stumpbb1c8602009-07-31 21:31:32 +0000774
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000775// This has to perform the lookup every time, since posing and related
776// techniques can modify the name -> class mapping.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000777llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000778 const ObjCInterfaceDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000779 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString());
David Chisnall41d63ed2010-01-08 00:14:31 +0000780 // With the incompatible ABI, this will need to be replaced with a direct
781 // reference to the class symbol. For the compatible nonfragile ABI we are
782 // still performing this lookup at run time but emitting the symbol for the
783 // class externally so that we can make the switch later.
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000784 EmitClassRef(OID->getNameAsString());
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000785 ClassName = Builder.CreateStructGEP(ClassName, 0);
786
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000787 llvm::Constant *ClassLookupFn =
Benjamin Kramer95d318c2011-05-28 14:26:31 +0000788 CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, PtrToInt8Ty, true),
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000789 "objc_lookup_class");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000790 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner391d77a2008-03-30 23:03:07 +0000791}
792
Fariborz Jahanian03b29602010-06-17 19:56:20 +0000793llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel,
David Chisnall9f6614e2011-03-23 16:36:54 +0000794 const std::string &TypeEncoding, bool lval) {
795
796 llvm::SmallVector<TypedSelector, 2> &Types = SelectorTable[Sel];
797 llvm::GlobalAlias *SelValue = 0;
798
799
800 for (llvm::SmallVectorImpl<TypedSelector>::iterator i = Types.begin(),
801 e = Types.end() ; i!=e ; i++) {
802 if (i->first == TypeEncoding) {
803 SelValue = i->second;
804 break;
805 }
806 }
807 if (0 == SelValue) {
David Chisnallc7ef4622011-03-23 22:52:06 +0000808 SelValue = new llvm::GlobalAlias(SelectorTy,
David Chisnall9f6614e2011-03-23 16:36:54 +0000809 llvm::GlobalValue::PrivateLinkage,
810 ".objc_selector_"+Sel.getAsString(), NULL,
811 &TheModule);
812 Types.push_back(TypedSelector(TypeEncoding, SelValue));
813 }
814
David Chisnallc7ef4622011-03-23 22:52:06 +0000815 if (lval) {
816 llvm::Value *tmp = Builder.CreateAlloca(SelValue->getType());
817 Builder.CreateStore(SelValue, tmp);
818 return tmp;
819 }
820 return SelValue;
David Chisnall9f6614e2011-03-23 16:36:54 +0000821}
822
823llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel,
824 bool lval) {
825 return GetSelector(Builder, Sel, std::string(), lval);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000826}
827
Daniel Dunbar6d5a1c22010-02-03 20:11:42 +0000828llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000829 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000830 std::string SelTypes;
831 CGM.getContext().getObjCEncodingForMethodDecl(Method, SelTypes);
David Chisnall9f6614e2011-03-23 16:36:54 +0000832 return GetSelector(Builder, Method->getSelector(), SelTypes, false);
Chris Lattner8e67b632008-06-26 04:37:12 +0000833}
834
John McCall5a180392010-07-24 00:37:23 +0000835llvm::Constant *CGObjCGNU::GetEHType(QualType T) {
David Chisnall9735ca62011-03-25 11:57:33 +0000836 if (!CGM.getLangOptions().CPlusPlus) {
837 if (T->isObjCIdType()
838 || T->isObjCQualifiedIdType()) {
839 // With the old ABI, there was only one kind of catchall, which broke
840 // foreign exceptions. With the new ABI, we use __objc_id_typeinfo as
841 // a pointer indicating object catchalls, and NULL to indicate real
842 // catchalls
843 if (CGM.getLangOptions().ObjCNonFragileABI) {
844 return MakeConstantString("@id");
845 } else {
846 return 0;
847 }
848 }
849
850 // All other types should be Objective-C interface pointer types.
851 const ObjCObjectPointerType *OPT =
852 T->getAs<ObjCObjectPointerType>();
853 assert(OPT && "Invalid @catch type.");
854 const ObjCInterfaceDecl *IDecl =
855 OPT->getObjectType()->getInterface();
856 assert(IDecl && "Invalid @catch type.");
857 return MakeConstantString(IDecl->getIdentifier()->getName());
858 }
David Chisnall80558d22011-03-20 21:35:39 +0000859 // For Objective-C++, we want to provide the ability to catch both C++ and
860 // Objective-C objects in the same function.
861
862 // There's a particular fixed type info for 'id'.
863 if (T->isObjCIdType() ||
864 T->isObjCQualifiedIdType()) {
865 llvm::Constant *IDEHType =
866 CGM.getModule().getGlobalVariable("__objc_id_type_info");
867 if (!IDEHType)
868 IDEHType =
869 new llvm::GlobalVariable(CGM.getModule(), PtrToInt8Ty,
870 false,
871 llvm::GlobalValue::ExternalLinkage,
872 0, "__objc_id_type_info");
873 return llvm::ConstantExpr::getBitCast(IDEHType, PtrToInt8Ty);
874 }
875
876 const ObjCObjectPointerType *PT =
877 T->getAs<ObjCObjectPointerType>();
878 assert(PT && "Invalid @catch type.");
879 const ObjCInterfaceType *IT = PT->getInterfaceType();
880 assert(IT && "Invalid @catch type.");
881 std::string className = IT->getDecl()->getIdentifier()->getName();
882
883 std::string typeinfoName = "__objc_eh_typeinfo_" + className;
884
885 // Return the existing typeinfo if it exists
886 llvm::Constant *typeinfo = TheModule.getGlobalVariable(typeinfoName);
887 if (typeinfo) return typeinfo;
888
889 // Otherwise create it.
890
891 // vtable for gnustep::libobjc::__objc_class_type_info
892 // It's quite ugly hard-coding this. Ideally we'd generate it using the host
893 // platform's name mangling.
894 const char *vtableName = "_ZTVN7gnustep7libobjc22__objc_class_type_infoE";
895 llvm::Constant *Vtable = TheModule.getGlobalVariable(vtableName);
896 if (!Vtable) {
897 Vtable = new llvm::GlobalVariable(TheModule, PtrToInt8Ty, true,
898 llvm::GlobalValue::ExternalLinkage, 0, vtableName);
899 }
900 llvm::Constant *Two = llvm::ConstantInt::get(IntTy, 2);
901 Vtable = llvm::ConstantExpr::getGetElementPtr(Vtable, &Two, 1);
902 Vtable = llvm::ConstantExpr::getBitCast(Vtable, PtrToInt8Ty);
903
904 llvm::Constant *typeName =
905 ExportUniqueString(className, "__objc_eh_typename_");
906
907 std::vector<llvm::Constant*> fields;
908 fields.push_back(Vtable);
909 fields.push_back(typeName);
910 llvm::Constant *TI =
Chris Lattner7650d952011-06-18 22:49:11 +0000911 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty,
David Chisnall80558d22011-03-20 21:35:39 +0000912 NULL), fields, "__objc_eh_typeinfo_" + className,
913 llvm::GlobalValue::LinkOnceODRLinkage);
914 return llvm::ConstantExpr::getBitCast(TI, PtrToInt8Ty);
John McCall5a180392010-07-24 00:37:23 +0000915}
916
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000917/// Generate an NSConstantString object.
David Chisnall0d13f6f2010-01-23 02:40:42 +0000918llvm::Constant *CGObjCGNU::GenerateConstantString(const StringLiteral *SL) {
David Chisnall48272a02010-01-27 12:49:23 +0000919
Benjamin Kramer2f4eaef2010-08-17 12:54:38 +0000920 std::string Str = SL->getString().str();
David Chisnall0d13f6f2010-01-23 02:40:42 +0000921
David Chisnall48272a02010-01-27 12:49:23 +0000922 // Look for an existing one
923 llvm::StringMap<llvm::Constant*>::iterator old = ObjCStrings.find(Str);
924 if (old != ObjCStrings.end())
925 return old->getValue();
926
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000927 std::vector<llvm::Constant*> Ivars;
928 Ivars.push_back(NULLPtr);
Chris Lattner13fd7e52008-06-21 21:44:18 +0000929 Ivars.push_back(MakeConstantString(Str));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000930 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000931 llvm::Constant *ObjCStr = MakeGlobal(
Chris Lattner7650d952011-06-18 22:49:11 +0000932 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000933 Ivars, ".objc_str");
David Chisnall48272a02010-01-27 12:49:23 +0000934 ObjCStr = llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty);
935 ObjCStrings[Str] = ObjCStr;
936 ConstantStrings.push_back(ObjCStr);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000937 return ObjCStr;
938}
939
940///Generates a message send where the super is the receiver. This is a message
941///send to self with special delivery semantics indicating which class's method
942///should be called.
David Chisnall9f6614e2011-03-23 16:36:54 +0000943RValue
944CGObjCGNU::GenerateMessageSendSuper(CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +0000945 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000946 QualType ResultType,
947 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000948 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000949 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000950 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000951 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000952 const CallArgList &CallArgs,
953 const ObjCMethodDecl *Method) {
David Chisnall0bbe0cf2011-05-28 14:09:01 +0000954 CGBuilderTy &Builder = CGF.Builder;
David Chisnalle6a11a62011-05-23 23:13:25 +0000955 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) {
David Chisnallef6e0f32010-02-03 15:59:02 +0000956 if (Sel == RetainSel || Sel == AutoreleaseSel) {
David Chisnall0bbe0cf2011-05-28 14:09:01 +0000957 return RValue::get(EnforceType(Builder, Receiver,
958 CGM.getTypes().ConvertType(ResultType)));
David Chisnallef6e0f32010-02-03 15:59:02 +0000959 }
960 if (Sel == ReleaseSel) {
961 return RValue::get(0);
962 }
963 }
David Chisnalldb831942010-05-01 12:37:16 +0000964
David Chisnalldb831942010-05-01 12:37:16 +0000965 llvm::Value *cmd = GetSelector(Builder, Sel);
966
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000967
968 CallArgList ActualArgs;
969
Eli Friedman04c9a492011-05-02 17:57:46 +0000970 ActualArgs.add(RValue::get(EnforceType(Builder, Receiver, IdTy)), ASTIdTy);
971 ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType());
John McCallf85e1932011-06-15 23:02:42 +0000972 ActualArgs.addFrom(CallArgs);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000973
974 CodeGenTypes &Types = CGM.getTypes();
John McCall04a67a62010-02-05 21:31:56 +0000975 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +0000976 FunctionType::ExtInfo());
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000977
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000978 llvm::Value *ReceiverClass = 0;
Chris Lattner48e6e7e2009-05-08 15:39:58 +0000979 if (isCategoryImpl) {
980 llvm::Constant *classLookupFunction = 0;
Chris Lattner48e6e7e2009-05-08 15:39:58 +0000981 if (IsClassMessage) {
Owen Anderson96e0fc72009-07-29 22:16:19 +0000982 classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Benjamin Kramer95d318c2011-05-28 14:26:31 +0000983 IdTy, PtrTy, true), "objc_get_meta_class");
Chris Lattner48e6e7e2009-05-08 15:39:58 +0000984 } else {
Owen Anderson96e0fc72009-07-29 22:16:19 +0000985 classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Benjamin Kramer95d318c2011-05-28 14:26:31 +0000986 IdTy, PtrTy, true), "objc_get_class");
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000987 }
David Chisnalldb831942010-05-01 12:37:16 +0000988 ReceiverClass = Builder.CreateCall(classLookupFunction,
Chris Lattner48e6e7e2009-05-08 15:39:58 +0000989 MakeConstantString(Class->getNameAsString()));
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000990 } else {
Chris Lattner48e6e7e2009-05-08 15:39:58 +0000991 // Set up global aliases for the metaclass or class pointer if they do not
992 // already exist. These will are forward-references which will be set to
Mike Stumpbb1c8602009-07-31 21:31:32 +0000993 // pointers to the class and metaclass structure created for the runtime
994 // load function. To send a message to super, we look up the value of the
Chris Lattner48e6e7e2009-05-08 15:39:58 +0000995 // super_class pointer from either the class or metaclass structure.
996 if (IsClassMessage) {
997 if (!MetaClassPtrAlias) {
998 MetaClassPtrAlias = new llvm::GlobalAlias(IdTy,
999 llvm::GlobalValue::InternalLinkage, ".objc_metaclass_ref" +
1000 Class->getNameAsString(), NULL, &TheModule);
1001 }
1002 ReceiverClass = MetaClassPtrAlias;
1003 } else {
1004 if (!ClassPtrAlias) {
1005 ClassPtrAlias = new llvm::GlobalAlias(IdTy,
1006 llvm::GlobalValue::InternalLinkage, ".objc_class_ref" +
1007 Class->getNameAsString(), NULL, &TheModule);
1008 }
1009 ReceiverClass = ClassPtrAlias;
Daniel Dunbar5efccb12009-05-04 15:31:17 +00001010 }
Chris Lattner71238f62009-04-25 23:19:45 +00001011 }
Daniel Dunbar5efccb12009-05-04 15:31:17 +00001012 // Cast the pointer to a simplified version of the class structure
David Chisnalldb831942010-05-01 12:37:16 +00001013 ReceiverClass = Builder.CreateBitCast(ReceiverClass,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001014 llvm::PointerType::getUnqual(
Chris Lattner7650d952011-06-18 22:49:11 +00001015 llvm::StructType::get(IdTy, IdTy, NULL)));
Daniel Dunbar5efccb12009-05-04 15:31:17 +00001016 // Get the superclass pointer
David Chisnalldb831942010-05-01 12:37:16 +00001017 ReceiverClass = Builder.CreateStructGEP(ReceiverClass, 1);
Daniel Dunbar5efccb12009-05-04 15:31:17 +00001018 // Load the superclass pointer
David Chisnalldb831942010-05-01 12:37:16 +00001019 ReceiverClass = Builder.CreateLoad(ReceiverClass);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001020 // Construct the structure used to look up the IMP
Chris Lattner7650d952011-06-18 22:49:11 +00001021 llvm::StructType *ObjCSuperTy = llvm::StructType::get(
Owen Anderson47a434f2009-08-05 23:18:46 +00001022 Receiver->getType(), IdTy, NULL);
David Chisnalldb831942010-05-01 12:37:16 +00001023 llvm::Value *ObjCSuper = Builder.CreateAlloca(ObjCSuperTy);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +00001024
David Chisnalldb831942010-05-01 12:37:16 +00001025 Builder.CreateStore(Receiver, Builder.CreateStructGEP(ObjCSuper, 0));
1026 Builder.CreateStore(ReceiverClass, Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001027
David Chisnallc7ef4622011-03-23 22:52:06 +00001028 ObjCSuper = EnforceType(Builder, ObjCSuper, PtrToObjCSuperTy);
1029 const llvm::FunctionType *impType =
1030 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
1031
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001032 // Get the IMP
David Chisnallc7ef4622011-03-23 22:52:06 +00001033 llvm::Value *imp = LookupIMPSuper(CGF, ObjCSuper, cmd);
1034 imp = EnforceType(Builder, imp, llvm::PointerType::getUnqual(impType));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001035
David Chisnalldd5c98f2010-05-01 11:15:56 +00001036 llvm::Value *impMD[] = {
1037 llvm::MDString::get(VMContext, Sel.getAsString()),
1038 llvm::MDString::get(VMContext, Class->getSuperClass()->getNameAsString()),
1039 llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsClassMessage)
1040 };
Jay Foad6f141652011-04-21 19:59:12 +00001041 llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD);
David Chisnalldd5c98f2010-05-01 11:15:56 +00001042
David Chisnall4b02afc2010-05-02 13:41:58 +00001043 llvm::Instruction *call;
John McCallef072fd2010-05-22 01:48:05 +00001044 RValue msgRet = CGF.EmitCall(FnInfo, imp, Return, ActualArgs,
David Chisnall4b02afc2010-05-02 13:41:58 +00001045 0, &call);
1046 call->setMetadata(msgSendMDKind, node);
1047 return msgRet;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001048}
1049
Mike Stump1eb44332009-09-09 15:08:12 +00001050/// Generate code for a message send expression.
David Chisnall9f6614e2011-03-23 16:36:54 +00001051RValue
1052CGObjCGNU::GenerateMessageSend(CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001053 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001054 QualType ResultType,
1055 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001056 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001057 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001058 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001059 const ObjCMethodDecl *Method) {
David Chisnall0bbe0cf2011-05-28 14:09:01 +00001060 CGBuilderTy &Builder = CGF.Builder;
1061
David Chisnall664b7c72010-04-27 15:08:48 +00001062 // Strip out message sends to retain / release in GC mode
David Chisnalle6a11a62011-05-23 23:13:25 +00001063 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) {
David Chisnallef6e0f32010-02-03 15:59:02 +00001064 if (Sel == RetainSel || Sel == AutoreleaseSel) {
David Chisnall0bbe0cf2011-05-28 14:09:01 +00001065 return RValue::get(EnforceType(Builder, Receiver,
1066 CGM.getTypes().ConvertType(ResultType)));
David Chisnallef6e0f32010-02-03 15:59:02 +00001067 }
1068 if (Sel == ReleaseSel) {
1069 return RValue::get(0);
1070 }
1071 }
David Chisnall664b7c72010-04-27 15:08:48 +00001072
David Chisnall664b7c72010-04-27 15:08:48 +00001073 // If the return type is something that goes in an integer register, the
1074 // runtime will handle 0 returns. For other cases, we fill in the 0 value
1075 // ourselves.
1076 //
1077 // The language spec says the result of this kind of message send is
1078 // undefined, but lots of people seem to have forgotten to read that
1079 // paragraph and insist on sending messages to nil that have structure
1080 // returns. With GCC, this generates a random return value (whatever happens
1081 // to be on the stack / in those registers at the time) on most platforms,
David Chisnallc7ef4622011-03-23 22:52:06 +00001082 // and generates an illegal instruction trap on SPARC. With LLVM it corrupts
1083 // the stack.
1084 bool isPointerSizedReturn = (ResultType->isAnyPointerType() ||
1085 ResultType->isIntegralOrEnumerationType() || ResultType->isVoidType());
David Chisnall664b7c72010-04-27 15:08:48 +00001086
1087 llvm::BasicBlock *startBB = 0;
1088 llvm::BasicBlock *messageBB = 0;
David Chisnalla54da052010-05-20 13:45:48 +00001089 llvm::BasicBlock *continueBB = 0;
David Chisnall664b7c72010-04-27 15:08:48 +00001090
1091 if (!isPointerSizedReturn) {
1092 startBB = Builder.GetInsertBlock();
1093 messageBB = CGF.createBasicBlock("msgSend");
David Chisnalla54da052010-05-20 13:45:48 +00001094 continueBB = CGF.createBasicBlock("continue");
David Chisnall664b7c72010-04-27 15:08:48 +00001095
1096 llvm::Value *isNil = Builder.CreateICmpEQ(Receiver,
1097 llvm::Constant::getNullValue(Receiver->getType()));
David Chisnalla54da052010-05-20 13:45:48 +00001098 Builder.CreateCondBr(isNil, continueBB, messageBB);
David Chisnall664b7c72010-04-27 15:08:48 +00001099 CGF.EmitBlock(messageBB);
1100 }
1101
David Chisnall0f436562009-08-17 16:35:33 +00001102 IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001103 llvm::Value *cmd;
1104 if (Method)
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001105 cmd = GetSelector(Builder, Method);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001106 else
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001107 cmd = GetSelector(Builder, Sel);
David Chisnallc7ef4622011-03-23 22:52:06 +00001108 cmd = EnforceType(Builder, cmd, SelectorTy);
1109 Receiver = EnforceType(Builder, Receiver, IdTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001110
David Chisnallc7ef4622011-03-23 22:52:06 +00001111 llvm::Value *impMD[] = {
1112 llvm::MDString::get(VMContext, Sel.getAsString()),
1113 llvm::MDString::get(VMContext, Class ? Class->getNameAsString() :""),
1114 llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), Class!=0)
1115 };
Jay Foad6f141652011-04-21 19:59:12 +00001116 llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD);
David Chisnallc7ef4622011-03-23 22:52:06 +00001117
1118 // Get the IMP to call
1119 llvm::Value *imp = LookupIMP(CGF, Receiver, cmd, node);
1120
1121 CallArgList ActualArgs;
Eli Friedman04c9a492011-05-02 17:57:46 +00001122 ActualArgs.add(RValue::get(Receiver), ASTIdTy);
1123 ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType());
John McCallf85e1932011-06-15 23:02:42 +00001124 ActualArgs.addFrom(CallArgs);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +00001125
1126 CodeGenTypes &Types = CGM.getTypes();
John McCall04a67a62010-02-05 21:31:56 +00001127 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00001128 FunctionType::ExtInfo());
Daniel Dunbar67939662009-09-17 04:01:40 +00001129 const llvm::FunctionType *impType =
1130 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
David Chisnallc7ef4622011-03-23 22:52:06 +00001131 imp = EnforceType(Builder, imp, llvm::PointerType::getUnqual(impType));
David Chisnall63e742b2010-05-01 12:56:56 +00001132
1133
Fariborz Jahanian34e65772009-05-22 20:17:16 +00001134 // For sender-aware dispatch, we pass the sender as the third argument to a
1135 // lookup function. When sending messages from C code, the sender is nil.
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001136 // objc_msg_lookup_sender(id *receiver, SEL selector, id sender);
David Chisnall4b02afc2010-05-02 13:41:58 +00001137 llvm::Instruction *call;
John McCallef072fd2010-05-22 01:48:05 +00001138 RValue msgRet = CGF.EmitCall(FnInfo, imp, Return, ActualArgs,
David Chisnall4b02afc2010-05-02 13:41:58 +00001139 0, &call);
1140 call->setMetadata(msgSendMDKind, node);
David Chisnall664b7c72010-04-27 15:08:48 +00001141
David Chisnalla54da052010-05-20 13:45:48 +00001142
David Chisnall664b7c72010-04-27 15:08:48 +00001143 if (!isPointerSizedReturn) {
David Chisnalla54da052010-05-20 13:45:48 +00001144 messageBB = CGF.Builder.GetInsertBlock();
1145 CGF.Builder.CreateBr(continueBB);
1146 CGF.EmitBlock(continueBB);
David Chisnall664b7c72010-04-27 15:08:48 +00001147 if (msgRet.isScalar()) {
1148 llvm::Value *v = msgRet.getScalarVal();
Jay Foadbbf3bac2011-03-30 11:28:58 +00001149 llvm::PHINode *phi = Builder.CreatePHI(v->getType(), 2);
David Chisnall664b7c72010-04-27 15:08:48 +00001150 phi->addIncoming(v, messageBB);
1151 phi->addIncoming(llvm::Constant::getNullValue(v->getType()), startBB);
1152 msgRet = RValue::get(phi);
1153 } else if (msgRet.isAggregate()) {
1154 llvm::Value *v = msgRet.getAggregateAddr();
Jay Foadbbf3bac2011-03-30 11:28:58 +00001155 llvm::PHINode *phi = Builder.CreatePHI(v->getType(), 2);
David Chisnall664b7c72010-04-27 15:08:48 +00001156 const llvm::PointerType *RetTy = cast<llvm::PointerType>(v->getType());
David Chisnall866163b2010-04-30 13:36:12 +00001157 llvm::AllocaInst *NullVal =
1158 CGF.CreateTempAlloca(RetTy->getElementType(), "null");
David Chisnall664b7c72010-04-27 15:08:48 +00001159 CGF.InitTempAlloca(NullVal,
1160 llvm::Constant::getNullValue(RetTy->getElementType()));
1161 phi->addIncoming(v, messageBB);
1162 phi->addIncoming(NullVal, startBB);
1163 msgRet = RValue::getAggregate(phi);
1164 } else /* isComplex() */ {
1165 std::pair<llvm::Value*,llvm::Value*> v = msgRet.getComplexVal();
Jay Foadbbf3bac2011-03-30 11:28:58 +00001166 llvm::PHINode *phi = Builder.CreatePHI(v.first->getType(), 2);
David Chisnall664b7c72010-04-27 15:08:48 +00001167 phi->addIncoming(v.first, messageBB);
1168 phi->addIncoming(llvm::Constant::getNullValue(v.first->getType()),
1169 startBB);
Jay Foadbbf3bac2011-03-30 11:28:58 +00001170 llvm::PHINode *phi2 = Builder.CreatePHI(v.second->getType(), 2);
David Chisnall664b7c72010-04-27 15:08:48 +00001171 phi2->addIncoming(v.second, messageBB);
1172 phi2->addIncoming(llvm::Constant::getNullValue(v.second->getType()),
1173 startBB);
1174 msgRet = RValue::getComplex(phi, phi2);
1175 }
1176 }
1177 return msgRet;
Chris Lattner0f984262008-03-01 08:50:34 +00001178}
1179
Mike Stump1eb44332009-09-09 15:08:12 +00001180/// Generates a MethodList. Used in construction of a objc_class and
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001181/// objc_category structures.
David Chisnall9f6614e2011-03-23 16:36:54 +00001182llvm::Constant *CGObjCGNU::GenerateMethodList(const llvm::StringRef &ClassName,
1183 const llvm::StringRef &CategoryName,
Mike Stump1eb44332009-09-09 15:08:12 +00001184 const llvm::SmallVectorImpl<Selector> &MethodSels,
1185 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001186 bool isClassMethodList) {
David Chisnall0f436562009-08-17 16:35:33 +00001187 if (MethodSels.empty())
1188 return NULLPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001189 // Get the method structure type.
Chris Lattner7650d952011-06-18 22:49:11 +00001190 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001191 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
1192 PtrToInt8Ty, // Method types
David Chisnallc7ef4622011-03-23 22:52:06 +00001193 IMPTy, //Method pointer
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001194 NULL);
1195 std::vector<llvm::Constant*> Methods;
1196 std::vector<llvm::Constant*> Elements;
1197 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
1198 Elements.clear();
David Chisnall9f6614e2011-03-23 16:36:54 +00001199 llvm::Constant *Method =
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001200 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
David Chisnall9f6614e2011-03-23 16:36:54 +00001201 MethodSels[i],
1202 isClassMethodList));
1203 assert(Method && "Can't generate metadata for method that doesn't exist");
1204 llvm::Constant *C = MakeConstantString(MethodSels[i].getAsString());
1205 Elements.push_back(C);
1206 Elements.push_back(MethodTypes[i]);
1207 Method = llvm::ConstantExpr::getBitCast(Method,
David Chisnallc7ef4622011-03-23 22:52:06 +00001208 IMPTy);
David Chisnall9f6614e2011-03-23 16:36:54 +00001209 Elements.push_back(Method);
1210 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001211 }
1212
1213 // Array of method structures
Owen Anderson96e0fc72009-07-29 22:16:19 +00001214 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Fariborz Jahanian1e64a952009-05-17 16:49:27 +00001215 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00001216 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerfba67632008-06-26 04:52:29 +00001217 Methods);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001218
1219 // Structure containing list pointer, array and array count
1220 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
Owen Anderson8c8f69e2009-08-13 23:27:53 +00001221 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get(VMContext);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001222 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
Chris Lattner7650d952011-06-18 22:49:11 +00001223 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(
Mike Stump1eb44332009-09-09 15:08:12 +00001224 NextPtrTy,
1225 IntTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001226 ObjCMethodArrayTy,
1227 NULL);
1228 // Refine next pointer type to concrete type
1229 llvm::cast<llvm::OpaqueType>(
1230 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
1231 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
1232
1233 Methods.clear();
Owen Anderson03e20502009-07-30 23:11:26 +00001234 Methods.push_back(llvm::ConstantPointerNull::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00001235 llvm::PointerType::getUnqual(ObjCMethodListTy)));
Owen Anderson0032b272009-08-13 21:57:51 +00001236 Methods.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001237 MethodTypes.size()));
1238 Methods.push_back(MethodArray);
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001240 // Create an instance of the structure
1241 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
1242}
1243
1244/// Generates an IvarList. Used in construction of a objc_class.
1245llvm::Constant *CGObjCGNU::GenerateIvarList(
1246 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
1247 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
1248 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
David Chisnall18044632009-11-16 19:05:54 +00001249 if (IvarNames.size() == 0)
1250 return NULLPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001251 // Get the method structure type.
Chris Lattner7650d952011-06-18 22:49:11 +00001252 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001253 PtrToInt8Ty,
1254 PtrToInt8Ty,
1255 IntTy,
1256 NULL);
1257 std::vector<llvm::Constant*> Ivars;
1258 std::vector<llvm::Constant*> Elements;
1259 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
1260 Elements.clear();
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001261 Elements.push_back(IvarNames[i]);
1262 Elements.push_back(IvarTypes[i]);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001263 Elements.push_back(IvarOffsets[i]);
Owen Anderson08e25242009-07-27 22:29:56 +00001264 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001265 }
1266
1267 // Array of method structures
Owen Anderson96e0fc72009-07-29 22:16:19 +00001268 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001269 IvarNames.size());
1270
Mike Stump1eb44332009-09-09 15:08:12 +00001271
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001272 Elements.clear();
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001273 Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
Owen Anderson7db6d832009-07-28 18:33:04 +00001274 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001275 // Structure containing array and array count
Chris Lattner7650d952011-06-18 22:49:11 +00001276 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001277 ObjCIvarArrayTy,
1278 NULL);
1279
1280 // Create an instance of the structure
1281 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
1282}
1283
1284/// Generate a class structure
1285llvm::Constant *CGObjCGNU::GenerateClassStructure(
1286 llvm::Constant *MetaClass,
1287 llvm::Constant *SuperClass,
1288 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +00001289 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001290 llvm::Constant *Version,
1291 llvm::Constant *InstanceSize,
1292 llvm::Constant *IVars,
1293 llvm::Constant *Methods,
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001294 llvm::Constant *Protocols,
1295 llvm::Constant *IvarOffsets,
David Chisnall8c757f92010-04-28 14:29:56 +00001296 llvm::Constant *Properties,
1297 bool isMeta) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001298 // Set up the class structure
1299 // Note: Several of these are char*s when they should be ids. This is
1300 // because the runtime performs this translation on load.
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001301 //
1302 // Fields marked New ABI are part of the GNUstep runtime. We emit them
1303 // anyway; the classes will still work with the GNU runtime, they will just
1304 // be ignored.
Chris Lattner7650d952011-06-18 22:49:11 +00001305 llvm::StructType *ClassTy = llvm::StructType::get(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001306 PtrToInt8Ty, // class_pointer
1307 PtrToInt8Ty, // super_class
1308 PtrToInt8Ty, // name
1309 LongTy, // version
1310 LongTy, // info
1311 LongTy, // instance_size
1312 IVars->getType(), // ivars
1313 Methods->getType(), // methods
Mike Stump1eb44332009-09-09 15:08:12 +00001314 // These are all filled in by the runtime, so we pretend
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001315 PtrTy, // dtable
1316 PtrTy, // subclass_list
1317 PtrTy, // sibling_class
1318 PtrTy, // protocols
1319 PtrTy, // gc_object_type
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001320 // New ABI:
1321 LongTy, // abi_version
1322 IvarOffsets->getType(), // ivar_offsets
1323 Properties->getType(), // properties
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001324 NULL);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001325 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001326 // Fill in the structure
1327 std::vector<llvm::Constant*> Elements;
Owen Anderson3c4972d2009-07-29 18:54:39 +00001328 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001329 Elements.push_back(SuperClass);
Chris Lattnerd002cc62008-06-26 04:47:04 +00001330 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001331 Elements.push_back(Zero);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001332 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
David Chisnall05f3a502011-02-21 23:47:40 +00001333 if (isMeta) {
1334 llvm::TargetData td(&TheModule);
Ken Dycke0afc892011-04-22 17:59:22 +00001335 Elements.push_back(
1336 llvm::ConstantInt::get(LongTy,
1337 td.getTypeSizeInBits(ClassTy) /
1338 CGM.getContext().getCharWidth()));
David Chisnall05f3a502011-02-21 23:47:40 +00001339 } else
1340 Elements.push_back(InstanceSize);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001341 Elements.push_back(IVars);
1342 Elements.push_back(Methods);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001343 Elements.push_back(NULLPtr);
1344 Elements.push_back(NULLPtr);
1345 Elements.push_back(NULLPtr);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001346 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001347 Elements.push_back(NULLPtr);
1348 Elements.push_back(Zero);
1349 Elements.push_back(IvarOffsets);
1350 Elements.push_back(Properties);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001351 // Create an instance of the structure
David Chisnall41d63ed2010-01-08 00:14:31 +00001352 // This is now an externally visible symbol, so that we can speed up class
1353 // messages in the next ABI.
David Chisnall8c757f92010-04-28 14:29:56 +00001354 return MakeGlobal(ClassTy, Elements, (isMeta ? "_OBJC_METACLASS_":
1355 "_OBJC_CLASS_") + std::string(Name), llvm::GlobalValue::ExternalLinkage);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001356}
1357
1358llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
1359 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
1360 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
Mike Stump1eb44332009-09-09 15:08:12 +00001361 // Get the method structure type.
Chris Lattner7650d952011-06-18 22:49:11 +00001362 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001363 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
1364 PtrToInt8Ty,
1365 NULL);
1366 std::vector<llvm::Constant*> Methods;
1367 std::vector<llvm::Constant*> Elements;
1368 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
1369 Elements.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00001370 Elements.push_back(MethodNames[i]);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001371 Elements.push_back(MethodTypes[i]);
Owen Anderson08e25242009-07-27 22:29:56 +00001372 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001373 }
Owen Anderson96e0fc72009-07-29 22:16:19 +00001374 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001375 MethodNames.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00001376 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy,
Mike Stumpbb1c8602009-07-31 21:31:32 +00001377 Methods);
Chris Lattner7650d952011-06-18 22:49:11 +00001378 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001379 IntTy, ObjCMethodArrayTy, NULL);
1380 Methods.clear();
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001381 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001382 Methods.push_back(Array);
1383 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
1384}
Mike Stumpbb1c8602009-07-31 21:31:32 +00001385
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001386// Create the protocol list structure used in classes, categories and so on
1387llvm::Constant *CGObjCGNU::GenerateProtocolList(
1388 const llvm::SmallVectorImpl<std::string> &Protocols) {
Owen Anderson96e0fc72009-07-29 22:16:19 +00001389 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001390 Protocols.size());
Chris Lattner7650d952011-06-18 22:49:11 +00001391 llvm::StructType *ProtocolListTy = llvm::StructType::get(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001392 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
David Chisnall8fac25d2010-12-26 22:13:16 +00001393 SizeTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001394 ProtocolArrayTy,
1395 NULL);
Mike Stump1eb44332009-09-09 15:08:12 +00001396 std::vector<llvm::Constant*> Elements;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001397 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
1398 iter != endIter ; iter++) {
David Chisnallff80fab2009-11-20 14:50:59 +00001399 llvm::Constant *protocol = 0;
1400 llvm::StringMap<llvm::Constant*>::iterator value =
1401 ExistingProtocols.find(*iter);
1402 if (value == ExistingProtocols.end()) {
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +00001403 protocol = GenerateEmptyProtocol(*iter);
David Chisnallff80fab2009-11-20 14:50:59 +00001404 } else {
1405 protocol = value->getValue();
1406 }
Owen Anderson3c4972d2009-07-29 18:54:39 +00001407 llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(protocol,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001408 PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001409 Elements.push_back(Ptr);
1410 }
Owen Anderson7db6d832009-07-28 18:33:04 +00001411 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001412 Elements);
1413 Elements.clear();
1414 Elements.push_back(NULLPtr);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001415 Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001416 Elements.push_back(ProtocolArray);
1417 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
1418}
1419
Mike Stump1eb44332009-09-09 15:08:12 +00001420llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001421 const ObjCProtocolDecl *PD) {
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +00001422 llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()];
Mike Stump1eb44332009-09-09 15:08:12 +00001423 const llvm::Type *T =
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +00001424 CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00001425 return Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +00001426}
1427
1428llvm::Constant *CGObjCGNU::GenerateEmptyProtocol(
1429 const std::string &ProtocolName) {
1430 llvm::SmallVector<std::string, 0> EmptyStringVector;
1431 llvm::SmallVector<llvm::Constant*, 0> EmptyConstantVector;
1432
1433 llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001434 llvm::Constant *MethodList =
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +00001435 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
1436 // Protocols are objects containing lists of the methods implemented and
1437 // protocols adopted.
Chris Lattner7650d952011-06-18 22:49:11 +00001438 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +00001439 PtrToInt8Ty,
1440 ProtocolList->getType(),
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001441 MethodList->getType(),
1442 MethodList->getType(),
1443 MethodList->getType(),
1444 MethodList->getType(),
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +00001445 NULL);
Mike Stump1eb44332009-09-09 15:08:12 +00001446 std::vector<llvm::Constant*> Elements;
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +00001447 // The isa pointer must be set to a magic number so the runtime knows it's
1448 // the correct layout.
Owen Anderson3c4972d2009-07-29 18:54:39 +00001449 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
David Chisnall9f6614e2011-03-23 16:36:54 +00001450 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
1451 ProtocolVersion), IdTy));
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +00001452 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
1453 Elements.push_back(ProtocolList);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001454 Elements.push_back(MethodList);
1455 Elements.push_back(MethodList);
1456 Elements.push_back(MethodList);
1457 Elements.push_back(MethodList);
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +00001458 return MakeGlobal(ProtocolTy, Elements, ".objc_protocol");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001459}
1460
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001461void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
1462 ASTContext &Context = CGM.getContext();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001463 std::string ProtocolName = PD->getNameAsString();
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001464 llvm::SmallVector<std::string, 16> Protocols;
1465 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
1466 E = PD->protocol_end(); PI != E; ++PI)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001467 Protocols.push_back((*PI)->getNameAsString());
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001468 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
1469 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001470 llvm::SmallVector<llvm::Constant*, 16> OptionalInstanceMethodNames;
1471 llvm::SmallVector<llvm::Constant*, 16> OptionalInstanceMethodTypes;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001472 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
1473 E = PD->instmeth_end(); iter != E; iter++) {
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001474 std::string TypeStr;
1475 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001476 if ((*iter)->getImplementationControl() == ObjCMethodDecl::Optional) {
1477 InstanceMethodNames.push_back(
1478 MakeConstantString((*iter)->getSelector().getAsString()));
1479 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
1480 } else {
1481 OptionalInstanceMethodNames.push_back(
1482 MakeConstantString((*iter)->getSelector().getAsString()));
1483 OptionalInstanceMethodTypes.push_back(MakeConstantString(TypeStr));
1484 }
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001485 }
1486 // Collect information about class methods:
1487 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
1488 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001489 llvm::SmallVector<llvm::Constant*, 16> OptionalClassMethodNames;
1490 llvm::SmallVector<llvm::Constant*, 16> OptionalClassMethodTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00001491 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001492 iter = PD->classmeth_begin(), endIter = PD->classmeth_end();
1493 iter != endIter ; iter++) {
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001494 std::string TypeStr;
1495 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001496 if ((*iter)->getImplementationControl() == ObjCMethodDecl::Optional) {
1497 ClassMethodNames.push_back(
1498 MakeConstantString((*iter)->getSelector().getAsString()));
1499 ClassMethodTypes.push_back(MakeConstantString(TypeStr));
1500 } else {
1501 OptionalClassMethodNames.push_back(
1502 MakeConstantString((*iter)->getSelector().getAsString()));
1503 OptionalClassMethodTypes.push_back(MakeConstantString(TypeStr));
1504 }
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001505 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001506
1507 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
1508 llvm::Constant *InstanceMethodList =
1509 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
1510 llvm::Constant *ClassMethodList =
1511 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001512 llvm::Constant *OptionalInstanceMethodList =
1513 GenerateProtocolMethodList(OptionalInstanceMethodNames,
1514 OptionalInstanceMethodTypes);
1515 llvm::Constant *OptionalClassMethodList =
1516 GenerateProtocolMethodList(OptionalClassMethodNames,
1517 OptionalClassMethodTypes);
1518
1519 // Property metadata: name, attributes, isSynthesized, setter name, setter
1520 // types, getter name, getter types.
1521 // The isSynthesized value is always set to 0 in a protocol. It exists to
1522 // simplify the runtime library by allowing it to use the same data
1523 // structures for protocol metadata everywhere.
Chris Lattner7650d952011-06-18 22:49:11 +00001524 llvm::StructType *PropertyMetadataTy = llvm::StructType::get(
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001525 PtrToInt8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty,
1526 PtrToInt8Ty, NULL);
1527 std::vector<llvm::Constant*> Properties;
1528 std::vector<llvm::Constant*> OptionalProperties;
1529
1530 // Add all of the property methods need adding to the method list and to the
1531 // property metadata list.
1532 for (ObjCContainerDecl::prop_iterator
1533 iter = PD->prop_begin(), endIter = PD->prop_end();
1534 iter != endIter ; iter++) {
1535 std::vector<llvm::Constant*> Fields;
1536 ObjCPropertyDecl *property = (*iter);
1537
1538 Fields.push_back(MakeConstantString(property->getNameAsString()));
1539 Fields.push_back(llvm::ConstantInt::get(Int8Ty,
1540 property->getPropertyAttributes()));
1541 Fields.push_back(llvm::ConstantInt::get(Int8Ty, 0));
1542 if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
1543 std::string TypeStr;
1544 Context.getObjCEncodingForMethodDecl(getter,TypeStr);
1545 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
1546 InstanceMethodTypes.push_back(TypeEncoding);
1547 Fields.push_back(MakeConstantString(getter->getSelector().getAsString()));
1548 Fields.push_back(TypeEncoding);
1549 } else {
1550 Fields.push_back(NULLPtr);
1551 Fields.push_back(NULLPtr);
1552 }
1553 if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
1554 std::string TypeStr;
1555 Context.getObjCEncodingForMethodDecl(setter,TypeStr);
1556 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
1557 InstanceMethodTypes.push_back(TypeEncoding);
1558 Fields.push_back(MakeConstantString(setter->getSelector().getAsString()));
1559 Fields.push_back(TypeEncoding);
1560 } else {
1561 Fields.push_back(NULLPtr);
1562 Fields.push_back(NULLPtr);
1563 }
1564 if (property->getPropertyImplementation() == ObjCPropertyDecl::Optional) {
1565 OptionalProperties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1566 } else {
1567 Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1568 }
1569 }
1570 llvm::Constant *PropertyArray = llvm::ConstantArray::get(
1571 llvm::ArrayType::get(PropertyMetadataTy, Properties.size()), Properties);
1572 llvm::Constant* PropertyListInitFields[] =
1573 {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray};
1574
1575 llvm::Constant *PropertyListInit =
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001576 llvm::ConstantStruct::get(VMContext, PropertyListInitFields, 3, false);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001577 llvm::Constant *PropertyList = new llvm::GlobalVariable(TheModule,
1578 PropertyListInit->getType(), false, llvm::GlobalValue::InternalLinkage,
1579 PropertyListInit, ".objc_property_list");
1580
1581 llvm::Constant *OptionalPropertyArray =
1582 llvm::ConstantArray::get(llvm::ArrayType::get(PropertyMetadataTy,
1583 OptionalProperties.size()) , OptionalProperties);
1584 llvm::Constant* OptionalPropertyListInitFields[] = {
1585 llvm::ConstantInt::get(IntTy, OptionalProperties.size()), NULLPtr,
1586 OptionalPropertyArray };
1587
1588 llvm::Constant *OptionalPropertyListInit =
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001589 llvm::ConstantStruct::get(VMContext, OptionalPropertyListInitFields, 3, false);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001590 llvm::Constant *OptionalPropertyList = new llvm::GlobalVariable(TheModule,
1591 OptionalPropertyListInit->getType(), false,
1592 llvm::GlobalValue::InternalLinkage, OptionalPropertyListInit,
1593 ".objc_property_list");
1594
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001595 // Protocols are objects containing lists of the methods implemented and
1596 // protocols adopted.
Chris Lattner7650d952011-06-18 22:49:11 +00001597 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001598 PtrToInt8Ty,
1599 ProtocolList->getType(),
1600 InstanceMethodList->getType(),
1601 ClassMethodList->getType(),
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001602 OptionalInstanceMethodList->getType(),
1603 OptionalClassMethodList->getType(),
1604 PropertyList->getType(),
1605 OptionalPropertyList->getType(),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001606 NULL);
Mike Stump1eb44332009-09-09 15:08:12 +00001607 std::vector<llvm::Constant*> Elements;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001608 // The isa pointer must be set to a magic number so the runtime knows it's
1609 // the correct layout.
Owen Anderson3c4972d2009-07-29 18:54:39 +00001610 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
David Chisnall9f6614e2011-03-23 16:36:54 +00001611 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
1612 ProtocolVersion), IdTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001613 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
1614 Elements.push_back(ProtocolList);
1615 Elements.push_back(InstanceMethodList);
1616 Elements.push_back(ClassMethodList);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001617 Elements.push_back(OptionalInstanceMethodList);
1618 Elements.push_back(OptionalClassMethodList);
1619 Elements.push_back(PropertyList);
1620 Elements.push_back(OptionalPropertyList);
Mike Stump1eb44332009-09-09 15:08:12 +00001621 ExistingProtocols[ProtocolName] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00001622 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001623 ".objc_protocol"), IdTy);
1624}
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001625void CGObjCGNU::GenerateProtocolHolderCategory(void) {
1626 // Collect information about instance methods
1627 llvm::SmallVector<Selector, 1> MethodSels;
1628 llvm::SmallVector<llvm::Constant*, 1> MethodTypes;
1629
1630 std::vector<llvm::Constant*> Elements;
1631 const std::string ClassName = "__ObjC_Protocol_Holder_Ugly_Hack";
1632 const std::string CategoryName = "AnotherHack";
1633 Elements.push_back(MakeConstantString(CategoryName));
1634 Elements.push_back(MakeConstantString(ClassName));
1635 // Instance method list
1636 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1637 ClassName, CategoryName, MethodSels, MethodTypes, false), PtrTy));
1638 // Class method list
1639 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
1640 ClassName, CategoryName, MethodSels, MethodTypes, true), PtrTy));
1641 // Protocol list
1642 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrTy,
1643 ExistingProtocols.size());
Chris Lattner7650d952011-06-18 22:49:11 +00001644 llvm::StructType *ProtocolListTy = llvm::StructType::get(
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001645 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
David Chisnall8fac25d2010-12-26 22:13:16 +00001646 SizeTy,
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001647 ProtocolArrayTy,
1648 NULL);
1649 std::vector<llvm::Constant*> ProtocolElements;
1650 for (llvm::StringMapIterator<llvm::Constant*> iter =
1651 ExistingProtocols.begin(), endIter = ExistingProtocols.end();
1652 iter != endIter ; iter++) {
1653 llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(iter->getValue(),
1654 PtrTy);
1655 ProtocolElements.push_back(Ptr);
1656 }
1657 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
1658 ProtocolElements);
1659 ProtocolElements.clear();
1660 ProtocolElements.push_back(NULLPtr);
1661 ProtocolElements.push_back(llvm::ConstantInt::get(LongTy,
1662 ExistingProtocols.size()));
1663 ProtocolElements.push_back(ProtocolArray);
1664 Elements.push_back(llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolListTy,
1665 ProtocolElements, ".objc_protocol_list"), PtrTy));
1666 Categories.push_back(llvm::ConstantExpr::getBitCast(
Chris Lattner7650d952011-06-18 22:49:11 +00001667 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty,
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001668 PtrTy, PtrTy, PtrTy, NULL), Elements), PtrTy));
1669}
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001670
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001671void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Chris Lattner8ec03f52008-11-24 03:54:41 +00001672 std::string ClassName = OCD->getClassInterface()->getNameAsString();
1673 std::string CategoryName = OCD->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001674 // Collect information about instance methods
1675 llvm::SmallVector<Selector, 16> InstanceMethodSels;
1676 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001677 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001678 iter = OCD->instmeth_begin(), endIter = OCD->instmeth_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00001679 iter != endIter ; iter++) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001680 InstanceMethodSels.push_back((*iter)->getSelector());
1681 std::string TypeStr;
1682 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001683 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001684 }
1685
1686 // Collect information about class methods
1687 llvm::SmallVector<Selector, 16> ClassMethodSels;
1688 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00001689 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001690 iter = OCD->classmeth_begin(), endIter = OCD->classmeth_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00001691 iter != endIter ; iter++) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001692 ClassMethodSels.push_back((*iter)->getSelector());
1693 std::string TypeStr;
1694 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001695 ClassMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001696 }
1697
1698 // Collect the names of referenced protocols
1699 llvm::SmallVector<std::string, 16> Protocols;
David Chisnallad9e06d2010-03-13 22:20:45 +00001700 const ObjCCategoryDecl *CatDecl = OCD->getCategoryDecl();
1701 const ObjCList<ObjCProtocolDecl> &Protos = CatDecl->getReferencedProtocols();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001702 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
1703 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001704 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001705
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001706 std::vector<llvm::Constant*> Elements;
1707 Elements.push_back(MakeConstantString(CategoryName));
1708 Elements.push_back(MakeConstantString(ClassName));
Mike Stump1eb44332009-09-09 15:08:12 +00001709 // Instance method list
Owen Anderson3c4972d2009-07-29 18:54:39 +00001710 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +00001711 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001712 false), PtrTy));
1713 // Class method list
Owen Anderson3c4972d2009-07-29 18:54:39 +00001714 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +00001715 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001716 PtrTy));
1717 // Protocol list
Owen Anderson3c4972d2009-07-29 18:54:39 +00001718 Elements.push_back(llvm::ConstantExpr::getBitCast(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001719 GenerateProtocolList(Protocols), PtrTy));
Owen Anderson3c4972d2009-07-29 18:54:39 +00001720 Categories.push_back(llvm::ConstantExpr::getBitCast(
Chris Lattner7650d952011-06-18 22:49:11 +00001721 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty,
Owen Anderson47a434f2009-08-05 23:18:46 +00001722 PtrTy, PtrTy, PtrTy, NULL), Elements), PtrTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001723}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001724
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001725llvm::Constant *CGObjCGNU::GeneratePropertyList(const ObjCImplementationDecl *OID,
1726 llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
1727 llvm::SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes) {
1728 ASTContext &Context = CGM.getContext();
1729 //
1730 // Property metadata: name, attributes, isSynthesized, setter name, setter
1731 // types, getter name, getter types.
Chris Lattner7650d952011-06-18 22:49:11 +00001732 llvm::StructType *PropertyMetadataTy = llvm::StructType::get(
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001733 PtrToInt8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty,
1734 PtrToInt8Ty, NULL);
1735 std::vector<llvm::Constant*> Properties;
1736
1737
1738 // Add all of the property methods need adding to the method list and to the
1739 // property metadata list.
1740 for (ObjCImplDecl::propimpl_iterator
1741 iter = OID->propimpl_begin(), endIter = OID->propimpl_end();
1742 iter != endIter ; iter++) {
1743 std::vector<llvm::Constant*> Fields;
1744 ObjCPropertyDecl *property = (*iter)->getPropertyDecl();
David Chisnall42ba04a2010-02-26 01:11:38 +00001745 ObjCPropertyImplDecl *propertyImpl = *iter;
1746 bool isSynthesized = (propertyImpl->getPropertyImplementation() ==
1747 ObjCPropertyImplDecl::Synthesize);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001748
1749 Fields.push_back(MakeConstantString(property->getNameAsString()));
1750 Fields.push_back(llvm::ConstantInt::get(Int8Ty,
1751 property->getPropertyAttributes()));
David Chisnall42ba04a2010-02-26 01:11:38 +00001752 Fields.push_back(llvm::ConstantInt::get(Int8Ty, isSynthesized));
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001753 if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001754 std::string TypeStr;
1755 Context.getObjCEncodingForMethodDecl(getter,TypeStr);
1756 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
David Chisnall42ba04a2010-02-26 01:11:38 +00001757 if (isSynthesized) {
1758 InstanceMethodTypes.push_back(TypeEncoding);
1759 InstanceMethodSels.push_back(getter->getSelector());
1760 }
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001761 Fields.push_back(MakeConstantString(getter->getSelector().getAsString()));
1762 Fields.push_back(TypeEncoding);
1763 } else {
1764 Fields.push_back(NULLPtr);
1765 Fields.push_back(NULLPtr);
1766 }
1767 if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001768 std::string TypeStr;
1769 Context.getObjCEncodingForMethodDecl(setter,TypeStr);
1770 llvm::Constant *TypeEncoding = MakeConstantString(TypeStr);
David Chisnall42ba04a2010-02-26 01:11:38 +00001771 if (isSynthesized) {
1772 InstanceMethodTypes.push_back(TypeEncoding);
1773 InstanceMethodSels.push_back(setter->getSelector());
1774 }
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001775 Fields.push_back(MakeConstantString(setter->getSelector().getAsString()));
1776 Fields.push_back(TypeEncoding);
1777 } else {
1778 Fields.push_back(NULLPtr);
1779 Fields.push_back(NULLPtr);
1780 }
1781 Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields));
1782 }
1783 llvm::ArrayType *PropertyArrayTy =
1784 llvm::ArrayType::get(PropertyMetadataTy, Properties.size());
1785 llvm::Constant *PropertyArray = llvm::ConstantArray::get(PropertyArrayTy,
1786 Properties);
1787 llvm::Constant* PropertyListInitFields[] =
1788 {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray};
1789
1790 llvm::Constant *PropertyListInit =
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001791 llvm::ConstantStruct::get(VMContext, PropertyListInitFields, 3, false);
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001792 return new llvm::GlobalVariable(TheModule, PropertyListInit->getType(), false,
1793 llvm::GlobalValue::InternalLinkage, PropertyListInit,
1794 ".objc_property_list");
1795}
1796
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001797void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
1798 ASTContext &Context = CGM.getContext();
1799
1800 // Get the superclass name.
Mike Stump1eb44332009-09-09 15:08:12 +00001801 const ObjCInterfaceDecl * SuperClassDecl =
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001802 OID->getClassInterface()->getSuperClass();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001803 std::string SuperClassName;
Chris Lattner2a8e4e12009-06-15 01:09:11 +00001804 if (SuperClassDecl) {
Chris Lattner8ec03f52008-11-24 03:54:41 +00001805 SuperClassName = SuperClassDecl->getNameAsString();
Chris Lattner2a8e4e12009-06-15 01:09:11 +00001806 EmitClassRef(SuperClassName);
1807 }
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001808
1809 // Get the class name
Chris Lattner09dc6662009-04-01 02:00:48 +00001810 ObjCInterfaceDecl *ClassDecl =
1811 const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001812 std::string ClassName = ClassDecl->getNameAsString();
Chris Lattner2a8e4e12009-06-15 01:09:11 +00001813 // Emit the symbol that is used to generate linker errors if this class is
1814 // referenced in other modules but not declared.
Fariborz Jahanianc51db232009-07-03 15:10:14 +00001815 std::string classSymbolName = "__objc_class_name_" + ClassName;
Mike Stump1eb44332009-09-09 15:08:12 +00001816 if (llvm::GlobalVariable *symbol =
Fariborz Jahanianc51db232009-07-03 15:10:14 +00001817 TheModule.getGlobalVariable(classSymbolName)) {
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001818 symbol->setInitializer(llvm::ConstantInt::get(LongTy, 0));
Fariborz Jahanianc51db232009-07-03 15:10:14 +00001819 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00001820 new llvm::GlobalVariable(TheModule, LongTy, false,
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001821 llvm::GlobalValue::ExternalLinkage, llvm::ConstantInt::get(LongTy, 0),
Owen Anderson1c431b32009-07-08 19:05:04 +00001822 classSymbolName);
Fariborz Jahanianc51db232009-07-03 15:10:14 +00001823 }
Mike Stump1eb44332009-09-09 15:08:12 +00001824
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00001825 // Get the size of instances.
Ken Dyck5f022d82011-02-09 01:59:34 +00001826 int instanceSize =
1827 Context.getASTObjCImplementationLayout(OID).getSize().getQuantity();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001828
1829 // Collect information about instance variables.
1830 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
1831 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
1832 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
Mike Stump1eb44332009-09-09 15:08:12 +00001833
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001834 std::vector<llvm::Constant*> IvarOffsetValues;
1835
Mike Stump1eb44332009-09-09 15:08:12 +00001836 int superInstanceSize = !SuperClassDecl ? 0 :
Ken Dyck5f022d82011-02-09 01:59:34 +00001837 Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize().getQuantity();
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001838 // For non-fragile ivars, set the instance size to 0 - {the size of just this
1839 // class}. The runtime will then set this to the correct value on load.
1840 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
1841 instanceSize = 0 - (instanceSize - superInstanceSize);
1842 }
David Chisnall7f63cb02010-04-19 00:45:34 +00001843
1844 // Collect declared and synthesized ivars.
1845 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
1846 CGM.getContext().ShallowCollectObjCIvars(ClassDecl, OIvars);
1847
1848 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
1849 ObjCIvarDecl *IVD = OIvars[i];
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001850 // Store the name
David Chisnall7f63cb02010-04-19 00:45:34 +00001851 IvarNames.push_back(MakeConstantString(IVD->getNameAsString()));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001852 // Get the type encoding for this ivar
1853 std::string TypeStr;
David Chisnall7f63cb02010-04-19 00:45:34 +00001854 Context.getObjCEncodingForType(IVD->getType(), TypeStr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001855 IvarTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001856 // Get the offset
David Chisnalld901da52010-04-19 01:37:25 +00001857 uint64_t BaseOffset = ComputeIvarBaseOffset(CGM, OID, IVD);
David Chisnallaecbf242009-11-17 19:32:15 +00001858 uint64_t Offset = BaseOffset;
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001859 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001860 Offset = BaseOffset - superInstanceSize;
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001861 }
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001862 IvarOffsets.push_back(
Owen Anderson0032b272009-08-13 21:57:51 +00001863 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Offset));
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001864 IvarOffsetValues.push_back(new llvm::GlobalVariable(TheModule, IntTy,
1865 false, llvm::GlobalValue::ExternalLinkage,
David Chisnalle0d98762010-11-03 16:12:44 +00001866 llvm::ConstantInt::get(IntTy, Offset),
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001867 "__objc_ivar_offset_value_" + ClassName +"." +
David Chisnall7f63cb02010-04-19 00:45:34 +00001868 IVD->getNameAsString()));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001869 }
David Chisnall9f6614e2011-03-23 16:36:54 +00001870 llvm::GlobalVariable *IvarOffsetArray =
1871 MakeGlobalArray(PtrToIntTy, IvarOffsetValues, ".ivar.offsets");
1872
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001873
1874 // Collect information about instance methods
1875 llvm::SmallVector<Selector, 16> InstanceMethodSels;
1876 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Mike Stump1eb44332009-09-09 15:08:12 +00001877 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001878 iter = OID->instmeth_begin(), endIter = OID->instmeth_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00001879 iter != endIter ; iter++) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001880 InstanceMethodSels.push_back((*iter)->getSelector());
1881 std::string TypeStr;
1882 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001883 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001884 }
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001885
1886 llvm::Constant *Properties = GeneratePropertyList(OID, InstanceMethodSels,
1887 InstanceMethodTypes);
1888
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001889
1890 // Collect information about class methods
1891 llvm::SmallVector<Selector, 16> ClassMethodSels;
1892 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001893 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001894 iter = OID->classmeth_begin(), endIter = OID->classmeth_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00001895 iter != endIter ; iter++) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001896 ClassMethodSels.push_back((*iter)->getSelector());
1897 std::string TypeStr;
1898 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001899 ClassMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001900 }
1901 // Collect the names of referenced protocols
1902 llvm::SmallVector<std::string, 16> Protocols;
1903 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
1904 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
1905 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001906 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001907
1908
1909
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001910 // Get the superclass pointer.
1911 llvm::Constant *SuperClass;
Chris Lattner8ec03f52008-11-24 03:54:41 +00001912 if (!SuperClassName.empty()) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001913 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
1914 } else {
Owen Anderson03e20502009-07-30 23:11:26 +00001915 SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001916 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001917 // Empty vector used to construct empty method lists
1918 llvm::SmallVector<llvm::Constant*, 1> empty;
1919 // Generate the method and instance variable lists
1920 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +00001921 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001922 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +00001923 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001924 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
1925 IvarOffsets);
Mike Stump1eb44332009-09-09 15:08:12 +00001926 // Irrespective of whether we are compiling for a fragile or non-fragile ABI,
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001927 // we emit a symbol containing the offset for each ivar in the class. This
1928 // allows code compiled for the non-Fragile ABI to inherit from code compiled
1929 // for the legacy ABI, without causing problems. The converse is also
1930 // possible, but causes all ivar accesses to be fragile.
David Chisnalle0d98762010-11-03 16:12:44 +00001931
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001932 // Offset pointer for getting at the correct field in the ivar list when
1933 // setting up the alias. These are: The base address for the global, the
1934 // ivar array (second field), the ivar in this list (set for each ivar), and
1935 // the offset (third field in ivar structure)
1936 const llvm::Type *IndexTy = llvm::Type::getInt32Ty(VMContext);
1937 llvm::Constant *offsetPointerIndexes[] = {Zeros[0],
Mike Stump1eb44332009-09-09 15:08:12 +00001938 llvm::ConstantInt::get(IndexTy, 1), 0,
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001939 llvm::ConstantInt::get(IndexTy, 2) };
1940
David Chisnalle0d98762010-11-03 16:12:44 +00001941
1942 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
1943 ObjCIvarDecl *IVD = OIvars[i];
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001944 const std::string Name = "__objc_ivar_offset_" + ClassName + '.'
David Chisnalle0d98762010-11-03 16:12:44 +00001945 + IVD->getNameAsString();
1946 offsetPointerIndexes[2] = llvm::ConstantInt::get(IndexTy, i);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001947 // Get the correct ivar field
1948 llvm::Constant *offsetValue = llvm::ConstantExpr::getGetElementPtr(
1949 IvarList, offsetPointerIndexes, 4);
David Chisnalle0d98762010-11-03 16:12:44 +00001950 // Get the existing variable, if one exists.
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001951 llvm::GlobalVariable *offset = TheModule.getNamedGlobal(Name);
1952 if (offset) {
1953 offset->setInitializer(offsetValue);
1954 // If this is the real definition, change its linkage type so that
1955 // different modules will use this one, rather than their private
1956 // copy.
1957 offset->setLinkage(llvm::GlobalValue::ExternalLinkage);
1958 } else {
1959 // Add a new alias if there isn't one already.
1960 offset = new llvm::GlobalVariable(TheModule, offsetValue->getType(),
1961 false, llvm::GlobalValue::ExternalLinkage, offsetValue, Name);
1962 }
1963 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001964 //Generate metaclass for class methods
1965 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
David Chisnall18044632009-11-16 19:05:54 +00001966 NULLPtr, 0x12L, ClassName.c_str(), 0, Zeros[0], GenerateIvarList(
David Chisnall8c757f92010-04-28 14:29:56 +00001967 empty, empty, empty), ClassMethodList, NULLPtr, NULLPtr, NULLPtr, true);
Daniel Dunbar5efccb12009-05-04 15:31:17 +00001968
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001969 // Generate the class structure
Chris Lattner8ec03f52008-11-24 03:54:41 +00001970 llvm::Constant *ClassStruct =
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001971 GenerateClassStructure(MetaClassStruct, SuperClass, 0x11L,
Chris Lattner8ec03f52008-11-24 03:54:41 +00001972 ClassName.c_str(), 0,
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001973 llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00001974 MethodList, GenerateProtocolList(Protocols), IvarOffsetArray,
1975 Properties);
Daniel Dunbar5efccb12009-05-04 15:31:17 +00001976
1977 // Resolve the class aliases, if they exist.
1978 if (ClassPtrAlias) {
David Chisnall0b9c22b2010-11-09 11:21:43 +00001979 ClassPtrAlias->replaceAllUsesWith(
Owen Anderson3c4972d2009-07-29 18:54:39 +00001980 llvm::ConstantExpr::getBitCast(ClassStruct, IdTy));
David Chisnall0b9c22b2010-11-09 11:21:43 +00001981 ClassPtrAlias->eraseFromParent();
Daniel Dunbar5efccb12009-05-04 15:31:17 +00001982 ClassPtrAlias = 0;
1983 }
1984 if (MetaClassPtrAlias) {
David Chisnall0b9c22b2010-11-09 11:21:43 +00001985 MetaClassPtrAlias->replaceAllUsesWith(
Owen Anderson3c4972d2009-07-29 18:54:39 +00001986 llvm::ConstantExpr::getBitCast(MetaClassStruct, IdTy));
David Chisnall0b9c22b2010-11-09 11:21:43 +00001987 MetaClassPtrAlias->eraseFromParent();
Daniel Dunbar5efccb12009-05-04 15:31:17 +00001988 MetaClassPtrAlias = 0;
1989 }
1990
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001991 // Add class structure to list to be added to the symtab later
Owen Anderson3c4972d2009-07-29 18:54:39 +00001992 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001993 Classes.push_back(ClassStruct);
1994}
1995
Fariborz Jahanianc38e9af2009-06-23 21:47:46 +00001996
Mike Stump1eb44332009-09-09 15:08:12 +00001997llvm::Function *CGObjCGNU::ModuleInitFunction() {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001998 // Only emit an ObjC load function if no Objective-C stuff has been called
1999 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
David Chisnall9f6614e2011-03-23 16:36:54 +00002000 ExistingProtocols.empty() && SelectorTable.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002001 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +00002002
Fariborz Jahaniand9a1db32009-09-10 21:48:21 +00002003 // Add all referenced protocols to a category.
2004 GenerateProtocolHolderCategory();
2005
Chris Lattnere160c9b2009-01-27 05:06:01 +00002006 const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
2007 SelectorTy->getElementType());
2008 const llvm::Type *SelStructPtrTy = SelectorTy;
Chris Lattnere160c9b2009-01-27 05:06:01 +00002009 if (SelStructTy == 0) {
Chris Lattner7650d952011-06-18 22:49:11 +00002010 SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00002011 SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
Chris Lattnere160c9b2009-01-27 05:06:01 +00002012 }
2013
Eli Friedman1b8956e2008-06-01 16:00:02 +00002014 // Name the ObjC types to make the IR a bit easier to read
Chris Lattnere160c9b2009-01-27 05:06:01 +00002015 TheModule.addTypeName(".objc_selector", SelStructPtrTy);
Eli Friedman1b8956e2008-06-01 16:00:02 +00002016 TheModule.addTypeName(".objc_id", IdTy);
2017 TheModule.addTypeName(".objc_imp", IMPTy);
2018
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002019 std::vector<llvm::Constant*> Elements;
Chris Lattner71238f62009-04-25 23:19:45 +00002020 llvm::Constant *Statics = NULLPtr;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002021 // Generate statics list:
Chris Lattner71238f62009-04-25 23:19:45 +00002022 if (ConstantStrings.size()) {
Owen Anderson96e0fc72009-07-29 22:16:19 +00002023 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
Chris Lattner71238f62009-04-25 23:19:45 +00002024 ConstantStrings.size() + 1);
2025 ConstantStrings.push_back(NULLPtr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002026
Daniel Dunbar1b096952009-11-29 02:38:47 +00002027 llvm::StringRef StringClass = CGM.getLangOptions().ObjCConstantStringClass;
David Chisnall9f6614e2011-03-23 16:36:54 +00002028
Daniel Dunbar1b096952009-11-29 02:38:47 +00002029 if (StringClass.empty()) StringClass = "NXConstantString";
David Chisnall9f6614e2011-03-23 16:36:54 +00002030
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002031 Elements.push_back(MakeConstantString(StringClass,
2032 ".objc_static_class_name"));
Owen Anderson7db6d832009-07-28 18:33:04 +00002033 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy,
Chris Lattner71238f62009-04-25 23:19:45 +00002034 ConstantStrings));
Mike Stump1eb44332009-09-09 15:08:12 +00002035 llvm::StructType *StaticsListTy =
Chris Lattner7650d952011-06-18 22:49:11 +00002036 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002037 llvm::Type *StaticsListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002038 llvm::PointerType::getUnqual(StaticsListTy);
Chris Lattner71238f62009-04-25 23:19:45 +00002039 Statics = MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Mike Stump1eb44332009-09-09 15:08:12 +00002040 llvm::ArrayType *StaticsListArrayTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00002041 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner71238f62009-04-25 23:19:45 +00002042 Elements.clear();
2043 Elements.push_back(Statics);
Owen Andersonc9c88b42009-07-31 20:28:54 +00002044 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner71238f62009-04-25 23:19:45 +00002045 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Owen Anderson3c4972d2009-07-29 18:54:39 +00002046 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
Chris Lattner71238f62009-04-25 23:19:45 +00002047 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002048 // Array of classes, categories, and constant objects
Owen Anderson96e0fc72009-07-29 22:16:19 +00002049 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002050 Classes.size() + Categories.size() + 2);
Chris Lattner7650d952011-06-18 22:49:11 +00002051 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy,
Owen Anderson0032b272009-08-13 21:57:51 +00002052 llvm::Type::getInt16Ty(VMContext),
2053 llvm::Type::getInt16Ty(VMContext),
Chris Lattner630404b2008-06-26 04:10:42 +00002054 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002055
2056 Elements.clear();
2057 // Pointer to an array of selectors used in this module.
2058 std::vector<llvm::Constant*> Selectors;
David Chisnall9f6614e2011-03-23 16:36:54 +00002059 std::vector<llvm::GlobalAlias*> SelectorAliases;
2060 for (SelectorMap::iterator iter = SelectorTable.begin(),
2061 iterEnd = SelectorTable.end(); iter != iterEnd ; ++iter) {
2062
2063 std::string SelNameStr = iter->first.getAsString();
2064 llvm::Constant *SelName = ExportUniqueString(SelNameStr, ".objc_sel_name");
2065
2066 llvm::SmallVectorImpl<TypedSelector> &Types = iter->second;
2067 for (llvm::SmallVectorImpl<TypedSelector>::iterator i = Types.begin(),
2068 e = Types.end() ; i!=e ; i++) {
2069
2070 llvm::Constant *SelectorTypeEncoding = NULLPtr;
2071 if (!i->first.empty())
2072 SelectorTypeEncoding = MakeConstantString(i->first, ".objc_sel_types");
2073
2074 Elements.push_back(SelName);
2075 Elements.push_back(SelectorTypeEncoding);
2076 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
2077 Elements.clear();
2078
2079 // Store the selector alias for later replacement
2080 SelectorAliases.push_back(i->second);
2081 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002082 }
David Chisnall9f6614e2011-03-23 16:36:54 +00002083 unsigned SelectorCount = Selectors.size();
2084 // NULL-terminate the selector list. This should not actually be required,
2085 // because the selector list has a length field. Unfortunately, the GCC
2086 // runtime decides to ignore the length field and expects a NULL terminator,
2087 // and GCC cooperates with this by always setting the length to 0.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002088 Elements.push_back(NULLPtr);
2089 Elements.push_back(NULLPtr);
Owen Anderson08e25242009-07-27 22:29:56 +00002090 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002091 Elements.clear();
David Chisnall9f6614e2011-03-23 16:36:54 +00002092
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002093 // Number of static selectors
David Chisnall9f6614e2011-03-23 16:36:54 +00002094 Elements.push_back(llvm::ConstantInt::get(LongTy, SelectorCount));
2095 llvm::Constant *SelectorList = MakeGlobalArray(SelStructTy, Selectors,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002096 ".objc_selector_list");
Mike Stump1eb44332009-09-09 15:08:12 +00002097 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
Chris Lattnere160c9b2009-01-27 05:06:01 +00002098 SelStructPtrTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002099
2100 // Now that all of the static selectors exist, create pointers to them.
David Chisnall9f6614e2011-03-23 16:36:54 +00002101 for (unsigned int i=0 ; i<SelectorCount ; i++) {
2102
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002103 llvm::Constant *Idxs[] = {Zeros[0],
David Chisnall9f6614e2011-03-23 16:36:54 +00002104 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), i), Zeros[0]};
2105 // FIXME: We're generating redundant loads and stores here!
David Chisnallc7ef4622011-03-23 22:52:06 +00002106 llvm::Constant *SelPtr = llvm::ConstantExpr::getGetElementPtr(SelectorList,
2107 Idxs, 2);
Chris Lattnere160c9b2009-01-27 05:06:01 +00002108 // If selectors are defined as an opaque type, cast the pointer to this
2109 // type.
David Chisnallc7ef4622011-03-23 22:52:06 +00002110 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr, SelectorTy);
David Chisnall9f6614e2011-03-23 16:36:54 +00002111 SelectorAliases[i]->replaceAllUsesWith(SelPtr);
2112 SelectorAliases[i]->eraseFromParent();
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002113 }
David Chisnall9f6614e2011-03-23 16:36:54 +00002114
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002115 // Number of classes defined.
Mike Stump1eb44332009-09-09 15:08:12 +00002116 Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002117 Classes.size()));
2118 // Number of categories defined
Mike Stump1eb44332009-09-09 15:08:12 +00002119 Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002120 Categories.size()));
2121 // Create an array of classes, then categories, then static object instances
2122 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
2123 // NULL-terminated list of static object instances (mainly constant strings)
2124 Classes.push_back(Statics);
2125 Classes.push_back(NULLPtr);
Owen Anderson7db6d832009-07-28 18:33:04 +00002126 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002127 Elements.push_back(ClassList);
Mike Stump1eb44332009-09-09 15:08:12 +00002128 // Construct the symbol table
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002129 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
2130
2131 // The symbol table is contained in a module which has some version-checking
2132 // constants
Chris Lattner7650d952011-06-18 22:49:11 +00002133 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
David Chisnalla2120032011-05-22 22:37:08 +00002134 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy),
2135 (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) ? NULL : IntTy,
2136 NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002137 Elements.clear();
David Chisnall9f6614e2011-03-23 16:36:54 +00002138 // Runtime version, used for ABI compatibility checking.
2139 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
Fariborz Jahanian91a0b512009-04-01 19:49:42 +00002140 // sizeof(ModuleTy)
Benjamin Kramer74a8bbf2010-02-09 19:31:24 +00002141 llvm::TargetData td(&TheModule);
Ken Dycke0afc892011-04-22 17:59:22 +00002142 Elements.push_back(
2143 llvm::ConstantInt::get(LongTy,
2144 td.getTypeSizeInBits(ModuleTy) /
2145 CGM.getContext().getCharWidth()));
David Chisnall9f6614e2011-03-23 16:36:54 +00002146
2147 // The path to the source file where this module was declared
2148 SourceManager &SM = CGM.getContext().getSourceManager();
2149 const FileEntry *mainFile = SM.getFileEntryForID(SM.getMainFileID());
2150 std::string path =
2151 std::string(mainFile->getDir()->getName()) + '/' + mainFile->getName();
2152 Elements.push_back(MakeConstantString(path, ".objc_source_file_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002153 Elements.push_back(SymTab);
David Chisnalla2120032011-05-22 22:37:08 +00002154
2155 switch (CGM.getLangOptions().getGCMode()) {
2156 case LangOptions::GCOnly:
2157 Elements.push_back(llvm::ConstantInt::get(IntTy, 2));
2158 case LangOptions::NonGC:
2159 break;
2160 case LangOptions::HybridGC:
2161 Elements.push_back(llvm::ConstantInt::get(IntTy, 1));
2162 }
2163
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002164 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
2165
2166 // Create the load function calling the runtime entry point with the module
2167 // structure
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002168 llvm::Function * LoadFunction = llvm::Function::Create(
Owen Anderson0032b272009-08-13 21:57:51 +00002169 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002170 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
2171 &TheModule);
Owen Anderson0032b272009-08-13 21:57:51 +00002172 llvm::BasicBlock *EntryBB =
2173 llvm::BasicBlock::Create(VMContext, "entry", LoadFunction);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002174 CGBuilderTy Builder(VMContext);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002175 Builder.SetInsertPoint(EntryBB);
Fariborz Jahanian26c82942009-03-30 18:02:14 +00002176
Benjamin Kramer95d318c2011-05-28 14:26:31 +00002177 llvm::FunctionType *FT =
2178 llvm::FunctionType::get(Builder.getVoidTy(),
2179 llvm::PointerType::getUnqual(ModuleTy), true);
2180 llvm::Value *Register = CGM.CreateRuntimeFunction(FT, "__objc_exec_class");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002181 Builder.CreateCall(Register, Module);
2182 Builder.CreateRetVoid();
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002183
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002184 return LoadFunction;
2185}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002186
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002187llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
Mike Stump1eb44332009-09-09 15:08:12 +00002188 const ObjCContainerDecl *CD) {
2189 const ObjCCategoryImplDecl *OCD =
Steve Naroff3e0a5402009-01-08 19:41:02 +00002190 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
David Chisnall9f6614e2011-03-23 16:36:54 +00002191 llvm::StringRef CategoryName = OCD ? OCD->getName() : "";
2192 llvm::StringRef ClassName = CD->getName();
2193 Selector MethodName = OMD->getSelector();
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002194 bool isClassMethod = !OMD->isInstanceMethod();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002195
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002196 CodeGenTypes &Types = CGM.getTypes();
Mike Stump1eb44332009-09-09 15:08:12 +00002197 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002198 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00002199 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
2200 MethodName, isClassMethod);
2201
Daniel Dunbard6c93d72009-09-17 04:01:22 +00002202 llvm::Function *Method
Mike Stump1eb44332009-09-09 15:08:12 +00002203 = llvm::Function::Create(MethodTy,
2204 llvm::GlobalValue::InternalLinkage,
2205 FunctionName,
2206 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +00002207 return Method;
2208}
2209
David Chisnall789ecde2011-05-23 22:33:28 +00002210llvm::Constant *CGObjCGNU::GetPropertyGetFunction() {
David Chisnall9f6614e2011-03-23 16:36:54 +00002211 return GetPropertyFn;
Daniel Dunbar49f66022008-09-24 03:38:44 +00002212}
2213
David Chisnall789ecde2011-05-23 22:33:28 +00002214llvm::Constant *CGObjCGNU::GetPropertySetFunction() {
David Chisnall9f6614e2011-03-23 16:36:54 +00002215 return SetPropertyFn;
Daniel Dunbar49f66022008-09-24 03:38:44 +00002216}
2217
David Chisnall789ecde2011-05-23 22:33:28 +00002218llvm::Constant *CGObjCGNU::GetGetStructFunction() {
David Chisnall9f6614e2011-03-23 16:36:54 +00002219 return GetStructPropertyFn;
David Chisnall8fac25d2010-12-26 22:13:16 +00002220}
David Chisnall789ecde2011-05-23 22:33:28 +00002221llvm::Constant *CGObjCGNU::GetSetStructFunction() {
David Chisnall9f6614e2011-03-23 16:36:54 +00002222 return SetStructPropertyFn;
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002223}
2224
Daniel Dunbar309a4362009-07-24 07:40:24 +00002225llvm::Constant *CGObjCGNU::EnumerationMutationFunction() {
David Chisnall9f6614e2011-03-23 16:36:54 +00002226 return EnumerationMutationFn;
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002227}
2228
David Chisnall9f6614e2011-03-23 16:36:54 +00002229void CGObjCGNU::EmitSynchronizedStmt(CodeGenFunction &CGF,
John McCallf1549f62010-07-06 01:34:17 +00002230 const ObjCAtSynchronizedStmt &S) {
David Chisnall9735ca62011-03-25 11:57:33 +00002231 EmitAtSynchronizedStmt(CGF, S, SyncEnterFn, SyncExitFn);
John McCallf1549f62010-07-06 01:34:17 +00002232}
Chris Lattner5dc08672009-05-08 00:11:50 +00002233
David Chisnall0faa5162009-12-24 02:26:34 +00002234
David Chisnall9f6614e2011-03-23 16:36:54 +00002235void CGObjCGNU::EmitTryStmt(CodeGenFunction &CGF,
John McCallf1549f62010-07-06 01:34:17 +00002236 const ObjCAtTryStmt &S) {
2237 // Unlike the Apple non-fragile runtimes, which also uses
2238 // unwind-based zero cost exceptions, the GNU Objective C runtime's
2239 // EH support isn't a veneer over C++ EH. Instead, exception
2240 // objects are created by __objc_exception_throw and destroyed by
2241 // the personality function; this avoids the need for bracketing
2242 // catch handlers with calls to __blah_begin_catch/__blah_end_catch
2243 // (or even _Unwind_DeleteException), but probably doesn't
2244 // interoperate very well with foreign exceptions.
David Chisnall9735ca62011-03-25 11:57:33 +00002245 //
David Chisnall80558d22011-03-20 21:35:39 +00002246 // In Objective-C++ mode, we actually emit something equivalent to the C++
David Chisnall9735ca62011-03-25 11:57:33 +00002247 // exception handler.
2248 EmitTryCatchStmt(CGF, S, EnterCatchFn, ExitCatchFn, ExceptionReThrowFn);
2249 return ;
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002250}
2251
David Chisnall9f6614e2011-03-23 16:36:54 +00002252void CGObjCGNU::EmitThrowStmt(CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +00002253 const ObjCAtThrowStmt &S) {
Chris Lattner5dc08672009-05-08 00:11:50 +00002254 llvm::Value *ExceptionAsObject;
2255
Chris Lattner5dc08672009-05-08 00:11:50 +00002256 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2257 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Fariborz Jahanian1e64a952009-05-17 16:49:27 +00002258 ExceptionAsObject = Exception;
Chris Lattner5dc08672009-05-08 00:11:50 +00002259 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00002260 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Chris Lattner5dc08672009-05-08 00:11:50 +00002261 "Unexpected rethrow outside @catch block.");
2262 ExceptionAsObject = CGF.ObjCEHValueStack.back();
2263 }
Fariborz Jahanian1e64a952009-05-17 16:49:27 +00002264 ExceptionAsObject =
2265 CGF.Builder.CreateBitCast(ExceptionAsObject, IdTy, "tmp");
Mike Stump1eb44332009-09-09 15:08:12 +00002266
Chris Lattner5dc08672009-05-08 00:11:50 +00002267 // Note: This may have to be an invoke, if we want to support constructs like:
2268 // @try {
2269 // @throw(obj);
2270 // }
2271 // @catch(id) ...
2272 //
2273 // This is effectively turning @throw into an incredibly-expensive goto, but
2274 // it may happen as a result of inlining followed by missed optimizations, or
2275 // as a result of stupidity.
2276 llvm::BasicBlock *UnwindBB = CGF.getInvokeDest();
2277 if (!UnwindBB) {
David Chisnall9f6614e2011-03-23 16:36:54 +00002278 CGF.Builder.CreateCall(ExceptionThrowFn, ExceptionAsObject);
Chris Lattner5dc08672009-05-08 00:11:50 +00002279 CGF.Builder.CreateUnreachable();
2280 } else {
David Chisnall9f6614e2011-03-23 16:36:54 +00002281 CGF.Builder.CreateInvoke(ExceptionThrowFn, UnwindBB, UnwindBB, &ExceptionAsObject,
Chris Lattner5dc08672009-05-08 00:11:50 +00002282 &ExceptionAsObject+1);
2283 }
2284 // Clear the insertion point to indicate we are in unreachable code.
2285 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002286}
2287
David Chisnall9f6614e2011-03-23 16:36:54 +00002288llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00002289 llvm::Value *AddrWeakObj) {
David Chisnallef6e0f32010-02-03 15:59:02 +00002290 CGBuilderTy B = CGF.Builder;
David Chisnall31fc0c12011-05-30 12:00:26 +00002291 AddrWeakObj = EnforceType(B, AddrWeakObj, PtrToIdTy);
David Chisnallef6e0f32010-02-03 15:59:02 +00002292 return B.CreateCall(WeakReadFn, AddrWeakObj);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002293}
2294
David Chisnall9f6614e2011-03-23 16:36:54 +00002295void CGObjCGNU::EmitObjCWeakAssign(CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00002296 llvm::Value *src, llvm::Value *dst) {
David Chisnallef6e0f32010-02-03 15:59:02 +00002297 CGBuilderTy B = CGF.Builder;
2298 src = EnforceType(B, src, IdTy);
2299 dst = EnforceType(B, dst, PtrToIdTy);
2300 B.CreateCall2(WeakAssignFn, src, dst);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002301}
2302
David Chisnall9f6614e2011-03-23 16:36:54 +00002303void CGObjCGNU::EmitObjCGlobalAssign(CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00002304 llvm::Value *src, llvm::Value *dst,
2305 bool threadlocal) {
David Chisnallef6e0f32010-02-03 15:59:02 +00002306 CGBuilderTy B = CGF.Builder;
2307 src = EnforceType(B, src, IdTy);
2308 dst = EnforceType(B, dst, PtrToIdTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00002309 if (!threadlocal)
2310 B.CreateCall2(GlobalAssignFn, src, dst);
2311 else
2312 // FIXME. Add threadloca assign API
2313 assert(false && "EmitObjCGlobalAssign - Threal Local API NYI");
Fariborz Jahanian58626502008-11-19 00:59:10 +00002314}
2315
David Chisnall9f6614e2011-03-23 16:36:54 +00002316void CGObjCGNU::EmitObjCIvarAssign(CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00002317 llvm::Value *src, llvm::Value *dst,
2318 llvm::Value *ivarOffset) {
David Chisnallef6e0f32010-02-03 15:59:02 +00002319 CGBuilderTy B = CGF.Builder;
2320 src = EnforceType(B, src, IdTy);
David Chisnallb44eda32011-05-25 20:33:17 +00002321 dst = EnforceType(B, dst, IdTy);
David Chisnallef6e0f32010-02-03 15:59:02 +00002322 B.CreateCall3(IvarAssignFn, src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002323}
2324
David Chisnall9f6614e2011-03-23 16:36:54 +00002325void CGObjCGNU::EmitObjCStrongCastAssign(CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00002326 llvm::Value *src, llvm::Value *dst) {
David Chisnallef6e0f32010-02-03 15:59:02 +00002327 CGBuilderTy B = CGF.Builder;
2328 src = EnforceType(B, src, IdTy);
2329 dst = EnforceType(B, dst, PtrToIdTy);
2330 B.CreateCall2(StrongCastAssignFn, src, dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00002331}
2332
David Chisnall9f6614e2011-03-23 16:36:54 +00002333void CGObjCGNU::EmitGCMemmoveCollectable(CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00002334 llvm::Value *DestPtr,
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002335 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00002336 llvm::Value *Size) {
David Chisnallef6e0f32010-02-03 15:59:02 +00002337 CGBuilderTy B = CGF.Builder;
David Chisnall68e5e132011-05-28 14:23:43 +00002338 DestPtr = EnforceType(B, DestPtr, PtrTy);
2339 SrcPtr = EnforceType(B, SrcPtr, PtrTy);
David Chisnallef6e0f32010-02-03 15:59:02 +00002340
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00002341 B.CreateCall3(MemMoveFn, DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002342}
2343
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002344llvm::GlobalVariable *CGObjCGNU::ObjCIvarOffsetVariable(
2345 const ObjCInterfaceDecl *ID,
2346 const ObjCIvarDecl *Ivar) {
2347 const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString()
2348 + '.' + Ivar->getNameAsString();
2349 // Emit the variable and initialize it with what we think the correct value
2350 // is. This allows code compiled with non-fragile ivars to work correctly
2351 // when linked against code which isn't (most of the time).
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002352 llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name);
2353 if (!IvarOffsetPointer) {
David Chisnalle0d98762010-11-03 16:12:44 +00002354 // This will cause a run-time crash if we accidentally use it. A value of
2355 // 0 would seem more sensible, but will silently overwrite the isa pointer
2356 // causing a great deal of confusion.
2357 uint64_t Offset = -1;
2358 // We can't call ComputeIvarBaseOffset() here if we have the
2359 // implementation, because it will create an invalid ASTRecordLayout object
2360 // that we are then stuck with forever, so we only initialize the ivar
2361 // offset variable with a guess if we only have the interface. The
2362 // initializer will be reset later anyway, when we are generating the class
2363 // description.
2364 if (!CGM.getContext().getObjCImplementation(
Dan Gohmancb421fa2010-04-19 16:39:44 +00002365 const_cast<ObjCInterfaceDecl *>(ID)))
David Chisnalld901da52010-04-19 01:37:25 +00002366 Offset = ComputeIvarBaseOffset(CGM, ID, Ivar);
2367
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002368 llvm::ConstantInt *OffsetGuess =
David Chisnallf9508372010-01-11 19:02:35 +00002369 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Offset, "ivar");
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002370 // Don't emit the guess in non-PIC code because the linker will not be able
2371 // to replace it with the real version for a library. In non-PIC code you
2372 // must compile with the fragile ABI if you want to use ivars from a
Mike Stump1eb44332009-09-09 15:08:12 +00002373 // GCC-compiled class.
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002374 if (CGM.getLangOptions().PICLevel) {
2375 llvm::GlobalVariable *IvarOffsetGV = new llvm::GlobalVariable(TheModule,
2376 llvm::Type::getInt32Ty(VMContext), false,
2377 llvm::GlobalValue::PrivateLinkage, OffsetGuess, Name+".guess");
2378 IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
2379 IvarOffsetGV->getType(), false, llvm::GlobalValue::LinkOnceAnyLinkage,
2380 IvarOffsetGV, Name);
2381 } else {
2382 IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00002383 llvm::Type::getInt32PtrTy(VMContext), false,
2384 llvm::GlobalValue::ExternalLinkage, 0, Name);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002385 }
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002386 }
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002387 return IvarOffsetPointer;
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002388}
2389
David Chisnall9f6614e2011-03-23 16:36:54 +00002390LValue CGObjCGNU::EmitObjCValueForIvar(CodeGenFunction &CGF,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002391 QualType ObjectTy,
2392 llvm::Value *BaseValue,
2393 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002394 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00002395 const ObjCInterfaceDecl *ID =
2396 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00002397 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
2398 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002399}
Mike Stumpbb1c8602009-07-31 21:31:32 +00002400
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002401static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context,
2402 const ObjCInterfaceDecl *OID,
2403 const ObjCIvarDecl *OIVD) {
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002404 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002405 Context.ShallowCollectObjCIvars(OID, Ivars);
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002406 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
2407 if (OIVD == Ivars[k])
2408 return OID;
2409 }
Mike Stump1eb44332009-09-09 15:08:12 +00002410
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002411 // Otherwise check in the super class.
2412 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
2413 return FindIvarInterface(Context, Super, OIVD);
Mike Stump1eb44332009-09-09 15:08:12 +00002414
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002415 return 0;
2416}
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002417
David Chisnall9f6614e2011-03-23 16:36:54 +00002418llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00002419 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002420 const ObjCIvarDecl *Ivar) {
David Chisnall8a5a9aa2009-08-31 16:41:57 +00002421 if (CGM.getLangOptions().ObjCNonFragileABI) {
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002422 Interface = FindIvarInterface(CGM.getContext(), Interface, Ivar);
David Chisnall7e02d1a2011-03-22 19:57:51 +00002423 return CGF.Builder.CreateZExtOrBitCast(
2424 CGF.Builder.CreateLoad(CGF.Builder.CreateLoad(
2425 ObjCIvarOffsetVariable(Interface, Ivar), false, "ivar")),
2426 PtrDiffTy);
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00002427 }
Daniel Dunbar97776872009-04-22 07:32:20 +00002428 uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar);
David Chisnall7e02d1a2011-03-22 19:57:51 +00002429 return llvm::ConstantInt::get(PtrDiffTy, Offset, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002430}
2431
David Chisnall9f6614e2011-03-23 16:36:54 +00002432CGObjCRuntime *
2433clang::CodeGen::CreateGNUObjCRuntime(CodeGenModule &CGM) {
2434 if (CGM.getLangOptions().ObjCNonFragileABI)
2435 return new CGObjCGNUstep(CGM);
2436 return new CGObjCGCC(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +00002437}