Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1 | //===------- CGObjCGNU.cpp - Emit LLVM Code from ASTs for a Module --------===// |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 10 | // This provides Objective-C code generation targeting the GNU runtime. The |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 11 | // class in this file generates structures used by the GNU Objective-C runtime |
| 12 | // library. These structures are defined in objc/objc.h and objc/objc-api.h in |
| 13 | // the GNU runtime distribution. |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "CGObjCRuntime.h" |
John McCall | 36f893c | 2011-01-28 11:13:47 +0000 | [diff] [blame] | 18 | #include "CGCleanup.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "CodeGenFunction.h" |
| 20 | #include "CodeGenModule.h" |
Chris Lattner | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 22 | #include "clang/AST/Decl.h" |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | 16f0049 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 25 | #include "clang/AST/StmtObjC.h" |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 26 | #include "clang/Basic/FileManager.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 27 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallVector.h" |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/StringMap.h" |
Chandler Carruth | 3b844ba | 2013-01-02 11:45:17 +0000 | [diff] [blame] | 30 | #include "llvm/IR/DataLayout.h" |
| 31 | #include "llvm/IR/Intrinsics.h" |
| 32 | #include "llvm/IR/LLVMContext.h" |
| 33 | #include "llvm/IR/Module.h" |
David Chisnall | 80558d2 | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CallSite.h" |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 36 | #include <cstdarg> |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 37 | |
| 38 | |
Chris Lattner | dce1406 | 2008-06-26 04:19:03 +0000 | [diff] [blame] | 39 | using namespace clang; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 40 | using namespace CodeGen; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 43 | namespace { |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 44 | /// Class that lazily initialises the runtime function. Avoids inserting the |
| 45 | /// types and the function declaration into a module if they're not used, and |
| 46 | /// avoids constructing the type more than once if it's used more than once. |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 47 | class LazyRuntimeFunction { |
| 48 | CodeGenModule *CGM; |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 49 | std::vector<llvm::Type*> ArgTys; |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 50 | const char *FunctionName; |
David Chisnall | 789ecde | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 51 | llvm::Constant *Function; |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 52 | public: |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 53 | /// Constructor leaves this class uninitialized, because it is intended to |
| 54 | /// be used as a field in another class and not all of the types that are |
| 55 | /// used as arguments will necessarily be available at construction time. |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 56 | LazyRuntimeFunction() : CGM(0), FunctionName(0), Function(0) {} |
| 57 | |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 58 | /// Initialises the lazy function with the name, return type, and the types |
| 59 | /// of the arguments. |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 60 | END_WITH_NULL |
| 61 | void init(CodeGenModule *Mod, const char *name, |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 62 | llvm::Type *RetTy, ...) { |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 63 | CGM =Mod; |
| 64 | FunctionName = name; |
| 65 | Function = 0; |
David Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 66 | ArgTys.clear(); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 67 | va_list Args; |
| 68 | va_start(Args, RetTy); |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 69 | while (llvm::Type *ArgTy = va_arg(Args, llvm::Type*)) |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 70 | ArgTys.push_back(ArgTy); |
| 71 | va_end(Args); |
| 72 | // Push the return type on at the end so we can pop it off easily |
| 73 | ArgTys.push_back(RetTy); |
| 74 | } |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 75 | /// Overloaded cast operator, allows the class to be implicitly cast to an |
| 76 | /// LLVM constant. |
David Chisnall | 789ecde | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 77 | operator llvm::Constant*() { |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 78 | if (!Function) { |
David Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 79 | if (0 == FunctionName) return 0; |
| 80 | // We put the return type on the end of the vector, so pop it back off |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 81 | llvm::Type *RetTy = ArgTys.back(); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 82 | ArgTys.pop_back(); |
| 83 | llvm::FunctionType *FTy = llvm::FunctionType::get(RetTy, ArgTys, false); |
| 84 | Function = |
David Chisnall | 789ecde | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 85 | cast<llvm::Constant>(CGM->CreateRuntimeFunction(FTy, FunctionName)); |
David Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 86 | // We won't need to use the types again, so we may as well clean up the |
| 87 | // vector now |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 88 | ArgTys.resize(0); |
| 89 | } |
| 90 | return Function; |
| 91 | } |
David Chisnall | 789ecde | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 92 | operator llvm::Function*() { |
David Chisnall | 5f0bcc4 | 2011-05-23 23:15:11 +0000 | [diff] [blame] | 93 | return cast<llvm::Function>((llvm::Constant*)*this); |
David Chisnall | 789ecde | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 94 | } |
David Chisnall | 5f0bcc4 | 2011-05-23 23:15:11 +0000 | [diff] [blame] | 95 | |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 99 | /// GNU Objective-C runtime code generation. This class implements the parts of |
John McCall | f7226fb | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 100 | /// Objective-C support that are specific to the GNU family of runtimes (GCC, |
| 101 | /// GNUstep and ObjFW). |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 102 | class CGObjCGNU : public CGObjCRuntime { |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 103 | protected: |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 104 | /// The LLVM module into which output is inserted |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 105 | llvm::Module &TheModule; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 106 | /// strut objc_super. Used for sending messages to super. This structure |
| 107 | /// contains the receiver (object) and the expected class. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 108 | llvm::StructType *ObjCSuperTy; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 109 | /// struct objc_super*. The type of the argument to the superclass message |
| 110 | /// lookup functions. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 111 | llvm::PointerType *PtrToObjCSuperTy; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 112 | /// LLVM type for selectors. Opaque pointer (i8*) unless a header declaring |
| 113 | /// SEL is included in a header somewhere, in which case it will be whatever |
| 114 | /// type is declared in that header, most likely {i8*, i8*}. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 115 | llvm::PointerType *SelectorTy; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 116 | /// LLVM i8 type. Cached here to avoid repeatedly getting it in all of the |
| 117 | /// places where it's used |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 118 | llvm::IntegerType *Int8Ty; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 119 | /// Pointer to i8 - LLVM type of char*, for all of the places where the |
| 120 | /// runtime needs to deal with C strings. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 121 | llvm::PointerType *PtrToInt8Ty; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 122 | /// Instance Method Pointer type. This is a pointer to a function that takes, |
| 123 | /// at a minimum, an object and a selector, and is the generic type for |
| 124 | /// Objective-C methods. Due to differences between variadic / non-variadic |
| 125 | /// calling conventions, it must always be cast to the correct type before |
| 126 | /// actually being used. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 127 | llvm::PointerType *IMPTy; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 128 | /// Type of an untyped Objective-C object. Clang treats id as a built-in type |
| 129 | /// when compiling Objective-C code, so this may be an opaque pointer (i8*), |
| 130 | /// but if the runtime header declaring it is included then it may be a |
| 131 | /// pointer to a structure. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 132 | llvm::PointerType *IdTy; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 133 | /// Pointer to a pointer to an Objective-C object. Used in the new ABI |
| 134 | /// message lookup function and some GC-related functions. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 135 | llvm::PointerType *PtrToIdTy; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 136 | /// The clang type of id. Used when using the clang CGCall infrastructure to |
| 137 | /// call Objective-C methods. |
John McCall | ead608a | 2010-02-26 00:48:12 +0000 | [diff] [blame] | 138 | CanQualType ASTIdTy; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 139 | /// LLVM type for C int type. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 140 | llvm::IntegerType *IntTy; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 141 | /// LLVM type for an opaque pointer. This is identical to PtrToInt8Ty, but is |
| 142 | /// used in the code to document the difference between i8* meaning a pointer |
| 143 | /// to a C string and i8* meaning a pointer to some opaque type. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 144 | llvm::PointerType *PtrTy; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 145 | /// LLVM type for C long type. The runtime uses this in a lot of places where |
| 146 | /// it should be using intptr_t, but we can't fix this without breaking |
| 147 | /// compatibility with GCC... |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 148 | llvm::IntegerType *LongTy; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 149 | /// LLVM type for C size_t. Used in various runtime data structures. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 150 | llvm::IntegerType *SizeTy; |
David Chisnall | 49de528 | 2011-10-08 08:54:36 +0000 | [diff] [blame] | 151 | /// LLVM type for C intptr_t. |
| 152 | llvm::IntegerType *IntPtrTy; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 153 | /// LLVM type for C ptrdiff_t. Mainly used in property accessor functions. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 154 | llvm::IntegerType *PtrDiffTy; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 155 | /// LLVM type for C int*. Used for GCC-ABI-compatible non-fragile instance |
| 156 | /// variables. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 157 | llvm::PointerType *PtrToIntTy; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 158 | /// LLVM type for Objective-C BOOL type. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 159 | llvm::Type *BoolTy; |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 160 | /// 32-bit integer type, to save us needing to look it up every time it's used. |
| 161 | llvm::IntegerType *Int32Ty; |
| 162 | /// 64-bit integer type, to save us needing to look it up every time it's used. |
| 163 | llvm::IntegerType *Int64Ty; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 164 | /// 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 Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 167 | unsigned msgSendMDKind; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 168 | /// 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 Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 171 | llvm::Constant *MakeConstantString(const std::string &Str, |
| 172 | const std::string &Name="") { |
| 173 | llvm::Constant *ConstStr = CGM.GetAddrOfConstantCString(Str, Name.c_str()); |
Jay Foad | a5c0434 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 174 | return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros); |
David Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 175 | } |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 176 | /// 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 Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 180 | 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) { |
Chris Lattner | 9401069 | 2012-02-05 02:30:40 +0000 | [diff] [blame] | 185 | llvm::Constant *value = llvm::ConstantDataArray::getString(VMContext,Str); |
David Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 186 | ConstStr = new llvm::GlobalVariable(TheModule, value->getType(), true, |
| 187 | llvm::GlobalValue::LinkOnceODRLinkage, value, prefix + Str); |
| 188 | } |
Jay Foad | a5c0434 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 189 | return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros); |
David Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 190 | } |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 191 | /// 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. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 194 | llvm::GlobalVariable *MakeGlobal(llvm::StructType *Ty, |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 195 | llvm::ArrayRef<llvm::Constant*> V, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 196 | StringRef Name="", |
David Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 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 Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 203 | /// 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. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 206 | llvm::GlobalVariable *MakeGlobal(llvm::ArrayType *Ty, |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 207 | llvm::ArrayRef<llvm::Constant*> V, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 208 | StringRef Name="", |
David Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 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 Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 215 | /// Generates a global array, inferring the array type from the specified |
| 216 | /// element type and the size of the initialiser. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 217 | llvm::GlobalVariable *MakeGlobalArray(llvm::Type *Ty, |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 218 | llvm::ArrayRef<llvm::Constant*> V, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 219 | StringRef Name="", |
David Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 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 Chisnall | 891dac7 | 2012-10-16 15:11:55 +0000 | [diff] [blame] | 225 | /// Returns a property name and encoding string. |
| 226 | llvm::Constant *MakePropertyEncodingString(const ObjCPropertyDecl *PD, |
| 227 | const Decl *Container) { |
| 228 | ObjCRuntime R = CGM.getLangOpts().ObjCRuntime; |
| 229 | if ((R.getKind() == ObjCRuntime::GNUstep) && |
| 230 | (R.getVersion() >= VersionTuple(1, 6))) { |
| 231 | std::string NameAndAttributes; |
| 232 | std::string TypeStr; |
| 233 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
| 234 | NameAndAttributes += '\0'; |
| 235 | NameAndAttributes += TypeStr.length() + 3; |
| 236 | NameAndAttributes += TypeStr; |
| 237 | NameAndAttributes += '\0'; |
| 238 | NameAndAttributes += PD->getNameAsString(); |
| 239 | return llvm::ConstantExpr::getGetElementPtr( |
| 240 | CGM.GetAddrOfConstantString(NameAndAttributes), Zeros); |
| 241 | } |
| 242 | return MakeConstantString(PD->getNameAsString()); |
| 243 | } |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 244 | /// Ensures that the value has the required type, by inserting a bitcast if |
| 245 | /// required. This function lets us avoid inserting bitcasts that are |
| 246 | /// redundant. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 247 | llvm::Value* EnforceType(CGBuilderTy B, llvm::Value *V, llvm::Type *Ty){ |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 248 | if (V->getType() == Ty) return V; |
| 249 | return B.CreateBitCast(V, Ty); |
| 250 | } |
| 251 | // Some zeros used for GEPs in lots of places. |
| 252 | llvm::Constant *Zeros[2]; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 253 | /// Null pointer value. Mainly used as a terminator in various arrays. |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 254 | llvm::Constant *NULLPtr; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 255 | /// LLVM context. |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 256 | llvm::LLVMContext &VMContext; |
| 257 | private: |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 258 | /// Placeholder for the class. Lots of things refer to the class before we've |
| 259 | /// actually emitted it. We use this alias as a placeholder, and then replace |
| 260 | /// it with a pointer to the class structure before finally emitting the |
| 261 | /// module. |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 262 | llvm::GlobalAlias *ClassPtrAlias; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 263 | /// Placeholder for the metaclass. Lots of things refer to the class before |
| 264 | /// we've / actually emitted it. We use this alias as a placeholder, and then |
| 265 | /// replace / it with a pointer to the metaclass structure before finally |
| 266 | /// emitting the / module. |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 267 | llvm::GlobalAlias *MetaClassPtrAlias; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 268 | /// All of the classes that have been generated for this compilation units. |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 269 | std::vector<llvm::Constant*> Classes; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 270 | /// All of the categories that have been generated for this compilation units. |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 271 | std::vector<llvm::Constant*> Categories; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 272 | /// All of the Objective-C constant strings that have been generated for this |
| 273 | /// compilation units. |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 274 | std::vector<llvm::Constant*> ConstantStrings; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 275 | /// Map from string values to Objective-C constant strings in the output. |
| 276 | /// Used to prevent emitting Objective-C strings more than once. This should |
| 277 | /// not be required at all - CodeGenModule should manage this list. |
David Chisnall | 48272a0 | 2010-01-27 12:49:23 +0000 | [diff] [blame] | 278 | llvm::StringMap<llvm::Constant*> ObjCStrings; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 279 | /// All of the protocols that have been declared. |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 280 | llvm::StringMap<llvm::Constant*> ExistingProtocols; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 281 | /// For each variant of a selector, we store the type encoding and a |
| 282 | /// placeholder value. For an untyped selector, the type will be the empty |
| 283 | /// string. Selector references are all done via the module's selector table, |
| 284 | /// so we create an alias as a placeholder and then replace it with the real |
| 285 | /// value later. |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 286 | typedef std::pair<std::string, llvm::GlobalAlias*> TypedSelector; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 287 | /// Type of the selector map. This is roughly equivalent to the structure |
| 288 | /// used in the GNUstep runtime, which maintains a list of all of the valid |
| 289 | /// types for a selector in a table. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 290 | typedef llvm::DenseMap<Selector, SmallVector<TypedSelector, 2> > |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 291 | SelectorMap; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 292 | /// A map from selectors to selector types. This allows us to emit all |
| 293 | /// selectors of the same name and type together. |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 294 | SelectorMap SelectorTable; |
| 295 | |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 296 | /// Selectors related to memory management. When compiling in GC mode, we |
| 297 | /// omit these. |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 298 | Selector RetainSel, ReleaseSel, AutoreleaseSel; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 299 | /// Runtime functions used for memory management in GC mode. Note that clang |
| 300 | /// supports code generation for calling these functions, but neither GNU |
| 301 | /// runtime actually supports this API properly yet. |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 302 | LazyRuntimeFunction IvarAssignFn, StrongCastAssignFn, MemMoveFn, WeakReadFn, |
| 303 | WeakAssignFn, GlobalAssignFn; |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 304 | |
David Chisnall | 29254f4 | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 305 | typedef std::pair<std::string, std::string> ClassAliasPair; |
| 306 | /// All classes that have aliases set for them. |
| 307 | std::vector<ClassAliasPair> ClassAliases; |
| 308 | |
David Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 309 | protected: |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 310 | /// Function used for throwing Objective-C exceptions. |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 311 | LazyRuntimeFunction ExceptionThrowFn; |
James Dennett | 809d1be | 2012-06-13 22:07:09 +0000 | [diff] [blame] | 312 | /// Function used for rethrowing exceptions, used at the end of \@finally or |
| 313 | /// \@synchronize blocks. |
David Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 314 | LazyRuntimeFunction ExceptionReThrowFn; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 315 | /// Function called when entering a catch function. This is required for |
| 316 | /// differentiating Objective-C exceptions and foreign exceptions. |
David Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 317 | LazyRuntimeFunction EnterCatchFn; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 318 | /// Function called when exiting from a catch block. Used to do exception |
| 319 | /// cleanup. |
David Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 320 | LazyRuntimeFunction ExitCatchFn; |
James Dennett | 809d1be | 2012-06-13 22:07:09 +0000 | [diff] [blame] | 321 | /// Function called when entering an \@synchronize block. Acquires the lock. |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 322 | LazyRuntimeFunction SyncEnterFn; |
James Dennett | 809d1be | 2012-06-13 22:07:09 +0000 | [diff] [blame] | 323 | /// Function called when exiting an \@synchronize block. Releases the lock. |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 324 | LazyRuntimeFunction SyncExitFn; |
| 325 | |
David Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 326 | private: |
| 327 | |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 328 | /// Function called if fast enumeration detects that the collection is |
| 329 | /// modified during the update. |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 330 | LazyRuntimeFunction EnumerationMutationFn; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 331 | /// Function for implementing synthesized property getters that return an |
| 332 | /// object. |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 333 | LazyRuntimeFunction GetPropertyFn; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 334 | /// Function for implementing synthesized property setters that return an |
| 335 | /// object. |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 336 | LazyRuntimeFunction SetPropertyFn; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 337 | /// Function used for non-object declared property getters. |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 338 | LazyRuntimeFunction GetStructPropertyFn; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 339 | /// Function used for non-object declared property setters. |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 340 | LazyRuntimeFunction SetStructPropertyFn; |
| 341 | |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 342 | /// The version of the runtime that this class targets. Must match the |
| 343 | /// version in the runtime. |
David Chisnall | a212003 | 2011-05-22 22:37:08 +0000 | [diff] [blame] | 344 | int RuntimeVersion; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 345 | /// The version of the protocol class. Used to differentiate between ObjC1 |
| 346 | /// and ObjC2 protocols. Objective-C 1 protocols can not contain optional |
| 347 | /// components and can not contain declared properties. We always emit |
| 348 | /// Objective-C 2 property structures, but we have to pretend that they're |
| 349 | /// Objective-C 1 property structures when targeting the GCC runtime or it |
| 350 | /// will abort. |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 351 | const int ProtocolVersion; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 352 | private: |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 353 | /// Generates an instance variable list structure. This is a structure |
| 354 | /// containing a size and an array of structures containing instance variable |
| 355 | /// metadata. This is used purely for introspection in the fragile ABI. In |
| 356 | /// the non-fragile ABI, it's used for instance variable fixup. |
Bill Wendling | 795b100 | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 357 | llvm::Constant *GenerateIvarList(ArrayRef<llvm::Constant *> IvarNames, |
| 358 | ArrayRef<llvm::Constant *> IvarTypes, |
| 359 | ArrayRef<llvm::Constant *> IvarOffsets); |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 360 | /// Generates a method list structure. This is a structure containing a size |
| 361 | /// and an array of structures containing method metadata. |
| 362 | /// |
| 363 | /// This structure is used by both classes and categories, and contains a next |
| 364 | /// pointer allowing them to be chained together in a linked list. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 365 | llvm::Constant *GenerateMethodList(const StringRef &ClassName, |
| 366 | const StringRef &CategoryName, |
Bill Wendling | 795b100 | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 367 | ArrayRef<Selector> MethodSels, |
| 368 | ArrayRef<llvm::Constant *> MethodTypes, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 369 | bool isClassMethodList); |
James Dennett | 809d1be | 2012-06-13 22:07:09 +0000 | [diff] [blame] | 370 | /// Emits an empty protocol. This is used for \@protocol() where no protocol |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 371 | /// is found. The runtime will (hopefully) fix up the pointer to refer to the |
| 372 | /// real protocol. |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 373 | llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName); |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 374 | /// Generates a list of property metadata structures. This follows the same |
| 375 | /// pattern as method and instance variable metadata lists. |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 376 | llvm::Constant *GeneratePropertyList(const ObjCImplementationDecl *OID, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 377 | SmallVectorImpl<Selector> &InstanceMethodSels, |
| 378 | SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes); |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 379 | /// Generates a list of referenced protocols. Classes, categories, and |
| 380 | /// protocols all use this structure. |
Bill Wendling | 795b100 | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 381 | llvm::Constant *GenerateProtocolList(ArrayRef<std::string> Protocols); |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 382 | /// To ensure that all protocols are seen by the runtime, we add a category on |
| 383 | /// a class defined in the runtime, declaring no methods, but adopting the |
| 384 | /// protocols. This is a horribly ugly hack, but it allows us to collect all |
| 385 | /// of the protocols without changing the ABI. |
Dmitri Gribenko | c4a7790 | 2012-11-15 14:28:07 +0000 | [diff] [blame] | 386 | void GenerateProtocolHolderCategory(); |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 387 | /// Generates a class structure. |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 388 | llvm::Constant *GenerateClassStructure( |
| 389 | llvm::Constant *MetaClass, |
| 390 | llvm::Constant *SuperClass, |
| 391 | unsigned info, |
Chris Lattner | d002cc6 | 2008-06-26 04:47:04 +0000 | [diff] [blame] | 392 | const char *Name, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 393 | llvm::Constant *Version, |
| 394 | llvm::Constant *InstanceSize, |
| 395 | llvm::Constant *IVars, |
| 396 | llvm::Constant *Methods, |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 397 | llvm::Constant *Protocols, |
| 398 | llvm::Constant *IvarOffsets, |
David Chisnall | 8c757f9 | 2010-04-28 14:29:56 +0000 | [diff] [blame] | 399 | llvm::Constant *Properties, |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 400 | llvm::Constant *StrongIvarBitmap, |
| 401 | llvm::Constant *WeakIvarBitmap, |
David Chisnall | 8c757f9 | 2010-04-28 14:29:56 +0000 | [diff] [blame] | 402 | bool isMeta=false); |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 403 | /// Generates a method list. This is used by protocols to define the required |
| 404 | /// and optional methods. |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 405 | llvm::Constant *GenerateProtocolMethodList( |
Bill Wendling | 795b100 | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 406 | ArrayRef<llvm::Constant *> MethodNames, |
| 407 | ArrayRef<llvm::Constant *> MethodTypes); |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 408 | /// Returns a selector with the specified type encoding. An empty string is |
| 409 | /// used to return an untyped selector (with the types field set to NULL). |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 410 | llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 411 | const std::string &TypeEncoding, bool lval); |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 412 | /// Returns the variable used to store the offset of an instance variable. |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 413 | llvm::GlobalVariable *ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID, |
| 414 | const ObjCIvarDecl *Ivar); |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 415 | /// Emits a reference to a class. This allows the linker to object if there |
| 416 | /// is no class of the matching name. |
John McCall | f7226fb | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 417 | protected: |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 418 | void EmitClassRef(const std::string &className); |
David Chisnall | c7aed3b | 2011-06-29 13:16:41 +0000 | [diff] [blame] | 419 | /// Emits a pointer to the named class |
John McCall | f7226fb | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 420 | virtual llvm::Value *GetClassNamed(CGBuilderTy &Builder, |
| 421 | const std::string &Name, bool isWeak); |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 422 | /// Looks up the method for sending a message to the specified object. This |
| 423 | /// mechanism differs between the GCC and GNU runtimes, so this method must be |
| 424 | /// overridden in subclasses. |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 425 | virtual llvm::Value *LookupIMP(CodeGenFunction &CGF, |
| 426 | llvm::Value *&Receiver, |
| 427 | llvm::Value *cmd, |
| 428 | llvm::MDNode *node) = 0; |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 429 | /// Looks up the method for sending a message to a superclass. This |
| 430 | /// mechanism differs between the GCC and GNU runtimes, so this method must |
| 431 | /// be overridden in subclasses. |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 432 | virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, |
| 433 | llvm::Value *ObjCSuper, |
| 434 | llvm::Value *cmd) = 0; |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 435 | /// Libobjc2 uses a bitfield representation where small(ish) bitfields are |
| 436 | /// stored in a 64-bit value with the low bit set to 1 and the remaining 63 |
| 437 | /// bits set to their values, LSB first, while larger ones are stored in a |
| 438 | /// structure of this / form: |
| 439 | /// |
| 440 | /// struct { int32_t length; int32_t values[length]; }; |
| 441 | /// |
| 442 | /// The values in the array are stored in host-endian format, with the least |
| 443 | /// significant bit being assumed to come first in the bitfield. Therefore, |
| 444 | /// a bitfield with the 64th bit set will be (int64_t)&{ 2, [0, 1<<31] }, |
| 445 | /// while a bitfield / with the 63rd bit set will be 1<<64. |
Bill Wendling | 795b100 | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 446 | llvm::Constant *MakeBitField(ArrayRef<bool> bits); |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 447 | public: |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 448 | CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion, |
| 449 | unsigned protocolClassVersion); |
| 450 | |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 451 | virtual llvm::Constant *GenerateConstantString(const StringLiteral *); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 452 | |
| 453 | virtual RValue |
| 454 | GenerateMessageSend(CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 455 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 456 | QualType ResultType, |
| 457 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 458 | llvm::Value *Receiver, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 459 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 460 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 461 | const ObjCMethodDecl *Method); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 462 | virtual RValue |
| 463 | GenerateMessageSendSuper(CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 464 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 465 | QualType ResultType, |
| 466 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 467 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 468 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 469 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 470 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 471 | const CallArgList &CallArgs, |
| 472 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 473 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 474 | const ObjCInterfaceDecl *OID); |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 475 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 476 | bool lval = false); |
Daniel Dunbar | 6d5a1c2 | 2010-02-03 20:11:42 +0000 | [diff] [blame] | 477 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl |
| 478 | *Method); |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 479 | virtual llvm::Constant *GetEHType(QualType T); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | |
| 481 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 482 | const ObjCContainerDecl *CD); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 483 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
| 484 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
David Chisnall | 29254f4 | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 485 | virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD); |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 486 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 487 | const ObjCProtocolDecl *PD); |
| 488 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 489 | virtual llvm::Function *ModuleInitFunction(); |
David Chisnall | 789ecde | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 490 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 491 | virtual llvm::Constant *GetPropertySetFunction(); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 492 | virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic, |
| 493 | bool copy); |
David Chisnall | 789ecde | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 494 | virtual llvm::Constant *GetSetStructFunction(); |
| 495 | virtual llvm::Constant *GetGetStructFunction(); |
David Chisnall | d397cfe | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 496 | virtual llvm::Constant *GetCppAtomicObjectGetFunction(); |
| 497 | virtual llvm::Constant *GetCppAtomicObjectSetFunction(); |
Daniel Dunbar | 309a436 | 2009-07-24 07:40:24 +0000 | [diff] [blame] | 498 | virtual llvm::Constant *EnumerationMutationFunction(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 499 | |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 500 | virtual void EmitTryStmt(CodeGenFunction &CGF, |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 501 | const ObjCAtTryStmt &S); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 502 | virtual void EmitSynchronizedStmt(CodeGenFunction &CGF, |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 503 | const ObjCAtSynchronizedStmt &S); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 504 | virtual void EmitThrowStmt(CodeGenFunction &CGF, |
Fariborz Jahanian | 6a3c70e | 2013-01-10 19:02:56 +0000 | [diff] [blame^] | 505 | const ObjCAtThrowStmt &S, |
| 506 | bool ClearInsertionPoint=true); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 507 | virtual llvm::Value * EmitObjCWeakRead(CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 508 | llvm::Value *AddrWeakObj); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 509 | virtual void EmitObjCWeakAssign(CodeGenFunction &CGF, |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 510 | llvm::Value *src, llvm::Value *dst); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 511 | virtual void EmitObjCGlobalAssign(CodeGenFunction &CGF, |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 512 | llvm::Value *src, llvm::Value *dest, |
| 513 | bool threadlocal=false); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 514 | virtual void EmitObjCIvarAssign(CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 515 | llvm::Value *src, llvm::Value *dest, |
| 516 | llvm::Value *ivarOffset); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 517 | virtual void EmitObjCStrongCastAssign(CodeGenFunction &CGF, |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 518 | llvm::Value *src, llvm::Value *dest); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 519 | virtual void EmitGCMemmoveCollectable(CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 520 | llvm::Value *DestPtr, |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 521 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 522 | llvm::Value *Size); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 523 | virtual LValue EmitObjCValueForIvar(CodeGenFunction &CGF, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 524 | QualType ObjectTy, |
| 525 | llvm::Value *BaseValue, |
| 526 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 527 | unsigned CVRQualifiers); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 528 | virtual llvm::Value *EmitIvarOffset(CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 529 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 530 | const ObjCIvarDecl *Ivar); |
David Chisnall | c7aed3b | 2011-06-29 13:16:41 +0000 | [diff] [blame] | 531 | virtual llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 532 | virtual llvm::Constant *BuildGCBlockLayout(CodeGenModule &CGM, |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 533 | const CGBlockInfo &blockInfo) { |
Fariborz Jahanian | 89ecd41 | 2010-08-04 16:57:49 +0000 | [diff] [blame] | 534 | return NULLPtr; |
| 535 | } |
Fariborz Jahanian | c46b435 | 2012-10-27 21:10:38 +0000 | [diff] [blame] | 536 | virtual llvm::Constant *BuildRCBlockLayout(CodeGenModule &CGM, |
| 537 | const CGBlockInfo &blockInfo) { |
| 538 | return NULLPtr; |
| 539 | } |
Fariborz Jahanian | 3ca23d7 | 2012-11-14 17:15:51 +0000 | [diff] [blame] | 540 | |
| 541 | virtual llvm::Constant *BuildByrefLayout(CodeGenModule &CGM, |
| 542 | QualType T) { |
| 543 | return NULLPtr; |
| 544 | } |
| 545 | |
Fariborz Jahanian | 6f40e22 | 2011-05-17 22:21:16 +0000 | [diff] [blame] | 546 | virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) { |
| 547 | return 0; |
| 548 | } |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 549 | }; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 550 | /// Class representing the legacy GCC Objective-C ABI. This is the default when |
| 551 | /// -fobjc-nonfragile-abi is not specified. |
| 552 | /// |
| 553 | /// The GCC ABI target actually generates code that is approximately compatible |
| 554 | /// with the new GNUstep runtime ABI, but refrains from using any features that |
| 555 | /// would not work with the GCC runtime. For example, clang always generates |
| 556 | /// the extended form of the class structure, and the extra fields are simply |
| 557 | /// ignored by GCC libobjc. |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 558 | class CGObjCGCC : public CGObjCGNU { |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 559 | /// The GCC ABI message lookup function. Returns an IMP pointing to the |
| 560 | /// method implementation for this message. |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 561 | LazyRuntimeFunction MsgLookupFn; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 562 | /// The GCC ABI superclass message lookup function. Takes a pointer to a |
| 563 | /// structure describing the receiver and the class, and a selector as |
| 564 | /// arguments. Returns the IMP for the corresponding method. |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 565 | LazyRuntimeFunction MsgLookupSuperFn; |
| 566 | protected: |
| 567 | virtual llvm::Value *LookupIMP(CodeGenFunction &CGF, |
| 568 | llvm::Value *&Receiver, |
| 569 | llvm::Value *cmd, |
| 570 | llvm::MDNode *node) { |
| 571 | CGBuilderTy &Builder = CGF.Builder; |
David Chisnall | 6f3887e | 2011-10-28 17:55:06 +0000 | [diff] [blame] | 572 | llvm::Value *args[] = { |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 573 | EnforceType(Builder, Receiver, IdTy), |
David Chisnall | 6f3887e | 2011-10-28 17:55:06 +0000 | [diff] [blame] | 574 | EnforceType(Builder, cmd, SelectorTy) }; |
| 575 | llvm::CallSite imp = CGF.EmitCallOrInvoke(MsgLookupFn, args); |
| 576 | imp->setMetadata(msgSendMDKind, node); |
| 577 | return imp.getInstruction(); |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 578 | } |
| 579 | virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, |
| 580 | llvm::Value *ObjCSuper, |
| 581 | llvm::Value *cmd) { |
| 582 | CGBuilderTy &Builder = CGF.Builder; |
| 583 | llvm::Value *lookupArgs[] = {EnforceType(Builder, ObjCSuper, |
| 584 | PtrToObjCSuperTy), cmd}; |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 585 | return Builder.CreateCall(MsgLookupSuperFn, lookupArgs); |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 586 | } |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 587 | public: |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 588 | CGObjCGCC(CodeGenModule &Mod) : CGObjCGNU(Mod, 8, 2) { |
| 589 | // IMP objc_msg_lookup(id, SEL); |
| 590 | MsgLookupFn.init(&CGM, "objc_msg_lookup", IMPTy, IdTy, SelectorTy, NULL); |
| 591 | // IMP objc_msg_lookup_super(struct objc_super*, SEL); |
| 592 | MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy, |
| 593 | PtrToObjCSuperTy, SelectorTy, NULL); |
| 594 | } |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 595 | }; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 596 | /// Class used when targeting the new GNUstep runtime ABI. |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 597 | class CGObjCGNUstep : public CGObjCGNU { |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 598 | /// The slot lookup function. Returns a pointer to a cacheable structure |
| 599 | /// that contains (among other things) the IMP. |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 600 | LazyRuntimeFunction SlotLookupFn; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 601 | /// The GNUstep ABI superclass message lookup function. Takes a pointer to |
| 602 | /// a structure describing the receiver and the class, and a selector as |
| 603 | /// arguments. Returns the slot for the corresponding method. Superclass |
| 604 | /// message lookup rarely changes, so this is a good caching opportunity. |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 605 | LazyRuntimeFunction SlotLookupSuperFn; |
David Chisnall | d397cfe | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 606 | /// Specialised function for setting atomic retain properties |
| 607 | LazyRuntimeFunction SetPropertyAtomic; |
| 608 | /// Specialised function for setting atomic copy properties |
| 609 | LazyRuntimeFunction SetPropertyAtomicCopy; |
| 610 | /// Specialised function for setting nonatomic retain properties |
| 611 | LazyRuntimeFunction SetPropertyNonAtomic; |
| 612 | /// Specialised function for setting nonatomic copy properties |
| 613 | LazyRuntimeFunction SetPropertyNonAtomicCopy; |
| 614 | /// Function to perform atomic copies of C++ objects with nontrivial copy |
| 615 | /// constructors from Objective-C ivars. |
| 616 | LazyRuntimeFunction CxxAtomicObjectGetFn; |
| 617 | /// Function to perform atomic copies of C++ objects with nontrivial copy |
| 618 | /// constructors to Objective-C ivars. |
| 619 | LazyRuntimeFunction CxxAtomicObjectSetFn; |
David Chisnall | 81a65f5 | 2011-03-26 11:48:37 +0000 | [diff] [blame] | 620 | /// Type of an slot structure pointer. This is returned by the various |
| 621 | /// lookup functions. |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 622 | llvm::Type *SlotTy; |
John McCall | 2b07dd3 | 2012-11-14 09:08:34 +0000 | [diff] [blame] | 623 | public: |
| 624 | virtual llvm::Constant *GetEHType(QualType T); |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 625 | protected: |
| 626 | virtual llvm::Value *LookupIMP(CodeGenFunction &CGF, |
| 627 | llvm::Value *&Receiver, |
| 628 | llvm::Value *cmd, |
| 629 | llvm::MDNode *node) { |
| 630 | CGBuilderTy &Builder = CGF.Builder; |
| 631 | llvm::Function *LookupFn = SlotLookupFn; |
| 632 | |
| 633 | // Store the receiver on the stack so that we can reload it later |
| 634 | llvm::Value *ReceiverPtr = CGF.CreateTempAlloca(Receiver->getType()); |
| 635 | Builder.CreateStore(Receiver, ReceiverPtr); |
| 636 | |
| 637 | llvm::Value *self; |
| 638 | |
| 639 | if (isa<ObjCMethodDecl>(CGF.CurCodeDecl)) { |
| 640 | self = CGF.LoadObjCSelf(); |
| 641 | } else { |
| 642 | self = llvm::ConstantPointerNull::get(IdTy); |
| 643 | } |
| 644 | |
| 645 | // The lookup function is guaranteed not to capture the receiver pointer. |
| 646 | LookupFn->setDoesNotCapture(1); |
| 647 | |
David Chisnall | 6f3887e | 2011-10-28 17:55:06 +0000 | [diff] [blame] | 648 | llvm::Value *args[] = { |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 649 | EnforceType(Builder, ReceiverPtr, PtrToIdTy), |
| 650 | EnforceType(Builder, cmd, SelectorTy), |
David Chisnall | 6f3887e | 2011-10-28 17:55:06 +0000 | [diff] [blame] | 651 | EnforceType(Builder, self, IdTy) }; |
| 652 | llvm::CallSite slot = CGF.EmitCallOrInvoke(LookupFn, args); |
| 653 | slot.setOnlyReadsMemory(); |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 654 | slot->setMetadata(msgSendMDKind, node); |
| 655 | |
| 656 | // Load the imp from the slot |
David Chisnall | 6f3887e | 2011-10-28 17:55:06 +0000 | [diff] [blame] | 657 | llvm::Value *imp = |
| 658 | Builder.CreateLoad(Builder.CreateStructGEP(slot.getInstruction(), 4)); |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 659 | |
| 660 | // The lookup function may have changed the receiver, so make sure we use |
| 661 | // the new one. |
| 662 | Receiver = Builder.CreateLoad(ReceiverPtr, true); |
| 663 | return imp; |
| 664 | } |
| 665 | virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, |
| 666 | llvm::Value *ObjCSuper, |
| 667 | llvm::Value *cmd) { |
| 668 | CGBuilderTy &Builder = CGF.Builder; |
| 669 | llvm::Value *lookupArgs[] = {ObjCSuper, cmd}; |
| 670 | |
Jay Foad | 4c7d9f1 | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 671 | llvm::CallInst *slot = Builder.CreateCall(SlotLookupSuperFn, lookupArgs); |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 672 | slot->setOnlyReadsMemory(); |
| 673 | |
| 674 | return Builder.CreateLoad(Builder.CreateStructGEP(slot, 4)); |
| 675 | } |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 676 | public: |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 677 | CGObjCGNUstep(CodeGenModule &Mod) : CGObjCGNU(Mod, 9, 3) { |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 678 | llvm::StructType *SlotStructTy = llvm::StructType::get(PtrTy, |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 679 | PtrTy, PtrTy, IntTy, IMPTy, NULL); |
| 680 | SlotTy = llvm::PointerType::getUnqual(SlotStructTy); |
| 681 | // Slot_t objc_msg_lookup_sender(id *receiver, SEL selector, id sender); |
| 682 | SlotLookupFn.init(&CGM, "objc_msg_lookup_sender", SlotTy, PtrToIdTy, |
| 683 | SelectorTy, IdTy, NULL); |
| 684 | // Slot_t objc_msg_lookup_super(struct objc_super*, SEL); |
| 685 | SlotLookupSuperFn.init(&CGM, "objc_slot_lookup_super", SlotTy, |
| 686 | PtrToObjCSuperTy, SelectorTy, NULL); |
David Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 687 | // If we're in ObjC++ mode, then we want to make |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 688 | if (CGM.getLangOpts().CPlusPlus) { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 689 | llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext); |
David Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 690 | // void *__cxa_begin_catch(void *e) |
| 691 | EnterCatchFn.init(&CGM, "__cxa_begin_catch", PtrTy, PtrTy, NULL); |
| 692 | // void __cxa_end_catch(void) |
David Chisnall | 4bd5d09 | 2011-08-08 17:26:06 +0000 | [diff] [blame] | 693 | ExitCatchFn.init(&CGM, "__cxa_end_catch", VoidTy, NULL); |
David Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 694 | // void _Unwind_Resume_or_Rethrow(void*) |
David Chisnall | d397cfe | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 695 | ExceptionReThrowFn.init(&CGM, "_Unwind_Resume_or_Rethrow", VoidTy, |
| 696 | PtrTy, NULL); |
David Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 697 | } |
David Chisnall | d397cfe | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 698 | llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext); |
| 699 | SetPropertyAtomic.init(&CGM, "objc_setProperty_atomic", VoidTy, IdTy, |
| 700 | SelectorTy, IdTy, PtrDiffTy, NULL); |
| 701 | SetPropertyAtomicCopy.init(&CGM, "objc_setProperty_atomic_copy", VoidTy, |
| 702 | IdTy, SelectorTy, IdTy, PtrDiffTy, NULL); |
| 703 | SetPropertyNonAtomic.init(&CGM, "objc_setProperty_nonatomic", VoidTy, |
| 704 | IdTy, SelectorTy, IdTy, PtrDiffTy, NULL); |
| 705 | SetPropertyNonAtomicCopy.init(&CGM, "objc_setProperty_nonatomic_copy", |
| 706 | VoidTy, IdTy, SelectorTy, IdTy, PtrDiffTy, NULL); |
| 707 | // void objc_setCppObjectAtomic(void *dest, const void *src, void |
| 708 | // *helper); |
| 709 | CxxAtomicObjectSetFn.init(&CGM, "objc_setCppObjectAtomic", VoidTy, PtrTy, |
| 710 | PtrTy, PtrTy, NULL); |
| 711 | // void objc_getCppObjectAtomic(void *dest, const void *src, void |
| 712 | // *helper); |
| 713 | CxxAtomicObjectGetFn.init(&CGM, "objc_getCppObjectAtomic", VoidTy, PtrTy, |
| 714 | PtrTy, PtrTy, NULL); |
| 715 | } |
| 716 | virtual llvm::Constant *GetCppAtomicObjectGetFunction() { |
| 717 | // The optimised functions were added in version 1.7 of the GNUstep |
| 718 | // runtime. |
| 719 | assert (CGM.getLangOpts().ObjCRuntime.getVersion() >= |
| 720 | VersionTuple(1, 7)); |
| 721 | return CxxAtomicObjectGetFn; |
| 722 | } |
| 723 | virtual llvm::Constant *GetCppAtomicObjectSetFunction() { |
| 724 | // The optimised functions were added in version 1.7 of the GNUstep |
| 725 | // runtime. |
| 726 | assert (CGM.getLangOpts().ObjCRuntime.getVersion() >= |
| 727 | VersionTuple(1, 7)); |
| 728 | return CxxAtomicObjectSetFn; |
| 729 | } |
| 730 | virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic, |
| 731 | bool copy) { |
| 732 | // The optimised property functions omit the GC check, and so are not |
| 733 | // safe to use in GC mode. The standard functions are fast in GC mode, |
| 734 | // so there is less advantage in using them. |
| 735 | assert ((CGM.getLangOpts().getGC() == LangOptions::NonGC)); |
| 736 | // The optimised functions were added in version 1.7 of the GNUstep |
| 737 | // runtime. |
| 738 | assert (CGM.getLangOpts().ObjCRuntime.getVersion() >= |
| 739 | VersionTuple(1, 7)); |
| 740 | |
| 741 | if (atomic) { |
| 742 | if (copy) return SetPropertyAtomicCopy; |
| 743 | return SetPropertyAtomic; |
| 744 | } |
| 745 | if (copy) return SetPropertyNonAtomicCopy; |
| 746 | return SetPropertyNonAtomic; |
| 747 | |
| 748 | return 0; |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 749 | } |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 750 | }; |
| 751 | |
John McCall | 0a7dd78 | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 752 | /// Support for the ObjFW runtime. Support here is due to |
| 753 | /// Jonathan Schleifer <js@webkeks.org>, the ObjFW maintainer. |
| 754 | class CGObjCObjFW: public CGObjCGNU { |
| 755 | protected: |
| 756 | /// The GCC ABI message lookup function. Returns an IMP pointing to the |
| 757 | /// method implementation for this message. |
| 758 | LazyRuntimeFunction MsgLookupFn; |
| 759 | /// The GCC ABI superclass message lookup function. Takes a pointer to a |
| 760 | /// structure describing the receiver and the class, and a selector as |
| 761 | /// arguments. Returns the IMP for the corresponding method. |
| 762 | LazyRuntimeFunction MsgLookupSuperFn; |
| 763 | |
| 764 | virtual llvm::Value *LookupIMP(CodeGenFunction &CGF, |
| 765 | llvm::Value *&Receiver, |
| 766 | llvm::Value *cmd, |
| 767 | llvm::MDNode *node) { |
| 768 | CGBuilderTy &Builder = CGF.Builder; |
| 769 | llvm::Value *args[] = { |
| 770 | EnforceType(Builder, Receiver, IdTy), |
| 771 | EnforceType(Builder, cmd, SelectorTy) }; |
| 772 | llvm::CallSite imp = CGF.EmitCallOrInvoke(MsgLookupFn, args); |
| 773 | imp->setMetadata(msgSendMDKind, node); |
| 774 | return imp.getInstruction(); |
| 775 | } |
| 776 | |
| 777 | virtual llvm::Value *LookupIMPSuper(CodeGenFunction &CGF, |
| 778 | llvm::Value *ObjCSuper, |
| 779 | llvm::Value *cmd) { |
| 780 | CGBuilderTy &Builder = CGF.Builder; |
| 781 | llvm::Value *lookupArgs[] = {EnforceType(Builder, ObjCSuper, |
| 782 | PtrToObjCSuperTy), cmd}; |
| 783 | return Builder.CreateCall(MsgLookupSuperFn, lookupArgs); |
| 784 | } |
| 785 | |
John McCall | f7226fb | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 786 | virtual llvm::Value *GetClassNamed(CGBuilderTy &Builder, |
| 787 | const std::string &Name, bool isWeak) { |
| 788 | if (isWeak) |
| 789 | return CGObjCGNU::GetClassNamed(Builder, Name, isWeak); |
| 790 | |
| 791 | EmitClassRef(Name); |
| 792 | |
| 793 | std::string SymbolName = "_OBJC_CLASS_" + Name; |
| 794 | |
| 795 | llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(SymbolName); |
| 796 | |
| 797 | if (!ClassSymbol) |
| 798 | ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false, |
| 799 | llvm::GlobalValue::ExternalLinkage, |
| 800 | 0, SymbolName); |
| 801 | |
| 802 | return ClassSymbol; |
| 803 | } |
| 804 | |
| 805 | public: |
John McCall | 0a7dd78 | 2012-08-21 02:47:43 +0000 | [diff] [blame] | 806 | CGObjCObjFW(CodeGenModule &Mod): CGObjCGNU(Mod, 9, 3) { |
| 807 | // IMP objc_msg_lookup(id, SEL); |
| 808 | MsgLookupFn.init(&CGM, "objc_msg_lookup", IMPTy, IdTy, SelectorTy, NULL); |
| 809 | // IMP objc_msg_lookup_super(struct objc_super*, SEL); |
| 810 | MsgLookupSuperFn.init(&CGM, "objc_msg_lookup_super", IMPTy, |
| 811 | PtrToObjCSuperTy, SelectorTy, NULL); |
| 812 | } |
John McCall | f7226fb | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 813 | }; |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 814 | } // end anonymous namespace |
| 815 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 816 | |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 817 | /// Emits a reference to a dummy variable which is emitted with each class. |
| 818 | /// This ensures that a linker error will be generated when trying to link |
| 819 | /// together modules where a referenced class is not defined. |
Mike Stump | bb1c860 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 820 | void CGObjCGNU::EmitClassRef(const std::string &className) { |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 821 | std::string symbolRef = "__objc_class_ref_" + className; |
| 822 | // Don't emit two copies of the same symbol |
Mike Stump | bb1c860 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 823 | if (TheModule.getGlobalVariable(symbolRef)) |
| 824 | return; |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 825 | std::string symbolName = "__objc_class_name_" + className; |
| 826 | llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(symbolName); |
| 827 | if (!ClassSymbol) { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 828 | ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false, |
| 829 | llvm::GlobalValue::ExternalLinkage, 0, symbolName); |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 830 | } |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 831 | new llvm::GlobalVariable(TheModule, ClassSymbol->getType(), true, |
Chris Lattner | f35271b | 2009-08-05 05:25:18 +0000 | [diff] [blame] | 832 | llvm::GlobalValue::WeakAnyLinkage, ClassSymbol, symbolRef); |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 833 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 834 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 835 | static std::string SymbolNameForMethod(const StringRef &ClassName, |
| 836 | const StringRef &CategoryName, const Selector MethodName, |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 837 | bool isClassMethod) { |
| 838 | std::string MethodNameColonStripped = MethodName.getAsString(); |
David Chisnall | d346736 | 2010-01-14 14:08:19 +0000 | [diff] [blame] | 839 | std::replace(MethodNameColonStripped.begin(), MethodNameColonStripped.end(), |
| 840 | ':', '_'); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 841 | return (Twine(isClassMethod ? "_c_" : "_i_") + ClassName + "_" + |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 842 | CategoryName + "_" + MethodNameColonStripped).str(); |
David Chisnall | 87935a8 | 2010-05-08 20:58:05 +0000 | [diff] [blame] | 843 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 844 | |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 845 | CGObjCGNU::CGObjCGNU(CodeGenModule &cgm, unsigned runtimeABIVersion, |
| 846 | unsigned protocolClassVersion) |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 847 | : CGObjCRuntime(cgm), TheModule(CGM.getModule()), |
| 848 | VMContext(cgm.getLLVMContext()), ClassPtrAlias(0), MetaClassPtrAlias(0), |
| 849 | RuntimeVersion(runtimeABIVersion), ProtocolVersion(protocolClassVersion) { |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 850 | |
| 851 | msgSendMDKind = VMContext.getMDKindID("GNUObjCMessageSend"); |
| 852 | |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 853 | CodeGenTypes &Types = CGM.getTypes(); |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 854 | IntTy = cast<llvm::IntegerType>( |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 855 | Types.ConvertType(CGM.getContext().IntTy)); |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 856 | LongTy = cast<llvm::IntegerType>( |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 857 | Types.ConvertType(CGM.getContext().LongTy)); |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 858 | SizeTy = cast<llvm::IntegerType>( |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 859 | Types.ConvertType(CGM.getContext().getSizeType())); |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 860 | PtrDiffTy = cast<llvm::IntegerType>( |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 861 | Types.ConvertType(CGM.getContext().getPointerDiffType())); |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 862 | BoolTy = CGM.getTypes().ConvertType(CGM.getContext().BoolTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 864 | Int8Ty = llvm::Type::getInt8Ty(VMContext); |
| 865 | // C string type. Used in lots of places. |
| 866 | PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty); |
| 867 | |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 868 | Zeros[0] = llvm::ConstantInt::get(LongTy, 0); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 869 | Zeros[1] = Zeros[0]; |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 870 | NULLPtr = llvm::ConstantPointerNull::get(PtrToInt8Ty); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 871 | // Get the selector Type. |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 872 | QualType selTy = CGM.getContext().getObjCSelType(); |
| 873 | if (QualType() == selTy) { |
| 874 | SelectorTy = PtrToInt8Ty; |
| 875 | } else { |
| 876 | SelectorTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(selTy)); |
| 877 | } |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 878 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 879 | PtrToIntTy = llvm::PointerType::getUnqual(IntTy); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 880 | PtrTy = PtrToInt8Ty; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 881 | |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 882 | Int32Ty = llvm::Type::getInt32Ty(VMContext); |
| 883 | Int64Ty = llvm::Type::getInt64Ty(VMContext); |
| 884 | |
David Chisnall | 49de528 | 2011-10-08 08:54:36 +0000 | [diff] [blame] | 885 | IntPtrTy = |
| 886 | TheModule.getPointerSize() == llvm::Module::Pointer32 ? Int32Ty : Int64Ty; |
| 887 | |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 888 | // Object type |
David Chisnall | 7bcf6c3 | 2011-04-29 14:10:35 +0000 | [diff] [blame] | 889 | QualType UnqualIdTy = CGM.getContext().getObjCIdType(); |
| 890 | ASTIdTy = CanQualType(); |
| 891 | if (UnqualIdTy != QualType()) { |
| 892 | ASTIdTy = CGM.getContext().getCanonicalType(UnqualIdTy); |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 893 | IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy)); |
David Chisnall | 7bcf6c3 | 2011-04-29 14:10:35 +0000 | [diff] [blame] | 894 | } else { |
| 895 | IdTy = PtrToInt8Ty; |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 896 | } |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 897 | PtrToIdTy = llvm::PointerType::getUnqual(IdTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 898 | |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 899 | ObjCSuperTy = llvm::StructType::get(IdTy, IdTy, NULL); |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 900 | PtrToObjCSuperTy = llvm::PointerType::getUnqual(ObjCSuperTy); |
| 901 | |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 902 | llvm::Type *VoidTy = llvm::Type::getVoidTy(VMContext); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 903 | |
| 904 | // void objc_exception_throw(id); |
| 905 | ExceptionThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy, NULL); |
David Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 906 | ExceptionReThrowFn.init(&CGM, "objc_exception_throw", VoidTy, IdTy, NULL); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 907 | // int objc_sync_enter(id); |
| 908 | SyncEnterFn.init(&CGM, "objc_sync_enter", IntTy, IdTy, NULL); |
| 909 | // int objc_sync_exit(id); |
| 910 | SyncExitFn.init(&CGM, "objc_sync_exit", IntTy, IdTy, NULL); |
| 911 | |
| 912 | // void objc_enumerationMutation (id) |
| 913 | EnumerationMutationFn.init(&CGM, "objc_enumerationMutation", VoidTy, |
| 914 | IdTy, NULL); |
| 915 | |
| 916 | // id objc_getProperty(id, SEL, ptrdiff_t, BOOL) |
| 917 | GetPropertyFn.init(&CGM, "objc_getProperty", IdTy, IdTy, SelectorTy, |
| 918 | PtrDiffTy, BoolTy, NULL); |
| 919 | // void objc_setProperty(id, SEL, ptrdiff_t, id, BOOL, BOOL) |
| 920 | SetPropertyFn.init(&CGM, "objc_setProperty", VoidTy, IdTy, SelectorTy, |
| 921 | PtrDiffTy, IdTy, BoolTy, BoolTy, NULL); |
| 922 | // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL) |
| 923 | GetStructPropertyFn.init(&CGM, "objc_getPropertyStruct", VoidTy, PtrTy, PtrTy, |
| 924 | PtrDiffTy, BoolTy, BoolTy, NULL); |
| 925 | // void objc_setPropertyStruct(void*, void*, ptrdiff_t, BOOL, BOOL) |
| 926 | SetStructPropertyFn.init(&CGM, "objc_setPropertyStruct", VoidTy, PtrTy, PtrTy, |
| 927 | PtrDiffTy, BoolTy, BoolTy, NULL); |
| 928 | |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 929 | // IMP type |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 930 | llvm::Type *IMPArgs[] = { IdTy, SelectorTy }; |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 931 | IMPTy = llvm::PointerType::getUnqual(llvm::FunctionType::get(IdTy, IMPArgs, |
| 932 | true)); |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 933 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 934 | const LangOptions &Opts = CGM.getLangOpts(); |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 935 | if ((Opts.getGC() != LangOptions::NonGC) || Opts.ObjCAutoRefCount) |
David Chisnall | f074885 | 2011-07-07 11:22:31 +0000 | [diff] [blame] | 936 | RuntimeVersion = 10; |
| 937 | |
David Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 938 | // Don't bother initialising the GC stuff unless we're compiling in GC mode |
Douglas Gregor | e289d81 | 2011-09-13 17:21:33 +0000 | [diff] [blame] | 939 | if (Opts.getGC() != LangOptions::NonGC) { |
David Chisnall | a212003 | 2011-05-22 22:37:08 +0000 | [diff] [blame] | 940 | // This is a bit of an hack. We should sort this out by having a proper |
| 941 | // CGObjCGNUstep subclass for GC, but we may want to really support the old |
| 942 | // ABI and GC added in ObjectiveC2.framework, so we fudge it a bit for now |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 943 | // Get selectors needed in GC mode |
| 944 | RetainSel = GetNullarySelector("retain", CGM.getContext()); |
| 945 | ReleaseSel = GetNullarySelector("release", CGM.getContext()); |
| 946 | AutoreleaseSel = GetNullarySelector("autorelease", CGM.getContext()); |
| 947 | |
| 948 | // Get functions needed in GC mode |
| 949 | |
| 950 | // id objc_assign_ivar(id, id, ptrdiff_t); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 951 | IvarAssignFn.init(&CGM, "objc_assign_ivar", IdTy, IdTy, IdTy, PtrDiffTy, |
| 952 | NULL); |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 953 | // id objc_assign_strongCast (id, id*) |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 954 | StrongCastAssignFn.init(&CGM, "objc_assign_strongCast", IdTy, IdTy, |
| 955 | PtrToIdTy, NULL); |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 956 | // id objc_assign_global(id, id*); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 957 | GlobalAssignFn.init(&CGM, "objc_assign_global", IdTy, IdTy, PtrToIdTy, |
| 958 | NULL); |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 959 | // id objc_assign_weak(id, id*); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 960 | WeakAssignFn.init(&CGM, "objc_assign_weak", IdTy, IdTy, PtrToIdTy, NULL); |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 961 | // id objc_read_weak(id*); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 962 | WeakReadFn.init(&CGM, "objc_read_weak", IdTy, PtrToIdTy, NULL); |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 963 | // void *objc_memmove_collectable(void*, void *, size_t); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 964 | MemMoveFn.init(&CGM, "objc_memmove_collectable", PtrTy, PtrTy, PtrTy, |
| 965 | SizeTy, NULL); |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 966 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 967 | } |
Mike Stump | bb1c860 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 968 | |
David Chisnall | c7aed3b | 2011-06-29 13:16:41 +0000 | [diff] [blame] | 969 | llvm::Value *CGObjCGNU::GetClassNamed(CGBuilderTy &Builder, |
David Chisnall | d3fc729 | 2011-06-30 10:14:37 +0000 | [diff] [blame] | 970 | const std::string &Name, |
| 971 | bool isWeak) { |
David Chisnall | c7aed3b | 2011-06-29 13:16:41 +0000 | [diff] [blame] | 972 | llvm::Value *ClassName = CGM.GetAddrOfConstantCString(Name); |
David Chisnall | 41d63ed | 2010-01-08 00:14:31 +0000 | [diff] [blame] | 973 | // With the incompatible ABI, this will need to be replaced with a direct |
| 974 | // reference to the class symbol. For the compatible nonfragile ABI we are |
| 975 | // still performing this lookup at run time but emitting the symbol for the |
| 976 | // class externally so that we can make the switch later. |
David Chisnall | c7aed3b | 2011-06-29 13:16:41 +0000 | [diff] [blame] | 977 | // |
| 978 | // Libobjc2 contains an LLVM pass that replaces calls to objc_lookup_class |
| 979 | // with memoized versions or with static references if it's safe to do so. |
David Chisnall | d3fc729 | 2011-06-30 10:14:37 +0000 | [diff] [blame] | 980 | if (!isWeak) |
| 981 | EmitClassRef(Name); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 982 | ClassName = Builder.CreateStructGEP(ClassName, 0); |
| 983 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 984 | llvm::Constant *ClassLookupFn = |
Jay Foad | da549e8 | 2011-07-29 13:56:53 +0000 | [diff] [blame] | 985 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, PtrToInt8Ty, true), |
Fariborz Jahanian | 26c8294 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 986 | "objc_lookup_class"); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 987 | return Builder.CreateCall(ClassLookupFn, ClassName); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 988 | } |
| 989 | |
David Chisnall | c7aed3b | 2011-06-29 13:16:41 +0000 | [diff] [blame] | 990 | // This has to perform the lookup every time, since posing and related |
| 991 | // techniques can modify the name -> class mapping. |
| 992 | llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder, |
| 993 | const ObjCInterfaceDecl *OID) { |
David Chisnall | d3fc729 | 2011-06-30 10:14:37 +0000 | [diff] [blame] | 994 | return GetClassNamed(Builder, OID->getNameAsString(), OID->isWeakImported()); |
David Chisnall | c7aed3b | 2011-06-29 13:16:41 +0000 | [diff] [blame] | 995 | } |
| 996 | llvm::Value *CGObjCGNU::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) { |
David Chisnall | d3fc729 | 2011-06-30 10:14:37 +0000 | [diff] [blame] | 997 | return GetClassNamed(Builder, "NSAutoreleasePool", false); |
David Chisnall | c7aed3b | 2011-06-29 13:16:41 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Fariborz Jahanian | 03b2960 | 2010-06-17 19:56:20 +0000 | [diff] [blame] | 1000 | llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel, |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1001 | const std::string &TypeEncoding, bool lval) { |
| 1002 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1003 | SmallVector<TypedSelector, 2> &Types = SelectorTable[Sel]; |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1004 | llvm::GlobalAlias *SelValue = 0; |
| 1005 | |
| 1006 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1007 | for (SmallVectorImpl<TypedSelector>::iterator i = Types.begin(), |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1008 | e = Types.end() ; i!=e ; i++) { |
| 1009 | if (i->first == TypeEncoding) { |
| 1010 | SelValue = i->second; |
| 1011 | break; |
| 1012 | } |
| 1013 | } |
| 1014 | if (0 == SelValue) { |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1015 | SelValue = new llvm::GlobalAlias(SelectorTy, |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1016 | llvm::GlobalValue::PrivateLinkage, |
| 1017 | ".objc_selector_"+Sel.getAsString(), NULL, |
| 1018 | &TheModule); |
| 1019 | Types.push_back(TypedSelector(TypeEncoding, SelValue)); |
| 1020 | } |
| 1021 | |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1022 | if (lval) { |
| 1023 | llvm::Value *tmp = Builder.CreateAlloca(SelValue->getType()); |
| 1024 | Builder.CreateStore(SelValue, tmp); |
| 1025 | return tmp; |
| 1026 | } |
| 1027 | return SelValue; |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel, |
| 1031 | bool lval) { |
| 1032 | return GetSelector(Builder, Sel, std::string(), lval); |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Daniel Dunbar | 6d5a1c2 | 2010-02-03 20:11:42 +0000 | [diff] [blame] | 1035 | llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1036 | *Method) { |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1037 | std::string SelTypes; |
| 1038 | CGM.getContext().getObjCEncodingForMethodDecl(Method, SelTypes); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1039 | return GetSelector(Builder, Method->getSelector(), SelTypes, false); |
Chris Lattner | 8e67b63 | 2008-06-26 04:37:12 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Fariborz Jahanian | cf5abc7 | 2011-06-23 19:00:08 +0000 | [diff] [blame] | 1042 | llvm::Constant *CGObjCGNU::GetEHType(QualType T) { |
John McCall | 2b07dd3 | 2012-11-14 09:08:34 +0000 | [diff] [blame] | 1043 | if (T->isObjCIdType() || T->isObjCQualifiedIdType()) { |
| 1044 | // With the old ABI, there was only one kind of catchall, which broke |
| 1045 | // foreign exceptions. With the new ABI, we use __objc_id_typeinfo as |
| 1046 | // a pointer indicating object catchalls, and NULL to indicate real |
| 1047 | // catchalls |
| 1048 | if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) { |
| 1049 | return MakeConstantString("@id"); |
| 1050 | } else { |
| 1051 | return 0; |
| 1052 | } |
David Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 1053 | } |
John McCall | 2b07dd3 | 2012-11-14 09:08:34 +0000 | [diff] [blame] | 1054 | |
| 1055 | // All other types should be Objective-C interface pointer types. |
| 1056 | const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>(); |
| 1057 | assert(OPT && "Invalid @catch type."); |
| 1058 | const ObjCInterfaceDecl *IDecl = OPT->getObjectType()->getInterface(); |
| 1059 | assert(IDecl && "Invalid @catch type."); |
| 1060 | return MakeConstantString(IDecl->getIdentifier()->getName()); |
| 1061 | } |
| 1062 | |
| 1063 | llvm::Constant *CGObjCGNUstep::GetEHType(QualType T) { |
| 1064 | if (!CGM.getLangOpts().CPlusPlus) |
| 1065 | return CGObjCGNU::GetEHType(T); |
| 1066 | |
David Chisnall | 80558d2 | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 1067 | // For Objective-C++, we want to provide the ability to catch both C++ and |
| 1068 | // Objective-C objects in the same function. |
| 1069 | |
| 1070 | // There's a particular fixed type info for 'id'. |
| 1071 | if (T->isObjCIdType() || |
| 1072 | T->isObjCQualifiedIdType()) { |
| 1073 | llvm::Constant *IDEHType = |
| 1074 | CGM.getModule().getGlobalVariable("__objc_id_type_info"); |
| 1075 | if (!IDEHType) |
| 1076 | IDEHType = |
| 1077 | new llvm::GlobalVariable(CGM.getModule(), PtrToInt8Ty, |
| 1078 | false, |
| 1079 | llvm::GlobalValue::ExternalLinkage, |
| 1080 | 0, "__objc_id_type_info"); |
| 1081 | return llvm::ConstantExpr::getBitCast(IDEHType, PtrToInt8Ty); |
| 1082 | } |
| 1083 | |
| 1084 | const ObjCObjectPointerType *PT = |
| 1085 | T->getAs<ObjCObjectPointerType>(); |
| 1086 | assert(PT && "Invalid @catch type."); |
| 1087 | const ObjCInterfaceType *IT = PT->getInterfaceType(); |
| 1088 | assert(IT && "Invalid @catch type."); |
| 1089 | std::string className = IT->getDecl()->getIdentifier()->getName(); |
| 1090 | |
| 1091 | std::string typeinfoName = "__objc_eh_typeinfo_" + className; |
| 1092 | |
| 1093 | // Return the existing typeinfo if it exists |
| 1094 | llvm::Constant *typeinfo = TheModule.getGlobalVariable(typeinfoName); |
David Chisnall | acd76fe | 2012-03-20 16:25:52 +0000 | [diff] [blame] | 1095 | if (typeinfo) |
| 1096 | return llvm::ConstantExpr::getBitCast(typeinfo, PtrToInt8Ty); |
David Chisnall | 80558d2 | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 1097 | |
| 1098 | // Otherwise create it. |
| 1099 | |
| 1100 | // vtable for gnustep::libobjc::__objc_class_type_info |
| 1101 | // It's quite ugly hard-coding this. Ideally we'd generate it using the host |
| 1102 | // platform's name mangling. |
| 1103 | const char *vtableName = "_ZTVN7gnustep7libobjc22__objc_class_type_infoE"; |
| 1104 | llvm::Constant *Vtable = TheModule.getGlobalVariable(vtableName); |
| 1105 | if (!Vtable) { |
| 1106 | Vtable = new llvm::GlobalVariable(TheModule, PtrToInt8Ty, true, |
| 1107 | llvm::GlobalValue::ExternalLinkage, 0, vtableName); |
| 1108 | } |
| 1109 | llvm::Constant *Two = llvm::ConstantInt::get(IntTy, 2); |
Jay Foad | a5c0434 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 1110 | Vtable = llvm::ConstantExpr::getGetElementPtr(Vtable, Two); |
David Chisnall | 80558d2 | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 1111 | Vtable = llvm::ConstantExpr::getBitCast(Vtable, PtrToInt8Ty); |
| 1112 | |
| 1113 | llvm::Constant *typeName = |
| 1114 | ExportUniqueString(className, "__objc_eh_typename_"); |
| 1115 | |
| 1116 | std::vector<llvm::Constant*> fields; |
| 1117 | fields.push_back(Vtable); |
| 1118 | fields.push_back(typeName); |
| 1119 | llvm::Constant *TI = |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1120 | MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, |
David Chisnall | 80558d2 | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 1121 | NULL), fields, "__objc_eh_typeinfo_" + className, |
| 1122 | llvm::GlobalValue::LinkOnceODRLinkage); |
| 1123 | return llvm::ConstantExpr::getBitCast(TI, PtrToInt8Ty); |
John McCall | 5a18039 | 2010-07-24 00:37:23 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1126 | /// Generate an NSConstantString object. |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 1127 | llvm::Constant *CGObjCGNU::GenerateConstantString(const StringLiteral *SL) { |
David Chisnall | 48272a0 | 2010-01-27 12:49:23 +0000 | [diff] [blame] | 1128 | |
Benjamin Kramer | 2f4eaef | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 1129 | std::string Str = SL->getString().str(); |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 1130 | |
David Chisnall | 48272a0 | 2010-01-27 12:49:23 +0000 | [diff] [blame] | 1131 | // Look for an existing one |
| 1132 | llvm::StringMap<llvm::Constant*>::iterator old = ObjCStrings.find(Str); |
| 1133 | if (old != ObjCStrings.end()) |
| 1134 | return old->getValue(); |
| 1135 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1136 | StringRef StringClass = CGM.getLangOpts().ObjCConstantStringClass; |
David Chisnall | 13df6f6 | 2012-01-04 12:02:13 +0000 | [diff] [blame] | 1137 | |
| 1138 | if (StringClass.empty()) StringClass = "NXConstantString"; |
| 1139 | |
| 1140 | std::string Sym = "_OBJC_CLASS_"; |
| 1141 | Sym += StringClass; |
| 1142 | |
| 1143 | llvm::Constant *isa = TheModule.getNamedGlobal(Sym); |
| 1144 | |
| 1145 | if (!isa) |
| 1146 | isa = new llvm::GlobalVariable(TheModule, IdTy, /* isConstant */false, |
| 1147 | llvm::GlobalValue::ExternalWeakLinkage, 0, Sym); |
| 1148 | else if (isa->getType() != PtrToIdTy) |
| 1149 | isa = llvm::ConstantExpr::getBitCast(isa, PtrToIdTy); |
| 1150 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1151 | std::vector<llvm::Constant*> Ivars; |
David Chisnall | 13df6f6 | 2012-01-04 12:02:13 +0000 | [diff] [blame] | 1152 | Ivars.push_back(isa); |
Chris Lattner | 13fd7e5 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 1153 | Ivars.push_back(MakeConstantString(Str)); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1154 | Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size())); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1155 | llvm::Constant *ObjCStr = MakeGlobal( |
David Chisnall | 13df6f6 | 2012-01-04 12:02:13 +0000 | [diff] [blame] | 1156 | llvm::StructType::get(PtrToIdTy, PtrToInt8Ty, IntTy, NULL), |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1157 | Ivars, ".objc_str"); |
David Chisnall | 48272a0 | 2010-01-27 12:49:23 +0000 | [diff] [blame] | 1158 | ObjCStr = llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty); |
| 1159 | ObjCStrings[Str] = ObjCStr; |
| 1160 | ConstantStrings.push_back(ObjCStr); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1161 | return ObjCStr; |
| 1162 | } |
| 1163 | |
| 1164 | ///Generates a message send where the super is the receiver. This is a message |
| 1165 | ///send to self with special delivery semantics indicating which class's method |
| 1166 | ///should be called. |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1167 | RValue |
| 1168 | CGObjCGNU::GenerateMessageSendSuper(CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1169 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1170 | QualType ResultType, |
| 1171 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1172 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1173 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1174 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1175 | bool IsClassMessage, |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 1176 | const CallArgList &CallArgs, |
| 1177 | const ObjCMethodDecl *Method) { |
David Chisnall | 0bbe0cf | 2011-05-28 14:09:01 +0000 | [diff] [blame] | 1178 | CGBuilderTy &Builder = CGF.Builder; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1179 | if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) { |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 1180 | if (Sel == RetainSel || Sel == AutoreleaseSel) { |
David Chisnall | 0bbe0cf | 2011-05-28 14:09:01 +0000 | [diff] [blame] | 1181 | return RValue::get(EnforceType(Builder, Receiver, |
| 1182 | CGM.getTypes().ConvertType(ResultType))); |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 1183 | } |
| 1184 | if (Sel == ReleaseSel) { |
| 1185 | return RValue::get(0); |
| 1186 | } |
| 1187 | } |
David Chisnall | db83194 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 1188 | |
David Chisnall | db83194 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 1189 | llvm::Value *cmd = GetSelector(Builder, Sel); |
| 1190 | |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 1191 | |
| 1192 | CallArgList ActualArgs; |
| 1193 | |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1194 | ActualArgs.add(RValue::get(EnforceType(Builder, Receiver, IdTy)), ASTIdTy); |
| 1195 | ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1196 | ActualArgs.addFrom(CallArgs); |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 1197 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1198 | MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs); |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 1199 | |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1200 | llvm::Value *ReceiverClass = 0; |
Chris Lattner | 48e6e7e | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 1201 | if (isCategoryImpl) { |
| 1202 | llvm::Constant *classLookupFunction = 0; |
Chris Lattner | 48e6e7e | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 1203 | if (IsClassMessage) { |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1204 | classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get( |
Jay Foad | da549e8 | 2011-07-29 13:56:53 +0000 | [diff] [blame] | 1205 | IdTy, PtrTy, true), "objc_get_meta_class"); |
Chris Lattner | 48e6e7e | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 1206 | } else { |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1207 | classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get( |
Jay Foad | da549e8 | 2011-07-29 13:56:53 +0000 | [diff] [blame] | 1208 | IdTy, PtrTy, true), "objc_get_class"); |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1209 | } |
David Chisnall | db83194 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 1210 | ReceiverClass = Builder.CreateCall(classLookupFunction, |
Chris Lattner | 48e6e7e | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 1211 | MakeConstantString(Class->getNameAsString())); |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1212 | } else { |
Chris Lattner | 48e6e7e | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 1213 | // Set up global aliases for the metaclass or class pointer if they do not |
| 1214 | // already exist. These will are forward-references which will be set to |
Mike Stump | bb1c860 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 1215 | // pointers to the class and metaclass structure created for the runtime |
| 1216 | // load function. To send a message to super, we look up the value of the |
Chris Lattner | 48e6e7e | 2009-05-08 15:39:58 +0000 | [diff] [blame] | 1217 | // super_class pointer from either the class or metaclass structure. |
| 1218 | if (IsClassMessage) { |
| 1219 | if (!MetaClassPtrAlias) { |
| 1220 | MetaClassPtrAlias = new llvm::GlobalAlias(IdTy, |
| 1221 | llvm::GlobalValue::InternalLinkage, ".objc_metaclass_ref" + |
| 1222 | Class->getNameAsString(), NULL, &TheModule); |
| 1223 | } |
| 1224 | ReceiverClass = MetaClassPtrAlias; |
| 1225 | } else { |
| 1226 | if (!ClassPtrAlias) { |
| 1227 | ClassPtrAlias = new llvm::GlobalAlias(IdTy, |
| 1228 | llvm::GlobalValue::InternalLinkage, ".objc_class_ref" + |
| 1229 | Class->getNameAsString(), NULL, &TheModule); |
| 1230 | } |
| 1231 | ReceiverClass = ClassPtrAlias; |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1232 | } |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 1233 | } |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1234 | // Cast the pointer to a simplified version of the class structure |
David Chisnall | db83194 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 1235 | ReceiverClass = Builder.CreateBitCast(ReceiverClass, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1236 | llvm::PointerType::getUnqual( |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1237 | llvm::StructType::get(IdTy, IdTy, NULL))); |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1238 | // Get the superclass pointer |
David Chisnall | db83194 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 1239 | ReceiverClass = Builder.CreateStructGEP(ReceiverClass, 1); |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 1240 | // Load the superclass pointer |
David Chisnall | db83194 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 1241 | ReceiverClass = Builder.CreateLoad(ReceiverClass); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1242 | // Construct the structure used to look up the IMP |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1243 | llvm::StructType *ObjCSuperTy = llvm::StructType::get( |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1244 | Receiver->getType(), IdTy, NULL); |
David Chisnall | db83194 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 1245 | llvm::Value *ObjCSuper = Builder.CreateAlloca(ObjCSuperTy); |
Fariborz Jahanian | b3716ef | 2009-02-04 20:31:19 +0000 | [diff] [blame] | 1246 | |
David Chisnall | db83194 | 2010-05-01 12:37:16 +0000 | [diff] [blame] | 1247 | Builder.CreateStore(Receiver, Builder.CreateStructGEP(ObjCSuper, 0)); |
| 1248 | Builder.CreateStore(ReceiverClass, Builder.CreateStructGEP(ObjCSuper, 1)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1249 | |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1250 | ObjCSuper = EnforceType(Builder, ObjCSuper, PtrToObjCSuperTy); |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1251 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1252 | // Get the IMP |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1253 | llvm::Value *imp = LookupIMPSuper(CGF, ObjCSuper, cmd); |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1254 | imp = EnforceType(Builder, imp, MSI.MessengerType); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1255 | |
David Chisnall | dd5c98f | 2010-05-01 11:15:56 +0000 | [diff] [blame] | 1256 | llvm::Value *impMD[] = { |
| 1257 | llvm::MDString::get(VMContext, Sel.getAsString()), |
| 1258 | llvm::MDString::get(VMContext, Class->getSuperClass()->getNameAsString()), |
| 1259 | llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), IsClassMessage) |
| 1260 | }; |
Jay Foad | 6f14165 | 2011-04-21 19:59:12 +0000 | [diff] [blame] | 1261 | llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD); |
David Chisnall | dd5c98f | 2010-05-01 11:15:56 +0000 | [diff] [blame] | 1262 | |
David Chisnall | 4b02afc | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 1263 | llvm::Instruction *call; |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1264 | RValue msgRet = CGF.EmitCall(MSI.CallInfo, imp, Return, ActualArgs, 0, &call); |
David Chisnall | 4b02afc | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 1265 | call->setMetadata(msgSendMDKind, node); |
| 1266 | return msgRet; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1269 | /// Generate code for a message send expression. |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1270 | RValue |
| 1271 | CGObjCGNU::GenerateMessageSend(CodeGenFunction &CGF, |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1272 | ReturnValueSlot Return, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1273 | QualType ResultType, |
| 1274 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1275 | llvm::Value *Receiver, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1276 | const CallArgList &CallArgs, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1277 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1278 | const ObjCMethodDecl *Method) { |
David Chisnall | 0bbe0cf | 2011-05-28 14:09:01 +0000 | [diff] [blame] | 1279 | CGBuilderTy &Builder = CGF.Builder; |
| 1280 | |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1281 | // Strip out message sends to retain / release in GC mode |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1282 | if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) { |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 1283 | if (Sel == RetainSel || Sel == AutoreleaseSel) { |
David Chisnall | 0bbe0cf | 2011-05-28 14:09:01 +0000 | [diff] [blame] | 1284 | return RValue::get(EnforceType(Builder, Receiver, |
| 1285 | CGM.getTypes().ConvertType(ResultType))); |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 1286 | } |
| 1287 | if (Sel == ReleaseSel) { |
| 1288 | return RValue::get(0); |
| 1289 | } |
| 1290 | } |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1291 | |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1292 | // If the return type is something that goes in an integer register, the |
| 1293 | // runtime will handle 0 returns. For other cases, we fill in the 0 value |
| 1294 | // ourselves. |
| 1295 | // |
| 1296 | // The language spec says the result of this kind of message send is |
| 1297 | // undefined, but lots of people seem to have forgotten to read that |
| 1298 | // paragraph and insist on sending messages to nil that have structure |
| 1299 | // returns. With GCC, this generates a random return value (whatever happens |
| 1300 | // to be on the stack / in those registers at the time) on most platforms, |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1301 | // and generates an illegal instruction trap on SPARC. With LLVM it corrupts |
| 1302 | // the stack. |
| 1303 | bool isPointerSizedReturn = (ResultType->isAnyPointerType() || |
| 1304 | ResultType->isIntegralOrEnumerationType() || ResultType->isVoidType()); |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1305 | |
| 1306 | llvm::BasicBlock *startBB = 0; |
| 1307 | llvm::BasicBlock *messageBB = 0; |
David Chisnall | a54da05 | 2010-05-20 13:45:48 +0000 | [diff] [blame] | 1308 | llvm::BasicBlock *continueBB = 0; |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1309 | |
| 1310 | if (!isPointerSizedReturn) { |
| 1311 | startBB = Builder.GetInsertBlock(); |
| 1312 | messageBB = CGF.createBasicBlock("msgSend"); |
David Chisnall | a54da05 | 2010-05-20 13:45:48 +0000 | [diff] [blame] | 1313 | continueBB = CGF.createBasicBlock("continue"); |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1314 | |
| 1315 | llvm::Value *isNil = Builder.CreateICmpEQ(Receiver, |
| 1316 | llvm::Constant::getNullValue(Receiver->getType())); |
David Chisnall | a54da05 | 2010-05-20 13:45:48 +0000 | [diff] [blame] | 1317 | Builder.CreateCondBr(isNil, continueBB, messageBB); |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1318 | CGF.EmitBlock(messageBB); |
| 1319 | } |
| 1320 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 1321 | IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy)); |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1322 | llvm::Value *cmd; |
| 1323 | if (Method) |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1324 | cmd = GetSelector(Builder, Method); |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1325 | else |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1326 | cmd = GetSelector(Builder, Sel); |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1327 | cmd = EnforceType(Builder, cmd, SelectorTy); |
| 1328 | Receiver = EnforceType(Builder, Receiver, IdTy); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1329 | |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1330 | llvm::Value *impMD[] = { |
| 1331 | llvm::MDString::get(VMContext, Sel.getAsString()), |
| 1332 | llvm::MDString::get(VMContext, Class ? Class->getNameAsString() :""), |
| 1333 | llvm::ConstantInt::get(llvm::Type::getInt1Ty(VMContext), Class!=0) |
| 1334 | }; |
Jay Foad | 6f14165 | 2011-04-21 19:59:12 +0000 | [diff] [blame] | 1335 | llvm::MDNode *node = llvm::MDNode::get(VMContext, impMD); |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1336 | |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1337 | CallArgList ActualArgs; |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1338 | ActualArgs.add(RValue::get(Receiver), ASTIdTy); |
| 1339 | ActualArgs.add(RValue::get(cmd), CGF.getContext().getObjCSelType()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1340 | ActualArgs.addFrom(CallArgs); |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1341 | |
| 1342 | MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs); |
| 1343 | |
David Chisnall | 89c3004 | 2011-10-24 14:07:03 +0000 | [diff] [blame] | 1344 | // Get the IMP to call |
| 1345 | llvm::Value *imp; |
| 1346 | |
| 1347 | // If we have non-legacy dispatch specified, we try using the objc_msgSend() |
| 1348 | // functions. These are not supported on all platforms (or all runtimes on a |
| 1349 | // given platform), so we |
| 1350 | switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) { |
David Chisnall | 89c3004 | 2011-10-24 14:07:03 +0000 | [diff] [blame] | 1351 | case CodeGenOptions::Legacy: |
David Chisnall | 89c3004 | 2011-10-24 14:07:03 +0000 | [diff] [blame] | 1352 | imp = LookupIMP(CGF, Receiver, cmd, node); |
| 1353 | break; |
| 1354 | case CodeGenOptions::Mixed: |
David Chisnall | 89c3004 | 2011-10-24 14:07:03 +0000 | [diff] [blame] | 1355 | case CodeGenOptions::NonLegacy: |
David Chisnall | 6f3887e | 2011-10-28 17:55:06 +0000 | [diff] [blame] | 1356 | if (CGM.ReturnTypeUsesFPRet(ResultType)) { |
| 1357 | imp = CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true), |
| 1358 | "objc_msgSend_fpret"); |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1359 | } else if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) { |
David Chisnall | 89c3004 | 2011-10-24 14:07:03 +0000 | [diff] [blame] | 1360 | // The actual types here don't matter - we're going to bitcast the |
| 1361 | // function anyway |
| 1362 | imp = CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true), |
| 1363 | "objc_msgSend_stret"); |
| 1364 | } else { |
| 1365 | imp = CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy, IdTy, true), |
| 1366 | "objc_msgSend"); |
| 1367 | } |
| 1368 | } |
| 1369 | |
David Chisnall | 403bc3f | 2011-12-01 18:40:09 +0000 | [diff] [blame] | 1370 | // Reset the receiver in case the lookup modified it |
| 1371 | ActualArgs[0] = CallArg(RValue::get(Receiver), ASTIdTy, false); |
David Chisnall | 89c3004 | 2011-10-24 14:07:03 +0000 | [diff] [blame] | 1372 | |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1373 | imp = EnforceType(Builder, imp, MSI.MessengerType); |
David Chisnall | 63e742b | 2010-05-01 12:56:56 +0000 | [diff] [blame] | 1374 | |
David Chisnall | 4b02afc | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 1375 | llvm::Instruction *call; |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 1376 | RValue msgRet = CGF.EmitCall(MSI.CallInfo, imp, Return, ActualArgs, |
David Chisnall | 4b02afc | 2010-05-02 13:41:58 +0000 | [diff] [blame] | 1377 | 0, &call); |
| 1378 | call->setMetadata(msgSendMDKind, node); |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1379 | |
David Chisnall | a54da05 | 2010-05-20 13:45:48 +0000 | [diff] [blame] | 1380 | |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1381 | if (!isPointerSizedReturn) { |
David Chisnall | a54da05 | 2010-05-20 13:45:48 +0000 | [diff] [blame] | 1382 | messageBB = CGF.Builder.GetInsertBlock(); |
| 1383 | CGF.Builder.CreateBr(continueBB); |
| 1384 | CGF.EmitBlock(continueBB); |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1385 | if (msgRet.isScalar()) { |
| 1386 | llvm::Value *v = msgRet.getScalarVal(); |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 1387 | llvm::PHINode *phi = Builder.CreatePHI(v->getType(), 2); |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1388 | phi->addIncoming(v, messageBB); |
| 1389 | phi->addIncoming(llvm::Constant::getNullValue(v->getType()), startBB); |
| 1390 | msgRet = RValue::get(phi); |
| 1391 | } else if (msgRet.isAggregate()) { |
| 1392 | llvm::Value *v = msgRet.getAggregateAddr(); |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 1393 | llvm::PHINode *phi = Builder.CreatePHI(v->getType(), 2); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1394 | llvm::PointerType *RetTy = cast<llvm::PointerType>(v->getType()); |
David Chisnall | 866163b | 2010-04-30 13:36:12 +0000 | [diff] [blame] | 1395 | llvm::AllocaInst *NullVal = |
| 1396 | CGF.CreateTempAlloca(RetTy->getElementType(), "null"); |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1397 | CGF.InitTempAlloca(NullVal, |
| 1398 | llvm::Constant::getNullValue(RetTy->getElementType())); |
| 1399 | phi->addIncoming(v, messageBB); |
| 1400 | phi->addIncoming(NullVal, startBB); |
| 1401 | msgRet = RValue::getAggregate(phi); |
| 1402 | } else /* isComplex() */ { |
| 1403 | std::pair<llvm::Value*,llvm::Value*> v = msgRet.getComplexVal(); |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 1404 | llvm::PHINode *phi = Builder.CreatePHI(v.first->getType(), 2); |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1405 | phi->addIncoming(v.first, messageBB); |
| 1406 | phi->addIncoming(llvm::Constant::getNullValue(v.first->getType()), |
| 1407 | startBB); |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 1408 | llvm::PHINode *phi2 = Builder.CreatePHI(v.second->getType(), 2); |
David Chisnall | 664b7c7 | 2010-04-27 15:08:48 +0000 | [diff] [blame] | 1409 | phi2->addIncoming(v.second, messageBB); |
| 1410 | phi2->addIncoming(llvm::Constant::getNullValue(v.second->getType()), |
| 1411 | startBB); |
| 1412 | msgRet = RValue::getComplex(phi, phi2); |
| 1413 | } |
| 1414 | } |
| 1415 | return msgRet; |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | /// Generates a MethodList. Used in construction of a objc_class and |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1419 | /// objc_category structures. |
Bill Wendling | 795b100 | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 1420 | llvm::Constant *CGObjCGNU:: |
| 1421 | GenerateMethodList(const StringRef &ClassName, |
| 1422 | const StringRef &CategoryName, |
| 1423 | ArrayRef<Selector> MethodSels, |
| 1424 | ArrayRef<llvm::Constant *> MethodTypes, |
| 1425 | bool isClassMethodList) { |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 1426 | if (MethodSels.empty()) |
| 1427 | return NULLPtr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | // Get the method structure type. |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1429 | llvm::StructType *ObjCMethodTy = llvm::StructType::get( |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1430 | PtrToInt8Ty, // Really a selector, but the runtime creates it us. |
| 1431 | PtrToInt8Ty, // Method types |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1432 | IMPTy, //Method pointer |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1433 | NULL); |
| 1434 | std::vector<llvm::Constant*> Methods; |
| 1435 | std::vector<llvm::Constant*> Elements; |
| 1436 | for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) { |
| 1437 | Elements.clear(); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1438 | llvm::Constant *Method = |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1439 | TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName, |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1440 | MethodSels[i], |
| 1441 | isClassMethodList)); |
| 1442 | assert(Method && "Can't generate metadata for method that doesn't exist"); |
| 1443 | llvm::Constant *C = MakeConstantString(MethodSels[i].getAsString()); |
| 1444 | Elements.push_back(C); |
| 1445 | Elements.push_back(MethodTypes[i]); |
| 1446 | Method = llvm::ConstantExpr::getBitCast(Method, |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 1447 | IMPTy); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 1448 | Elements.push_back(Method); |
| 1449 | Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | // Array of method structures |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1453 | llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy, |
Fariborz Jahanian | 1e64a95 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 1454 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 1455 | llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy, |
Chris Lattner | fba6763 | 2008-06-26 04:52:29 +0000 | [diff] [blame] | 1456 | Methods); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1457 | |
| 1458 | // Structure containing list pointer, array and array count |
Chris Lattner | c1c2011 | 2011-08-12 17:43:31 +0000 | [diff] [blame] | 1459 | llvm::StructType *ObjCMethodListTy = llvm::StructType::create(VMContext); |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1460 | llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(ObjCMethodListTy); |
| 1461 | ObjCMethodListTy->setBody( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1462 | NextPtrTy, |
| 1463 | IntTy, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1464 | ObjCMethodArrayTy, |
| 1465 | NULL); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1466 | |
| 1467 | Methods.clear(); |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 1468 | Methods.push_back(llvm::ConstantPointerNull::get( |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1469 | llvm::PointerType::getUnqual(ObjCMethodListTy))); |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 1470 | Methods.push_back(llvm::ConstantInt::get(Int32Ty, MethodTypes.size())); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1471 | Methods.push_back(MethodArray); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1472 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1473 | // Create an instance of the structure |
| 1474 | return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list"); |
| 1475 | } |
| 1476 | |
| 1477 | /// Generates an IvarList. Used in construction of a objc_class. |
Bill Wendling | 795b100 | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 1478 | llvm::Constant *CGObjCGNU:: |
| 1479 | GenerateIvarList(ArrayRef<llvm::Constant *> IvarNames, |
| 1480 | ArrayRef<llvm::Constant *> IvarTypes, |
| 1481 | ArrayRef<llvm::Constant *> IvarOffsets) { |
David Chisnall | 1804463 | 2009-11-16 19:05:54 +0000 | [diff] [blame] | 1482 | if (IvarNames.size() == 0) |
| 1483 | return NULLPtr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1484 | // Get the method structure type. |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1485 | llvm::StructType *ObjCIvarTy = llvm::StructType::get( |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1486 | PtrToInt8Ty, |
| 1487 | PtrToInt8Ty, |
| 1488 | IntTy, |
| 1489 | NULL); |
| 1490 | std::vector<llvm::Constant*> Ivars; |
| 1491 | std::vector<llvm::Constant*> Elements; |
| 1492 | for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) { |
| 1493 | Elements.clear(); |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1494 | Elements.push_back(IvarNames[i]); |
| 1495 | Elements.push_back(IvarTypes[i]); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1496 | Elements.push_back(IvarOffsets[i]); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1497 | Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | // Array of method structures |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1501 | llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1502 | IvarNames.size()); |
| 1503 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1504 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1505 | Elements.clear(); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1506 | Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size())); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 1507 | Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1508 | // Structure containing array and array count |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1509 | llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1510 | ObjCIvarArrayTy, |
| 1511 | NULL); |
| 1512 | |
| 1513 | // Create an instance of the structure |
| 1514 | return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list"); |
| 1515 | } |
| 1516 | |
| 1517 | /// Generate a class structure |
| 1518 | llvm::Constant *CGObjCGNU::GenerateClassStructure( |
| 1519 | llvm::Constant *MetaClass, |
| 1520 | llvm::Constant *SuperClass, |
| 1521 | unsigned info, |
Chris Lattner | d002cc6 | 2008-06-26 04:47:04 +0000 | [diff] [blame] | 1522 | const char *Name, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1523 | llvm::Constant *Version, |
| 1524 | llvm::Constant *InstanceSize, |
| 1525 | llvm::Constant *IVars, |
| 1526 | llvm::Constant *Methods, |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1527 | llvm::Constant *Protocols, |
| 1528 | llvm::Constant *IvarOffsets, |
David Chisnall | 8c757f9 | 2010-04-28 14:29:56 +0000 | [diff] [blame] | 1529 | llvm::Constant *Properties, |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 1530 | llvm::Constant *StrongIvarBitmap, |
| 1531 | llvm::Constant *WeakIvarBitmap, |
David Chisnall | 8c757f9 | 2010-04-28 14:29:56 +0000 | [diff] [blame] | 1532 | bool isMeta) { |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1533 | // Set up the class structure |
| 1534 | // Note: Several of these are char*s when they should be ids. This is |
| 1535 | // because the runtime performs this translation on load. |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1536 | // |
| 1537 | // Fields marked New ABI are part of the GNUstep runtime. We emit them |
| 1538 | // anyway; the classes will still work with the GNU runtime, they will just |
| 1539 | // be ignored. |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1540 | llvm::StructType *ClassTy = llvm::StructType::get( |
David Chisnall | 13df6f6 | 2012-01-04 12:02:13 +0000 | [diff] [blame] | 1541 | PtrToInt8Ty, // isa |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1542 | PtrToInt8Ty, // super_class |
| 1543 | PtrToInt8Ty, // name |
| 1544 | LongTy, // version |
| 1545 | LongTy, // info |
| 1546 | LongTy, // instance_size |
| 1547 | IVars->getType(), // ivars |
| 1548 | Methods->getType(), // methods |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1549 | // These are all filled in by the runtime, so we pretend |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1550 | PtrTy, // dtable |
| 1551 | PtrTy, // subclass_list |
| 1552 | PtrTy, // sibling_class |
| 1553 | PtrTy, // protocols |
| 1554 | PtrTy, // gc_object_type |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1555 | // New ABI: |
| 1556 | LongTy, // abi_version |
| 1557 | IvarOffsets->getType(), // ivar_offsets |
| 1558 | Properties->getType(), // properties |
David Chisnall | 9d06ba8 | 2011-10-25 10:12:21 +0000 | [diff] [blame] | 1559 | IntPtrTy, // strong_pointers |
| 1560 | IntPtrTy, // weak_pointers |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1561 | NULL); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1562 | llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1563 | // Fill in the structure |
| 1564 | std::vector<llvm::Constant*> Elements; |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1565 | Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1566 | Elements.push_back(SuperClass); |
Chris Lattner | d002cc6 | 2008-06-26 04:47:04 +0000 | [diff] [blame] | 1567 | Elements.push_back(MakeConstantString(Name, ".class_name")); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1568 | Elements.push_back(Zero); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1569 | Elements.push_back(llvm::ConstantInt::get(LongTy, info)); |
David Chisnall | 05f3a50 | 2011-02-21 23:47:40 +0000 | [diff] [blame] | 1570 | if (isMeta) { |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 1571 | llvm::DataLayout td(&TheModule); |
Ken Dyck | e0afc89 | 2011-04-22 17:59:22 +0000 | [diff] [blame] | 1572 | Elements.push_back( |
| 1573 | llvm::ConstantInt::get(LongTy, |
| 1574 | td.getTypeSizeInBits(ClassTy) / |
| 1575 | CGM.getContext().getCharWidth())); |
David Chisnall | 05f3a50 | 2011-02-21 23:47:40 +0000 | [diff] [blame] | 1576 | } else |
| 1577 | Elements.push_back(InstanceSize); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1578 | Elements.push_back(IVars); |
| 1579 | Elements.push_back(Methods); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1580 | Elements.push_back(NULLPtr); |
| 1581 | Elements.push_back(NULLPtr); |
| 1582 | Elements.push_back(NULLPtr); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1583 | Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy)); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1584 | Elements.push_back(NULLPtr); |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 1585 | Elements.push_back(llvm::ConstantInt::get(LongTy, 1)); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1586 | Elements.push_back(IvarOffsets); |
| 1587 | Elements.push_back(Properties); |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 1588 | Elements.push_back(StrongIvarBitmap); |
| 1589 | Elements.push_back(WeakIvarBitmap); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1590 | // Create an instance of the structure |
David Chisnall | 41d63ed | 2010-01-08 00:14:31 +0000 | [diff] [blame] | 1591 | // This is now an externally visible symbol, so that we can speed up class |
David Chisnall | 13df6f6 | 2012-01-04 12:02:13 +0000 | [diff] [blame] | 1592 | // messages in the next ABI. We may already have some weak references to |
| 1593 | // this, so check and fix them properly. |
| 1594 | std::string ClassSym((isMeta ? "_OBJC_METACLASS_": "_OBJC_CLASS_") + |
| 1595 | std::string(Name)); |
| 1596 | llvm::GlobalVariable *ClassRef = TheModule.getNamedGlobal(ClassSym); |
| 1597 | llvm::Constant *Class = MakeGlobal(ClassTy, Elements, ClassSym, |
| 1598 | llvm::GlobalValue::ExternalLinkage); |
| 1599 | if (ClassRef) { |
| 1600 | ClassRef->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(Class, |
| 1601 | ClassRef->getType())); |
| 1602 | ClassRef->removeFromParent(); |
| 1603 | Class->setName(ClassSym); |
| 1604 | } |
| 1605 | return Class; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
Bill Wendling | 795b100 | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 1608 | llvm::Constant *CGObjCGNU:: |
| 1609 | GenerateProtocolMethodList(ArrayRef<llvm::Constant *> MethodNames, |
| 1610 | ArrayRef<llvm::Constant *> MethodTypes) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1611 | // Get the method structure type. |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1612 | llvm::StructType *ObjCMethodDescTy = llvm::StructType::get( |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1613 | PtrToInt8Ty, // Really a selector, but the runtime does the casting for us. |
| 1614 | PtrToInt8Ty, |
| 1615 | NULL); |
| 1616 | std::vector<llvm::Constant*> Methods; |
| 1617 | std::vector<llvm::Constant*> Elements; |
| 1618 | for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) { |
| 1619 | Elements.clear(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1620 | Elements.push_back(MethodNames[i]); |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1621 | Elements.push_back(MethodTypes[i]); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1622 | Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1623 | } |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1624 | llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1625 | MethodNames.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 1626 | llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, |
Mike Stump | bb1c860 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 1627 | Methods); |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1628 | llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get( |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1629 | IntTy, ObjCMethodArrayTy, NULL); |
| 1630 | Methods.clear(); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1631 | Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size())); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1632 | Methods.push_back(Array); |
| 1633 | return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list"); |
| 1634 | } |
Mike Stump | bb1c860 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 1635 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1636 | // Create the protocol list structure used in classes, categories and so on |
Bill Wendling | 795b100 | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 1637 | llvm::Constant *CGObjCGNU::GenerateProtocolList(ArrayRef<std::string>Protocols){ |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1638 | llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1639 | Protocols.size()); |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1640 | llvm::StructType *ProtocolListTy = llvm::StructType::get( |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1641 | PtrTy, //Should be a recurisve pointer, but it's always NULL here. |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1642 | SizeTy, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1643 | ProtocolArrayTy, |
| 1644 | NULL); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1645 | std::vector<llvm::Constant*> Elements; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1646 | for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end(); |
| 1647 | iter != endIter ; iter++) { |
David Chisnall | ff80fab | 2009-11-20 14:50:59 +0000 | [diff] [blame] | 1648 | llvm::Constant *protocol = 0; |
| 1649 | llvm::StringMap<llvm::Constant*>::iterator value = |
| 1650 | ExistingProtocols.find(*iter); |
| 1651 | if (value == ExistingProtocols.end()) { |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1652 | protocol = GenerateEmptyProtocol(*iter); |
David Chisnall | ff80fab | 2009-11-20 14:50:59 +0000 | [diff] [blame] | 1653 | } else { |
| 1654 | protocol = value->getValue(); |
| 1655 | } |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1656 | llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(protocol, |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1657 | PtrToInt8Ty); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1658 | Elements.push_back(Ptr); |
| 1659 | } |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 1660 | llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1661 | Elements); |
| 1662 | Elements.clear(); |
| 1663 | Elements.push_back(NULLPtr); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1664 | Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size())); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1665 | Elements.push_back(ProtocolArray); |
| 1666 | return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list"); |
| 1667 | } |
| 1668 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1669 | llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1670 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1671 | llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()]; |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1672 | llvm::Type *T = |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1673 | CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 1674 | return Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T)); |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | llvm::Constant *CGObjCGNU::GenerateEmptyProtocol( |
| 1678 | const std::string &ProtocolName) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1679 | SmallVector<std::string, 0> EmptyStringVector; |
| 1680 | SmallVector<llvm::Constant*, 0> EmptyConstantVector; |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1681 | |
| 1682 | llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1683 | llvm::Constant *MethodList = |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1684 | GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector); |
| 1685 | // Protocols are objects containing lists of the methods implemented and |
| 1686 | // protocols adopted. |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1687 | llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy, |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1688 | PtrToInt8Ty, |
| 1689 | ProtocolList->getType(), |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1690 | MethodList->getType(), |
| 1691 | MethodList->getType(), |
| 1692 | MethodList->getType(), |
| 1693 | MethodList->getType(), |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1694 | NULL); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1695 | std::vector<llvm::Constant*> Elements; |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1696 | // The isa pointer must be set to a magic number so the runtime knows it's |
| 1697 | // the correct layout. |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1698 | Elements.push_back(llvm::ConstantExpr::getIntToPtr( |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 1699 | llvm::ConstantInt::get(Int32Ty, ProtocolVersion), IdTy)); |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1700 | Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name")); |
| 1701 | Elements.push_back(ProtocolList); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1702 | Elements.push_back(MethodList); |
| 1703 | Elements.push_back(MethodList); |
| 1704 | Elements.push_back(MethodList); |
| 1705 | Elements.push_back(MethodList); |
Fariborz Jahanian | f8c4f54 | 2009-03-31 18:27:22 +0000 | [diff] [blame] | 1706 | return MakeGlobal(ProtocolTy, Elements, ".objc_protocol"); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1709 | void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) { |
| 1710 | ASTContext &Context = CGM.getContext(); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1711 | std::string ProtocolName = PD->getNameAsString(); |
Douglas Gregor | 1d784b2 | 2012-01-01 19:51:50 +0000 | [diff] [blame] | 1712 | |
| 1713 | // Use the protocol definition, if there is one. |
| 1714 | if (const ObjCProtocolDecl *Def = PD->getDefinition()) |
| 1715 | PD = Def; |
| 1716 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1717 | SmallVector<std::string, 16> Protocols; |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1718 | for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(), |
| 1719 | E = PD->protocol_end(); PI != E; ++PI) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1720 | Protocols.push_back((*PI)->getNameAsString()); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1721 | SmallVector<llvm::Constant*, 16> InstanceMethodNames; |
| 1722 | SmallVector<llvm::Constant*, 16> InstanceMethodTypes; |
| 1723 | SmallVector<llvm::Constant*, 16> OptionalInstanceMethodNames; |
| 1724 | SmallVector<llvm::Constant*, 16> OptionalInstanceMethodTypes; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1725 | for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(), |
| 1726 | E = PD->instmeth_end(); iter != E; iter++) { |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1727 | std::string TypeStr; |
| 1728 | Context.getObjCEncodingForMethodDecl(*iter, TypeStr); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1729 | if ((*iter)->getImplementationControl() == ObjCMethodDecl::Optional) { |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1730 | OptionalInstanceMethodNames.push_back( |
| 1731 | MakeConstantString((*iter)->getSelector().getAsString())); |
| 1732 | OptionalInstanceMethodTypes.push_back(MakeConstantString(TypeStr)); |
David Chisnall | a904e01 | 2012-08-23 12:17:21 +0000 | [diff] [blame] | 1733 | } else { |
| 1734 | InstanceMethodNames.push_back( |
| 1735 | MakeConstantString((*iter)->getSelector().getAsString())); |
| 1736 | InstanceMethodTypes.push_back(MakeConstantString(TypeStr)); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1737 | } |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1738 | } |
| 1739 | // Collect information about class methods: |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1740 | SmallVector<llvm::Constant*, 16> ClassMethodNames; |
| 1741 | SmallVector<llvm::Constant*, 16> ClassMethodTypes; |
| 1742 | SmallVector<llvm::Constant*, 16> OptionalClassMethodNames; |
| 1743 | SmallVector<llvm::Constant*, 16> OptionalClassMethodTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1744 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1745 | iter = PD->classmeth_begin(), endIter = PD->classmeth_end(); |
| 1746 | iter != endIter ; iter++) { |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1747 | std::string TypeStr; |
| 1748 | Context.getObjCEncodingForMethodDecl((*iter),TypeStr); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1749 | if ((*iter)->getImplementationControl() == ObjCMethodDecl::Optional) { |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1750 | OptionalClassMethodNames.push_back( |
| 1751 | MakeConstantString((*iter)->getSelector().getAsString())); |
| 1752 | OptionalClassMethodTypes.push_back(MakeConstantString(TypeStr)); |
David Chisnall | a904e01 | 2012-08-23 12:17:21 +0000 | [diff] [blame] | 1753 | } else { |
| 1754 | ClassMethodNames.push_back( |
| 1755 | MakeConstantString((*iter)->getSelector().getAsString())); |
| 1756 | ClassMethodTypes.push_back(MakeConstantString(TypeStr)); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1757 | } |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1758 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1759 | |
| 1760 | llvm::Constant *ProtocolList = GenerateProtocolList(Protocols); |
| 1761 | llvm::Constant *InstanceMethodList = |
| 1762 | GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes); |
| 1763 | llvm::Constant *ClassMethodList = |
| 1764 | GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1765 | llvm::Constant *OptionalInstanceMethodList = |
| 1766 | GenerateProtocolMethodList(OptionalInstanceMethodNames, |
| 1767 | OptionalInstanceMethodTypes); |
| 1768 | llvm::Constant *OptionalClassMethodList = |
| 1769 | GenerateProtocolMethodList(OptionalClassMethodNames, |
| 1770 | OptionalClassMethodTypes); |
| 1771 | |
| 1772 | // Property metadata: name, attributes, isSynthesized, setter name, setter |
| 1773 | // types, getter name, getter types. |
| 1774 | // The isSynthesized value is always set to 0 in a protocol. It exists to |
| 1775 | // simplify the runtime library by allowing it to use the same data |
| 1776 | // structures for protocol metadata everywhere. |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1777 | llvm::StructType *PropertyMetadataTy = llvm::StructType::get( |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1778 | PtrToInt8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty, |
| 1779 | PtrToInt8Ty, NULL); |
| 1780 | std::vector<llvm::Constant*> Properties; |
| 1781 | std::vector<llvm::Constant*> OptionalProperties; |
| 1782 | |
| 1783 | // Add all of the property methods need adding to the method list and to the |
| 1784 | // property metadata list. |
| 1785 | for (ObjCContainerDecl::prop_iterator |
| 1786 | iter = PD->prop_begin(), endIter = PD->prop_end(); |
| 1787 | iter != endIter ; iter++) { |
| 1788 | std::vector<llvm::Constant*> Fields; |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1789 | ObjCPropertyDecl *property = *iter; |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1790 | |
David Chisnall | 891dac7 | 2012-10-16 15:11:55 +0000 | [diff] [blame] | 1791 | |
| 1792 | Fields.push_back(MakePropertyEncodingString(property, PD)); |
| 1793 | |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1794 | Fields.push_back(llvm::ConstantInt::get(Int8Ty, |
| 1795 | property->getPropertyAttributes())); |
| 1796 | Fields.push_back(llvm::ConstantInt::get(Int8Ty, 0)); |
| 1797 | if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) { |
| 1798 | std::string TypeStr; |
| 1799 | Context.getObjCEncodingForMethodDecl(getter,TypeStr); |
| 1800 | llvm::Constant *TypeEncoding = MakeConstantString(TypeStr); |
| 1801 | InstanceMethodTypes.push_back(TypeEncoding); |
| 1802 | Fields.push_back(MakeConstantString(getter->getSelector().getAsString())); |
| 1803 | Fields.push_back(TypeEncoding); |
| 1804 | } else { |
| 1805 | Fields.push_back(NULLPtr); |
| 1806 | Fields.push_back(NULLPtr); |
| 1807 | } |
| 1808 | if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) { |
| 1809 | std::string TypeStr; |
| 1810 | Context.getObjCEncodingForMethodDecl(setter,TypeStr); |
| 1811 | llvm::Constant *TypeEncoding = MakeConstantString(TypeStr); |
| 1812 | InstanceMethodTypes.push_back(TypeEncoding); |
| 1813 | Fields.push_back(MakeConstantString(setter->getSelector().getAsString())); |
| 1814 | Fields.push_back(TypeEncoding); |
| 1815 | } else { |
| 1816 | Fields.push_back(NULLPtr); |
| 1817 | Fields.push_back(NULLPtr); |
| 1818 | } |
| 1819 | if (property->getPropertyImplementation() == ObjCPropertyDecl::Optional) { |
| 1820 | OptionalProperties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields)); |
| 1821 | } else { |
| 1822 | Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields)); |
| 1823 | } |
| 1824 | } |
| 1825 | llvm::Constant *PropertyArray = llvm::ConstantArray::get( |
| 1826 | llvm::ArrayType::get(PropertyMetadataTy, Properties.size()), Properties); |
| 1827 | llvm::Constant* PropertyListInitFields[] = |
| 1828 | {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray}; |
| 1829 | |
| 1830 | llvm::Constant *PropertyListInit = |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 1831 | llvm::ConstantStruct::getAnon(PropertyListInitFields); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1832 | llvm::Constant *PropertyList = new llvm::GlobalVariable(TheModule, |
| 1833 | PropertyListInit->getType(), false, llvm::GlobalValue::InternalLinkage, |
| 1834 | PropertyListInit, ".objc_property_list"); |
| 1835 | |
| 1836 | llvm::Constant *OptionalPropertyArray = |
| 1837 | llvm::ConstantArray::get(llvm::ArrayType::get(PropertyMetadataTy, |
| 1838 | OptionalProperties.size()) , OptionalProperties); |
| 1839 | llvm::Constant* OptionalPropertyListInitFields[] = { |
| 1840 | llvm::ConstantInt::get(IntTy, OptionalProperties.size()), NULLPtr, |
| 1841 | OptionalPropertyArray }; |
| 1842 | |
| 1843 | llvm::Constant *OptionalPropertyListInit = |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 1844 | llvm::ConstantStruct::getAnon(OptionalPropertyListInitFields); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1845 | llvm::Constant *OptionalPropertyList = new llvm::GlobalVariable(TheModule, |
| 1846 | OptionalPropertyListInit->getType(), false, |
| 1847 | llvm::GlobalValue::InternalLinkage, OptionalPropertyListInit, |
| 1848 | ".objc_property_list"); |
| 1849 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1850 | // Protocols are objects containing lists of the methods implemented and |
| 1851 | // protocols adopted. |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1852 | llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1853 | PtrToInt8Ty, |
| 1854 | ProtocolList->getType(), |
| 1855 | InstanceMethodList->getType(), |
| 1856 | ClassMethodList->getType(), |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1857 | OptionalInstanceMethodList->getType(), |
| 1858 | OptionalClassMethodList->getType(), |
| 1859 | PropertyList->getType(), |
| 1860 | OptionalPropertyList->getType(), |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1861 | NULL); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1862 | std::vector<llvm::Constant*> Elements; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1863 | // The isa pointer must be set to a magic number so the runtime knows it's |
| 1864 | // the correct layout. |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1865 | Elements.push_back(llvm::ConstantExpr::getIntToPtr( |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 1866 | llvm::ConstantInt::get(Int32Ty, ProtocolVersion), IdTy)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1867 | Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name")); |
| 1868 | Elements.push_back(ProtocolList); |
| 1869 | Elements.push_back(InstanceMethodList); |
| 1870 | Elements.push_back(ClassMethodList); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1871 | Elements.push_back(OptionalInstanceMethodList); |
| 1872 | Elements.push_back(OptionalClassMethodList); |
| 1873 | Elements.push_back(PropertyList); |
| 1874 | Elements.push_back(OptionalPropertyList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1875 | ExistingProtocols[ProtocolName] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1876 | llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1877 | ".objc_protocol"), IdTy); |
| 1878 | } |
Dmitri Gribenko | c4a7790 | 2012-11-15 14:28:07 +0000 | [diff] [blame] | 1879 | void CGObjCGNU::GenerateProtocolHolderCategory() { |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1880 | // Collect information about instance methods |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1881 | SmallVector<Selector, 1> MethodSels; |
| 1882 | SmallVector<llvm::Constant*, 1> MethodTypes; |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1883 | |
| 1884 | std::vector<llvm::Constant*> Elements; |
| 1885 | const std::string ClassName = "__ObjC_Protocol_Holder_Ugly_Hack"; |
| 1886 | const std::string CategoryName = "AnotherHack"; |
| 1887 | Elements.push_back(MakeConstantString(CategoryName)); |
| 1888 | Elements.push_back(MakeConstantString(ClassName)); |
| 1889 | // Instance method list |
| 1890 | Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList( |
| 1891 | ClassName, CategoryName, MethodSels, MethodTypes, false), PtrTy)); |
| 1892 | // Class method list |
| 1893 | Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList( |
| 1894 | ClassName, CategoryName, MethodSels, MethodTypes, true), PtrTy)); |
| 1895 | // Protocol list |
| 1896 | llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrTy, |
| 1897 | ExistingProtocols.size()); |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1898 | llvm::StructType *ProtocolListTy = llvm::StructType::get( |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1899 | PtrTy, //Should be a recurisve pointer, but it's always NULL here. |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 1900 | SizeTy, |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1901 | ProtocolArrayTy, |
| 1902 | NULL); |
| 1903 | std::vector<llvm::Constant*> ProtocolElements; |
| 1904 | for (llvm::StringMapIterator<llvm::Constant*> iter = |
| 1905 | ExistingProtocols.begin(), endIter = ExistingProtocols.end(); |
| 1906 | iter != endIter ; iter++) { |
| 1907 | llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(iter->getValue(), |
| 1908 | PtrTy); |
| 1909 | ProtocolElements.push_back(Ptr); |
| 1910 | } |
| 1911 | llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy, |
| 1912 | ProtocolElements); |
| 1913 | ProtocolElements.clear(); |
| 1914 | ProtocolElements.push_back(NULLPtr); |
| 1915 | ProtocolElements.push_back(llvm::ConstantInt::get(LongTy, |
| 1916 | ExistingProtocols.size())); |
| 1917 | ProtocolElements.push_back(ProtocolArray); |
| 1918 | Elements.push_back(llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolListTy, |
| 1919 | ProtocolElements, ".objc_protocol_list"), PtrTy)); |
| 1920 | Categories.push_back(llvm::ConstantExpr::getBitCast( |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 1921 | MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 1922 | PtrTy, PtrTy, PtrTy, NULL), Elements), PtrTy)); |
| 1923 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 1924 | |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 1925 | /// Libobjc2 uses a bitfield representation where small(ish) bitfields are |
| 1926 | /// stored in a 64-bit value with the low bit set to 1 and the remaining 63 |
| 1927 | /// bits set to their values, LSB first, while larger ones are stored in a |
| 1928 | /// structure of this / form: |
| 1929 | /// |
| 1930 | /// struct { int32_t length; int32_t values[length]; }; |
| 1931 | /// |
| 1932 | /// The values in the array are stored in host-endian format, with the least |
| 1933 | /// significant bit being assumed to come first in the bitfield. Therefore, a |
| 1934 | /// bitfield with the 64th bit set will be (int64_t)&{ 2, [0, 1<<31] }, while a |
| 1935 | /// bitfield / with the 63rd bit set will be 1<<64. |
Bill Wendling | 795b100 | 2012-02-22 09:30:11 +0000 | [diff] [blame] | 1936 | llvm::Constant *CGObjCGNU::MakeBitField(ArrayRef<bool> bits) { |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 1937 | int bitCount = bits.size(); |
David Chisnall | 9d06ba8 | 2011-10-25 10:12:21 +0000 | [diff] [blame] | 1938 | int ptrBits = |
| 1939 | (TheModule.getPointerSize() == llvm::Module::Pointer32) ? 32 : 64; |
| 1940 | if (bitCount < ptrBits) { |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 1941 | uint64_t val = 1; |
| 1942 | for (int i=0 ; i<bitCount ; ++i) { |
Eli Friedman | e3c944a | 2011-10-08 01:03:47 +0000 | [diff] [blame] | 1943 | if (bits[i]) val |= 1ULL<<(i+1); |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 1944 | } |
David Chisnall | 9d06ba8 | 2011-10-25 10:12:21 +0000 | [diff] [blame] | 1945 | return llvm::ConstantInt::get(IntPtrTy, val); |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 1946 | } |
| 1947 | llvm::SmallVector<llvm::Constant*, 8> values; |
| 1948 | int v=0; |
| 1949 | while (v < bitCount) { |
| 1950 | int32_t word = 0; |
| 1951 | for (int i=0 ; (i<32) && (v<bitCount) ; ++i) { |
| 1952 | if (bits[v]) word |= 1<<i; |
| 1953 | v++; |
| 1954 | } |
| 1955 | values.push_back(llvm::ConstantInt::get(Int32Ty, word)); |
| 1956 | } |
| 1957 | llvm::ArrayType *arrayTy = llvm::ArrayType::get(Int32Ty, values.size()); |
| 1958 | llvm::Constant *array = llvm::ConstantArray::get(arrayTy, values); |
| 1959 | llvm::Constant *fields[2] = { |
| 1960 | llvm::ConstantInt::get(Int32Ty, values.size()), |
| 1961 | array }; |
| 1962 | llvm::Constant *GS = MakeGlobal(llvm::StructType::get(Int32Ty, arrayTy, |
| 1963 | NULL), fields); |
David Chisnall | 49de528 | 2011-10-08 08:54:36 +0000 | [diff] [blame] | 1964 | llvm::Constant *ptr = llvm::ConstantExpr::getPtrToInt(GS, IntPtrTy); |
David Chisnall | 49de528 | 2011-10-08 08:54:36 +0000 | [diff] [blame] | 1965 | return ptr; |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 1966 | } |
| 1967 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1968 | void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1969 | std::string ClassName = OCD->getClassInterface()->getNameAsString(); |
| 1970 | std::string CategoryName = OCD->getNameAsString(); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1971 | // Collect information about instance methods |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1972 | SmallVector<Selector, 16> InstanceMethodSels; |
| 1973 | SmallVector<llvm::Constant*, 16> InstanceMethodTypes; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1974 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1975 | iter = OCD->instmeth_begin(), endIter = OCD->instmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1976 | iter != endIter ; iter++) { |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1977 | InstanceMethodSels.push_back((*iter)->getSelector()); |
| 1978 | std::string TypeStr; |
| 1979 | CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr); |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1980 | InstanceMethodTypes.push_back(MakeConstantString(TypeStr)); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | // Collect information about class methods |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1984 | SmallVector<Selector, 16> ClassMethodSels; |
| 1985 | SmallVector<llvm::Constant*, 16> ClassMethodTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1986 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1987 | iter = OCD->classmeth_begin(), endIter = OCD->classmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 1988 | iter != endIter ; iter++) { |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1989 | ClassMethodSels.push_back((*iter)->getSelector()); |
| 1990 | std::string TypeStr; |
| 1991 | CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr); |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 1992 | ClassMethodTypes.push_back(MakeConstantString(TypeStr)); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
| 1995 | // Collect the names of referenced protocols |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1996 | SmallVector<std::string, 16> Protocols; |
David Chisnall | ad9e06d | 2010-03-13 22:20:45 +0000 | [diff] [blame] | 1997 | const ObjCCategoryDecl *CatDecl = OCD->getCategoryDecl(); |
| 1998 | const ObjCList<ObjCProtocolDecl> &Protos = CatDecl->getReferencedProtocols(); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1999 | for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(), |
| 2000 | E = Protos.end(); I != E; ++I) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2001 | Protocols.push_back((*I)->getNameAsString()); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2002 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2003 | std::vector<llvm::Constant*> Elements; |
| 2004 | Elements.push_back(MakeConstantString(CategoryName)); |
| 2005 | Elements.push_back(MakeConstantString(ClassName)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2006 | // Instance method list |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2007 | Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList( |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 2008 | ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2009 | false), PtrTy)); |
| 2010 | // Class method list |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2011 | Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList( |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 2012 | ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true), |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2013 | PtrTy)); |
| 2014 | // Protocol list |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2015 | Elements.push_back(llvm::ConstantExpr::getBitCast( |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2016 | GenerateProtocolList(Protocols), PtrTy)); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2017 | Categories.push_back(llvm::ConstantExpr::getBitCast( |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2018 | MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 2019 | PtrTy, PtrTy, PtrTy, NULL), Elements), PtrTy)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2020 | } |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2021 | |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2022 | llvm::Constant *CGObjCGNU::GeneratePropertyList(const ObjCImplementationDecl *OID, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2023 | SmallVectorImpl<Selector> &InstanceMethodSels, |
| 2024 | SmallVectorImpl<llvm::Constant*> &InstanceMethodTypes) { |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2025 | ASTContext &Context = CGM.getContext(); |
| 2026 | // |
| 2027 | // Property metadata: name, attributes, isSynthesized, setter name, setter |
| 2028 | // types, getter name, getter types. |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2029 | llvm::StructType *PropertyMetadataTy = llvm::StructType::get( |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2030 | PtrToInt8Ty, Int8Ty, Int8Ty, PtrToInt8Ty, PtrToInt8Ty, PtrToInt8Ty, |
| 2031 | PtrToInt8Ty, NULL); |
| 2032 | std::vector<llvm::Constant*> Properties; |
| 2033 | |
| 2034 | |
| 2035 | // Add all of the property methods need adding to the method list and to the |
| 2036 | // property metadata list. |
| 2037 | for (ObjCImplDecl::propimpl_iterator |
| 2038 | iter = OID->propimpl_begin(), endIter = OID->propimpl_end(); |
| 2039 | iter != endIter ; iter++) { |
| 2040 | std::vector<llvm::Constant*> Fields; |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 2041 | ObjCPropertyDecl *property = iter->getPropertyDecl(); |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2042 | ObjCPropertyImplDecl *propertyImpl = *iter; |
David Chisnall | 42ba04a | 2010-02-26 01:11:38 +0000 | [diff] [blame] | 2043 | bool isSynthesized = (propertyImpl->getPropertyImplementation() == |
| 2044 | ObjCPropertyImplDecl::Synthesize); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2045 | |
David Chisnall | 891dac7 | 2012-10-16 15:11:55 +0000 | [diff] [blame] | 2046 | Fields.push_back(MakePropertyEncodingString(property, OID)); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2047 | Fields.push_back(llvm::ConstantInt::get(Int8Ty, |
| 2048 | property->getPropertyAttributes())); |
David Chisnall | 42ba04a | 2010-02-26 01:11:38 +0000 | [diff] [blame] | 2049 | Fields.push_back(llvm::ConstantInt::get(Int8Ty, isSynthesized)); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2050 | if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) { |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2051 | std::string TypeStr; |
| 2052 | Context.getObjCEncodingForMethodDecl(getter,TypeStr); |
| 2053 | llvm::Constant *TypeEncoding = MakeConstantString(TypeStr); |
David Chisnall | 42ba04a | 2010-02-26 01:11:38 +0000 | [diff] [blame] | 2054 | if (isSynthesized) { |
| 2055 | InstanceMethodTypes.push_back(TypeEncoding); |
| 2056 | InstanceMethodSels.push_back(getter->getSelector()); |
| 2057 | } |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2058 | Fields.push_back(MakeConstantString(getter->getSelector().getAsString())); |
| 2059 | Fields.push_back(TypeEncoding); |
| 2060 | } else { |
| 2061 | Fields.push_back(NULLPtr); |
| 2062 | Fields.push_back(NULLPtr); |
| 2063 | } |
| 2064 | if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) { |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2065 | std::string TypeStr; |
| 2066 | Context.getObjCEncodingForMethodDecl(setter,TypeStr); |
| 2067 | llvm::Constant *TypeEncoding = MakeConstantString(TypeStr); |
David Chisnall | 42ba04a | 2010-02-26 01:11:38 +0000 | [diff] [blame] | 2068 | if (isSynthesized) { |
| 2069 | InstanceMethodTypes.push_back(TypeEncoding); |
| 2070 | InstanceMethodSels.push_back(setter->getSelector()); |
| 2071 | } |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2072 | Fields.push_back(MakeConstantString(setter->getSelector().getAsString())); |
| 2073 | Fields.push_back(TypeEncoding); |
| 2074 | } else { |
| 2075 | Fields.push_back(NULLPtr); |
| 2076 | Fields.push_back(NULLPtr); |
| 2077 | } |
| 2078 | Properties.push_back(llvm::ConstantStruct::get(PropertyMetadataTy, Fields)); |
| 2079 | } |
| 2080 | llvm::ArrayType *PropertyArrayTy = |
| 2081 | llvm::ArrayType::get(PropertyMetadataTy, Properties.size()); |
| 2082 | llvm::Constant *PropertyArray = llvm::ConstantArray::get(PropertyArrayTy, |
| 2083 | Properties); |
| 2084 | llvm::Constant* PropertyListInitFields[] = |
| 2085 | {llvm::ConstantInt::get(IntTy, Properties.size()), NULLPtr, PropertyArray}; |
| 2086 | |
| 2087 | llvm::Constant *PropertyListInit = |
Chris Lattner | c5cbb90 | 2011-06-20 04:01:35 +0000 | [diff] [blame] | 2088 | llvm::ConstantStruct::getAnon(PropertyListInitFields); |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2089 | return new llvm::GlobalVariable(TheModule, PropertyListInit->getType(), false, |
| 2090 | llvm::GlobalValue::InternalLinkage, PropertyListInit, |
| 2091 | ".objc_property_list"); |
| 2092 | } |
| 2093 | |
David Chisnall | 29254f4 | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 2094 | void CGObjCGNU::RegisterAlias(const ObjCCompatibleAliasDecl *OAD) { |
| 2095 | // Get the class declaration for which the alias is specified. |
| 2096 | ObjCInterfaceDecl *ClassDecl = |
| 2097 | const_cast<ObjCInterfaceDecl *>(OAD->getClassInterface()); |
| 2098 | std::string ClassName = ClassDecl->getNameAsString(); |
| 2099 | std::string AliasName = OAD->getNameAsString(); |
| 2100 | ClassAliases.push_back(ClassAliasPair(ClassName,AliasName)); |
| 2101 | } |
| 2102 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2103 | void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) { |
| 2104 | ASTContext &Context = CGM.getContext(); |
| 2105 | |
| 2106 | // Get the superclass name. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2107 | const ObjCInterfaceDecl * SuperClassDecl = |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2108 | OID->getClassInterface()->getSuperClass(); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2109 | std::string SuperClassName; |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 2110 | if (SuperClassDecl) { |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2111 | SuperClassName = SuperClassDecl->getNameAsString(); |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 2112 | EmitClassRef(SuperClassName); |
| 2113 | } |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2114 | |
| 2115 | // Get the class name |
Chris Lattner | 09dc666 | 2009-04-01 02:00:48 +0000 | [diff] [blame] | 2116 | ObjCInterfaceDecl *ClassDecl = |
| 2117 | const_cast<ObjCInterfaceDecl *>(OID->getClassInterface()); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2118 | std::string ClassName = ClassDecl->getNameAsString(); |
Chris Lattner | 2a8e4e1 | 2009-06-15 01:09:11 +0000 | [diff] [blame] | 2119 | // Emit the symbol that is used to generate linker errors if this class is |
| 2120 | // referenced in other modules but not declared. |
Fariborz Jahanian | c51db23 | 2009-07-03 15:10:14 +0000 | [diff] [blame] | 2121 | std::string classSymbolName = "__objc_class_name_" + ClassName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2122 | if (llvm::GlobalVariable *symbol = |
Fariborz Jahanian | c51db23 | 2009-07-03 15:10:14 +0000 | [diff] [blame] | 2123 | TheModule.getGlobalVariable(classSymbolName)) { |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2124 | symbol->setInitializer(llvm::ConstantInt::get(LongTy, 0)); |
Fariborz Jahanian | c51db23 | 2009-07-03 15:10:14 +0000 | [diff] [blame] | 2125 | } else { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2126 | new llvm::GlobalVariable(TheModule, LongTy, false, |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2127 | llvm::GlobalValue::ExternalLinkage, llvm::ConstantInt::get(LongTy, 0), |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2128 | classSymbolName); |
Fariborz Jahanian | c51db23 | 2009-07-03 15:10:14 +0000 | [diff] [blame] | 2129 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2130 | |
Daniel Dunbar | 2bebbf0 | 2009-05-03 10:46:44 +0000 | [diff] [blame] | 2131 | // Get the size of instances. |
Ken Dyck | 5f022d8 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 2132 | int instanceSize = |
| 2133 | Context.getASTObjCImplementationLayout(OID).getSize().getQuantity(); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2134 | |
| 2135 | // Collect information about instance variables. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2136 | SmallVector<llvm::Constant*, 16> IvarNames; |
| 2137 | SmallVector<llvm::Constant*, 16> IvarTypes; |
| 2138 | SmallVector<llvm::Constant*, 16> IvarOffsets; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2139 | |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2140 | std::vector<llvm::Constant*> IvarOffsetValues; |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 2141 | SmallVector<bool, 16> WeakIvars; |
| 2142 | SmallVector<bool, 16> StrongIvars; |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2143 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2144 | int superInstanceSize = !SuperClassDecl ? 0 : |
Ken Dyck | 5f022d8 | 2011-02-09 01:59:34 +0000 | [diff] [blame] | 2145 | Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize().getQuantity(); |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2146 | // For non-fragile ivars, set the instance size to 0 - {the size of just this |
| 2147 | // class}. The runtime will then set this to the correct value on load. |
Richard Smith | 7edf9e3 | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 2148 | if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2149 | instanceSize = 0 - (instanceSize - superInstanceSize); |
| 2150 | } |
David Chisnall | 7f63cb0 | 2010-04-19 00:45:34 +0000 | [diff] [blame] | 2151 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2152 | for (const ObjCIvarDecl *IVD = ClassDecl->all_declared_ivar_begin(); IVD; |
| 2153 | IVD = IVD->getNextIvar()) { |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2154 | // Store the name |
David Chisnall | 7f63cb0 | 2010-04-19 00:45:34 +0000 | [diff] [blame] | 2155 | IvarNames.push_back(MakeConstantString(IVD->getNameAsString())); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2156 | // Get the type encoding for this ivar |
| 2157 | std::string TypeStr; |
David Chisnall | 7f63cb0 | 2010-04-19 00:45:34 +0000 | [diff] [blame] | 2158 | Context.getObjCEncodingForType(IVD->getType(), TypeStr); |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2159 | IvarTypes.push_back(MakeConstantString(TypeStr)); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2160 | // Get the offset |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 2161 | uint64_t BaseOffset = ComputeIvarBaseOffset(CGM, OID, IVD); |
David Chisnall | aecbf24 | 2009-11-17 19:32:15 +0000 | [diff] [blame] | 2162 | uint64_t Offset = BaseOffset; |
Richard Smith | 7edf9e3 | 2012-11-01 22:30:59 +0000 | [diff] [blame] | 2163 | if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2164 | Offset = BaseOffset - superInstanceSize; |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2165 | } |
David Chisnall | 63ff703 | 2011-07-07 12:34:51 +0000 | [diff] [blame] | 2166 | llvm::Constant *OffsetValue = llvm::ConstantInt::get(IntTy, Offset); |
| 2167 | // Create the direct offset value |
| 2168 | std::string OffsetName = "__objc_ivar_offset_value_" + ClassName +"." + |
| 2169 | IVD->getNameAsString(); |
| 2170 | llvm::GlobalVariable *OffsetVar = TheModule.getGlobalVariable(OffsetName); |
| 2171 | if (OffsetVar) { |
| 2172 | OffsetVar->setInitializer(OffsetValue); |
| 2173 | // If this is the real definition, change its linkage type so that |
| 2174 | // different modules will use this one, rather than their private |
| 2175 | // copy. |
| 2176 | OffsetVar->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 2177 | } else |
| 2178 | OffsetVar = new llvm::GlobalVariable(TheModule, IntTy, |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2179 | false, llvm::GlobalValue::ExternalLinkage, |
David Chisnall | 63ff703 | 2011-07-07 12:34:51 +0000 | [diff] [blame] | 2180 | OffsetValue, |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2181 | "__objc_ivar_offset_value_" + ClassName +"." + |
David Chisnall | 63ff703 | 2011-07-07 12:34:51 +0000 | [diff] [blame] | 2182 | IVD->getNameAsString()); |
| 2183 | IvarOffsets.push_back(OffsetValue); |
| 2184 | IvarOffsetValues.push_back(OffsetVar); |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 2185 | Qualifiers::ObjCLifetime lt = IVD->getType().getQualifiers().getObjCLifetime(); |
| 2186 | switch (lt) { |
| 2187 | case Qualifiers::OCL_Strong: |
| 2188 | StrongIvars.push_back(true); |
| 2189 | WeakIvars.push_back(false); |
| 2190 | break; |
| 2191 | case Qualifiers::OCL_Weak: |
| 2192 | StrongIvars.push_back(false); |
| 2193 | WeakIvars.push_back(true); |
| 2194 | break; |
| 2195 | default: |
| 2196 | StrongIvars.push_back(false); |
| 2197 | WeakIvars.push_back(false); |
| 2198 | } |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2199 | } |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 2200 | llvm::Constant *StrongIvarBitmap = MakeBitField(StrongIvars); |
| 2201 | llvm::Constant *WeakIvarBitmap = MakeBitField(WeakIvars); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2202 | llvm::GlobalVariable *IvarOffsetArray = |
| 2203 | MakeGlobalArray(PtrToIntTy, IvarOffsetValues, ".ivar.offsets"); |
| 2204 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2205 | |
| 2206 | // Collect information about instance methods |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2207 | SmallVector<Selector, 16> InstanceMethodSels; |
| 2208 | SmallVector<llvm::Constant*, 16> InstanceMethodTypes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2209 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2210 | iter = OID->instmeth_begin(), endIter = OID->instmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2211 | iter != endIter ; iter++) { |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2212 | InstanceMethodSels.push_back((*iter)->getSelector()); |
| 2213 | std::string TypeStr; |
| 2214 | Context.getObjCEncodingForMethodDecl((*iter),TypeStr); |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2215 | InstanceMethodTypes.push_back(MakeConstantString(TypeStr)); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2216 | } |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2217 | |
| 2218 | llvm::Constant *Properties = GeneratePropertyList(OID, InstanceMethodSels, |
| 2219 | InstanceMethodTypes); |
| 2220 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2221 | |
| 2222 | // Collect information about class methods |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2223 | SmallVector<Selector, 16> ClassMethodSels; |
| 2224 | SmallVector<llvm::Constant*, 16> ClassMethodTypes; |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2225 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2226 | iter = OID->classmeth_begin(), endIter = OID->classmeth_end(); |
Douglas Gregor | 653f1b1 | 2009-04-23 01:02:12 +0000 | [diff] [blame] | 2227 | iter != endIter ; iter++) { |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2228 | ClassMethodSels.push_back((*iter)->getSelector()); |
| 2229 | std::string TypeStr; |
| 2230 | Context.getObjCEncodingForMethodDecl((*iter),TypeStr); |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2231 | ClassMethodTypes.push_back(MakeConstantString(TypeStr)); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2232 | } |
| 2233 | // Collect the names of referenced protocols |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2234 | SmallVector<std::string, 16> Protocols; |
Argyrios Kyrtzidis | a5f4441 | 2012-03-13 01:09:41 +0000 | [diff] [blame] | 2235 | for (ObjCInterfaceDecl::protocol_iterator |
| 2236 | I = ClassDecl->protocol_begin(), |
| 2237 | E = ClassDecl->protocol_end(); I != E; ++I) |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2238 | Protocols.push_back((*I)->getNameAsString()); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2239 | |
| 2240 | |
| 2241 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2242 | // Get the superclass pointer. |
| 2243 | llvm::Constant *SuperClass; |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2244 | if (!SuperClassName.empty()) { |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2245 | SuperClass = MakeConstantString(SuperClassName, ".super_class_name"); |
| 2246 | } else { |
Owen Anderson | 03e2050 | 2009-07-30 23:11:26 +0000 | [diff] [blame] | 2247 | SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2248 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2249 | // Empty vector used to construct empty method lists |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2250 | SmallVector<llvm::Constant*, 1> empty; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2251 | // Generate the method and instance variable lists |
| 2252 | llvm::Constant *MethodList = GenerateMethodList(ClassName, "", |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 2253 | InstanceMethodSels, InstanceMethodTypes, false); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2254 | llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "", |
Chris Lattner | a421007 | 2008-06-26 05:08:00 +0000 | [diff] [blame] | 2255 | ClassMethodSels, ClassMethodTypes, true); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2256 | llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes, |
| 2257 | IvarOffsets); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2258 | // Irrespective of whether we are compiling for a fragile or non-fragile ABI, |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2259 | // we emit a symbol containing the offset for each ivar in the class. This |
| 2260 | // allows code compiled for the non-Fragile ABI to inherit from code compiled |
| 2261 | // for the legacy ABI, without causing problems. The converse is also |
| 2262 | // possible, but causes all ivar accesses to be fragile. |
David Chisnall | e0d9876 | 2010-11-03 16:12:44 +0000 | [diff] [blame] | 2263 | |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2264 | // Offset pointer for getting at the correct field in the ivar list when |
| 2265 | // setting up the alias. These are: The base address for the global, the |
| 2266 | // ivar array (second field), the ivar in this list (set for each ivar), and |
| 2267 | // the offset (third field in ivar structure) |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 2268 | llvm::Type *IndexTy = Int32Ty; |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2269 | llvm::Constant *offsetPointerIndexes[] = {Zeros[0], |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2270 | llvm::ConstantInt::get(IndexTy, 1), 0, |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2271 | llvm::ConstantInt::get(IndexTy, 2) }; |
| 2272 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2273 | unsigned ivarIndex = 0; |
| 2274 | for (const ObjCIvarDecl *IVD = ClassDecl->all_declared_ivar_begin(); IVD; |
| 2275 | IVD = IVD->getNextIvar()) { |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2276 | const std::string Name = "__objc_ivar_offset_" + ClassName + '.' |
David Chisnall | e0d9876 | 2010-11-03 16:12:44 +0000 | [diff] [blame] | 2277 | + IVD->getNameAsString(); |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2278 | offsetPointerIndexes[2] = llvm::ConstantInt::get(IndexTy, ivarIndex); |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2279 | // Get the correct ivar field |
| 2280 | llvm::Constant *offsetValue = llvm::ConstantExpr::getGetElementPtr( |
Jay Foad | a5c0434 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 2281 | IvarList, offsetPointerIndexes); |
David Chisnall | e0d9876 | 2010-11-03 16:12:44 +0000 | [diff] [blame] | 2282 | // Get the existing variable, if one exists. |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2283 | llvm::GlobalVariable *offset = TheModule.getNamedGlobal(Name); |
| 2284 | if (offset) { |
Ted Kremenek | 74a1a1f | 2012-04-04 00:55:25 +0000 | [diff] [blame] | 2285 | offset->setInitializer(offsetValue); |
| 2286 | // If this is the real definition, change its linkage type so that |
| 2287 | // different modules will use this one, rather than their private |
| 2288 | // copy. |
| 2289 | offset->setLinkage(llvm::GlobalValue::ExternalLinkage); |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2290 | } else { |
Ted Kremenek | 74a1a1f | 2012-04-04 00:55:25 +0000 | [diff] [blame] | 2291 | // Add a new alias if there isn't one already. |
| 2292 | offset = new llvm::GlobalVariable(TheModule, offsetValue->getType(), |
| 2293 | false, llvm::GlobalValue::ExternalLinkage, offsetValue, Name); |
| 2294 | (void) offset; // Silence dead store warning. |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2295 | } |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2296 | ++ivarIndex; |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2297 | } |
David Chisnall | 9d06ba8 | 2011-10-25 10:12:21 +0000 | [diff] [blame] | 2298 | llvm::Constant *ZeroPtr = llvm::ConstantInt::get(IntPtrTy, 0); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2299 | //Generate metaclass for class methods |
| 2300 | llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr, |
David Chisnall | 1804463 | 2009-11-16 19:05:54 +0000 | [diff] [blame] | 2301 | NULLPtr, 0x12L, ClassName.c_str(), 0, Zeros[0], GenerateIvarList( |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 2302 | empty, empty, empty), ClassMethodList, NULLPtr, |
David Chisnall | 9d06ba8 | 2011-10-25 10:12:21 +0000 | [diff] [blame] | 2303 | NULLPtr, NULLPtr, ZeroPtr, ZeroPtr, true); |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 2304 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2305 | // Generate the class structure |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2306 | llvm::Constant *ClassStruct = |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2307 | GenerateClassStructure(MetaClassStruct, SuperClass, 0x11L, |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2308 | ClassName.c_str(), 0, |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2309 | llvm::ConstantInt::get(LongTy, instanceSize), IvarList, |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2310 | MethodList, GenerateProtocolList(Protocols), IvarOffsetArray, |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 2311 | Properties, StrongIvarBitmap, WeakIvarBitmap); |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 2312 | |
| 2313 | // Resolve the class aliases, if they exist. |
| 2314 | if (ClassPtrAlias) { |
David Chisnall | 0b9c22b | 2010-11-09 11:21:43 +0000 | [diff] [blame] | 2315 | ClassPtrAlias->replaceAllUsesWith( |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2316 | llvm::ConstantExpr::getBitCast(ClassStruct, IdTy)); |
David Chisnall | 0b9c22b | 2010-11-09 11:21:43 +0000 | [diff] [blame] | 2317 | ClassPtrAlias->eraseFromParent(); |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 2318 | ClassPtrAlias = 0; |
| 2319 | } |
| 2320 | if (MetaClassPtrAlias) { |
David Chisnall | 0b9c22b | 2010-11-09 11:21:43 +0000 | [diff] [blame] | 2321 | MetaClassPtrAlias->replaceAllUsesWith( |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2322 | llvm::ConstantExpr::getBitCast(MetaClassStruct, IdTy)); |
David Chisnall | 0b9c22b | 2010-11-09 11:21:43 +0000 | [diff] [blame] | 2323 | MetaClassPtrAlias->eraseFromParent(); |
Daniel Dunbar | 5efccb1 | 2009-05-04 15:31:17 +0000 | [diff] [blame] | 2324 | MetaClassPtrAlias = 0; |
| 2325 | } |
| 2326 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2327 | // Add class structure to list to be added to the symtab later |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2328 | ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2329 | Classes.push_back(ClassStruct); |
| 2330 | } |
| 2331 | |
Fariborz Jahanian | c38e9af | 2009-06-23 21:47:46 +0000 | [diff] [blame] | 2332 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2333 | llvm::Function *CGObjCGNU::ModuleInitFunction() { |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2334 | // Only emit an ObjC load function if no Objective-C stuff has been called |
| 2335 | if (Classes.empty() && Categories.empty() && ConstantStrings.empty() && |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2336 | ExistingProtocols.empty() && SelectorTable.empty()) |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2337 | return NULL; |
Eli Friedman | 1b8956e | 2008-06-01 16:00:02 +0000 | [diff] [blame] | 2338 | |
Fariborz Jahanian | d9a1db3 | 2009-09-10 21:48:21 +0000 | [diff] [blame] | 2339 | // Add all referenced protocols to a category. |
| 2340 | GenerateProtocolHolderCategory(); |
| 2341 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2342 | llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>( |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 2343 | SelectorTy->getElementType()); |
Jay Foad | ef6de3d | 2011-07-11 09:56:20 +0000 | [diff] [blame] | 2344 | llvm::Type *SelStructPtrTy = SelectorTy; |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 2345 | if (SelStructTy == 0) { |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2346 | SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, NULL); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2347 | SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy); |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 2348 | } |
| 2349 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2350 | std::vector<llvm::Constant*> Elements; |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 2351 | llvm::Constant *Statics = NULLPtr; |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2352 | // Generate statics list: |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 2353 | if (ConstantStrings.size()) { |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2354 | llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty, |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 2355 | ConstantStrings.size() + 1); |
| 2356 | ConstantStrings.push_back(NULLPtr); |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2357 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2358 | StringRef StringClass = CGM.getLangOpts().ObjCConstantStringClass; |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2359 | |
Daniel Dunbar | 1b09695 | 2009-11-29 02:38:47 +0000 | [diff] [blame] | 2360 | if (StringClass.empty()) StringClass = "NXConstantString"; |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2361 | |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2362 | Elements.push_back(MakeConstantString(StringClass, |
| 2363 | ".objc_static_class_name")); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2364 | Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 2365 | ConstantStrings)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2366 | llvm::StructType *StaticsListTy = |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2367 | llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2368 | llvm::Type *StaticsListPtrTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2369 | llvm::PointerType::getUnqual(StaticsListTy); |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 2370 | Statics = MakeGlobal(StaticsListTy, Elements, ".objc_statics"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2371 | llvm::ArrayType *StaticsListArrayTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2372 | llvm::ArrayType::get(StaticsListPtrTy, 2); |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 2373 | Elements.clear(); |
| 2374 | Elements.push_back(Statics); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2375 | Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy)); |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 2376 | Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr"); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2377 | Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy); |
Chris Lattner | 71238f6 | 2009-04-25 23:19:45 +0000 | [diff] [blame] | 2378 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2379 | // Array of classes, categories, and constant objects |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2380 | llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2381 | Classes.size() + Categories.size() + 2); |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2382 | llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy, |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2383 | llvm::Type::getInt16Ty(VMContext), |
| 2384 | llvm::Type::getInt16Ty(VMContext), |
Chris Lattner | 630404b | 2008-06-26 04:10:42 +0000 | [diff] [blame] | 2385 | ClassListTy, NULL); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2386 | |
| 2387 | Elements.clear(); |
| 2388 | // Pointer to an array of selectors used in this module. |
| 2389 | std::vector<llvm::Constant*> Selectors; |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2390 | std::vector<llvm::GlobalAlias*> SelectorAliases; |
| 2391 | for (SelectorMap::iterator iter = SelectorTable.begin(), |
| 2392 | iterEnd = SelectorTable.end(); iter != iterEnd ; ++iter) { |
| 2393 | |
| 2394 | std::string SelNameStr = iter->first.getAsString(); |
| 2395 | llvm::Constant *SelName = ExportUniqueString(SelNameStr, ".objc_sel_name"); |
| 2396 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2397 | SmallVectorImpl<TypedSelector> &Types = iter->second; |
| 2398 | for (SmallVectorImpl<TypedSelector>::iterator i = Types.begin(), |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2399 | e = Types.end() ; i!=e ; i++) { |
| 2400 | |
| 2401 | llvm::Constant *SelectorTypeEncoding = NULLPtr; |
| 2402 | if (!i->first.empty()) |
| 2403 | SelectorTypeEncoding = MakeConstantString(i->first, ".objc_sel_types"); |
| 2404 | |
| 2405 | Elements.push_back(SelName); |
| 2406 | Elements.push_back(SelectorTypeEncoding); |
| 2407 | Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements)); |
| 2408 | Elements.clear(); |
| 2409 | |
| 2410 | // Store the selector alias for later replacement |
| 2411 | SelectorAliases.push_back(i->second); |
| 2412 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2413 | } |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2414 | unsigned SelectorCount = Selectors.size(); |
| 2415 | // NULL-terminate the selector list. This should not actually be required, |
| 2416 | // because the selector list has a length field. Unfortunately, the GCC |
| 2417 | // runtime decides to ignore the length field and expects a NULL terminator, |
| 2418 | // and GCC cooperates with this by always setting the length to 0. |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2419 | Elements.push_back(NULLPtr); |
| 2420 | Elements.push_back(NULLPtr); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2421 | Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2422 | Elements.clear(); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2423 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2424 | // Number of static selectors |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2425 | Elements.push_back(llvm::ConstantInt::get(LongTy, SelectorCount)); |
| 2426 | llvm::Constant *SelectorList = MakeGlobalArray(SelStructTy, Selectors, |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2427 | ".objc_selector_list"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2428 | Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList, |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 2429 | SelStructPtrTy)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2430 | |
| 2431 | // Now that all of the static selectors exist, create pointers to them. |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2432 | for (unsigned int i=0 ; i<SelectorCount ; i++) { |
| 2433 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2434 | llvm::Constant *Idxs[] = {Zeros[0], |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 2435 | llvm::ConstantInt::get(Int32Ty, i), Zeros[0]}; |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2436 | // FIXME: We're generating redundant loads and stores here! |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 2437 | llvm::Constant *SelPtr = llvm::ConstantExpr::getGetElementPtr(SelectorList, |
Jay Foad | a5c0434 | 2011-07-21 14:31:17 +0000 | [diff] [blame] | 2438 | makeArrayRef(Idxs, 2)); |
Chris Lattner | e160c9b | 2009-01-27 05:06:01 +0000 | [diff] [blame] | 2439 | // If selectors are defined as an opaque type, cast the pointer to this |
| 2440 | // type. |
David Chisnall | c7ef462 | 2011-03-23 22:52:06 +0000 | [diff] [blame] | 2441 | SelPtr = llvm::ConstantExpr::getBitCast(SelPtr, SelectorTy); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2442 | SelectorAliases[i]->replaceAllUsesWith(SelPtr); |
| 2443 | SelectorAliases[i]->eraseFromParent(); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2444 | } |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2445 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2446 | // Number of classes defined. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2447 | Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext), |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2448 | Classes.size())); |
| 2449 | // Number of categories defined |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2450 | Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext), |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2451 | Categories.size())); |
| 2452 | // Create an array of classes, then categories, then static object instances |
| 2453 | Classes.insert(Classes.end(), Categories.begin(), Categories.end()); |
| 2454 | // NULL-terminated list of static object instances (mainly constant strings) |
| 2455 | Classes.push_back(Statics); |
| 2456 | Classes.push_back(NULLPtr); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2457 | llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2458 | Elements.push_back(ClassList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2459 | // Construct the symbol table |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2460 | llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements); |
| 2461 | |
| 2462 | // The symbol table is contained in a module which has some version-checking |
| 2463 | // constants |
Chris Lattner | 7650d95 | 2011-06-18 22:49:11 +0000 | [diff] [blame] | 2464 | llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy, |
David Chisnall | a212003 | 2011-05-22 22:37:08 +0000 | [diff] [blame] | 2465 | PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), |
David Chisnall | f074885 | 2011-07-07 11:22:31 +0000 | [diff] [blame] | 2466 | (RuntimeVersion >= 10) ? IntTy : NULL, NULL); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2467 | Elements.clear(); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2468 | // Runtime version, used for ABI compatibility checking. |
| 2469 | Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion)); |
Fariborz Jahanian | 91a0b51 | 2009-04-01 19:49:42 +0000 | [diff] [blame] | 2470 | // sizeof(ModuleTy) |
Micah Villmow | 25a6a84 | 2012-10-08 16:25:52 +0000 | [diff] [blame] | 2471 | llvm::DataLayout td(&TheModule); |
Ken Dyck | e0afc89 | 2011-04-22 17:59:22 +0000 | [diff] [blame] | 2472 | Elements.push_back( |
| 2473 | llvm::ConstantInt::get(LongTy, |
| 2474 | td.getTypeSizeInBits(ModuleTy) / |
| 2475 | CGM.getContext().getCharWidth())); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2476 | |
| 2477 | // The path to the source file where this module was declared |
| 2478 | SourceManager &SM = CGM.getContext().getSourceManager(); |
| 2479 | const FileEntry *mainFile = SM.getFileEntryForID(SM.getMainFileID()); |
| 2480 | std::string path = |
| 2481 | std::string(mainFile->getDir()->getName()) + '/' + mainFile->getName(); |
| 2482 | Elements.push_back(MakeConstantString(path, ".objc_source_file_name")); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2483 | Elements.push_back(SymTab); |
David Chisnall | a212003 | 2011-05-22 22:37:08 +0000 | [diff] [blame] | 2484 | |
David Chisnall | f074885 | 2011-07-07 11:22:31 +0000 | [diff] [blame] | 2485 | if (RuntimeVersion >= 10) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2486 | switch (CGM.getLangOpts().getGC()) { |
David Chisnall | f074885 | 2011-07-07 11:22:31 +0000 | [diff] [blame] | 2487 | case LangOptions::GCOnly: |
David Chisnall | a212003 | 2011-05-22 22:37:08 +0000 | [diff] [blame] | 2488 | Elements.push_back(llvm::ConstantInt::get(IntTy, 2)); |
David Chisnall | a212003 | 2011-05-22 22:37:08 +0000 | [diff] [blame] | 2489 | break; |
David Chisnall | f074885 | 2011-07-07 11:22:31 +0000 | [diff] [blame] | 2490 | case LangOptions::NonGC: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2491 | if (CGM.getLangOpts().ObjCAutoRefCount) |
David Chisnall | f074885 | 2011-07-07 11:22:31 +0000 | [diff] [blame] | 2492 | Elements.push_back(llvm::ConstantInt::get(IntTy, 1)); |
| 2493 | else |
| 2494 | Elements.push_back(llvm::ConstantInt::get(IntTy, 0)); |
| 2495 | break; |
| 2496 | case LangOptions::HybridGC: |
| 2497 | Elements.push_back(llvm::ConstantInt::get(IntTy, 1)); |
| 2498 | break; |
| 2499 | } |
David Chisnall | a212003 | 2011-05-22 22:37:08 +0000 | [diff] [blame] | 2500 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2501 | llvm::Value *Module = MakeGlobal(ModuleTy, Elements); |
| 2502 | |
| 2503 | // Create the load function calling the runtime entry point with the module |
| 2504 | // structure |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2505 | llvm::Function * LoadFunction = llvm::Function::Create( |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2506 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false), |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2507 | llvm::GlobalValue::InternalLinkage, ".objc_load_function", |
| 2508 | &TheModule); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2509 | llvm::BasicBlock *EntryBB = |
| 2510 | llvm::BasicBlock::Create(VMContext, "entry", LoadFunction); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2511 | CGBuilderTy Builder(VMContext); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2512 | Builder.SetInsertPoint(EntryBB); |
Fariborz Jahanian | 26c8294 | 2009-03-30 18:02:14 +0000 | [diff] [blame] | 2513 | |
Benjamin Kramer | 95d318c | 2011-05-28 14:26:31 +0000 | [diff] [blame] | 2514 | llvm::FunctionType *FT = |
Jay Foad | da549e8 | 2011-07-29 13:56:53 +0000 | [diff] [blame] | 2515 | llvm::FunctionType::get(Builder.getVoidTy(), |
| 2516 | llvm::PointerType::getUnqual(ModuleTy), true); |
Benjamin Kramer | 95d318c | 2011-05-28 14:26:31 +0000 | [diff] [blame] | 2517 | llvm::Value *Register = CGM.CreateRuntimeFunction(FT, "__objc_exec_class"); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2518 | Builder.CreateCall(Register, Module); |
David Chisnall | 29254f4 | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 2519 | |
David Chisnall | dccaa23 | 2012-02-01 19:16:56 +0000 | [diff] [blame] | 2520 | if (!ClassAliases.empty()) { |
David Chisnall | 29254f4 | 2012-01-31 18:59:20 +0000 | [diff] [blame] | 2521 | llvm::Type *ArgTypes[2] = {PtrTy, PtrToInt8Ty}; |
| 2522 | llvm::FunctionType *RegisterAliasTy = |
| 2523 | llvm::FunctionType::get(Builder.getVoidTy(), |
| 2524 | ArgTypes, false); |
| 2525 | llvm::Function *RegisterAlias = llvm::Function::Create( |
| 2526 | RegisterAliasTy, |
| 2527 | llvm::GlobalValue::ExternalWeakLinkage, "class_registerAlias_np", |
| 2528 | &TheModule); |
| 2529 | llvm::BasicBlock *AliasBB = |
| 2530 | llvm::BasicBlock::Create(VMContext, "alias", LoadFunction); |
| 2531 | llvm::BasicBlock *NoAliasBB = |
| 2532 | llvm::BasicBlock::Create(VMContext, "no_alias", LoadFunction); |
| 2533 | |
| 2534 | // Branch based on whether the runtime provided class_registerAlias_np() |
| 2535 | llvm::Value *HasRegisterAlias = Builder.CreateICmpNE(RegisterAlias, |
| 2536 | llvm::Constant::getNullValue(RegisterAlias->getType())); |
| 2537 | Builder.CreateCondBr(HasRegisterAlias, AliasBB, NoAliasBB); |
| 2538 | |
| 2539 | // The true branch (has alias registration fucntion): |
| 2540 | Builder.SetInsertPoint(AliasBB); |
| 2541 | // Emit alias registration calls: |
| 2542 | for (std::vector<ClassAliasPair>::iterator iter = ClassAliases.begin(); |
| 2543 | iter != ClassAliases.end(); ++iter) { |
| 2544 | llvm::Constant *TheClass = |
| 2545 | TheModule.getGlobalVariable(("_OBJC_CLASS_" + iter->first).c_str(), |
| 2546 | true); |
| 2547 | if (0 != TheClass) { |
| 2548 | TheClass = llvm::ConstantExpr::getBitCast(TheClass, PtrTy); |
| 2549 | Builder.CreateCall2(RegisterAlias, TheClass, |
| 2550 | MakeConstantString(iter->second)); |
| 2551 | } |
| 2552 | } |
| 2553 | // Jump to end: |
| 2554 | Builder.CreateBr(NoAliasBB); |
| 2555 | |
| 2556 | // Missing alias registration function, just return from the function: |
| 2557 | Builder.SetInsertPoint(NoAliasBB); |
| 2558 | } |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2559 | Builder.CreateRetVoid(); |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2560 | |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2561 | return LoadFunction; |
| 2562 | } |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2563 | |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2564 | llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2565 | const ObjCContainerDecl *CD) { |
| 2566 | const ObjCCategoryImplDecl *OCD = |
Steve Naroff | 3e0a540 | 2009-01-08 19:41:02 +0000 | [diff] [blame] | 2567 | dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext()); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2568 | StringRef CategoryName = OCD ? OCD->getName() : ""; |
| 2569 | StringRef ClassName = CD->getName(); |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2570 | Selector MethodName = OMD->getSelector(); |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 2571 | bool isClassMethod = !OMD->isInstanceMethod(); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2572 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2573 | CodeGenTypes &Types = CGM.getTypes(); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 2574 | llvm::FunctionType *MethodTy = |
John McCall | de5d3c7 | 2012-02-17 03:33:10 +0000 | [diff] [blame] | 2575 | Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD)); |
Anton Korobeynikov | 20ff310 | 2008-06-01 14:13:53 +0000 | [diff] [blame] | 2576 | std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName, |
| 2577 | MethodName, isClassMethod); |
| 2578 | |
Daniel Dunbar | d6c93d7 | 2009-09-17 04:01:22 +0000 | [diff] [blame] | 2579 | llvm::Function *Method |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2580 | = llvm::Function::Create(MethodTy, |
| 2581 | llvm::GlobalValue::InternalLinkage, |
| 2582 | FunctionName, |
| 2583 | &TheModule); |
Chris Lattner | 391d77a | 2008-03-30 23:03:07 +0000 | [diff] [blame] | 2584 | return Method; |
| 2585 | } |
| 2586 | |
David Chisnall | 789ecde | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 2587 | llvm::Constant *CGObjCGNU::GetPropertyGetFunction() { |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2588 | return GetPropertyFn; |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2589 | } |
| 2590 | |
David Chisnall | 789ecde | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 2591 | llvm::Constant *CGObjCGNU::GetPropertySetFunction() { |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2592 | return SetPropertyFn; |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2593 | } |
| 2594 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2595 | llvm::Constant *CGObjCGNU::GetOptimizedPropertySetFunction(bool atomic, |
| 2596 | bool copy) { |
| 2597 | return 0; |
| 2598 | } |
| 2599 | |
David Chisnall | 789ecde | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 2600 | llvm::Constant *CGObjCGNU::GetGetStructFunction() { |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2601 | return GetStructPropertyFn; |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 2602 | } |
David Chisnall | 789ecde | 2011-05-23 22:33:28 +0000 | [diff] [blame] | 2603 | llvm::Constant *CGObjCGNU::GetSetStructFunction() { |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2604 | return SetStructPropertyFn; |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 2605 | } |
David Chisnall | d397cfe | 2012-12-17 18:54:24 +0000 | [diff] [blame] | 2606 | llvm::Constant *CGObjCGNU::GetCppAtomicObjectGetFunction() { |
| 2607 | return 0; |
| 2608 | } |
| 2609 | llvm::Constant *CGObjCGNU::GetCppAtomicObjectSetFunction() { |
Fariborz Jahanian | e317302 | 2012-01-06 18:07:23 +0000 | [diff] [blame] | 2610 | return 0; |
| 2611 | } |
Fariborz Jahanian | 6cc5906 | 2010-04-12 18:18:10 +0000 | [diff] [blame] | 2612 | |
Daniel Dunbar | 309a436 | 2009-07-24 07:40:24 +0000 | [diff] [blame] | 2613 | llvm::Constant *CGObjCGNU::EnumerationMutationFunction() { |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2614 | return EnumerationMutationFn; |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2615 | } |
| 2616 | |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2617 | void CGObjCGNU::EmitSynchronizedStmt(CodeGenFunction &CGF, |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2618 | const ObjCAtSynchronizedStmt &S) { |
David Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 2619 | EmitAtSynchronizedStmt(CGF, S, SyncEnterFn, SyncExitFn); |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2620 | } |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 2621 | |
David Chisnall | 0faa516 | 2009-12-24 02:26:34 +0000 | [diff] [blame] | 2622 | |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2623 | void CGObjCGNU::EmitTryStmt(CodeGenFunction &CGF, |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2624 | const ObjCAtTryStmt &S) { |
| 2625 | // Unlike the Apple non-fragile runtimes, which also uses |
| 2626 | // unwind-based zero cost exceptions, the GNU Objective C runtime's |
| 2627 | // EH support isn't a veneer over C++ EH. Instead, exception |
David Chisnall | c686004 | 2012-11-07 16:50:40 +0000 | [diff] [blame] | 2628 | // objects are created by objc_exception_throw and destroyed by |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 2629 | // the personality function; this avoids the need for bracketing |
| 2630 | // catch handlers with calls to __blah_begin_catch/__blah_end_catch |
| 2631 | // (or even _Unwind_DeleteException), but probably doesn't |
| 2632 | // interoperate very well with foreign exceptions. |
David Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 2633 | // |
David Chisnall | 80558d2 | 2011-03-20 21:35:39 +0000 | [diff] [blame] | 2634 | // In Objective-C++ mode, we actually emit something equivalent to the C++ |
David Chisnall | 9735ca6 | 2011-03-25 11:57:33 +0000 | [diff] [blame] | 2635 | // exception handler. |
| 2636 | EmitTryCatchStmt(CGF, S, EnterCatchFn, ExitCatchFn, ExceptionReThrowFn); |
| 2637 | return ; |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2638 | } |
| 2639 | |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2640 | void CGObjCGNU::EmitThrowStmt(CodeGenFunction &CGF, |
Fariborz Jahanian | 6a3c70e | 2013-01-10 19:02:56 +0000 | [diff] [blame^] | 2641 | const ObjCAtThrowStmt &S, |
| 2642 | bool ClearInsertionPoint) { |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 2643 | llvm::Value *ExceptionAsObject; |
| 2644 | |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 2645 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
John McCall | 2b014d6 | 2011-10-01 10:32:24 +0000 | [diff] [blame] | 2646 | llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr); |
Fariborz Jahanian | 1e64a95 | 2009-05-17 16:49:27 +0000 | [diff] [blame] | 2647 | ExceptionAsObject = Exception; |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 2648 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2649 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Chris Lattner | 5dc0867 | 2009-05-08 00:11:50 +0000 | [diff] [blame] | 2650 | "Unexpected rethrow outside @catch block."); |
| 2651 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
| 2652 | } |
Benjamin Kramer | 578faa8 | 2011-09-27 21:06:10 +0000 | [diff] [blame] | 2653 | ExceptionAsObject = CGF.Builder.CreateBitCast(ExceptionAsObject, IdTy); |
David Chisnall | c686004 | 2012-11-07 16:50:40 +0000 | [diff] [blame] | 2654 | llvm::CallSite Throw = |
| 2655 | CGF.EmitCallOrInvoke(ExceptionThrowFn, ExceptionAsObject); |
| 2656 | Throw.setDoesNotReturn(); |
Eli Friedman | c972c92 | 2012-08-10 21:26:17 +0000 | [diff] [blame] | 2657 | CGF.Builder.CreateUnreachable(); |
Fariborz Jahanian | 6a3c70e | 2013-01-10 19:02:56 +0000 | [diff] [blame^] | 2658 | if (ClearInsertionPoint) |
| 2659 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2660 | } |
| 2661 | |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2662 | llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2663 | llvm::Value *AddrWeakObj) { |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2664 | CGBuilderTy B = CGF.Builder; |
David Chisnall | 31fc0c1 | 2011-05-30 12:00:26 +0000 | [diff] [blame] | 2665 | AddrWeakObj = EnforceType(B, AddrWeakObj, PtrToIdTy); |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2666 | return B.CreateCall(WeakReadFn, AddrWeakObj); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2667 | } |
| 2668 | |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2669 | void CGObjCGNU::EmitObjCWeakAssign(CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2670 | llvm::Value *src, llvm::Value *dst) { |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2671 | CGBuilderTy B = CGF.Builder; |
| 2672 | src = EnforceType(B, src, IdTy); |
| 2673 | dst = EnforceType(B, dst, PtrToIdTy); |
| 2674 | B.CreateCall2(WeakAssignFn, src, dst); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2675 | } |
| 2676 | |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2677 | void CGObjCGNU::EmitObjCGlobalAssign(CodeGenFunction &CGF, |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 2678 | llvm::Value *src, llvm::Value *dst, |
| 2679 | bool threadlocal) { |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2680 | CGBuilderTy B = CGF.Builder; |
| 2681 | src = EnforceType(B, src, IdTy); |
| 2682 | dst = EnforceType(B, dst, PtrToIdTy); |
Fariborz Jahanian | 021a7a6 | 2010-07-20 20:30:03 +0000 | [diff] [blame] | 2683 | if (!threadlocal) |
| 2684 | B.CreateCall2(GlobalAssignFn, src, dst); |
| 2685 | else |
| 2686 | // FIXME. Add threadloca assign API |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2687 | llvm_unreachable("EmitObjCGlobalAssign - Threal Local API NYI"); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2688 | } |
| 2689 | |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2690 | void CGObjCGNU::EmitObjCIvarAssign(CodeGenFunction &CGF, |
Fariborz Jahanian | 6c7a1f3 | 2009-09-24 22:25:38 +0000 | [diff] [blame] | 2691 | llvm::Value *src, llvm::Value *dst, |
| 2692 | llvm::Value *ivarOffset) { |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2693 | CGBuilderTy B = CGF.Builder; |
| 2694 | src = EnforceType(B, src, IdTy); |
David Chisnall | b44eda3 | 2011-05-25 20:33:17 +0000 | [diff] [blame] | 2695 | dst = EnforceType(B, dst, IdTy); |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2696 | B.CreateCall3(IvarAssignFn, src, dst, ivarOffset); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2697 | } |
| 2698 | |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2699 | void CGObjCGNU::EmitObjCStrongCastAssign(CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2700 | llvm::Value *src, llvm::Value *dst) { |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2701 | CGBuilderTy B = CGF.Builder; |
| 2702 | src = EnforceType(B, src, IdTy); |
| 2703 | dst = EnforceType(B, dst, PtrToIdTy); |
| 2704 | B.CreateCall2(StrongCastAssignFn, src, dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2705 | } |
| 2706 | |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2707 | void CGObjCGNU::EmitGCMemmoveCollectable(CodeGenFunction &CGF, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2708 | llvm::Value *DestPtr, |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2709 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 2710 | llvm::Value *Size) { |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2711 | CGBuilderTy B = CGF.Builder; |
David Chisnall | 68e5e13 | 2011-05-28 14:23:43 +0000 | [diff] [blame] | 2712 | DestPtr = EnforceType(B, DestPtr, PtrTy); |
| 2713 | SrcPtr = EnforceType(B, SrcPtr, PtrTy); |
David Chisnall | ef6e0f3 | 2010-02-03 15:59:02 +0000 | [diff] [blame] | 2714 | |
Fariborz Jahanian | 55bcace | 2010-06-15 22:44:06 +0000 | [diff] [blame] | 2715 | B.CreateCall3(MemMoveFn, DestPtr, SrcPtr, Size); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2716 | } |
| 2717 | |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2718 | llvm::GlobalVariable *CGObjCGNU::ObjCIvarOffsetVariable( |
| 2719 | const ObjCInterfaceDecl *ID, |
| 2720 | const ObjCIvarDecl *Ivar) { |
| 2721 | const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString() |
| 2722 | + '.' + Ivar->getNameAsString(); |
| 2723 | // Emit the variable and initialize it with what we think the correct value |
| 2724 | // is. This allows code compiled with non-fragile ivars to work correctly |
| 2725 | // when linked against code which isn't (most of the time). |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2726 | llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name); |
| 2727 | if (!IvarOffsetPointer) { |
David Chisnall | e0d9876 | 2010-11-03 16:12:44 +0000 | [diff] [blame] | 2728 | // This will cause a run-time crash if we accidentally use it. A value of |
| 2729 | // 0 would seem more sensible, but will silently overwrite the isa pointer |
| 2730 | // causing a great deal of confusion. |
| 2731 | uint64_t Offset = -1; |
| 2732 | // We can't call ComputeIvarBaseOffset() here if we have the |
| 2733 | // implementation, because it will create an invalid ASTRecordLayout object |
| 2734 | // that we are then stuck with forever, so we only initialize the ivar |
| 2735 | // offset variable with a guess if we only have the interface. The |
| 2736 | // initializer will be reset later anyway, when we are generating the class |
| 2737 | // description. |
| 2738 | if (!CGM.getContext().getObjCImplementation( |
Dan Gohman | cb421fa | 2010-04-19 16:39:44 +0000 | [diff] [blame] | 2739 | const_cast<ObjCInterfaceDecl *>(ID))) |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 2740 | Offset = ComputeIvarBaseOffset(CGM, ID, Ivar); |
David Chisnall | d901da5 | 2010-04-19 01:37:25 +0000 | [diff] [blame] | 2741 | |
David Chisnall | 49de528 | 2011-10-08 08:54:36 +0000 | [diff] [blame] | 2742 | llvm::ConstantInt *OffsetGuess = llvm::ConstantInt::get(Int32Ty, Offset, |
Richard Trieu | 243f108 | 2011-09-21 02:46:06 +0000 | [diff] [blame] | 2743 | /*isSigned*/true); |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2744 | // Don't emit the guess in non-PIC code because the linker will not be able |
| 2745 | // to replace it with the real version for a library. In non-PIC code you |
| 2746 | // must compile with the fragile ABI if you want to use ivars from a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2747 | // GCC-compiled class. |
Chandler Carruth | 5e219cf | 2012-04-08 16:40:35 +0000 | [diff] [blame] | 2748 | if (CGM.getLangOpts().PICLevel || CGM.getLangOpts().PIELevel) { |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2749 | llvm::GlobalVariable *IvarOffsetGV = new llvm::GlobalVariable(TheModule, |
David Chisnall | 917b28b | 2011-10-04 15:35:30 +0000 | [diff] [blame] | 2750 | Int32Ty, false, |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2751 | llvm::GlobalValue::PrivateLinkage, OffsetGuess, Name+".guess"); |
| 2752 | IvarOffsetPointer = new llvm::GlobalVariable(TheModule, |
| 2753 | IvarOffsetGV->getType(), false, llvm::GlobalValue::LinkOnceAnyLinkage, |
| 2754 | IvarOffsetGV, Name); |
| 2755 | } else { |
| 2756 | IvarOffsetPointer = new llvm::GlobalVariable(TheModule, |
Benjamin Kramer | 3c0ef8c | 2009-10-13 10:07:13 +0000 | [diff] [blame] | 2757 | llvm::Type::getInt32PtrTy(VMContext), false, |
| 2758 | llvm::GlobalValue::ExternalLinkage, 0, Name); |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2759 | } |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2760 | } |
David Chisnall | 8a5a9aa | 2009-08-31 16:41:57 +0000 | [diff] [blame] | 2761 | return IvarOffsetPointer; |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2764 | LValue CGObjCGNU::EmitObjCValueForIvar(CodeGenFunction &CGF, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2765 | QualType ObjectTy, |
| 2766 | llvm::Value *BaseValue, |
| 2767 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2768 | unsigned CVRQualifiers) { |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2769 | const ObjCInterfaceDecl *ID = |
| 2770 | ObjectTy->getAs<ObjCObjectType>()->getInterface(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2771 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 2772 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2773 | } |
Mike Stump | bb1c860 | 2009-07-31 21:31:32 +0000 | [diff] [blame] | 2774 | |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2775 | static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context, |
| 2776 | const ObjCInterfaceDecl *OID, |
| 2777 | const ObjCIvarDecl *OIVD) { |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 2778 | for (const ObjCIvarDecl *next = OID->all_declared_ivar_begin(); next; |
| 2779 | next = next->getNextIvar()) { |
| 2780 | if (OIVD == next) |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2781 | return OID; |
| 2782 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2783 | |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2784 | // Otherwise check in the super class. |
| 2785 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
| 2786 | return FindIvarInterface(Context, Super, OIVD); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2787 | |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2788 | return 0; |
| 2789 | } |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2790 | |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2791 | llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 2792 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2793 | const ObjCIvarDecl *Ivar) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 2794 | if (CGM.getLangOpts().ObjCRuntime.isNonFragile()) { |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2795 | Interface = FindIvarInterface(CGM.getContext(), Interface, Ivar); |
David Chisnall | 63ff703 | 2011-07-07 12:34:51 +0000 | [diff] [blame] | 2796 | if (RuntimeVersion < 10) |
| 2797 | return CGF.Builder.CreateZExtOrBitCast( |
| 2798 | CGF.Builder.CreateLoad(CGF.Builder.CreateLoad( |
| 2799 | ObjCIvarOffsetVariable(Interface, Ivar), false, "ivar")), |
| 2800 | PtrDiffTy); |
| 2801 | std::string name = "__objc_ivar_offset_value_" + |
| 2802 | Interface->getNameAsString() +"." + Ivar->getNameAsString(); |
| 2803 | llvm::Value *Offset = TheModule.getGlobalVariable(name); |
| 2804 | if (!Offset) |
| 2805 | Offset = new llvm::GlobalVariable(TheModule, IntTy, |
David Chisnall | 3fc81d3 | 2011-08-01 17:36:53 +0000 | [diff] [blame] | 2806 | false, llvm::GlobalValue::LinkOnceAnyLinkage, |
| 2807 | llvm::Constant::getNullValue(IntTy), name); |
David Chisnall | 6614845 | 2012-04-06 15:39:12 +0000 | [diff] [blame] | 2808 | Offset = CGF.Builder.CreateLoad(Offset); |
| 2809 | if (Offset->getType() != PtrDiffTy) |
| 2810 | Offset = CGF.Builder.CreateZExtOrBitCast(Offset, PtrDiffTy); |
| 2811 | return Offset; |
Fariborz Jahanian | 9cd96ff | 2009-05-20 18:41:51 +0000 | [diff] [blame] | 2812 | } |
Eli Friedman | e5b4666 | 2012-11-06 22:15:52 +0000 | [diff] [blame] | 2813 | uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar); |
| 2814 | return llvm::ConstantInt::get(PtrDiffTy, Offset, /*isSigned*/true); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2815 | } |
| 2816 | |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2817 | CGObjCRuntime * |
| 2818 | clang::CodeGen::CreateGNUObjCRuntime(CodeGenModule &CGM) { |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 2819 | switch (CGM.getLangOpts().ObjCRuntime.getKind()) { |
David Chisnall | 11d3f4c | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 2820 | case ObjCRuntime::GNUstep: |
David Chisnall | 9f6614e | 2011-03-23 16:36:54 +0000 | [diff] [blame] | 2821 | return new CGObjCGNUstep(CGM); |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 2822 | |
David Chisnall | 11d3f4c | 2012-07-03 20:49:52 +0000 | [diff] [blame] | 2823 | case ObjCRuntime::GCC: |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 2824 | return new CGObjCGCC(CGM); |
| 2825 | |
John McCall | f7226fb | 2012-07-12 02:07:58 +0000 | [diff] [blame] | 2826 | case ObjCRuntime::ObjFW: |
| 2827 | return new CGObjCObjFW(CGM); |
| 2828 | |
John McCall | 260611a | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 2829 | case ObjCRuntime::FragileMacOSX: |
| 2830 | case ObjCRuntime::MacOSX: |
| 2831 | case ObjCRuntime::iOS: |
| 2832 | llvm_unreachable("these runtimes are not GNU runtimes"); |
| 2833 | } |
| 2834 | llvm_unreachable("bad runtime"); |
Chris Lattner | 0f98426 | 2008-03-01 08:50:34 +0000 | [diff] [blame] | 2835 | } |