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