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