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