Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1 | //===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===// |
| 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 | // |
| 10 | // This provides Objective-C code generation targetting the Apple runtime. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CGObjCRuntime.h" |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 15 | |
| 16 | #include "CodeGenModule.h" |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 17 | #include "CodeGenFunction.h" |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 19 | #include "clang/AST/Decl.h" |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 21 | #include "clang/AST/RecordLayout.h" |
Chris Lattner | 16f0049 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 22 | #include "clang/AST/StmtObjC.h" |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 23 | #include "clang/Basic/LangOptions.h" |
| 24 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 25 | #include "llvm/Intrinsics.h" |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 26 | #include "llvm/LLVMContext.h" |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 27 | #include "llvm/Module.h" |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/DenseSet.h" |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame^] | 29 | #include "llvm/ADT/SetVector.h" |
| 30 | #include "llvm/ADT/SmallString.h" |
| 31 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetData.h" |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 33 | #include <sstream> |
Torok Edwin | f42e4a6 | 2009-08-24 13:25:12 +0000 | [diff] [blame] | 34 | #include <cstdio> |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 35 | |
| 36 | using namespace clang; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 37 | using namespace CodeGen; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 38 | |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 39 | // Common CGObjCRuntime functions, these don't belong here, but they |
| 40 | // don't belong in CGObjCRuntime either so we will live with it for |
| 41 | // now. |
| 42 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 43 | /// FindIvarInterface - Find the interface containing the ivar. |
Daniel Dunbar | a243578 | 2009-04-22 12:00:04 +0000 | [diff] [blame] | 44 | /// |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 45 | /// FIXME: We shouldn't need to do this, the containing context should |
| 46 | /// be fixed. |
| 47 | static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context, |
| 48 | const ObjCInterfaceDecl *OID, |
| 49 | const ObjCIvarDecl *OIVD, |
| 50 | unsigned &Index) { |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 51 | // FIXME: The index here is closely tied to how |
| 52 | // ASTContext::getObjCLayout is implemented. This should be fixed to |
| 53 | // get the information from the layout directly. |
| 54 | Index = 0; |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 55 | llvm::SmallVector<ObjCIvarDecl*, 16> Ivars; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 56 | Context.ShallowCollectObjCIvars(OID, Ivars); |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 57 | for (unsigned k = 0, e = Ivars.size(); k != e; ++k) { |
| 58 | if (OIVD == Ivars[k]) |
| 59 | return OID; |
| 60 | ++Index; |
Daniel Dunbar | a80a0f6 | 2009-04-22 17:43:55 +0000 | [diff] [blame] | 61 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 62 | |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 63 | // Otherwise check in the super class. |
Daniel Dunbar | a81419d | 2009-05-05 00:36:57 +0000 | [diff] [blame] | 64 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 65 | return FindIvarInterface(Context, Super, OIVD, Index); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 66 | |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 67 | return 0; |
Daniel Dunbar | a243578 | 2009-04-22 12:00:04 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 70 | static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM, |
| 71 | const ObjCInterfaceDecl *OID, |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 72 | const ObjCImplementationDecl *ID, |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 73 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 74 | unsigned Index; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 75 | const ObjCInterfaceDecl *Container = |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 76 | FindIvarInterface(CGM.getContext(), OID, Ivar, Index); |
| 77 | assert(Container && "Unable to find ivar container"); |
| 78 | |
| 79 | // If we know have an implementation (and the ivar is in it) then |
| 80 | // look up in the implementation layout. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 81 | const ASTRecordLayout *RL; |
Daniel Dunbar | 532d4da | 2009-05-03 13:15:50 +0000 | [diff] [blame] | 82 | if (ID && ID->getClassInterface() == Container) |
| 83 | RL = &CGM.getContext().getASTObjCImplementationLayout(ID); |
| 84 | else |
| 85 | RL = &CGM.getContext().getASTObjCInterfaceLayout(Container); |
| 86 | return RL->getFieldOffset(Index); |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, |
| 90 | const ObjCInterfaceDecl *OID, |
| 91 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 92 | return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8; |
| 93 | } |
| 94 | |
| 95 | uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM, |
| 96 | const ObjCImplementationDecl *OID, |
| 97 | const ObjCIvarDecl *Ivar) { |
| 98 | return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8; |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF, |
| 102 | const ObjCInterfaceDecl *OID, |
| 103 | llvm::Value *BaseValue, |
| 104 | const ObjCIvarDecl *Ivar, |
| 105 | unsigned CVRQualifiers, |
| 106 | llvm::Value *Offset) { |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 107 | // Compute (type*) ( (char *) BaseValue + Offset) |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 108 | llvm::Type *I8Ptr = |
| 109 | llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(CGF.getLLVMContext())); |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 110 | QualType IvarTy = Ivar->getType(); |
| 111 | const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 112 | llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 113 | V = CGF.Builder.CreateGEP(V, Offset, "add.ptr"); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 114 | V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 115 | |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 116 | if (Ivar->isBitField()) { |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 117 | // We need to compute the bit offset for the bit-field, the offset |
| 118 | // is to the byte. Note, there is a subtle invariant here: we can |
| 119 | // only call this routine on non-sythesized ivars but we may be |
| 120 | // called for synthesized ivars. However, a synthesized ivar can |
| 121 | // never be a bit-field so this is safe. |
| 122 | uint64_t BitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar) % 8; |
| 123 | |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 124 | uint64_t BitFieldSize = |
| 125 | Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue(); |
| 126 | return LValue::MakeBitfield(V, BitOffset, BitFieldSize, |
Daniel Dunbar | e38df86 | 2009-05-03 07:52:00 +0000 | [diff] [blame] | 127 | IvarTy->isSignedIntegerType(), |
| 128 | IvarTy.getCVRQualifiers()|CVRQualifiers); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Daniel Dunbar | 1d7e539 | 2009-05-03 08:55:17 +0000 | [diff] [blame] | 131 | LValue LV = LValue::MakeAddr(V, IvarTy.getCVRQualifiers()|CVRQualifiers, |
| 132 | CGF.CGM.getContext().getObjCGCAttrKind(IvarTy)); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 133 | LValue::SetObjCIvar(LV, true); |
| 134 | return LV; |
| 135 | } |
| 136 | |
| 137 | /// |
| 138 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 139 | namespace { |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 140 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 141 | typedef std::vector<llvm::Constant*> ConstantVector; |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 142 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 143 | // FIXME: We should find a nicer way to make the labels for metadata, string |
| 144 | // concatenation is lame. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 145 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 146 | class ObjCCommonTypesHelper { |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 147 | protected: |
| 148 | llvm::LLVMContext &VMContext; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 149 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 150 | private: |
| 151 | llvm::Constant *getMessageSendFn() const { |
| 152 | // id objc_msgSend (id, SEL, ...) |
| 153 | std::vector<const llvm::Type*> Params; |
| 154 | Params.push_back(ObjectPtrTy); |
| 155 | Params.push_back(SelectorPtrTy); |
| 156 | return |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 157 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
| 158 | Params, true), |
| 159 | "objc_msgSend"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 160 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 161 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 162 | llvm::Constant *getMessageSendStretFn() const { |
| 163 | // id objc_msgSend_stret (id, SEL, ...) |
| 164 | std::vector<const llvm::Type*> Params; |
| 165 | Params.push_back(ObjectPtrTy); |
| 166 | Params.push_back(SelectorPtrTy); |
| 167 | return |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 168 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 169 | Params, true), |
| 170 | "objc_msgSend_stret"); |
| 171 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 172 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 173 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 174 | llvm::Constant *getMessageSendFpretFn() const { |
| 175 | // FIXME: This should be long double on x86_64? |
| 176 | // [double | long double] objc_msgSend_fpret(id self, SEL op, ...) |
| 177 | std::vector<const llvm::Type*> Params; |
| 178 | Params.push_back(ObjectPtrTy); |
| 179 | Params.push_back(SelectorPtrTy); |
| 180 | return |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 181 | CGM.CreateRuntimeFunction(llvm::FunctionType::get( |
| 182 | llvm::Type::getDoubleTy(VMContext), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 183 | Params, |
| 184 | true), |
| 185 | "objc_msgSend_fpret"); |
| 186 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 187 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 188 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 189 | llvm::Constant *getMessageSendSuperFn() const { |
| 190 | // id objc_msgSendSuper(struct objc_super *super, SEL op, ...) |
| 191 | const char *SuperName = "objc_msgSendSuper"; |
| 192 | std::vector<const llvm::Type*> Params; |
| 193 | Params.push_back(SuperPtrTy); |
| 194 | Params.push_back(SelectorPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 195 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 196 | Params, true), |
| 197 | SuperName); |
| 198 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 199 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 200 | llvm::Constant *getMessageSendSuperFn2() const { |
| 201 | // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...) |
| 202 | const char *SuperName = "objc_msgSendSuper2"; |
| 203 | std::vector<const llvm::Type*> Params; |
| 204 | Params.push_back(SuperPtrTy); |
| 205 | Params.push_back(SelectorPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 206 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 207 | Params, true), |
| 208 | SuperName); |
| 209 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 210 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 211 | llvm::Constant *getMessageSendSuperStretFn() const { |
| 212 | // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super, |
| 213 | // SEL op, ...) |
| 214 | std::vector<const llvm::Type*> Params; |
| 215 | Params.push_back(Int8PtrTy); |
| 216 | Params.push_back(SuperPtrTy); |
| 217 | Params.push_back(SelectorPtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 218 | return CGM.CreateRuntimeFunction( |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 219 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 220 | Params, true), |
| 221 | "objc_msgSendSuper_stret"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 222 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 223 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 224 | llvm::Constant *getMessageSendSuperStretFn2() const { |
| 225 | // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super, |
| 226 | // SEL op, ...) |
| 227 | std::vector<const llvm::Type*> Params; |
| 228 | Params.push_back(Int8PtrTy); |
| 229 | Params.push_back(SuperPtrTy); |
| 230 | Params.push_back(SelectorPtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 231 | return CGM.CreateRuntimeFunction( |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 232 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 233 | Params, true), |
| 234 | "objc_msgSendSuper2_stret"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 235 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 236 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 237 | llvm::Constant *getMessageSendSuperFpretFn() const { |
| 238 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 239 | return getMessageSendSuperFn(); |
| 240 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 241 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 242 | llvm::Constant *getMessageSendSuperFpretFn2() const { |
| 243 | // There is no objc_msgSendSuper_fpret? How can that work? |
| 244 | return getMessageSendSuperFn2(); |
| 245 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 246 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 247 | protected: |
| 248 | CodeGen::CodeGenModule &CGM; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 249 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 250 | public: |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 251 | const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 252 | const llvm::Type *Int8PtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 253 | |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 254 | /// ObjectPtrTy - LLVM type for object handles (typeof(id)) |
| 255 | const llvm::Type *ObjectPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 256 | |
Fariborz Jahanian | 6d657c4 | 2008-11-18 20:18:11 +0000 | [diff] [blame] | 257 | /// PtrObjectPtrTy - LLVM type for id * |
| 258 | const llvm::Type *PtrObjectPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 259 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 260 | /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL)) |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 261 | const llvm::Type *SelectorPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 262 | /// ProtocolPtrTy - LLVM type for external protocol handles |
| 263 | /// (typeof(Protocol)) |
| 264 | const llvm::Type *ExternalProtocolPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 265 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 266 | // SuperCTy - clang type for struct objc_super. |
| 267 | QualType SuperCTy; |
| 268 | // SuperPtrCTy - clang type for struct objc_super *. |
| 269 | QualType SuperPtrCTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 270 | |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 271 | /// SuperTy - LLVM type for struct objc_super. |
| 272 | const llvm::StructType *SuperTy; |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 273 | /// SuperPtrTy - LLVM type for struct objc_super *. |
| 274 | const llvm::Type *SuperPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 275 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 276 | /// PropertyTy - LLVM type for struct objc_property (struct _prop_t |
| 277 | /// in GCC parlance). |
| 278 | const llvm::StructType *PropertyTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 279 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 280 | /// PropertyListTy - LLVM type for struct objc_property_list |
| 281 | /// (_prop_list_t in GCC parlance). |
| 282 | const llvm::StructType *PropertyListTy; |
| 283 | /// PropertyListPtrTy - LLVM type for struct objc_property_list*. |
| 284 | const llvm::Type *PropertyListPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 285 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 286 | // MethodTy - LLVM type for struct objc_method. |
| 287 | const llvm::StructType *MethodTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 288 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 289 | /// CacheTy - LLVM type for struct objc_cache. |
| 290 | const llvm::Type *CacheTy; |
| 291 | /// CachePtrTy - LLVM type for struct objc_cache *. |
| 292 | const llvm::Type *CachePtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 293 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 294 | llvm::Constant *getGetPropertyFn() { |
| 295 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 296 | ASTContext &Ctx = CGM.getContext(); |
| 297 | // id objc_getProperty (id, SEL, ptrdiff_t, bool) |
| 298 | llvm::SmallVector<QualType,16> Params; |
| 299 | QualType IdType = Ctx.getObjCIdType(); |
| 300 | QualType SelType = Ctx.getObjCSelType(); |
| 301 | Params.push_back(IdType); |
| 302 | Params.push_back(SelType); |
| 303 | Params.push_back(Ctx.LongTy); |
| 304 | Params.push_back(Ctx.BoolTy); |
| 305 | const llvm::FunctionType *FTy = |
| 306 | Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false); |
| 307 | return CGM.CreateRuntimeFunction(FTy, "objc_getProperty"); |
| 308 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 309 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 310 | llvm::Constant *getSetPropertyFn() { |
| 311 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 312 | ASTContext &Ctx = CGM.getContext(); |
| 313 | // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool) |
| 314 | llvm::SmallVector<QualType,16> Params; |
| 315 | QualType IdType = Ctx.getObjCIdType(); |
| 316 | QualType SelType = Ctx.getObjCSelType(); |
| 317 | Params.push_back(IdType); |
| 318 | Params.push_back(SelType); |
| 319 | Params.push_back(Ctx.LongTy); |
| 320 | Params.push_back(IdType); |
| 321 | Params.push_back(Ctx.BoolTy); |
| 322 | Params.push_back(Ctx.BoolTy); |
| 323 | const llvm::FunctionType *FTy = |
| 324 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
| 325 | return CGM.CreateRuntimeFunction(FTy, "objc_setProperty"); |
| 326 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 327 | |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 328 | llvm::Constant *getEnumerationMutationFn() { |
Daniel Dunbar | c1ab900 | 2009-07-11 20:32:50 +0000 | [diff] [blame] | 329 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 330 | ASTContext &Ctx = CGM.getContext(); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 331 | // void objc_enumerationMutation (id) |
Daniel Dunbar | c1ab900 | 2009-07-11 20:32:50 +0000 | [diff] [blame] | 332 | llvm::SmallVector<QualType,16> Params; |
Daniel Dunbar | 309a436 | 2009-07-24 07:40:24 +0000 | [diff] [blame] | 333 | Params.push_back(Ctx.getObjCIdType()); |
Daniel Dunbar | c1ab900 | 2009-07-11 20:32:50 +0000 | [diff] [blame] | 334 | const llvm::FunctionType *FTy = |
| 335 | Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 336 | return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation"); |
| 337 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 338 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 339 | /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function. |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 340 | llvm::Constant *getGcReadWeakFn() { |
| 341 | // id objc_read_weak (id *) |
| 342 | std::vector<const llvm::Type*> Args; |
| 343 | Args.push_back(ObjectPtrTy->getPointerTo()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 344 | llvm::FunctionType *FTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 345 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 346 | return CGM.CreateRuntimeFunction(FTy, "objc_read_weak"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 349 | /// GcAssignWeakFn -- LLVM objc_assign_weak function. |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 350 | llvm::Constant *getGcAssignWeakFn() { |
| 351 | // id objc_assign_weak (id, id *) |
| 352 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 353 | Args.push_back(ObjectPtrTy->getPointerTo()); |
| 354 | llvm::FunctionType *FTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 355 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 356 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak"); |
| 357 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 358 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 359 | /// GcAssignGlobalFn -- LLVM objc_assign_global function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 360 | llvm::Constant *getGcAssignGlobalFn() { |
| 361 | // id objc_assign_global(id, id *) |
| 362 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 363 | Args.push_back(ObjectPtrTy->getPointerTo()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 364 | llvm::FunctionType *FTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 365 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 366 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_global"); |
| 367 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 368 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 369 | /// GcAssignIvarFn -- LLVM objc_assign_ivar function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 370 | llvm::Constant *getGcAssignIvarFn() { |
| 371 | // id objc_assign_ivar(id, id *) |
| 372 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 373 | Args.push_back(ObjectPtrTy->getPointerTo()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 374 | llvm::FunctionType *FTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 375 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 376 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar"); |
| 377 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 378 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 379 | /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function. |
| 380 | llvm::Constant *GcMemmoveCollectableFn() { |
| 381 | // void *objc_memmove_collectable(void *dst, const void *src, size_t size) |
| 382 | std::vector<const llvm::Type*> Args(1, Int8PtrTy); |
| 383 | Args.push_back(Int8PtrTy); |
| 384 | Args.push_back(LongTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 385 | llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 386 | return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable"); |
| 387 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 388 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 389 | /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 390 | llvm::Constant *getGcAssignStrongCastFn() { |
| 391 | // id objc_assign_global(id, id *) |
| 392 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 393 | Args.push_back(ObjectPtrTy->getPointerTo()); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 394 | llvm::FunctionType *FTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 395 | llvm::FunctionType::get(ObjectPtrTy, Args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 396 | return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast"); |
| 397 | } |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 398 | |
| 399 | /// ExceptionThrowFn - LLVM objc_exception_throw function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 400 | llvm::Constant *getExceptionThrowFn() { |
| 401 | // void objc_exception_throw(id) |
| 402 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 403 | llvm::FunctionType *FTy = |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 404 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 405 | return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw"); |
| 406 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 407 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 408 | /// SyncEnterFn - LLVM object_sync_enter function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 409 | llvm::Constant *getSyncEnterFn() { |
| 410 | // void objc_sync_enter (id) |
| 411 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 412 | llvm::FunctionType *FTy = |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 413 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 414 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter"); |
| 415 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 416 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 417 | /// SyncExitFn - LLVM object_sync_exit function. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 418 | llvm::Constant *getSyncExitFn() { |
| 419 | // void objc_sync_exit (id) |
| 420 | std::vector<const llvm::Type*> Args(1, ObjectPtrTy); |
| 421 | llvm::FunctionType *FTy = |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 422 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 423 | return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit"); |
| 424 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 425 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 426 | llvm::Constant *getSendFn(bool IsSuper) const { |
| 427 | return IsSuper ? getMessageSendSuperFn() : getMessageSendFn(); |
| 428 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 429 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 430 | llvm::Constant *getSendFn2(bool IsSuper) const { |
| 431 | return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn(); |
| 432 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 433 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 434 | llvm::Constant *getSendStretFn(bool IsSuper) const { |
| 435 | return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn(); |
| 436 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 437 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 438 | llvm::Constant *getSendStretFn2(bool IsSuper) const { |
| 439 | return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn(); |
| 440 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 441 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 442 | llvm::Constant *getSendFpretFn(bool IsSuper) const { |
| 443 | return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn(); |
| 444 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 445 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 446 | llvm::Constant *getSendFpretFn2(bool IsSuper) const { |
| 447 | return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn(); |
| 448 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 449 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 450 | ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm); |
| 451 | ~ObjCCommonTypesHelper(){} |
| 452 | }; |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 453 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 454 | /// ObjCTypesHelper - Helper class that encapsulates lazy |
| 455 | /// construction of varies types used during ObjC generation. |
| 456 | class ObjCTypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 457 | public: |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 458 | /// SymtabTy - LLVM type for struct objc_symtab. |
| 459 | const llvm::StructType *SymtabTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 460 | /// SymtabPtrTy - LLVM type for struct objc_symtab *. |
| 461 | const llvm::Type *SymtabPtrTy; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 462 | /// ModuleTy - LLVM type for struct objc_module. |
| 463 | const llvm::StructType *ModuleTy; |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 464 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 465 | /// ProtocolTy - LLVM type for struct objc_protocol. |
| 466 | const llvm::StructType *ProtocolTy; |
| 467 | /// ProtocolPtrTy - LLVM type for struct objc_protocol *. |
| 468 | const llvm::Type *ProtocolPtrTy; |
| 469 | /// ProtocolExtensionTy - LLVM type for struct |
| 470 | /// objc_protocol_extension. |
| 471 | const llvm::StructType *ProtocolExtensionTy; |
| 472 | /// ProtocolExtensionTy - LLVM type for struct |
| 473 | /// objc_protocol_extension *. |
| 474 | const llvm::Type *ProtocolExtensionPtrTy; |
| 475 | /// MethodDescriptionTy - LLVM type for struct |
| 476 | /// objc_method_description. |
| 477 | const llvm::StructType *MethodDescriptionTy; |
| 478 | /// MethodDescriptionListTy - LLVM type for struct |
| 479 | /// objc_method_description_list. |
| 480 | const llvm::StructType *MethodDescriptionListTy; |
| 481 | /// MethodDescriptionListPtrTy - LLVM type for struct |
| 482 | /// objc_method_description_list *. |
| 483 | const llvm::Type *MethodDescriptionListPtrTy; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 484 | /// ProtocolListTy - LLVM type for struct objc_property_list. |
| 485 | const llvm::Type *ProtocolListTy; |
| 486 | /// ProtocolListPtrTy - LLVM type for struct objc_property_list*. |
| 487 | const llvm::Type *ProtocolListPtrTy; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 488 | /// CategoryTy - LLVM type for struct objc_category. |
| 489 | const llvm::StructType *CategoryTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 490 | /// ClassTy - LLVM type for struct objc_class. |
| 491 | const llvm::StructType *ClassTy; |
| 492 | /// ClassPtrTy - LLVM type for struct objc_class *. |
| 493 | const llvm::Type *ClassPtrTy; |
| 494 | /// ClassExtensionTy - LLVM type for struct objc_class_ext. |
| 495 | const llvm::StructType *ClassExtensionTy; |
| 496 | /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *. |
| 497 | const llvm::Type *ClassExtensionPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 498 | // IvarTy - LLVM type for struct objc_ivar. |
| 499 | const llvm::StructType *IvarTy; |
| 500 | /// IvarListTy - LLVM type for struct objc_ivar_list. |
| 501 | const llvm::Type *IvarListTy; |
| 502 | /// IvarListPtrTy - LLVM type for struct objc_ivar_list *. |
| 503 | const llvm::Type *IvarListPtrTy; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 504 | /// MethodListTy - LLVM type for struct objc_method_list. |
| 505 | const llvm::Type *MethodListTy; |
| 506 | /// MethodListPtrTy - LLVM type for struct objc_method_list *. |
| 507 | const llvm::Type *MethodListPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 508 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 509 | /// ExceptionDataTy - LLVM type for struct _objc_exception_data. |
| 510 | const llvm::Type *ExceptionDataTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 511 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 512 | /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 513 | llvm::Constant *getExceptionTryEnterFn() { |
| 514 | std::vector<const llvm::Type*> Params; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 515 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 516 | return CGM.CreateRuntimeFunction( |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 517 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 518 | Params, false), |
| 519 | "objc_exception_try_enter"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 520 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 521 | |
| 522 | /// ExceptionTryExitFn - LLVM objc_exception_try_exit function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 523 | llvm::Constant *getExceptionTryExitFn() { |
| 524 | std::vector<const llvm::Type*> Params; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 525 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 526 | return CGM.CreateRuntimeFunction( |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 527 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 528 | Params, false), |
| 529 | "objc_exception_try_exit"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 530 | } |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 531 | |
| 532 | /// ExceptionExtractFn - LLVM objc_exception_extract function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 533 | llvm::Constant *getExceptionExtractFn() { |
| 534 | std::vector<const llvm::Type*> Params; |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 535 | Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy)); |
| 536 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 537 | Params, false), |
| 538 | "objc_exception_extract"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 539 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 540 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 541 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 542 | /// ExceptionMatchFn - LLVM objc_exception_match function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 543 | llvm::Constant *getExceptionMatchFn() { |
| 544 | std::vector<const llvm::Type*> Params; |
| 545 | Params.push_back(ClassPtrTy); |
| 546 | Params.push_back(ObjectPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 547 | return CGM.CreateRuntimeFunction( |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 548 | llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 549 | Params, false), |
| 550 | "objc_exception_match"); |
| 551 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 552 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 553 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 554 | /// SetJmpFn - LLVM _setjmp function. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 555 | llvm::Constant *getSetJmpFn() { |
| 556 | std::vector<const llvm::Type*> Params; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 557 | Params.push_back(llvm::PointerType::getUnqual(llvm::Type::getInt32Ty(VMContext))); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 558 | return |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 559 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext), |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 560 | Params, false), |
| 561 | "_setjmp"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 562 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 563 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 564 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 565 | public: |
| 566 | ObjCTypesHelper(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 567 | ~ObjCTypesHelper() {} |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 568 | }; |
| 569 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 570 | /// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 571 | /// modern abi |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 572 | class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper { |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 573 | public: |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 574 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 575 | // MethodListnfABITy - LLVM for struct _method_list_t |
| 576 | const llvm::StructType *MethodListnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 577 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 578 | // MethodListnfABIPtrTy - LLVM for struct _method_list_t* |
| 579 | const llvm::Type *MethodListnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 580 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 581 | // ProtocolnfABITy = LLVM for struct _protocol_t |
| 582 | const llvm::StructType *ProtocolnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 583 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 584 | // ProtocolnfABIPtrTy = LLVM for struct _protocol_t* |
| 585 | const llvm::Type *ProtocolnfABIPtrTy; |
| 586 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 587 | // ProtocolListnfABITy - LLVM for struct _objc_protocol_list |
| 588 | const llvm::StructType *ProtocolListnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 589 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 590 | // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list* |
| 591 | const llvm::Type *ProtocolListnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 592 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 593 | // ClassnfABITy - LLVM for struct _class_t |
| 594 | const llvm::StructType *ClassnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 595 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 596 | // ClassnfABIPtrTy - LLVM for struct _class_t* |
| 597 | const llvm::Type *ClassnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 598 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 599 | // IvarnfABITy - LLVM for struct _ivar_t |
| 600 | const llvm::StructType *IvarnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 601 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 602 | // IvarListnfABITy - LLVM for struct _ivar_list_t |
| 603 | const llvm::StructType *IvarListnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 604 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 605 | // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t* |
| 606 | const llvm::Type *IvarListnfABIPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 607 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 608 | // ClassRonfABITy - LLVM for struct _class_ro_t |
| 609 | const llvm::StructType *ClassRonfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 610 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 611 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 612 | const llvm::Type *ImpnfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 613 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 614 | // CategorynfABITy - LLVM for struct _category_t |
| 615 | const llvm::StructType *CategorynfABITy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 616 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 617 | // New types for nonfragile abi messaging. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 618 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 619 | // MessageRefTy - LLVM for: |
| 620 | // struct _message_ref_t { |
| 621 | // IMP messenger; |
| 622 | // SEL name; |
| 623 | // }; |
| 624 | const llvm::StructType *MessageRefTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 625 | // MessageRefCTy - clang type for struct _message_ref_t |
| 626 | QualType MessageRefCTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 627 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 628 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
| 629 | const llvm::Type *MessageRefPtrTy; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 630 | // MessageRefCPtrTy - clang type for struct _message_ref_t* |
| 631 | QualType MessageRefCPtrTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 632 | |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 633 | // MessengerTy - Type of the messenger (shown as IMP above) |
| 634 | const llvm::FunctionType *MessengerTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 635 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 636 | // SuperMessageRefTy - LLVM for: |
| 637 | // struct _super_message_ref_t { |
| 638 | // SUPER_IMP messenger; |
| 639 | // SEL name; |
| 640 | // }; |
| 641 | const llvm::StructType *SuperMessageRefTy; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 642 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 643 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
| 644 | const llvm::Type *SuperMessageRefPtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 645 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 646 | llvm::Constant *getMessageSendFixupFn() { |
| 647 | // id objc_msgSend_fixup(id, struct message_ref_t*, ...) |
| 648 | std::vector<const llvm::Type*> Params; |
| 649 | Params.push_back(ObjectPtrTy); |
| 650 | Params.push_back(MessageRefPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 651 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 652 | Params, true), |
| 653 | "objc_msgSend_fixup"); |
| 654 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 655 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 656 | llvm::Constant *getMessageSendFpretFixupFn() { |
| 657 | // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...) |
| 658 | std::vector<const llvm::Type*> Params; |
| 659 | Params.push_back(ObjectPtrTy); |
| 660 | Params.push_back(MessageRefPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 661 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 662 | Params, true), |
| 663 | "objc_msgSend_fpret_fixup"); |
| 664 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 665 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 666 | llvm::Constant *getMessageSendStretFixupFn() { |
| 667 | // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...) |
| 668 | std::vector<const llvm::Type*> Params; |
| 669 | Params.push_back(ObjectPtrTy); |
| 670 | Params.push_back(MessageRefPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 671 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 672 | Params, true), |
| 673 | "objc_msgSend_stret_fixup"); |
| 674 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 675 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 676 | llvm::Constant *getMessageSendIdFixupFn() { |
| 677 | // id objc_msgSendId_fixup(id, struct message_ref_t*, ...) |
| 678 | std::vector<const llvm::Type*> Params; |
| 679 | Params.push_back(ObjectPtrTy); |
| 680 | Params.push_back(MessageRefPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 681 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 682 | Params, true), |
| 683 | "objc_msgSendId_fixup"); |
| 684 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 685 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 686 | llvm::Constant *getMessageSendIdStretFixupFn() { |
| 687 | // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...) |
| 688 | std::vector<const llvm::Type*> Params; |
| 689 | Params.push_back(ObjectPtrTy); |
| 690 | Params.push_back(MessageRefPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 691 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 692 | Params, true), |
| 693 | "objc_msgSendId_stret_fixup"); |
| 694 | } |
| 695 | llvm::Constant *getMessageSendSuper2FixupFn() { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 696 | // id objc_msgSendSuper2_fixup (struct objc_super *, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 697 | // struct _super_message_ref_t*, ...) |
| 698 | std::vector<const llvm::Type*> Params; |
| 699 | Params.push_back(SuperPtrTy); |
| 700 | Params.push_back(SuperMessageRefPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 701 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 702 | Params, true), |
| 703 | "objc_msgSendSuper2_fixup"); |
| 704 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 705 | |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 706 | llvm::Constant *getMessageSendSuper2StretFixupFn() { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 707 | // id objc_msgSendSuper2_stret_fixup(struct objc_super *, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 708 | // struct _super_message_ref_t*, ...) |
| 709 | std::vector<const llvm::Type*> Params; |
| 710 | Params.push_back(SuperPtrTy); |
| 711 | Params.push_back(SuperMessageRefPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 712 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy, |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 713 | Params, true), |
| 714 | "objc_msgSendSuper2_stret_fixup"); |
| 715 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 716 | |
| 717 | |
| 718 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 719 | /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C |
| 720 | /// exception personality function. |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 721 | llvm::Value *getEHPersonalityPtr() { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 722 | llvm::Constant *Personality = |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 723 | CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext), |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 724 | true), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 725 | "__objc_personality_v0"); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 726 | return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 727 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 728 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 729 | llvm::Constant *getUnwindResumeOrRethrowFn() { |
| 730 | std::vector<const llvm::Type*> Params; |
| 731 | Params.push_back(Int8PtrTy); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 732 | return CGM.CreateRuntimeFunction( |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 733 | llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 734 | Params, false), |
| 735 | "_Unwind_Resume_or_Rethrow"); |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 736 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 737 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 738 | llvm::Constant *getObjCEndCatchFn() { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 739 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), |
Chris Lattner | b59761b | 2009-07-01 04:13:52 +0000 | [diff] [blame] | 740 | false), |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 741 | "objc_end_catch"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 742 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 743 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 744 | |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 745 | llvm::Constant *getObjCBeginCatchFn() { |
| 746 | std::vector<const llvm::Type*> Params; |
| 747 | Params.push_back(Int8PtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 748 | return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy, |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 749 | Params, false), |
| 750 | "objc_begin_catch"); |
| 751 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 752 | |
| 753 | const llvm::StructType *EHTypeTy; |
| 754 | const llvm::Type *EHTypePtrTy; |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 755 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 756 | ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm); |
| 757 | ~ObjCNonFragileABITypesHelper(){} |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 758 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 759 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 760 | class CGObjCCommonMac : public CodeGen::CGObjCRuntime { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 761 | public: |
| 762 | // FIXME - accessibility |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 763 | class GC_IVAR { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 764 | public: |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 765 | unsigned ivar_bytepos; |
| 766 | unsigned ivar_size; |
| 767 | GC_IVAR(unsigned bytepos = 0, unsigned size = 0) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 768 | : ivar_bytepos(bytepos), ivar_size(size) {} |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 769 | |
| 770 | // Allow sorting based on byte pos. |
| 771 | bool operator<(const GC_IVAR &b) const { |
| 772 | return ivar_bytepos < b.ivar_bytepos; |
| 773 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 774 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 775 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 776 | class SKIP_SCAN { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 777 | public: |
| 778 | unsigned skip; |
| 779 | unsigned scan; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 780 | SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0) |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 781 | : skip(_skip), scan(_scan) {} |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 782 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 783 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 784 | protected: |
| 785 | CodeGen::CodeGenModule &CGM; |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 786 | llvm::LLVMContext &VMContext; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 787 | // FIXME! May not be needing this after all. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 788 | unsigned ObjCABI; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 789 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 790 | // gc ivar layout bitmap calculation helper caches. |
| 791 | llvm::SmallVector<GC_IVAR, 16> SkipIvars; |
| 792 | llvm::SmallVector<GC_IVAR, 16> IvarsInfo; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 793 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 794 | /// LazySymbols - Symbols to generate a lazy reference for. See |
| 795 | /// DefinedSymbols and FinishModule(). |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame^] | 796 | llvm::SetVector<IdentifierInfo*> LazySymbols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 797 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 798 | /// DefinedSymbols - External symbols which are defined by this |
| 799 | /// module. The symbols in this list and LazySymbols are used to add |
| 800 | /// special linker symbols which ensure that Objective-C modules are |
| 801 | /// linked properly. |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame^] | 802 | llvm::SetVector<IdentifierInfo*> DefinedSymbols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 803 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 804 | /// ClassNames - uniqued class names. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 805 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 806 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 807 | /// MethodVarNames - uniqued method variable names. |
| 808 | llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 809 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 810 | /// MethodVarTypes - uniqued method type signatures. We have to use |
| 811 | /// a StringMap here because have no other unique reference. |
| 812 | llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 813 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 814 | /// MethodDefinitions - map of methods which have been defined in |
| 815 | /// this translation unit. |
| 816 | llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 817 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 818 | /// PropertyNames - uniqued method variable names. |
| 819 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 820 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 821 | /// ClassReferences - uniqued class references. |
| 822 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 823 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 824 | /// SelectorReferences - uniqued selector references. |
| 825 | llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 826 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 827 | /// Protocols - Protocols for which an objc_protocol structure has |
| 828 | /// been emitted. Forward declarations are handled by creating an |
| 829 | /// empty structure whose initializer is filled in when/if defined. |
| 830 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 831 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 832 | /// DefinedProtocols - Protocols which have actually been |
| 833 | /// defined. We should not need this, see FIXME in GenerateProtocol. |
| 834 | llvm::DenseSet<IdentifierInfo*> DefinedProtocols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 835 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 836 | /// DefinedClasses - List of defined classes. |
| 837 | std::vector<llvm::GlobalValue*> DefinedClasses; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 838 | |
| 839 | /// DefinedNonLazyClasses - List of defined "non-lazy" classes. |
| 840 | std::vector<llvm::GlobalValue*> DefinedNonLazyClasses; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 841 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 842 | /// DefinedCategories - List of defined categories. |
| 843 | std::vector<llvm::GlobalValue*> DefinedCategories; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 844 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 845 | /// DefinedNonLazyCategories - List of defined "non-lazy" categories. |
| 846 | std::vector<llvm::GlobalValue*> DefinedNonLazyCategories; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 847 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 848 | /// GetNameForMethod - Return a name for the given method. |
| 849 | /// \param[out] NameOut - The return value. |
| 850 | void GetNameForMethod(const ObjCMethodDecl *OMD, |
| 851 | const ObjCContainerDecl *CD, |
| 852 | std::string &NameOut); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 853 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 854 | /// GetMethodVarName - Return a unique constant for the given |
| 855 | /// selector's name. The return value has type char *. |
| 856 | llvm::Constant *GetMethodVarName(Selector Sel); |
| 857 | llvm::Constant *GetMethodVarName(IdentifierInfo *Ident); |
| 858 | llvm::Constant *GetMethodVarName(const std::string &Name); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 859 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 860 | /// GetMethodVarType - Return a unique constant for the given |
| 861 | /// selector's name. The return value has type char *. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 862 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 863 | // FIXME: This is a horrible name. |
| 864 | llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 865 | llvm::Constant *GetMethodVarType(const FieldDecl *D); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 866 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 867 | /// GetPropertyName - Return a unique constant for the given |
| 868 | /// name. The return value has type char *. |
| 869 | llvm::Constant *GetPropertyName(IdentifierInfo *Ident); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 870 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 871 | // FIXME: This can be dropped once string functions are unified. |
| 872 | llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 873 | const Decl *Container); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 874 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 875 | /// GetClassName - Return a unique constant for the given selector's |
| 876 | /// name. The return value has type char *. |
| 877 | llvm::Constant *GetClassName(IdentifierInfo *Ident); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 878 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 879 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 880 | /// implementation for the __strong or __weak case. |
| 881 | /// |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 882 | llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI, |
| 883 | bool ForStrongLayout); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 884 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 885 | void BuildAggrIvarRecordLayout(const RecordType *RT, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 886 | unsigned int BytePos, bool ForStrongLayout, |
| 887 | bool &HasUnion); |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 888 | void BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 889 | const llvm::StructLayout *Layout, |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 890 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 891 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 892 | unsigned int BytePos, bool ForStrongLayout, |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 893 | bool &HasUnion); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 894 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 895 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 896 | /// ivar layout bitmap. |
| 897 | llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident, |
| 898 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 899 | |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 900 | /// EmitPropertyList - Emit the given property list. The return |
| 901 | /// value has type PropertyListPtrTy. |
| 902 | llvm::Constant *EmitPropertyList(const std::string &Name, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 903 | const Decl *Container, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 904 | const ObjCContainerDecl *OCD, |
| 905 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 906 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 907 | /// GetProtocolRef - Return a reference to the internal protocol |
| 908 | /// description, creating an empty one if it has not been |
| 909 | /// defined. The return value has type ProtocolPtrTy. |
| 910 | llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD); |
Fariborz Jahanian | b21f07e | 2009-03-08 20:18:37 +0000 | [diff] [blame] | 911 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 912 | /// CreateMetadataVar - Create a global variable with internal |
| 913 | /// linkage for use by the Objective-C runtime. |
| 914 | /// |
| 915 | /// This is a convenience wrapper which not only creates the |
| 916 | /// variable, but also sets the section and alignment and adds the |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 917 | /// global to the "llvm.used" list. |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 918 | /// |
| 919 | /// \param Name - The variable name. |
| 920 | /// \param Init - The variable initializer; this is also used to |
| 921 | /// define the type of the variable. |
| 922 | /// \param Section - The section the variable should go into, or 0. |
| 923 | /// \param Align - The alignment for the variable, or 0. |
| 924 | /// \param AddToUsed - Whether the variable should be added to |
Daniel Dunbar | c158306 | 2009-04-14 17:42:51 +0000 | [diff] [blame] | 925 | /// "llvm.used". |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 926 | llvm::GlobalVariable *CreateMetadataVar(const std::string &Name, |
| 927 | llvm::Constant *Init, |
| 928 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 929 | unsigned Align, |
| 930 | bool AddToUsed); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 931 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 932 | CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF, |
| 933 | QualType ResultType, |
| 934 | llvm::Value *Sel, |
| 935 | llvm::Value *Arg0, |
| 936 | QualType Arg0Ty, |
| 937 | bool IsSuper, |
| 938 | const CallArgList &CallArgs, |
| 939 | const ObjCCommonTypesHelper &ObjCTypes); |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 940 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 941 | public: |
Owen Anderson | 6924382 | 2009-07-13 04:10:07 +0000 | [diff] [blame] | 942 | CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : |
| 943 | CGM(cgm), VMContext(cgm.getLLVMContext()) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 944 | { } |
| 945 | |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 946 | virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 947 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 948 | virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD, |
| 949 | const ObjCContainerDecl *CD=0); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 950 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 951 | virtual void GenerateProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 952 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 953 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 954 | /// declaration, emitting it if necessary. The return value has type |
| 955 | /// ProtocolPtrTy. |
| 956 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 957 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 958 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 959 | /// object for the given declaration, emitting it if needed. These |
| 960 | /// forward references will be filled in with empty bodies if no |
| 961 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 962 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 963 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 964 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 965 | class CGObjCMac : public CGObjCCommonMac { |
| 966 | private: |
| 967 | ObjCTypesHelper ObjCTypes; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 968 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 969 | /// level information. |
| 970 | void EmitImageInfo(); |
| 971 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 972 | /// EmitModuleInfo - Another marker encoding module level |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 973 | /// information. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 974 | void EmitModuleInfo(); |
| 975 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 976 | /// EmitModuleSymols - Emit module symbols, the list of defined |
| 977 | /// classes and categories. The result has type SymtabPtrTy. |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 978 | llvm::Constant *EmitModuleSymbols(); |
| 979 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 980 | /// FinishModule - Write out global data structures at the end of |
| 981 | /// processing a translation unit. |
| 982 | void FinishModule(); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 983 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 984 | /// EmitClassExtension - Generate the class extension structure used |
| 985 | /// to store the weak ivar layout and properties. The return value |
| 986 | /// has type ClassExtensionPtrTy. |
| 987 | llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID); |
| 988 | |
| 989 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 990 | /// for the given class. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 991 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 992 | const ObjCInterfaceDecl *ID); |
| 993 | |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 994 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 995 | QualType ResultType, |
| 996 | Selector Sel, |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 997 | llvm::Value *Arg0, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 998 | QualType Arg0Ty, |
| 999 | bool IsSuper, |
| 1000 | const CallArgList &CallArgs); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1001 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1002 | /// EmitIvarList - Emit the ivar list for the given |
| 1003 | /// implementation. If ForClass is true the list of class ivars |
| 1004 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1005 | /// interface ivars will be emitted. The return value has type |
| 1006 | /// IvarListPtrTy. |
| 1007 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 1008 | bool ForClass); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1009 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1010 | /// EmitMetaClass - Emit a forward reference to the class structure |
| 1011 | /// for the metaclass of the given interface. The return value has |
| 1012 | /// type ClassPtrTy. |
| 1013 | llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID); |
| 1014 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1015 | /// EmitMetaClass - Emit a class structure for the metaclass of the |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1016 | /// given implementation. The return value has type ClassPtrTy. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1017 | llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID, |
| 1018 | llvm::Constant *Protocols, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1019 | const ConstantVector &Methods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1020 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1021 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1022 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1023 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1024 | |
| 1025 | /// EmitMethodList - Emit the method list for the given |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 1026 | /// implementation. The return value has type MethodListPtrTy. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1027 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 1028 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1029 | const ConstantVector &Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1030 | |
| 1031 | /// EmitMethodDescList - Emit a method description list for a list of |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1032 | /// method declarations. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1033 | /// - TypeName: The name for the type containing the methods. |
| 1034 | /// - IsProtocol: True iff these methods are for a protocol. |
| 1035 | /// - ClassMethds: True iff these are class methods. |
| 1036 | /// - Required: When true, only "required" methods are |
| 1037 | /// listed. Similarly, when false only "optional" methods are |
| 1038 | /// listed. For classes this should always be true. |
| 1039 | /// - begin, end: The method list to output. |
| 1040 | /// |
| 1041 | /// The return value has type MethodDescriptionListPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1042 | llvm::Constant *EmitMethodDescList(const std::string &Name, |
| 1043 | const char *Section, |
| 1044 | const ConstantVector &Methods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1045 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1046 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1047 | /// declaration, emitting it if necessary. The return value has type |
| 1048 | /// ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1049 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1050 | |
| 1051 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1052 | /// object for the given declaration, emitting it if needed. These |
| 1053 | /// forward references will be filled in with empty bodies if no |
| 1054 | /// definition is seen. The return value has type ProtocolPtrTy. |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1055 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1056 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1057 | /// EmitProtocolExtension - Generate the protocol extension |
| 1058 | /// structure used to store optional instance and class methods, and |
| 1059 | /// protocol properties. The return value has type |
| 1060 | /// ProtocolExtensionPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1061 | llvm::Constant * |
| 1062 | EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1063 | const ConstantVector &OptInstanceMethods, |
| 1064 | const ConstantVector &OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1065 | |
| 1066 | /// EmitProtocolList - Generate the list of referenced |
| 1067 | /// protocols. The return value has type ProtocolListPtrTy. |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1068 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 1069 | ObjCProtocolDecl::protocol_iterator begin, |
| 1070 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1071 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1072 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1073 | /// for the given selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1074 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1075 | |
| 1076 | public: |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1077 | CGObjCMac(CodeGen::CodeGenModule &cgm); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1078 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1079 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1080 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1081 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1082 | QualType ResultType, |
| 1083 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1084 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1085 | bool IsClassMessage, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1086 | const CallArgList &CallArgs, |
| 1087 | const ObjCMethodDecl *Method); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1088 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1089 | virtual CodeGen::RValue |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1090 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1091 | QualType ResultType, |
| 1092 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1093 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1094 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1095 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1096 | bool IsClassMessage, |
| 1097 | const CallArgList &CallArgs); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1098 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1099 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1100 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1101 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1102 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel); |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1103 | |
| 1104 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1105 | /// untyped one. |
| 1106 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1107 | const ObjCMethodDecl *Method); |
| 1108 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1109 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1110 | |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1111 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1112 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1113 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1114 | const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1115 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1116 | virtual llvm::Constant *GetPropertyGetFunction(); |
| 1117 | virtual llvm::Constant *GetPropertySetFunction(); |
| 1118 | virtual llvm::Constant *EnumerationMutationFunction(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1119 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 1120 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 1121 | const Stmt &S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1122 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 1123 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1124 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1125 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 1126 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1127 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1128 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 1129 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 1130 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 1131 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 1132 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 1133 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1134 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1135 | llvm::Value *dest, llvm::Value *src, |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 1136 | QualType Ty); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1137 | |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1138 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1139 | QualType ObjectTy, |
| 1140 | llvm::Value *BaseValue, |
| 1141 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1142 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1143 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1144 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1145 | const ObjCIvarDecl *Ivar); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1146 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1147 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1148 | class CGObjCNonFragileABIMac : public CGObjCCommonMac { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1149 | private: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1150 | ObjCNonFragileABITypesHelper ObjCTypes; |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1151 | llvm::GlobalVariable* ObjCEmptyCacheVar; |
| 1152 | llvm::GlobalVariable* ObjCEmptyVtableVar; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1153 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1154 | /// SuperClassReferences - uniqued super class references. |
| 1155 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1156 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1157 | /// MetaClassReferences - uniqued meta class references. |
| 1158 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1159 | |
| 1160 | /// EHTypeReferences - uniqued class ehtype references. |
| 1161 | llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1162 | |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1163 | /// NonLegacyDispatchMethods - List of methods for which we do *not* generate |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1164 | /// legacy messaging dispatch. |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1165 | llvm::DenseSet<Selector> NonLegacyDispatchMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1166 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1167 | /// LegacyDispatchedSelector - Returns true if SEL is not in the list of |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1168 | /// NonLegacyDispatchMethods; false otherwise. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1169 | bool LegacyDispatchedSelector(Selector Sel); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1170 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1171 | /// FinishNonFragileABIModule - Write out global data structures at the end of |
| 1172 | /// processing a translation unit. |
| 1173 | void FinishNonFragileABIModule(); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1174 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 1175 | /// AddModuleClassList - Add the given list of class pointers to the |
| 1176 | /// module with the provided symbol and section names. |
| 1177 | void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container, |
| 1178 | const char *SymbolName, |
| 1179 | const char *SectionName); |
| 1180 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1181 | llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags, |
| 1182 | unsigned InstanceStart, |
| 1183 | unsigned InstanceSize, |
| 1184 | const ObjCImplementationDecl *ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1185 | llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1186 | llvm::Constant *IsAGV, |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 1187 | llvm::Constant *SuperClassGV, |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 1188 | llvm::Constant *ClassRoGV, |
| 1189 | bool HiddenVisibility); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1190 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1191 | llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1192 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 1193 | llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1194 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 1195 | /// EmitMethodList - Emit the method list for the given |
| 1196 | /// implementation. The return value has type MethodListnfABITy. |
| 1197 | llvm::Constant *EmitMethodList(const std::string &Name, |
| 1198 | const char *Section, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 1199 | const ConstantVector &Methods); |
| 1200 | /// EmitIvarList - Emit the ivar list for the given |
| 1201 | /// implementation. If ForClass is true the list of class ivars |
| 1202 | /// (i.e. metaclass ivars) is emitted, otherwise the list of |
| 1203 | /// interface ivars will be emitted. The return value has type |
| 1204 | /// IvarListnfABIPtrTy. |
| 1205 | llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1206 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1207 | llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID, |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 1208 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 1209 | unsigned long int offset); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1210 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1211 | /// GetOrEmitProtocol - Get the protocol object for the given |
| 1212 | /// declaration, emitting it if necessary. The return value has type |
| 1213 | /// ProtocolPtrTy. |
| 1214 | virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1215 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1216 | /// GetOrEmitProtocolRef - Get a forward reference to the protocol |
| 1217 | /// object for the given declaration, emitting it if needed. These |
| 1218 | /// forward references will be filled in with empty bodies if no |
| 1219 | /// definition is seen. The return value has type ProtocolPtrTy. |
| 1220 | virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1221 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1222 | /// EmitProtocolList - Generate the list of referenced |
| 1223 | /// protocols. The return value has type ProtocolListPtrTy. |
| 1224 | llvm::Constant *EmitProtocolList(const std::string &Name, |
| 1225 | ObjCProtocolDecl::protocol_iterator begin, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1226 | ObjCProtocolDecl::protocol_iterator end); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1227 | |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1228 | CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1229 | QualType ResultType, |
| 1230 | Selector Sel, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 1231 | llvm::Value *Receiver, |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 1232 | QualType Arg0Ty, |
| 1233 | bool IsSuper, |
| 1234 | const CallArgList &CallArgs); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 1235 | |
| 1236 | /// GetClassGlobal - Return the global variable for the Objective-C |
| 1237 | /// class of the given name. |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 1238 | llvm::GlobalVariable *GetClassGlobal(const std::string &Name); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1239 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1240 | /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1241 | /// for the given class reference. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1242 | llvm::Value *EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1243 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1244 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 1245 | /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy, |
| 1246 | /// for the given super class reference. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1247 | llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder, |
| 1248 | const ObjCInterfaceDecl *ID); |
| 1249 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1250 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 1251 | /// meta-data |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1252 | llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1253 | const ObjCInterfaceDecl *ID); |
| 1254 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 1255 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 1256 | /// the given ivar. |
| 1257 | /// |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 1258 | llvm::GlobalVariable * ObjCIvarOffsetVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1259 | const ObjCInterfaceDecl *ID, |
| 1260 | const ObjCIvarDecl *Ivar); |
| 1261 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1262 | /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy, |
| 1263 | /// for the given selector. |
| 1264 | llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1265 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1266 | /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 1267 | /// interface. The return value has type EHTypePtrTy. |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1268 | llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
| 1269 | bool ForDefinition); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1270 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1271 | const char *getMetaclassSymbolPrefix() const { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1272 | return "OBJC_METACLASS_$_"; |
| 1273 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1274 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 1275 | const char *getClassSymbolPrefix() const { |
| 1276 | return "OBJC_CLASS_$_"; |
| 1277 | } |
| 1278 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 1279 | void GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1280 | uint32_t &InstanceStart, |
| 1281 | uint32_t &InstanceSize); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1282 | |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1283 | // Shamelessly stolen from Analysis/CFRefCount.cpp |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1284 | Selector GetNullarySelector(const char* name) const { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1285 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1286 | return CGM.getContext().Selectors.getSelector(0, &II); |
| 1287 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1288 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1289 | Selector GetUnarySelector(const char* name) const { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 1290 | IdentifierInfo* II = &CGM.getContext().Idents.get(name); |
| 1291 | return CGM.getContext().Selectors.getSelector(1, &II); |
| 1292 | } |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 1293 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1294 | /// ImplementationIsNonLazy - Check whether the given category or |
| 1295 | /// class implementation is "non-lazy". |
Fariborz Jahanian | ecfbdcb | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 1296 | bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 1297 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1298 | public: |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 1299 | CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1300 | // FIXME. All stubs for now! |
| 1301 | virtual llvm::Function *ModuleInitFunction(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1302 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1303 | virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
| 1304 | QualType ResultType, |
| 1305 | Selector Sel, |
| 1306 | llvm::Value *Receiver, |
| 1307 | bool IsClassMessage, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1308 | const CallArgList &CallArgs, |
| 1309 | const ObjCMethodDecl *Method); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1310 | |
| 1311 | virtual CodeGen::RValue |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1312 | GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
| 1313 | QualType ResultType, |
| 1314 | Selector Sel, |
| 1315 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1316 | bool isCategoryImpl, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1317 | llvm::Value *Receiver, |
| 1318 | bool IsClassMessage, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 1319 | const CallArgList &CallArgs); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1320 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1321 | virtual llvm::Value *GetClass(CGBuilderTy &Builder, |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 1322 | const ObjCInterfaceDecl *ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1323 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1324 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel) |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 1325 | { return EmitSelector(Builder, Sel); } |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1326 | |
| 1327 | /// The NeXT/Apple runtimes do not support typed selectors; just emit an |
| 1328 | /// untyped one. |
| 1329 | virtual llvm::Value *GetSelector(CGBuilderTy &Builder, |
| 1330 | const ObjCMethodDecl *Method) |
| 1331 | { return EmitSelector(Builder, Method->getSelector()); } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1332 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 1333 | virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1334 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1335 | virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1336 | virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder, |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 1337 | const ObjCProtocolDecl *PD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1338 | |
| 1339 | virtual llvm::Constant *GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1340 | return ObjCTypes.getGetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1341 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1342 | virtual llvm::Constant *GetPropertySetFunction() { |
| 1343 | return ObjCTypes.getSetPropertyFn(); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1344 | } |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 1345 | virtual llvm::Constant *EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 1346 | return ObjCTypes.getEnumerationMutationFn(); |
Daniel Dunbar | 28ed084 | 2009-02-16 18:48:45 +0000 | [diff] [blame] | 1347 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1348 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1349 | virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 1350 | const Stmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1351 | virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 1352 | const ObjCAtThrowStmt &S); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1353 | virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1354 | llvm::Value *AddrWeakObj); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1355 | virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1356 | llvm::Value *src, llvm::Value *dst); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1357 | virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1358 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1359 | virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1360 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1361 | virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 1362 | llvm::Value *src, llvm::Value *dest); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 1363 | virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
| 1364 | llvm::Value *dest, llvm::Value *src, |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 1365 | QualType Ty); |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1366 | virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 1367 | QualType ObjectTy, |
| 1368 | llvm::Value *BaseValue, |
| 1369 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 1370 | unsigned CVRQualifiers); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1371 | virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 1372 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 1373 | const ObjCIvarDecl *Ivar); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1374 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1375 | |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1376 | } // end anonymous namespace |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1377 | |
| 1378 | /* *** Helper Functions *** */ |
| 1379 | |
| 1380 | /// getConstantGEP() - Help routine to construct simple GEPs. |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1381 | static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1382 | llvm::Constant *C, |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1383 | unsigned idx0, |
| 1384 | unsigned idx1) { |
| 1385 | llvm::Value *Idxs[] = { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 1386 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0), |
| 1387 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1388 | }; |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1389 | return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1392 | /// hasObjCExceptionAttribute - Return true if this class or any super |
| 1393 | /// class has the __objc_exception__ attribute. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1394 | static bool hasObjCExceptionAttribute(ASTContext &Context, |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1395 | const ObjCInterfaceDecl *OID) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1396 | if (OID->hasAttr<ObjCExceptionAttr>()) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1397 | return true; |
| 1398 | if (const ObjCInterfaceDecl *Super = OID->getSuperClass()) |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 1399 | return hasObjCExceptionAttribute(Context, Super); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 1400 | return false; |
| 1401 | } |
| 1402 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1403 | /* *** CGObjCMac Public Interface *** */ |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1404 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1405 | CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm), |
| 1406 | ObjCTypes(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1407 | { |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 1408 | ObjCABI = 1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1409 | EmitImageInfo(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 1412 | /// GetClass - Return a reference to the class for the given interface |
| 1413 | /// decl. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1414 | llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1415 | const ObjCInterfaceDecl *ID) { |
| 1416 | return EmitClassRef(Builder, ID); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | /// GetSelector - Return the pointer to the unique'd string for this selector. |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 1420 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 1421 | return EmitSelector(Builder, Sel); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1422 | } |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1423 | llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1424 | *Method) { |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1425 | return EmitSelector(Builder, Method->getSelector()); |
| 1426 | } |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1427 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1428 | /// Generate a constant CFString object. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1429 | /* |
| 1430 | struct __builtin_CFString { |
| 1431 | const int *isa; // point to __CFConstantStringClassReference |
| 1432 | int flags; |
| 1433 | const char *str; |
| 1434 | long length; |
| 1435 | }; |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 1436 | */ |
| 1437 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 1438 | llvm::Constant *CGObjCCommonMac::GenerateConstantString( |
Steve Naroff | 33fdb73 | 2009-03-31 16:53:37 +0000 | [diff] [blame] | 1439 | const ObjCStringLiteral *SL) { |
Steve Naroff | 8d4141f | 2009-04-01 13:55:36 +0000 | [diff] [blame] | 1440 | return CGM.GetAddrOfConstantCFString(SL->getString()); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | /// Generates a message send where the super is the receiver. This is |
| 1444 | /// a message send to self with special delivery semantics indicating |
| 1445 | /// which class's method should be called. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1446 | CodeGen::RValue |
| 1447 | CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1448 | QualType ResultType, |
| 1449 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1450 | const ObjCInterfaceDecl *Class, |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1451 | bool isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1452 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1453 | bool IsClassMessage, |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1454 | const CodeGen::CallArgList &CallArgs) { |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1455 | // Create and init a super structure; this is a (receiver, class) |
| 1456 | // pair we will pass to objc_msgSendSuper. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1457 | llvm::Value *ObjCSuper = |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1458 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1459 | llvm::Value *ReceiverAsObject = |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1460 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1461 | CGF.Builder.CreateStore(ReceiverAsObject, |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1462 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | e8b470d | 2008-08-23 04:28:29 +0000 | [diff] [blame] | 1463 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1464 | // If this is a class message the metaclass is passed as the target. |
| 1465 | llvm::Value *Target; |
| 1466 | if (IsClassMessage) { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1467 | if (isCategoryImpl) { |
| 1468 | // Message sent to 'super' in a class method defined in a category |
| 1469 | // implementation requires an odd treatment. |
| 1470 | // If we are in a class method, we must retrieve the |
| 1471 | // _metaclass_ for the current class, pointed at by |
| 1472 | // the class's "isa" pointer. The following assumes that |
| 1473 | // isa" is the first ivar in a class (which it must be). |
| 1474 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1475 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 1476 | Target = CGF.Builder.CreateLoad(Target); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1477 | } else { |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 1478 | llvm::Value *MetaClassPtr = EmitMetaClassRef(Class); |
| 1479 | llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1); |
| 1480 | llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr); |
| 1481 | Target = Super; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1482 | } |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1483 | } else { |
| 1484 | Target = EmitClassRef(CGF.Builder, Class->getSuperClass()); |
| 1485 | } |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1486 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 1487 | // ObjCTypes types. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1488 | const llvm::Type *ClassTy = |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1489 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1490 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1491 | CGF.Builder.CreateStore(Target, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1492 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1493 | return EmitLegacyMessageSend(CGF, ResultType, |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1494 | EmitSelector(CGF.Builder, Sel), |
| 1495 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 1496 | true, CallArgs, ObjCTypes); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1497 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1498 | |
| 1499 | /// Generate code for a message send expression. |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 1500 | CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 1501 | QualType ResultType, |
| 1502 | Selector Sel, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 1503 | llvm::Value *Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1504 | bool IsClassMessage, |
Fariborz Jahanian | df9ccc6 | 2009-05-05 21:36:57 +0000 | [diff] [blame] | 1505 | const CallArgList &CallArgs, |
| 1506 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1507 | return EmitLegacyMessageSend(CGF, ResultType, |
| 1508 | EmitSelector(CGF.Builder, Sel), |
| 1509 | Receiver, CGF.getContext().getObjCIdType(), |
| 1510 | false, CallArgs, ObjCTypes); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1513 | CodeGen::RValue CGObjCCommonMac::EmitLegacyMessageSend( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1514 | CodeGen::CodeGenFunction &CGF, |
| 1515 | QualType ResultType, |
| 1516 | llvm::Value *Sel, |
| 1517 | llvm::Value *Arg0, |
| 1518 | QualType Arg0Ty, |
| 1519 | bool IsSuper, |
| 1520 | const CallArgList &CallArgs, |
| 1521 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1522 | CallArgList ActualArgs; |
Fariborz Jahanian | d019d96 | 2009-04-24 21:07:43 +0000 | [diff] [blame] | 1523 | if (!IsSuper) |
| 1524 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 1525 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1526 | ActualArgs.push_back(std::make_pair(RValue::get(Sel), |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 1527 | CGF.getContext().getObjCSelType())); |
| 1528 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1529 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 1530 | CodeGenTypes &Types = CGM.getTypes(); |
| 1531 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1532 | // In 64bit ABI, type must be assumed VARARG. In 32bit abi, |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1533 | // it seems not to matter. |
| 1534 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, (ObjCABI == 2)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1535 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1536 | llvm::Constant *Fn = NULL; |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1537 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1538 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1539 | : ObjCTypes.getSendStretFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1540 | } else if (ResultType->isFloatingType()) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1541 | if (ObjCABI == 2) { |
| 1542 | if (const BuiltinType *BT = ResultType->getAsBuiltinType()) { |
| 1543 | BuiltinType::Kind k = BT->getKind(); |
| 1544 | Fn = (k == BuiltinType::LongDouble) ? ObjCTypes.getSendFpretFn2(IsSuper) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1545 | : ObjCTypes.getSendFn2(IsSuper); |
Daniel Dunbar | 42f963d | 2009-06-10 04:38:50 +0000 | [diff] [blame] | 1546 | } else { |
| 1547 | Fn = ObjCTypes.getSendFn2(IsSuper); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1548 | } |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 1549 | } else |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1550 | // FIXME. This currently matches gcc's API for x86-32. May need to change |
| 1551 | // for others if we have their API. |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1552 | Fn = ObjCTypes.getSendFpretFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1553 | } else { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1554 | Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1555 | : ObjCTypes.getSendFn(IsSuper); |
Daniel Dunbar | 5669e57 | 2008-10-17 03:24:53 +0000 | [diff] [blame] | 1556 | } |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 1557 | assert(Fn && "EmitLegacyMessageSend - unknown API"); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1558 | Fn = llvm::ConstantExpr::getBitCast(Fn, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1559 | llvm::PointerType::getUnqual(FTy)); |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 1560 | return CGF.EmitCall(FnInfo, Fn, ActualArgs); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1563 | llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | af2f62c | 2008-08-13 00:59:25 +0000 | [diff] [blame] | 1564 | const ObjCProtocolDecl *PD) { |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1565 | // FIXME: I don't understand why gcc generates this, or where it is |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1566 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | c67876d | 2008-09-04 04:33:15 +0000 | [diff] [blame] | 1567 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1568 | |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1569 | return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1570 | ObjCTypes.ExternalProtocolPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1573 | void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1574 | // FIXME: We shouldn't need this, the protocol decl should contain enough |
| 1575 | // information to tell us whether this was a declaration or a definition. |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1576 | DefinedProtocols.insert(PD->getIdentifier()); |
| 1577 | |
| 1578 | // If we have generated a forward reference to this protocol, emit |
| 1579 | // it now. Otherwise do nothing, the protocol objects are lazily |
| 1580 | // emitted. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1581 | if (Protocols.count(PD->getIdentifier())) |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1582 | GetOrEmitProtocol(PD); |
| 1583 | } |
| 1584 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 1585 | llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1586 | if (DefinedProtocols.count(PD->getIdentifier())) |
| 1587 | return GetOrEmitProtocol(PD); |
| 1588 | return GetOrEmitProtocolRef(PD); |
| 1589 | } |
| 1590 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1591 | /* |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1592 | // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions |
| 1593 | struct _objc_protocol { |
| 1594 | struct _objc_protocol_extension *isa; |
| 1595 | char *protocol_name; |
| 1596 | struct _objc_protocol_list *protocol_list; |
| 1597 | struct _objc__method_prototype_list *instance_methods; |
| 1598 | struct _objc__method_prototype_list *class_methods |
| 1599 | }; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1600 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1601 | See EmitProtocolExtension(). |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1602 | */ |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1603 | llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) { |
| 1604 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1605 | |
| 1606 | // Early exit if a defining object has already been generated. |
| 1607 | if (Entry && Entry->hasInitializer()) |
| 1608 | return Entry; |
| 1609 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1610 | // FIXME: I don't understand why gcc generates this, or where it is |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1611 | // resolved. Investigate. Its also wasteful to look this up over and over. |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1612 | LazySymbols.insert(&CGM.getContext().Idents.get("Protocol")); |
| 1613 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1614 | const char *ProtocolName = PD->getNameAsCString(); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1615 | |
| 1616 | // Construct method lists. |
| 1617 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 1618 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1619 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1620 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1621 | ObjCMethodDecl *MD = *i; |
| 1622 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1623 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1624 | OptInstanceMethods.push_back(C); |
| 1625 | } else { |
| 1626 | InstanceMethods.push_back(C); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1627 | } |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1630 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1631 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1632 | ObjCMethodDecl *MD = *i; |
| 1633 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
| 1634 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 1635 | OptClassMethods.push_back(C); |
| 1636 | } else { |
| 1637 | ClassMethods.push_back(C); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1638 | } |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1641 | std::vector<llvm::Constant*> Values(5); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1642 | Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1643 | Values[1] = GetClassName(PD->getIdentifier()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1644 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1645 | EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1646 | PD->protocol_begin(), |
| 1647 | PD->protocol_end()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1648 | Values[3] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1649 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1650 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1651 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1652 | InstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1653 | Values[4] = |
| 1654 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" |
| 1655 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1656 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1657 | ClassMethods); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1658 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1659 | Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1660 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1661 | if (Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1662 | // Already created, fix the linkage and update the initializer. |
| 1663 | Entry->setLinkage(llvm::GlobalValue::InternalLinkage); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1664 | Entry->setInitializer(Init); |
| 1665 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1666 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1667 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1668 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1669 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1670 | std::string("\01L_OBJC_PROTOCOL_")+ProtocolName); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1671 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1672 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1673 | // FIXME: Is this necessary? Why only for protocol? |
| 1674 | Entry->setAlignment(4); |
| 1675 | } |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 1676 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1677 | |
| 1678 | return Entry; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1681 | llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1682 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
| 1683 | |
| 1684 | if (!Entry) { |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1685 | // We use the initializer as a marker of whether this is a forward |
| 1686 | // reference or not. At module finalization we add the empty |
| 1687 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1688 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1689 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 1690 | llvm::GlobalValue::ExternalLinkage, |
| 1691 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 1692 | "\01L_OBJC_PROTOCOL_" + PD->getNameAsString()); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1693 | Entry->setSection("__OBJC,__protocol,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1694 | Entry->setAlignment(4); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1695 | // FIXME: Is this necessary? Why only for protocol? |
| 1696 | Entry->setAlignment(4); |
| 1697 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1698 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1699 | return Entry; |
| 1700 | } |
| 1701 | |
| 1702 | /* |
| 1703 | struct _objc_protocol_extension { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1704 | uint32_t size; |
| 1705 | struct objc_method_description_list *optional_instance_methods; |
| 1706 | struct objc_method_description_list *optional_class_methods; |
| 1707 | struct objc_property_list *instance_properties; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1708 | }; |
| 1709 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1710 | llvm::Constant * |
| 1711 | CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD, |
| 1712 | const ConstantVector &OptInstanceMethods, |
| 1713 | const ConstantVector &OptClassMethods) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1714 | uint64_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1715 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1716 | std::vector<llvm::Constant*> Values(4); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1717 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1718 | Values[1] = |
| 1719 | EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_" |
| 1720 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1721 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
| 1722 | OptInstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1723 | Values[2] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1724 | EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1725 | + PD->getNameAsString(), |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1726 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
| 1727 | OptClassMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1728 | Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + |
| 1729 | PD->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1730 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1731 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1732 | // Return null if no extension bits are used. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1733 | if (Values[1]->isNullValue() && Values[2]->isNullValue() && |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1734 | Values[3]->isNullValue()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1735 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1736 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1737 | llvm::Constant *Init = |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1738 | llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1739 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1740 | // No special section, but goes in llvm.used |
| 1741 | return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1742 | Init, |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1743 | 0, 0, true); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | /* |
| 1747 | struct objc_protocol_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1748 | struct objc_protocol_list *next; |
| 1749 | long count; |
| 1750 | Protocol *list[]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1751 | }; |
| 1752 | */ |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1753 | llvm::Constant * |
| 1754 | CGObjCMac::EmitProtocolList(const std::string &Name, |
| 1755 | ObjCProtocolDecl::protocol_iterator begin, |
| 1756 | ObjCProtocolDecl::protocol_iterator end) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1757 | std::vector<llvm::Constant*> ProtocolRefs; |
| 1758 | |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1759 | for (; begin != end; ++begin) |
| 1760 | ProtocolRefs.push_back(GetProtocolRef(*begin)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1761 | |
| 1762 | // Just return null for empty protocol lists |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1763 | if (ProtocolRefs.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1764 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1765 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1766 | // This list is null terminated. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1767 | ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy)); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1768 | |
| 1769 | std::vector<llvm::Constant*> Values(3); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1770 | // This field is only used by the runtime. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1771 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1772 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1773 | ProtocolRefs.size() - 1); |
| 1774 | Values[2] = |
| 1775 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy, |
| 1776 | ProtocolRefs.size()), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1777 | ProtocolRefs); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1778 | |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1779 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1780 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1781 | CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1782 | 4, false); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1783 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | /* |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1787 | struct _objc_property { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1788 | const char * const name; |
| 1789 | const char * const attributes; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1790 | }; |
| 1791 | |
| 1792 | struct _objc_property_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1793 | uint32_t entsize; // sizeof (struct _objc_property) |
| 1794 | uint32_t prop_count; |
| 1795 | struct _objc_property[prop_count]; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1796 | }; |
| 1797 | */ |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1798 | llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1799 | const Decl *Container, |
| 1800 | const ObjCContainerDecl *OCD, |
| 1801 | const ObjCCommonTypesHelper &ObjCTypes) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1802 | std::vector<llvm::Constant*> Properties, Prop(2); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1803 | for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(), |
| 1804 | E = OCD->prop_end(); I != E; ++I) { |
Steve Naroff | 93983f8 | 2009-01-11 12:47:58 +0000 | [diff] [blame] | 1805 | const ObjCPropertyDecl *PD = *I; |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1806 | Prop[0] = GetPropertyName(PD->getIdentifier()); |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 1807 | Prop[1] = GetPropertyTypeString(PD, Container); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1808 | Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1809 | Prop)); |
| 1810 | } |
| 1811 | |
| 1812 | // Return null for empty list. |
| 1813 | if (Properties.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1814 | return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1815 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1816 | unsigned PropertySize = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1817 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1818 | std::vector<llvm::Constant*> Values(3); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1819 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize); |
| 1820 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1821 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy, |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1822 | Properties.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 1823 | Values[2] = llvm::ConstantArray::get(AT, Properties); |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1824 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1825 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1826 | llvm::GlobalVariable *GV = |
| 1827 | CreateMetadataVar(Name, Init, |
| 1828 | (ObjCABI == 2) ? "__DATA, __objc_const" : |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1829 | "__OBJC,__property,regular,no_dead_strip", |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1830 | (ObjCABI == 2) ? 8 : 4, |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1831 | true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 1832 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | /* |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1836 | struct objc_method_description_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1837 | int count; |
| 1838 | struct objc_method_description list[]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1839 | }; |
| 1840 | */ |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1841 | llvm::Constant * |
| 1842 | CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 1843 | std::vector<llvm::Constant*> Desc(2); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 1844 | Desc[0] = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1845 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 1846 | ObjCTypes.SelectorPtrTy); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1847 | Desc[1] = GetMethodVarType(MD); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1848 | return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1849 | Desc); |
| 1850 | } |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1851 | |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1852 | llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name, |
| 1853 | const char *Section, |
| 1854 | const ConstantVector &Methods) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1855 | // Return null for empty list. |
| 1856 | if (Methods.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1857 | return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1858 | |
| 1859 | std::vector<llvm::Constant*> Values(2); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1860 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1861 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1862 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 1863 | Values[1] = llvm::ConstantArray::get(AT, Methods); |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 1864 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1865 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1866 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1867 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 1868 | ObjCTypes.MethodDescriptionListPtrTy); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1869 | } |
| 1870 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1871 | /* |
| 1872 | struct _objc_category { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1873 | char *category_name; |
| 1874 | char *class_name; |
| 1875 | struct _objc_method_list *instance_methods; |
| 1876 | struct _objc_method_list *class_methods; |
| 1877 | struct _objc_protocol_list *protocols; |
| 1878 | uint32_t size; // <rdar://4585769> |
| 1879 | struct _objc_property_list *instance_properties; |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1880 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1881 | */ |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 1882 | void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 1883 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1884 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1885 | // FIXME: This is poor design, the OCD should have a pointer to the category |
| 1886 | // decl. Additionally, note that Category can be null for the @implementation |
| 1887 | // w/o an @interface case. Sema should just create one for us as it does for |
| 1888 | // @implementation so everyone else can live life under a clear blue sky. |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1889 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1890 | const ObjCCategoryDecl *Category = |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1891 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1892 | std::string ExtName(Interface->getNameAsString() + "_" + |
| 1893 | OCD->getNameAsString()); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1894 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1895 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1896 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1897 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1898 | // Instance methods should always be defined. |
| 1899 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 1900 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1901 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1902 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1903 | // Class methods should always be defined. |
| 1904 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 1905 | } |
| 1906 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1907 | std::vector<llvm::Constant*> Values(7); |
| 1908 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 1909 | Values[1] = GetClassName(Interface->getIdentifier()); |
Fariborz Jahanian | 679cd7f | 2009-04-29 20:40:05 +0000 | [diff] [blame] | 1910 | LazySymbols.insert(Interface->getIdentifier()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1911 | Values[2] = |
| 1912 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") + |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1913 | ExtName, |
| 1914 | "__OBJC,__cat_inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1915 | InstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1916 | Values[3] = |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 1917 | EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName, |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 1918 | "__OBJC,__cat_cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1919 | ClassMethods); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1920 | if (Category) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1921 | Values[4] = |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1922 | EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName, |
| 1923 | Category->protocol_begin(), |
| 1924 | Category->protocol_end()); |
| 1925 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1926 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 1927 | } |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 1928 | Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1929 | |
| 1930 | // If there is no category @interface then there can be no properties. |
| 1931 | if (Category) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1932 | Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_")+ExtName, |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 1933 | OCD, Category, ObjCTypes); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1934 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 1935 | Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Daniel Dunbar | 86e2f40 | 2008-08-26 23:03:11 +0000 | [diff] [blame] | 1936 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1937 | |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 1938 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1939 | Values); |
| 1940 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1941 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 1942 | CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init, |
| 1943 | "__OBJC,__category,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 1944 | 4, true); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 1945 | DefinedCategories.push_back(GV); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1948 | // FIXME: Get from somewhere? |
| 1949 | enum ClassFlags { |
| 1950 | eClassFlags_Factory = 0x00001, |
| 1951 | eClassFlags_Meta = 0x00002, |
| 1952 | // <rdr://5142207> |
| 1953 | eClassFlags_HasCXXStructors = 0x02000, |
| 1954 | eClassFlags_Hidden = 0x20000, |
| 1955 | eClassFlags_ABI2_Hidden = 0x00010, |
| 1956 | eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634> |
| 1957 | }; |
| 1958 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1959 | /* |
| 1960 | struct _objc_class { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1961 | Class isa; |
| 1962 | Class super_class; |
| 1963 | const char *name; |
| 1964 | long version; |
| 1965 | long info; |
| 1966 | long instance_size; |
| 1967 | struct _objc_ivar_list *ivars; |
| 1968 | struct _objc_method_list *methods; |
| 1969 | struct _objc_cache *cache; |
| 1970 | struct _objc_protocol_list *protocols; |
| 1971 | // Objective-C 1.0 extensions (<rdr://4585769>) |
| 1972 | const char *ivar_layout; |
| 1973 | struct _objc_class_ext *ext; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1974 | }; |
| 1975 | |
| 1976 | See EmitClassExtension(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1977 | */ |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1978 | void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 1979 | DefinedSymbols.insert(ID->getIdentifier()); |
| 1980 | |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 1981 | std::string ClassName = ID->getNameAsString(); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1982 | // FIXME: Gross |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1983 | ObjCInterfaceDecl *Interface = |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1984 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1985 | llvm::Constant *Protocols = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 1986 | EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(), |
Daniel Dunbar | dbc9337 | 2008-08-21 21:57:41 +0000 | [diff] [blame] | 1987 | Interface->protocol_begin(), |
| 1988 | Interface->protocol_end()); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1989 | unsigned Flags = eClassFlags_Factory; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1990 | unsigned Size = |
Daniel Dunbar | 2bebbf0 | 2009-05-03 10:46:44 +0000 | [diff] [blame] | 1991 | CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1992 | |
| 1993 | // FIXME: Set CXX-structors flag. |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 1994 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 1995 | Flags |= eClassFlags_Hidden; |
| 1996 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 1997 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 1998 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1999 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2000 | // Instance methods should always be defined. |
| 2001 | InstanceMethods.push_back(GetMethodConstant(*i)); |
| 2002 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2003 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2004 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2005 | // Class methods should always be defined. |
| 2006 | ClassMethods.push_back(GetMethodConstant(*i)); |
| 2007 | } |
| 2008 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2009 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2010 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2011 | ObjCPropertyImplDecl *PID = *i; |
| 2012 | |
| 2013 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) { |
| 2014 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 2015 | |
| 2016 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 2017 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 2018 | InstanceMethods.push_back(C); |
| 2019 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 2020 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 2021 | InstanceMethods.push_back(C); |
| 2022 | } |
| 2023 | } |
| 2024 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2025 | std::vector<llvm::Constant*> Values(12); |
Daniel Dunbar | 5384b09 | 2009-05-03 08:56:52 +0000 | [diff] [blame] | 2026 | Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2027 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2028 | // Record a reference to the super class. |
| 2029 | LazySymbols.insert(Super->getIdentifier()); |
| 2030 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2031 | Values[ 1] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2032 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2033 | ObjCTypes.ClassPtrTy); |
| 2034 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2035 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2036 | } |
| 2037 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2038 | // Version is always 0. |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2039 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2040 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2041 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2042 | Values[ 6] = EmitIvarList(ID, false); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2043 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2044 | EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 2045 | "__OBJC,__inst_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2046 | InstanceMethods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2047 | // cache is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2048 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2049 | Values[ 9] = Protocols; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2050 | Values[10] = BuildIvarLayout(ID, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2051 | Values[11] = EmitClassExtension(ID); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2052 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2053 | Values); |
| 2054 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2055 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2056 | CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init, |
| 2057 | "__OBJC,__class,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2058 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2059 | DefinedClasses.push_back(GV); |
| 2060 | } |
| 2061 | |
| 2062 | llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID, |
| 2063 | llvm::Constant *Protocols, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2064 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2065 | unsigned Flags = eClassFlags_Meta; |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2066 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2067 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 2068 | if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden) |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2069 | Flags |= eClassFlags_Hidden; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2070 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2071 | std::vector<llvm::Constant*> Values(12); |
| 2072 | // The isa for the metaclass is the root of the hierarchy. |
| 2073 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 2074 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 2075 | Root = Super; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2076 | Values[ 0] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2077 | llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2078 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2079 | // The super class for the metaclass is emitted as the name of the |
| 2080 | // super class. The runtime fixes this up to point to the |
| 2081 | // *metaclass* for the super class. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2082 | if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2083 | Values[ 1] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2084 | llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2085 | ObjCTypes.ClassPtrTy); |
| 2086 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2087 | Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2088 | } |
| 2089 | Values[ 2] = GetClassName(ID->getIdentifier()); |
| 2090 | // Version is always 0. |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2091 | Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
| 2092 | Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags); |
| 2093 | Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2094 | Values[ 6] = EmitIvarList(ID, true); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2095 | Values[ 7] = |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2096 | EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(), |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2097 | "__OBJC,__cls_meth,regular,no_dead_strip", |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2098 | Methods); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2099 | // cache is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2100 | Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2101 | Values[ 9] = Protocols; |
| 2102 | // ivar_layout for metaclass is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2103 | Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2104 | // The class extension is always unused for metaclasses. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2105 | Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2106 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2107 | Values); |
| 2108 | |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2109 | std::string Name("\01L_OBJC_METACLASS_"); |
Chris Lattner | 8ec03f5 | 2008-11-24 03:54:41 +0000 | [diff] [blame] | 2110 | Name += ID->getNameAsCString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2111 | |
| 2112 | // Check for a forward reference. |
| 2113 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 2114 | if (GV) { |
| 2115 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2116 | "Forward metaclass reference has incorrect type."); |
| 2117 | GV->setLinkage(llvm::GlobalValue::InternalLinkage); |
| 2118 | GV->setInitializer(Init); |
| 2119 | } else { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2120 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2121 | llvm::GlobalValue::InternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2122 | Init, Name); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2123 | } |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2124 | GV->setSection("__OBJC,__meta_class,regular,no_dead_strip"); |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2125 | GV->setAlignment(4); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2126 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2127 | |
| 2128 | return GV; |
| 2129 | } |
| 2130 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2131 | llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) { |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 2132 | std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString(); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2133 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 2134 | // FIXME: Should we look these up somewhere other than the module. Its a bit |
| 2135 | // silly since we only generate these while processing an implementation, so |
| 2136 | // exactly one pointer would work if know when we entered/exitted an |
| 2137 | // implementation block. |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2138 | |
| 2139 | // Check for an existing forward reference. |
Fariborz Jahanian | b0d2794 | 2009-01-07 20:11:22 +0000 | [diff] [blame] | 2140 | // Previously, metaclass with internal linkage may have been defined. |
| 2141 | // pass 'true' as 2nd argument so it is returned. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2142 | if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, |
| 2143 | true)) { |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2144 | assert(GV->getType()->getElementType() == ObjCTypes.ClassTy && |
| 2145 | "Forward metaclass reference has incorrect type."); |
| 2146 | return GV; |
| 2147 | } else { |
| 2148 | // Generate as an external reference to keep a consistent |
| 2149 | // module. This will be patched up when we emit the metaclass. |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2150 | return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2151 | llvm::GlobalValue::ExternalLinkage, |
| 2152 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2153 | Name); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 2154 | } |
| 2155 | } |
| 2156 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2157 | /* |
| 2158 | struct objc_class_ext { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2159 | uint32_t size; |
| 2160 | const char *weak_ivar_layout; |
| 2161 | struct _objc_property_list *properties; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2162 | }; |
| 2163 | */ |
| 2164 | llvm::Constant * |
| 2165 | CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2166 | uint64_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2167 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2168 | |
| 2169 | std::vector<llvm::Constant*> Values(3); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2170 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | c71303d | 2009-04-22 23:00:43 +0000 | [diff] [blame] | 2171 | Values[1] = BuildIvarLayout(ID, false); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2172 | Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 2173 | ID, ID->getClassInterface(), ObjCTypes); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2174 | |
| 2175 | // Return null if no extension bits are used. |
| 2176 | if (Values[1]->isNullValue() && Values[2]->isNullValue()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2177 | return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2178 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2179 | llvm::Constant *Init = |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2180 | llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2181 | return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2182 | Init, "__OBJC,__class_ext,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2183 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2184 | } |
| 2185 | |
| 2186 | /* |
| 2187 | struct objc_ivar { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2188 | char *ivar_name; |
| 2189 | char *ivar_type; |
| 2190 | int ivar_offset; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2191 | }; |
| 2192 | |
| 2193 | struct objc_ivar_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2194 | int ivar_count; |
| 2195 | struct objc_ivar list[count]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2196 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2197 | */ |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2198 | llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID, |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2199 | bool ForClass) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2200 | std::vector<llvm::Constant*> Ivars, Ivar(3); |
| 2201 | |
| 2202 | // When emitting the root class GCC emits ivar entries for the |
| 2203 | // actual class structure. It is not clear if we need to follow this |
| 2204 | // behavior; for now lets try and get away with not doing it. If so, |
| 2205 | // the cleanest solution would be to make up an ObjCInterfaceDecl |
| 2206 | // for the class. |
| 2207 | if (ForClass) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2208 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2209 | |
| 2210 | ObjCInterfaceDecl *OID = |
Fariborz Jahanian | 46b86c6 | 2009-01-28 19:12:34 +0000 | [diff] [blame] | 2211 | const_cast<ObjCInterfaceDecl*>(ID->getClassInterface()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2212 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 2213 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2214 | CGM.getContext().ShallowCollectObjCIvars(OID, OIvars); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2215 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 2216 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 2217 | ObjCIvarDecl *IVD = OIvars[i]; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 2218 | // Ignore unnamed bit-fields. |
| 2219 | if (!IVD->getDeclName()) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2220 | continue; |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 2221 | Ivar[0] = GetMethodVarName(IVD->getIdentifier()); |
| 2222 | Ivar[1] = GetMethodVarType(IVD); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2223 | Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy, |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2224 | ComputeIvarBaseOffset(CGM, OID, IVD)); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2225 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar)); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
| 2228 | // Return null for empty list. |
| 2229 | if (Ivars.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2230 | return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2231 | |
| 2232 | std::vector<llvm::Constant*> Values(2); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2233 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2234 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2235 | Ivars.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2236 | Values[1] = llvm::ConstantArray::get(AT, Ivars); |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 2237 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2238 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2239 | llvm::GlobalVariable *GV; |
| 2240 | if (ForClass) |
| 2241 | GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2242 | Init, "__OBJC,__class_vars,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2243 | 4, true); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2244 | else |
| 2245 | GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" |
| 2246 | + ID->getNameAsString(), |
| 2247 | Init, "__OBJC,__instance_vars,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2248 | 4, true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2249 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2250 | } |
| 2251 | |
| 2252 | /* |
| 2253 | struct objc_method { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2254 | SEL method_name; |
| 2255 | char *method_types; |
| 2256 | void *method; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2257 | }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2258 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2259 | struct objc_method_list { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2260 | struct objc_method_list *obsolete; |
| 2261 | int count; |
| 2262 | struct objc_method methods_list[count]; |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2263 | }; |
| 2264 | */ |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2265 | |
| 2266 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 2267 | /// given method if it has been defined. The result is null if the |
| 2268 | /// method has not been defined. The return value has type MethodPtrTy. |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2269 | llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) { |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2270 | // FIXME: Use DenseMap::lookup |
| 2271 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 2272 | if (!Fn) |
| 2273 | return 0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2274 | |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2275 | std::vector<llvm::Constant*> Method(3); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2276 | Method[0] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2277 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2278 | ObjCTypes.SelectorPtrTy); |
| 2279 | Method[1] = GetMethodVarType(MD); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2280 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2281 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2284 | llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name, |
| 2285 | const char *Section, |
Daniel Dunbar | ae226fa | 2008-08-27 02:31:56 +0000 | [diff] [blame] | 2286 | const ConstantVector &Methods) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2287 | // Return null for empty list. |
| 2288 | if (Methods.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2289 | return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2290 | |
| 2291 | std::vector<llvm::Constant*> Values(3); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2292 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2293 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2294 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2295 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2296 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 2297 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2298 | |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2299 | llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2300 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2301 | ObjCTypes.MethodListPtrTy); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 2304 | llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2305 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2306 | std::string Name; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 2307 | GetNameForMethod(OMD, CD, Name); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2308 | |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2309 | CodeGenTypes &Types = CGM.getTypes(); |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2310 | const llvm::FunctionType *MethodTy = |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 2311 | Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2312 | llvm::Function *Method = |
Daniel Dunbar | 45c25ba | 2008-09-10 04:01:49 +0000 | [diff] [blame] | 2313 | llvm::Function::Create(MethodTy, |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2314 | llvm::GlobalValue::InternalLinkage, |
| 2315 | Name, |
| 2316 | &CGM.getModule()); |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 2317 | MethodDefinitions.insert(std::make_pair(OMD, Method)); |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2318 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 2319 | return Method; |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2322 | llvm::GlobalVariable * |
| 2323 | CGObjCCommonMac::CreateMetadataVar(const std::string &Name, |
| 2324 | llvm::Constant *Init, |
| 2325 | const char *Section, |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2326 | unsigned Align, |
| 2327 | bool AddToUsed) { |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2328 | const llvm::Type *Ty = Init->getType(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2329 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 2330 | new llvm::GlobalVariable(CGM.getModule(), Ty, false, |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2331 | llvm::GlobalValue::InternalLinkage, Init, Name); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2332 | if (Section) |
| 2333 | GV->setSection(Section); |
Daniel Dunbar | 35bd763 | 2009-03-09 20:50:13 +0000 | [diff] [blame] | 2334 | if (Align) |
| 2335 | GV->setAlignment(Align); |
| 2336 | if (AddToUsed) |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 2337 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | fd65d37 | 2009-03-09 20:09:19 +0000 | [diff] [blame] | 2338 | return GV; |
| 2339 | } |
| 2340 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2341 | llvm::Function *CGObjCMac::ModuleInitFunction() { |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2342 | // Abuse this interface function as a place to finalize. |
| 2343 | FinishModule(); |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 2344 | return NULL; |
| 2345 | } |
| 2346 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2347 | llvm::Constant *CGObjCMac::GetPropertyGetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2348 | return ObjCTypes.getGetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2349 | } |
| 2350 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2351 | llvm::Constant *CGObjCMac::GetPropertySetFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2352 | return ObjCTypes.getSetPropertyFn(); |
Daniel Dunbar | 49f6602 | 2008-09-24 03:38:44 +0000 | [diff] [blame] | 2353 | } |
| 2354 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 2355 | llvm::Constant *CGObjCMac::EnumerationMutationFunction() { |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2356 | return ObjCTypes.getEnumerationMutationFn(); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 2357 | } |
| 2358 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2359 | /* |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2360 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2361 | Objective-C setjmp-longjmp (sjlj) Exception Handling |
| 2362 | -- |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2363 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2364 | The basic framework for a @try-catch-finally is as follows: |
| 2365 | { |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2366 | objc_exception_data d; |
| 2367 | id _rethrow = null; |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2368 | bool _call_try_exit = true; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2369 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2370 | objc_exception_try_enter(&d); |
| 2371 | if (!setjmp(d.jmp_buf)) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2372 | ... try body ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2373 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2374 | // exception path |
| 2375 | id _caught = objc_exception_extract(&d); |
| 2376 | |
| 2377 | // enter new try scope for handlers |
| 2378 | if (!setjmp(d.jmp_buf)) { |
| 2379 | ... match exception and execute catch blocks ... |
| 2380 | |
| 2381 | // fell off end, rethrow. |
| 2382 | _rethrow = _caught; |
| 2383 | ... jump-through-finally to finally_rethrow ... |
| 2384 | } else { |
| 2385 | // exception in catch block |
| 2386 | _rethrow = objc_exception_extract(&d); |
| 2387 | _call_try_exit = false; |
| 2388 | ... jump-through-finally to finally_rethrow ... |
| 2389 | } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2390 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2391 | ... jump-through-finally to finally_end ... |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2392 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2393 | finally: |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2394 | if (_call_try_exit) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2395 | objc_exception_try_exit(&d); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2396 | |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2397 | ... finally block .... |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2398 | ... dispatch to finally destination ... |
| 2399 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2400 | finally_rethrow: |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2401 | objc_exception_throw(_rethrow); |
| 2402 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2403 | finally_end: |
| 2404 | } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2405 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2406 | This framework differs slightly from the one gcc uses, in that gcc |
| 2407 | uses _rethrow to determine if objc_exception_try_exit should be called |
| 2408 | and if the object should be rethrown. This breaks in the face of |
| 2409 | throwing nil and introduces unnecessary branches. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2410 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2411 | We specialize this framework for a few particular circumstances: |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2412 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2413 | - If there are no catch blocks, then we avoid emitting the second |
| 2414 | exception handling context. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2415 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2416 | - If there is a catch-all catch block (i.e. @catch(...) or @catch(id |
| 2417 | e)) we avoid emitting the code to rethrow an uncaught exception. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2418 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2419 | - FIXME: If there is no @finally block we can do a few more |
| 2420 | simplifications. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2421 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2422 | Rethrows and Jumps-Through-Finally |
| 2423 | -- |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2424 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2425 | Support for implicit rethrows and jumping through the finally block is |
| 2426 | handled by storing the current exception-handling context in |
| 2427 | ObjCEHStack. |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2428 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2429 | In order to implement proper @finally semantics, we support one basic |
| 2430 | mechanism for jumping through the finally block to an arbitrary |
| 2431 | destination. Constructs which generate exits from a @try or @catch |
| 2432 | block use this mechanism to implement the proper semantics by chaining |
| 2433 | jumps, as necessary. |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2434 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2435 | This mechanism works like the one used for indirect goto: we |
| 2436 | arbitrarily assign an ID to each destination and store the ID for the |
| 2437 | destination in a variable prior to entering the finally block. At the |
| 2438 | end of the finally block we simply create a switch to the proper |
| 2439 | destination. |
| 2440 | |
| 2441 | Code gen for @synchronized(expr) stmt; |
| 2442 | Effectively generating code for: |
| 2443 | objc_sync_enter(expr); |
| 2444 | @try stmt @finally { objc_sync_exit(expr); } |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2445 | */ |
| 2446 | |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2447 | void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 2448 | const Stmt &S) { |
| 2449 | bool isTry = isa<ObjCAtTryStmt>(S); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2450 | // Create various blocks we refer to for handling @finally. |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2451 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2452 | llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit"); |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2453 | llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit"); |
| 2454 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
| 2455 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2456 | |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2457 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 2458 | // evaluation of the expression must occur before we enter the |
| 2459 | // @synchronized. We can safely avoid a temp here because jumps into |
| 2460 | // @synchronized are illegal & this will dominate uses. |
| 2461 | llvm::Value *SyncArg = 0; |
| 2462 | if (!isTry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2463 | SyncArg = |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2464 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 2465 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 2466 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2467 | } |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2468 | |
| 2469 | // Push an EH context entry, used for handling rethrows and jumps |
| 2470 | // through finally. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2471 | CGF.PushCleanupBlock(FinallyBlock); |
| 2472 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2473 | CGF.ObjCEHValueStack.push_back(0); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2474 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2475 | // Allocate memory for the exception data and rethrow pointer. |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2476 | llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy, |
| 2477 | "exceptiondata.ptr"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2478 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 2479 | "_rethrow"); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2480 | llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca( |
| 2481 | llvm::Type::getInt1Ty(VMContext), |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2482 | "_call_try_exit"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2483 | CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(VMContext), |
| 2484 | CallTryExitPtr); |
| 2485 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2486 | // Enter a new try block and call setjmp. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2487 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2488 | llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0, |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2489 | "jmpbufarray"); |
| 2490 | JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp"); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2491 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2492 | JmpBufPtr, "result"); |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2493 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2494 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 2495 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2496 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2497 | TryHandler, TryBlock); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2498 | |
| 2499 | // Emit the @try block. |
| 2500 | CGF.EmitBlock(TryBlock); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2501 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 2502 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2503 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2504 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2505 | // Emit the "exception in @try" block. |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2506 | CGF.EmitBlock(TryHandler); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2507 | |
| 2508 | // Retrieve the exception object. We may emit multiple blocks but |
| 2509 | // nothing can cross this so the value is already in SSA form. |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2510 | llvm::Value *Caught = |
| 2511 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2512 | ExceptionData, "caught"); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2513 | CGF.ObjCEHValueStack.back() = Caught; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2514 | if (!isTry) { |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2515 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2516 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext), |
| 2517 | CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2518 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2519 | } else if (const ObjCAtCatchStmt* CatchStmt = |
| 2520 | cast<ObjCAtTryStmt>(S).getCatchStmts()) { |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2521 | // Enter a new exception try block (in case a @catch block throws |
| 2522 | // an exception). |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2523 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2524 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2525 | llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2526 | JmpBufPtr, "result"); |
Daniel Dunbar | 91cd320 | 2008-10-02 17:05:36 +0000 | [diff] [blame] | 2527 | llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2528 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2529 | llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch"); |
| 2530 | llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler"); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2531 | CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2532 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2533 | CGF.EmitBlock(CatchBlock); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2534 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2535 | // Handle catch list. As a special case we check if everything is |
| 2536 | // matched and avoid generating code for falling off the end if |
| 2537 | // so. |
| 2538 | bool AllMatched = false; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2539 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2540 | llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch"); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2541 | |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2542 | const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2543 | const ObjCObjectPointerType *OPT = 0; |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2544 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2545 | // catch(...) always matches. |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2546 | if (!CatchParam) { |
| 2547 | AllMatched = true; |
| 2548 | } else { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2549 | OPT = CatchParam->getType()->getAsObjCObjectPointerType(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2550 | |
| 2551 | // catch(id e) always matches. |
Daniel Dunbar | 97f61d1 | 2008-09-27 22:21:14 +0000 | [diff] [blame] | 2552 | // FIXME: For the time being we also match id<X>; this should |
| 2553 | // be rejected by Sema instead. |
Eli Friedman | 818e96f | 2009-07-11 00:57:02 +0000 | [diff] [blame] | 2554 | if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType())) |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2555 | AllMatched = true; |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2556 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2557 | |
| 2558 | if (AllMatched) { |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2559 | if (CatchParam) { |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2560 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2561 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2562 | CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam)); |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2563 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2564 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2565 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2566 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2567 | break; |
| 2568 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2569 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2570 | assert(OPT && "Unexpected non-object pointer type in @catch"); |
| 2571 | QualType T = OPT->getPointeeType(); |
Anders Carlsson | 4b7ff6e | 2008-09-11 06:35:14 +0000 | [diff] [blame] | 2572 | const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType(); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2573 | assert(ObjCType && "Catch parameter must have Objective-C type!"); |
| 2574 | |
| 2575 | // Check if the @catch block matches the exception object. |
| 2576 | llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2577 | |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2578 | llvm::Value *Match = |
| 2579 | CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(), |
| 2580 | Class, Caught, "match"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2581 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 2582 | llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2583 | |
| 2584 | CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"), |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2585 | MatchedBlock, NextCatchBlock); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2586 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2587 | // Emit the @catch block. |
| 2588 | CGF.EmitBlock(MatchedBlock); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2589 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2590 | assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?"); |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2591 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2592 | llvm::Value *Tmp = |
| 2593 | CGF.Builder.CreateBitCast(Caught, |
| 2594 | CGF.ConvertType(CatchParam->getType()), |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2595 | "tmp"); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 2596 | CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2597 | |
Anders Carlsson | dde0a94 | 2008-09-11 09:15:33 +0000 | [diff] [blame] | 2598 | CGF.EmitStmt(CatchStmt->getCatchBody()); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2599 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2600 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2601 | CGF.EmitBlock(NextCatchBlock); |
| 2602 | } |
| 2603 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2604 | if (!AllMatched) { |
| 2605 | // None of the handlers caught the exception, so store it to be |
| 2606 | // rethrown at the end of the @finally block. |
| 2607 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2608 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2609 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2610 | |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2611 | // Emit the exception handler for the @catch blocks. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2612 | CGF.EmitBlock(CatchHandler); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2613 | CGF.Builder.CreateStore( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2614 | CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(), |
| 2615 | ExceptionData), |
| 2616 | RethrowPtr); |
| 2617 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext), |
| 2618 | CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2619 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Daniel Dunbar | 55e4072 | 2008-09-27 07:03:52 +0000 | [diff] [blame] | 2620 | } else { |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2621 | CGF.Builder.CreateStore(Caught, RethrowPtr); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2622 | CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext), |
| 2623 | CallTryExitPtr); |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2624 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2625 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2626 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2627 | // Pop the exception-handling stack entry. It is important to do |
| 2628 | // this now, because the code in the @finally block is not in this |
| 2629 | // context. |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2630 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 2631 | |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2632 | CGF.ObjCEHValueStack.pop_back(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2633 | |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2634 | // Emit the @finally block. |
| 2635 | CGF.EmitBlock(FinallyBlock); |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2636 | llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2637 | |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2638 | CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2639 | |
Anders Carlsson | 190d00e | 2009-02-07 21:26:04 +0000 | [diff] [blame] | 2640 | CGF.EmitBlock(FinallyExit); |
Chris Lattner | 34b02a1 | 2009-04-22 02:26:14 +0000 | [diff] [blame] | 2641 | CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData); |
Daniel Dunbar | 129271a | 2008-09-27 07:36:24 +0000 | [diff] [blame] | 2642 | |
| 2643 | CGF.EmitBlock(FinallyNoExit); |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2644 | if (isTry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2645 | if (const ObjCAtFinallyStmt* FinallyStmt = |
| 2646 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 2647 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
Daniel Dunbar | 1c56667 | 2009-02-24 01:43:46 +0000 | [diff] [blame] | 2648 | } else { |
| 2649 | // Emit objc_sync_exit(expr); as finally's sole statement for |
| 2650 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2651 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Fariborz Jahanian | f2878e5 | 2008-11-21 19:21:53 +0000 | [diff] [blame] | 2652 | } |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2653 | |
Anders Carlsson | f3a79a9 | 2009-02-09 20:38:58 +0000 | [diff] [blame] | 2654 | // Emit the switch block |
| 2655 | if (Info.SwitchBlock) |
| 2656 | CGF.EmitBlock(Info.SwitchBlock); |
| 2657 | if (Info.EndBlock) |
| 2658 | CGF.EmitBlock(Info.EndBlock); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2659 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2660 | CGF.EmitBlock(FinallyRethrow); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2661 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2662 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | e4b5ee0 | 2008-09-27 23:30:04 +0000 | [diff] [blame] | 2663 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2664 | |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2665 | CGF.EmitBlock(FinallyEnd); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
| 2668 | void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 898d508 | 2008-09-30 01:06:03 +0000 | [diff] [blame] | 2669 | const ObjCAtThrowStmt &S) { |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2670 | llvm::Value *ExceptionAsObject; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2671 | |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2672 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
| 2673 | llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2674 | ExceptionAsObject = |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2675 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 2676 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2677 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 18ccc77 | 2008-09-28 01:03:14 +0000 | [diff] [blame] | 2678 | "Unexpected rethrow outside @catch block."); |
Anders Carlsson | 273558f | 2009-02-07 21:37:21 +0000 | [diff] [blame] | 2679 | ExceptionAsObject = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | 2b1e311 | 2008-09-09 16:16:55 +0000 | [diff] [blame] | 2680 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2681 | |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2682 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Anders Carlsson | 80f2567 | 2008-09-09 17:59:25 +0000 | [diff] [blame] | 2683 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 2684 | |
| 2685 | // Clear the insertion point to indicate we are in unreachable code. |
| 2686 | CGF.Builder.ClearInsertionPoint(); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 2687 | } |
| 2688 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2689 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2690 | /// object: objc_read_weak (id *src) |
| 2691 | /// |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2692 | llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF, |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2693 | llvm::Value *AddrWeakObj) |
| 2694 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2695 | const llvm::Type* DestTy = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2696 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
| 2697 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, |
| 2698 | ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 2699 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2700 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 2701 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6dc2317 | 2008-11-18 21:45:40 +0000 | [diff] [blame] | 2702 | return read_weak; |
| 2703 | } |
| 2704 | |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2705 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 2706 | /// objc_assign_weak (id src, id *dst) |
| 2707 | /// |
| 2708 | void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
| 2709 | llvm::Value *src, llvm::Value *dst) |
| 2710 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2711 | const llvm::Type * SrcTy = src->getType(); |
| 2712 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2713 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2714 | assert(Size <= 8 && "does not support size > 8"); |
| 2715 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2716 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2717 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2718 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2719 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2720 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 2721 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 3e283e3 | 2008-11-18 22:37:34 +0000 | [diff] [blame] | 2722 | src, dst, "weakassign"); |
| 2723 | return; |
| 2724 | } |
| 2725 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2726 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 2727 | /// objc_assign_global (id src, id *dst) |
| 2728 | /// |
| 2729 | void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
| 2730 | llvm::Value *src, llvm::Value *dst) |
| 2731 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2732 | const llvm::Type * SrcTy = src->getType(); |
| 2733 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2734 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2735 | assert(Size <= 8 && "does not support size > 8"); |
| 2736 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2737 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2738 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2739 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2740 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2741 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2742 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2743 | src, dst, "globalassign"); |
| 2744 | return; |
| 2745 | } |
| 2746 | |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2747 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 2748 | /// objc_assign_ivar (id src, id *dst) |
| 2749 | /// |
| 2750 | void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
| 2751 | llvm::Value *src, llvm::Value *dst) |
| 2752 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2753 | const llvm::Type * SrcTy = src->getType(); |
| 2754 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2755 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2756 | assert(Size <= 8 && "does not support size > 8"); |
| 2757 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2758 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2759 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2760 | } |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2761 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2762 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2763 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(), |
Fariborz Jahanian | 7eda836 | 2008-11-20 19:23:36 +0000 | [diff] [blame] | 2764 | src, dst, "assignivar"); |
| 2765 | return; |
| 2766 | } |
| 2767 | |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2768 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 2769 | /// objc_assign_strongCast (id src, id *dst) |
| 2770 | /// |
| 2771 | void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF, |
| 2772 | llvm::Value *src, llvm::Value *dst) |
| 2773 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2774 | const llvm::Type * SrcTy = src->getType(); |
| 2775 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2776 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 2777 | assert(Size <= 8 && "does not support size > 8"); |
| 2778 | src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2779 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 2780 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 2781 | } |
Fariborz Jahanian | dbd32c2 | 2008-11-19 17:34:06 +0000 | [diff] [blame] | 2782 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 2783 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 2784 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 5862650 | 2008-11-19 00:59:10 +0000 | [diff] [blame] | 2785 | src, dst, "weakassign"); |
| 2786 | return; |
| 2787 | } |
| 2788 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2789 | void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2790 | llvm::Value *DestPtr, |
| 2791 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 2792 | QualType Ty) { |
| 2793 | // Get size info for this aggregate. |
| 2794 | std::pair<uint64_t, unsigned> TypeInfo = CGM.getContext().getTypeInfo(Ty); |
| 2795 | unsigned long size = TypeInfo.second/8; |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2796 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 2797 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2798 | llvm::Value *N = llvm::ConstantInt::get(ObjCTypes.LongTy, size); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2799 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
| 2800 | DestPtr, SrcPtr, N); |
| 2801 | return; |
| 2802 | } |
| 2803 | |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2804 | /// EmitObjCValueForIvar - Code Gen for ivar reference. |
| 2805 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2806 | LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF, |
| 2807 | QualType ObjectTy, |
| 2808 | llvm::Value *BaseValue, |
| 2809 | const ObjCIvarDecl *Ivar, |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 2810 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 2811 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2812 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 2813 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 0bb2036 | 2009-02-02 20:02:29 +0000 | [diff] [blame] | 2814 | } |
| 2815 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2816 | llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 2a03192 | 2009-04-22 05:08:15 +0000 | [diff] [blame] | 2817 | const ObjCInterfaceDecl *Interface, |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2818 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 2819 | uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2820 | return llvm::ConstantInt::get( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2821 | CGM.getTypes().ConvertType(CGM.getContext().LongTy), |
| 2822 | Offset); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 2823 | } |
| 2824 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2825 | /* *** Private Interface *** */ |
| 2826 | |
| 2827 | /// EmitImageInfo - Emit the image info marker used to encode some module |
| 2828 | /// level information. |
| 2829 | /// |
| 2830 | /// See: <rdr://4810609&4810587&4810587> |
| 2831 | /// struct IMAGE_INFO { |
| 2832 | /// unsigned version; |
| 2833 | /// unsigned flags; |
| 2834 | /// }; |
| 2835 | enum ImageInfoFlags { |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2836 | eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what |
| 2837 | // this implies. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2838 | eImageInfo_GarbageCollected = (1 << 1), |
| 2839 | eImageInfo_GCOnly = (1 << 2), |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2840 | eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set. |
| 2841 | |
| 2842 | // A flag indicating that the module has no instances of an |
| 2843 | // @synthesize of a superclass variable. <rdar://problem/6803242> |
| 2844 | eImageInfo_CorrectedSynthesize = (1 << 4) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2845 | }; |
| 2846 | |
| 2847 | void CGObjCMac::EmitImageInfo() { |
| 2848 | unsigned version = 0; // Version is unused? |
| 2849 | unsigned flags = 0; |
| 2850 | |
| 2851 | // FIXME: Fix and continue? |
| 2852 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 2853 | flags |= eImageInfo_GarbageCollected; |
| 2854 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 2855 | flags |= eImageInfo_GCOnly; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2856 | |
Daniel Dunbar | c7c6dc0 | 2009-04-20 07:11:47 +0000 | [diff] [blame] | 2857 | // We never allow @synthesize of a superclass property. |
| 2858 | flags |= eImageInfo_CorrectedSynthesize; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2859 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2860 | // Emitted as int[2]; |
| 2861 | llvm::Constant *values[2] = { |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2862 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), version), |
| 2863 | llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), flags) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2864 | }; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2865 | llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext), 2); |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2866 | |
| 2867 | const char *Section; |
| 2868 | if (ObjCABI == 1) |
| 2869 | Section = "__OBJC, __image_info,regular"; |
| 2870 | else |
| 2871 | Section = "__DATA, __objc_imageinfo, regular, no_dead_strip"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2872 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2873 | CreateMetadataVar("\01L_OBJC_IMAGE_INFO", |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 2874 | llvm::ConstantArray::get(AT, values, 2), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2875 | Section, |
| 2876 | 0, |
| 2877 | true); |
| 2878 | GV->setConstant(true); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 2879 | } |
| 2880 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2881 | |
| 2882 | // struct objc_module { |
| 2883 | // unsigned long version; |
| 2884 | // unsigned long size; |
| 2885 | // const char *name; |
| 2886 | // Symtab symtab; |
| 2887 | // }; |
| 2888 | |
| 2889 | // FIXME: Get from somewhere |
| 2890 | static const int ModuleVersion = 7; |
| 2891 | |
| 2892 | void CGObjCMac::EmitModuleInfo() { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 2893 | uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2894 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2895 | std::vector<llvm::Constant*> Values(4); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2896 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion); |
| 2897 | Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size); |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 2898 | // This used to be the filename, now it is unused. <rdr://4327263> |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2899 | Values[2] = GetClassName(&CGM.getContext().Idents.get("")); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2900 | Values[3] = EmitModuleSymbols(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2901 | CreateMetadataVar("\01L_OBJC_MODULES", |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 2902 | llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2903 | "__OBJC,__module_info,regular,no_dead_strip", |
Daniel Dunbar | 58a2912 | 2009-03-09 22:18:41 +0000 | [diff] [blame] | 2904 | 4, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
| 2907 | llvm::Constant *CGObjCMac::EmitModuleSymbols() { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2908 | unsigned NumClasses = DefinedClasses.size(); |
| 2909 | unsigned NumCategories = DefinedCategories.size(); |
| 2910 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2911 | // Return null if no symbols were defined. |
| 2912 | if (!NumClasses && !NumCategories) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2913 | return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2914 | |
| 2915 | std::vector<llvm::Constant*> Values(5); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2916 | Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2917 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 2918 | Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses); |
| 2919 | Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2920 | |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2921 | // The runtime expects exactly the list of defined classes followed |
| 2922 | // by the list of defined categories, in a single array. |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2923 | std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories); |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2924 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2925 | Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i], |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2926 | ObjCTypes.Int8PtrTy); |
| 2927 | for (unsigned i=0; i<NumCategories; i++) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2928 | Symbols[NumClasses + i] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2929 | llvm::ConstantExpr::getBitCast(DefinedCategories[i], |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 2930 | ObjCTypes.Int8PtrTy); |
| 2931 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2932 | Values[4] = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 2933 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2934 | NumClasses + NumCategories), |
| 2935 | Symbols); |
| 2936 | |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 2937 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2938 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2939 | llvm::GlobalVariable *GV = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2940 | CreateMetadataVar("\01L_OBJC_SYMBOLS", Init, |
| 2941 | "__OBJC,__symbols,regular,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2942 | 4, true); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2943 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2944 | } |
| 2945 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2946 | llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2947 | const ObjCInterfaceDecl *ID) { |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 2948 | LazySymbols.insert(ID->getIdentifier()); |
| 2949 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2950 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2951 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2952 | if (!Entry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2953 | llvm::Constant *Casted = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2954 | llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2955 | ObjCTypes.ClassPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2956 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2957 | CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted, |
| 2958 | "__OBJC,__cls_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2959 | 4, true); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 2960 | } |
| 2961 | |
| 2962 | return Builder.CreateLoad(Entry, false, "tmp"); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2963 | } |
| 2964 | |
Daniel Dunbar | 45d196b | 2008-11-01 01:53:16 +0000 | [diff] [blame] | 2965 | llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2966 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2967 | |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2968 | if (!Entry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2969 | llvm::Constant *Casted = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 2970 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2971 | ObjCTypes.SelectorPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2972 | Entry = |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2973 | CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted, |
| 2974 | "__OBJC,__message_refs,literal_pointers,no_dead_strip", |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 2975 | 4, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 2976 | } |
| 2977 | |
| 2978 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 2979 | } |
| 2980 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 2981 | llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) { |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 2982 | llvm::GlobalVariable *&Entry = ClassNames[Ident]; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2983 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2984 | if (!Entry) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2985 | Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 2986 | llvm::ConstantArray::get(VMContext, Ident->getName()), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 2987 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 2988 | 1, true); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2989 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 2990 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 2993 | /// GetIvarLayoutName - Returns a unique constant for the given |
| 2994 | /// ivar layout bitmap. |
| 2995 | llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 2996 | const ObjCCommonTypesHelper &ObjCTypes) { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 2997 | return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Fariborz Jahanian | d80d81b | 2009-03-05 19:17:31 +0000 | [diff] [blame] | 2998 | } |
| 2999 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3000 | static QualType::GCAttrTypes GetGCAttrTypeForType(ASTContext &Ctx, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3001 | QualType FQT) { |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3002 | if (FQT.isObjCGCStrong()) |
| 3003 | return QualType::Strong; |
| 3004 | |
| 3005 | if (FQT.isObjCGCWeak()) |
| 3006 | return QualType::Weak; |
| 3007 | |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 3008 | if (FQT->isObjCObjectPointerType()) |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3009 | return QualType::Strong; |
| 3010 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3011 | if (const PointerType *PT = FQT->getAs<PointerType>()) |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3012 | return GetGCAttrTypeForType(Ctx, PT->getPointeeType()); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3013 | |
| 3014 | return QualType::GCNone; |
| 3015 | } |
| 3016 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3017 | void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3018 | unsigned int BytePos, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3019 | bool ForStrongLayout, |
| 3020 | bool &HasUnion) { |
| 3021 | const RecordDecl *RD = RT->getDecl(); |
| 3022 | // FIXME - Use iterator. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3023 | llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end()); |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3024 | const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3025 | const llvm::StructLayout *RecLayout = |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3026 | CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3027 | |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3028 | BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos, |
| 3029 | ForStrongLayout, HasUnion); |
| 3030 | } |
| 3031 | |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 3032 | void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3033 | const llvm::StructLayout *Layout, |
| 3034 | const RecordDecl *RD, |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3035 | const llvm::SmallVectorImpl<FieldDecl*> &RecFields, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3036 | unsigned int BytePos, bool ForStrongLayout, |
| 3037 | bool &HasUnion) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3038 | bool IsUnion = (RD && RD->isUnion()); |
| 3039 | uint64_t MaxUnionIvarSize = 0; |
| 3040 | uint64_t MaxSkippedUnionIvarSize = 0; |
| 3041 | FieldDecl *MaxField = 0; |
| 3042 | FieldDecl *MaxSkippedField = 0; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3043 | FieldDecl *LastFieldBitfield = 0; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3044 | uint64_t MaxFieldOffset = 0; |
| 3045 | uint64_t MaxSkippedFieldOffset = 0; |
| 3046 | uint64_t LastBitfieldOffset = 0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3047 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3048 | if (RecFields.empty()) |
| 3049 | return; |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3050 | unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0); |
| 3051 | unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth(); |
| 3052 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3053 | for (unsigned i = 0, e = RecFields.size(); i != e; ++i) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3054 | FieldDecl *Field = RecFields[i]; |
Daniel Dunbar | e05cc98 | 2009-05-03 23:35:23 +0000 | [diff] [blame] | 3055 | uint64_t FieldOffset; |
Anders Carlsson | fc514ab | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 3056 | if (RD) { |
| 3057 | if (Field->isBitField()) { |
| 3058 | CodeGenTypes::BitFieldInfo Info = CGM.getTypes().getBitFieldInfo(Field); |
Anders Carlsson | ef46d18 | 2009-08-25 02:07:02 +0000 | [diff] [blame] | 3059 | |
| 3060 | const llvm::Type *Ty = |
| 3061 | CGM.getTypes().ConvertTypeForMemRecursive(Field->getType()); |
| 3062 | uint64_t TypeSize = |
| 3063 | CGM.getTypes().getTargetData().getTypeAllocSize(Ty); |
| 3064 | FieldOffset = Info.FieldNo * TypeSize; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3065 | } else |
| 3066 | FieldOffset = |
Anders Carlsson | fc514ab | 2009-07-24 17:23:54 +0000 | [diff] [blame] | 3067 | Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field)); |
| 3068 | } else |
Daniel Dunbar | e05cc98 | 2009-05-03 23:35:23 +0000 | [diff] [blame] | 3069 | FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)); |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3070 | |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3071 | // Skip over unnamed or bitfields |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3072 | if (!Field->getIdentifier() || Field->isBitField()) { |
| 3073 | LastFieldBitfield = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3074 | LastBitfieldOffset = FieldOffset; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3075 | continue; |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3076 | } |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3077 | |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3078 | LastFieldBitfield = 0; |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3079 | QualType FQT = Field->getType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3080 | if (FQT->isRecordType() || FQT->isUnionType()) { |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3081 | if (FQT->isUnionType()) |
| 3082 | HasUnion = true; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3083 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3084 | BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(), |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3085 | BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3086 | ForStrongLayout, HasUnion); |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3087 | continue; |
| 3088 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3089 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3090 | if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3091 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3092 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3093 | uint64_t ElCount = CArray->getSize().getZExtValue(); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3094 | assert(CArray && "only array with known element size is supported"); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3095 | FQT = CArray->getElementType(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3096 | while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) { |
| 3097 | const ConstantArrayType *CArray = |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3098 | dyn_cast_or_null<ConstantArrayType>(Array); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3099 | ElCount *= CArray->getSize().getZExtValue(); |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3100 | FQT = CArray->getElementType(); |
| 3101 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3102 | |
| 3103 | assert(!FQT->isUnionType() && |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3104 | "layout for array of unions not supported"); |
| 3105 | if (FQT->isRecordType()) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3106 | int OldIndex = IvarsInfo.size() - 1; |
| 3107 | int OldSkIndex = SkipIvars.size() -1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3108 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3109 | const RecordType *RT = FQT->getAs<RecordType>(); |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3110 | BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset, |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3111 | ForStrongLayout, HasUnion); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3112 | |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3113 | // Replicate layout information for each array element. Note that |
| 3114 | // one element is already done. |
| 3115 | uint64_t ElIx = 1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3116 | for (int FirstIndex = IvarsInfo.size() - 1, |
| 3117 | FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3118 | uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits; |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3119 | for (int i = OldIndex+1; i <= FirstIndex; ++i) |
| 3120 | IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx, |
| 3121 | IvarsInfo[i].ivar_size)); |
| 3122 | for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i) |
| 3123 | SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx, |
| 3124 | SkipIvars[i].ivar_size)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3125 | } |
| 3126 | continue; |
| 3127 | } |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3128 | } |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3129 | // At this point, we are done with Record/Union and array there of. |
| 3130 | // For other arrays we are down to its element type. |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3131 | QualType::GCAttrTypes GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT); |
Daniel Dunbar | 5e563dd | 2009-05-03 13:55:09 +0000 | [diff] [blame] | 3132 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3133 | unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType()); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3134 | if ((ForStrongLayout && GCAttr == QualType::Strong) |
| 3135 | || (!ForStrongLayout && GCAttr == QualType::Weak)) { |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3136 | if (IsUnion) { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3137 | uint64_t UnionIvarSize = FieldSize / WordSizeInBits; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3138 | if (UnionIvarSize > MaxUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3139 | MaxUnionIvarSize = UnionIvarSize; |
| 3140 | MaxField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3141 | MaxFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3142 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3143 | } else { |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3144 | IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3145 | FieldSize / WordSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3146 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3147 | } else if ((ForStrongLayout && |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3148 | (GCAttr == QualType::GCNone || GCAttr == QualType::Weak)) |
| 3149 | || (!ForStrongLayout && GCAttr != QualType::Weak)) { |
| 3150 | if (IsUnion) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3151 | // FIXME: Why the asymmetry? We divide by word size in bits on other |
| 3152 | // side. |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3153 | uint64_t UnionIvarSize = FieldSize; |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3154 | if (UnionIvarSize > MaxSkippedUnionIvarSize) { |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3155 | MaxSkippedUnionIvarSize = UnionIvarSize; |
| 3156 | MaxSkippedField = Field; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3157 | MaxSkippedFieldOffset = FieldOffset; |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3158 | } |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3159 | } else { |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3160 | // FIXME: Why the asymmetry, we divide by byte size in bits here? |
Daniel Dunbar | 25d583e | 2009-05-03 14:17:18 +0000 | [diff] [blame] | 3161 | SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3162 | FieldSize / ByteSizeInBits)); |
Fariborz Jahanian | 820e020 | 2009-03-11 00:07:04 +0000 | [diff] [blame] | 3163 | } |
| 3164 | } |
| 3165 | } |
Daniel Dunbar | d58edcb | 2009-05-03 14:10:34 +0000 | [diff] [blame] | 3166 | |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3167 | if (LastFieldBitfield) { |
| 3168 | // Last field was a bitfield. Must update skip info. |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3169 | Expr *BitWidth = LastFieldBitfield->getBitWidth(); |
| 3170 | uint64_t BitFieldSize = |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 3171 | BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue(); |
Daniel Dunbar | 487993b | 2009-05-03 13:32:01 +0000 | [diff] [blame] | 3172 | GC_IVAR skivar; |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3173 | skivar.ivar_bytepos = BytePos + LastBitfieldOffset; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3174 | skivar.ivar_size = (BitFieldSize / ByteSizeInBits) |
| 3175 | + ((BitFieldSize % ByteSizeInBits) != 0); |
| 3176 | SkipIvars.push_back(skivar); |
Fariborz Jahanian | 7fb1627 | 2009-04-21 18:33:06 +0000 | [diff] [blame] | 3177 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3178 | |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3179 | if (MaxField) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3180 | IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3181 | MaxUnionIvarSize)); |
| 3182 | if (MaxSkippedField) |
Daniel Dunbar | 900c198 | 2009-05-03 23:31:46 +0000 | [diff] [blame] | 3183 | SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset, |
Daniel Dunbar | 8b2926c | 2009-05-03 13:44:42 +0000 | [diff] [blame] | 3184 | MaxSkippedUnionIvarSize)); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3185 | } |
| 3186 | |
| 3187 | /// BuildIvarLayout - Builds ivar layout bitmap for the class |
| 3188 | /// implementation for the __strong or __weak case. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3189 | /// The layout map displays which words in ivar list must be skipped |
| 3190 | /// and which must be scanned by GC (see below). String is built of bytes. |
| 3191 | /// Each byte is divided up in two nibbles (4-bit each). Left nibble is count |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3192 | /// of words to skip and right nibble is count of words to scan. So, each |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3193 | /// nibble represents up to 15 workds to skip or scan. Skipping the rest is |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3194 | /// represented by a 0x00 byte which also ends the string. |
| 3195 | /// 1. when ForStrongLayout is true, following ivars are scanned: |
| 3196 | /// - id, Class |
| 3197 | /// - object * |
| 3198 | /// - __strong anything |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3199 | /// |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3200 | /// 2. When ForStrongLayout is false, following ivars are scanned: |
| 3201 | /// - __weak anything |
| 3202 | /// |
Fariborz Jahanian | a5a10c3 | 2009-03-10 16:22:08 +0000 | [diff] [blame] | 3203 | llvm::Constant *CGObjCCommonMac::BuildIvarLayout( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3204 | const ObjCImplementationDecl *OMD, |
| 3205 | bool ForStrongLayout) { |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3206 | bool hasUnion = false; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3207 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3208 | unsigned int WordsToScan, WordsToSkip; |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3209 | const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3210 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3211 | return llvm::Constant::getNullValue(PtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3212 | |
Chris Lattner | f169085 | 2009-03-31 08:48:01 +0000 | [diff] [blame] | 3213 | llvm::SmallVector<FieldDecl*, 32> RecFields; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3214 | const ObjCInterfaceDecl *OI = OMD->getClassInterface(); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3215 | CGM.getContext().CollectObjCIvars(OI, RecFields); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3216 | |
Daniel Dunbar | 3715328 | 2009-05-04 04:10:48 +0000 | [diff] [blame] | 3217 | // Add this implementations synthesized ivars. |
Fariborz Jahanian | 9820074 | 2009-05-12 18:14:29 +0000 | [diff] [blame] | 3218 | llvm::SmallVector<ObjCIvarDecl*, 16> Ivars; |
| 3219 | CGM.getContext().CollectSynthesizedIvars(OI, Ivars); |
| 3220 | for (unsigned k = 0, e = Ivars.size(); k != e; ++k) |
| 3221 | RecFields.push_back(cast<FieldDecl>(Ivars[k])); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3222 | |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3223 | if (RecFields.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3224 | return llvm::Constant::getNullValue(PtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3225 | |
| 3226 | SkipIvars.clear(); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3227 | IvarsInfo.clear(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3228 | |
Daniel Dunbar | 5a5a803 | 2009-05-03 21:05:10 +0000 | [diff] [blame] | 3229 | BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion); |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3230 | if (IvarsInfo.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3231 | return llvm::Constant::getNullValue(PtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3232 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3233 | // Sort on byte position in case we encounterred a union nested in |
| 3234 | // the ivar list. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3235 | if (hasUnion && !IvarsInfo.empty()) |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 3236 | std::sort(IvarsInfo.begin(), IvarsInfo.end()); |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3237 | if (hasUnion && !SkipIvars.empty()) |
Daniel Dunbar | 0941b49 | 2009-04-23 01:29:05 +0000 | [diff] [blame] | 3238 | std::sort(SkipIvars.begin(), SkipIvars.end()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3239 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3240 | // Build the string of skip/scan nibbles |
Fariborz Jahanian | 8c2f2d1 | 2009-04-24 17:15:27 +0000 | [diff] [blame] | 3241 | llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3242 | unsigned int WordSize = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 3243 | CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3244 | if (IvarsInfo[0].ivar_bytepos == 0) { |
| 3245 | WordsToSkip = 0; |
| 3246 | WordsToScan = IvarsInfo[0].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3247 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3248 | WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize; |
| 3249 | WordsToScan = IvarsInfo[0].ivar_size; |
| 3250 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3251 | for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3252 | unsigned int TailPrevGCObjC = |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3253 | IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3254 | if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3255 | // consecutive 'scanned' object pointers. |
| 3256 | WordsToScan += IvarsInfo[i].ivar_size; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3257 | } else { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3258 | // Skip over 'gc'able object pointer which lay over each other. |
| 3259 | if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos) |
| 3260 | continue; |
| 3261 | // Must skip over 1 or more words. We save current skip/scan values |
| 3262 | // and start a new pair. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3263 | SKIP_SCAN SkScan; |
| 3264 | SkScan.skip = WordsToSkip; |
| 3265 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3266 | SkipScanIvars.push_back(SkScan); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3267 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3268 | // Skip the hole. |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3269 | SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize; |
| 3270 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3271 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3272 | WordsToSkip = 0; |
| 3273 | WordsToScan = IvarsInfo[i].ivar_size; |
| 3274 | } |
| 3275 | } |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3276 | if (WordsToScan > 0) { |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3277 | SKIP_SCAN SkScan; |
| 3278 | SkScan.skip = WordsToSkip; |
| 3279 | SkScan.scan = WordsToScan; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3280 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3281 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3282 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3283 | bool BytesSkipped = false; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3284 | if (!SkipIvars.empty()) { |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3285 | unsigned int LastIndex = SkipIvars.size()-1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3286 | int LastByteSkipped = |
| 3287 | SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3288 | LastIndex = IvarsInfo.size()-1; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3289 | int LastByteScanned = |
| 3290 | IvarsInfo[LastIndex].ivar_bytepos + |
| 3291 | IvarsInfo[LastIndex].ivar_size * WordSize; |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3292 | BytesSkipped = (LastByteSkipped > LastByteScanned); |
| 3293 | // Compute number of bytes to skip at the tail end of the last ivar scanned. |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3294 | if (BytesSkipped) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3295 | unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize; |
Fariborz Jahanian | c8ce9c8 | 2009-03-12 22:50:49 +0000 | [diff] [blame] | 3296 | SKIP_SCAN SkScan; |
| 3297 | SkScan.skip = TotalWords - (LastByteScanned/WordSize); |
| 3298 | SkScan.scan = 0; |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3299 | SkipScanIvars.push_back(SkScan); |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3300 | } |
| 3301 | } |
| 3302 | // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced |
| 3303 | // as 0xMN. |
Fariborz Jahanian | 81adc05 | 2009-04-24 16:17:09 +0000 | [diff] [blame] | 3304 | int SkipScan = SkipScanIvars.size()-1; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3305 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3306 | if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0 |
| 3307 | && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) { |
| 3308 | // 0xM0 followed by 0x0N detected. |
| 3309 | SkipScanIvars[i].scan = SkipScanIvars[i+1].scan; |
| 3310 | for (int j = i+1; j < SkipScan; j++) |
| 3311 | SkipScanIvars[j] = SkipScanIvars[j+1]; |
| 3312 | --SkipScan; |
| 3313 | } |
| 3314 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3315 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3316 | // Generate the string. |
| 3317 | std::string BitMap; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3318 | for (int i = 0; i <= SkipScan; i++) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3319 | unsigned char byte; |
| 3320 | unsigned int skip_small = SkipScanIvars[i].skip % 0xf; |
| 3321 | unsigned int scan_small = SkipScanIvars[i].scan % 0xf; |
| 3322 | unsigned int skip_big = SkipScanIvars[i].skip / 0xf; |
| 3323 | unsigned int scan_big = SkipScanIvars[i].scan / 0xf; |
| 3324 | |
| 3325 | if (skip_small > 0 || skip_big > 0) |
| 3326 | BytesSkipped = true; |
| 3327 | // first skip big. |
| 3328 | for (unsigned int ix = 0; ix < skip_big; ix++) |
| 3329 | BitMap += (unsigned char)(0xf0); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3330 | |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3331 | // next (skip small, scan) |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3332 | if (skip_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3333 | byte = skip_small << 4; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3334 | if (scan_big > 0) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3335 | byte |= 0xf; |
| 3336 | --scan_big; |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3337 | } else if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3338 | byte |= scan_small; |
| 3339 | scan_small = 0; |
| 3340 | } |
| 3341 | BitMap += byte; |
| 3342 | } |
| 3343 | // next scan big |
| 3344 | for (unsigned int ix = 0; ix < scan_big; ix++) |
| 3345 | BitMap += (unsigned char)(0x0f); |
| 3346 | // last scan small |
Daniel Dunbar | 31682fd | 2009-05-03 23:21:22 +0000 | [diff] [blame] | 3347 | if (scan_small) { |
Fariborz Jahanian | 9397e1d | 2009-03-11 20:59:05 +0000 | [diff] [blame] | 3348 | byte = scan_small; |
| 3349 | BitMap += byte; |
| 3350 | } |
| 3351 | } |
| 3352 | // null terminate string. |
Fariborz Jahanian | 667423a | 2009-03-25 22:36:49 +0000 | [diff] [blame] | 3353 | unsigned char zero = 0; |
| 3354 | BitMap += zero; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3355 | |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3356 | if (CGM.getLangOptions().ObjCGCBitmapPrint) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3357 | printf("\n%s ivar layout for class '%s': ", |
Fariborz Jahanian | 3d2ad66 | 2009-04-20 22:03:45 +0000 | [diff] [blame] | 3358 | ForStrongLayout ? "strong" : "weak", |
| 3359 | OMD->getClassInterface()->getNameAsCString()); |
| 3360 | const unsigned char *s = (unsigned char*)BitMap.c_str(); |
| 3361 | for (unsigned i = 0; i < BitMap.size(); i++) |
| 3362 | if (!(s[i] & 0xf0)) |
| 3363 | printf("0x0%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3364 | else |
| 3365 | printf("0x%x%s", s[i], s[i] != 0 ? ", " : ""); |
| 3366 | printf("\n"); |
| 3367 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3368 | llvm::GlobalVariable * Entry = |
| 3369 | CreateMetadataVar("\01L_OBJC_CLASS_NAME_", |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3370 | llvm::ConstantArray::get(VMContext, BitMap.c_str()), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3371 | "__TEXT,__cstring,cstring_literals", |
| 3372 | 1, true); |
| 3373 | return getConstantGEP(VMContext, Entry, 0, 0); |
Fariborz Jahanian | d61a50a | 2009-03-05 22:39:55 +0000 | [diff] [blame] | 3374 | } |
| 3375 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3376 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) { |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3377 | llvm::GlobalVariable *&Entry = MethodVarNames[Sel]; |
| 3378 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3379 | // FIXME: Avoid std::string copying. |
| 3380 | if (!Entry) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3381 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_", |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3382 | llvm::ConstantArray::get(VMContext, Sel.getAsString()), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3383 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3384 | 1, true); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3385 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3386 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3387 | } |
| 3388 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3389 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3390 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3391 | return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID)); |
| 3392 | } |
| 3393 | |
| 3394 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3395 | llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3396 | return GetMethodVarName(&CGM.getContext().Idents.get(Name)); |
| 3397 | } |
| 3398 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 3399 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) { |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3400 | std::string TypeStr; |
| 3401 | CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field); |
| 3402 | |
| 3403 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3404 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3405 | if (!Entry) |
| 3406 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3407 | llvm::ConstantArray::get(VMContext, TypeStr), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3408 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3409 | 1, true); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3410 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3411 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 259d93d | 2008-08-12 03:39:23 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3414 | llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) { |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3415 | std::string TypeStr; |
Daniel Dunbar | c45ef60 | 2008-08-26 21:51:14 +0000 | [diff] [blame] | 3416 | CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D), |
| 3417 | TypeStr); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3418 | |
| 3419 | llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr]; |
| 3420 | |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3421 | if (!Entry) |
| 3422 | Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_", |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3423 | llvm::ConstantArray::get(VMContext, TypeStr), |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3424 | "__TEXT,__cstring,cstring_literals", |
| 3425 | 1, true); |
Devang Patel | 7794bb8 | 2009-03-04 18:21:39 +0000 | [diff] [blame] | 3426 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3427 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3428 | } |
| 3429 | |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3430 | // FIXME: Merge into a single cstring creation function. |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3431 | llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) { |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3432 | llvm::GlobalVariable *&Entry = PropertyNames[Ident]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3433 | |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3434 | if (!Entry) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3435 | Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_", |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3436 | llvm::ConstantArray::get(VMContext, Ident->getName()), |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3437 | "__TEXT,__cstring,cstring_literals", |
Daniel Dunbar | b90bb00 | 2009-04-14 23:14:47 +0000 | [diff] [blame] | 3438 | 1, true); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3439 | |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3440 | return getConstantGEP(VMContext, Entry, 0, 0); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3441 | } |
| 3442 | |
| 3443 | // FIXME: Merge into a single cstring creation function. |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3444 | // FIXME: This Decl should be more precise. |
Daniel Dunbar | 63c5b50 | 2009-03-09 21:49:58 +0000 | [diff] [blame] | 3445 | llvm::Constant * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3446 | CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD, |
| 3447 | const Decl *Container) { |
Daniel Dunbar | c56f34a | 2008-08-28 04:38:10 +0000 | [diff] [blame] | 3448 | std::string TypeStr; |
| 3449 | CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr); |
Daniel Dunbar | c8ef551 | 2008-08-23 00:19:03 +0000 | [diff] [blame] | 3450 | return GetPropertyName(&CGM.getContext().Idents.get(TypeStr)); |
| 3451 | } |
| 3452 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3453 | void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D, |
Fariborz Jahanian | 56210f7 | 2009-01-21 23:34:32 +0000 | [diff] [blame] | 3454 | const ObjCContainerDecl *CD, |
| 3455 | std::string &NameOut) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 3456 | NameOut = '\01'; |
| 3457 | NameOut += (D->isInstanceMethod() ? '-' : '+'); |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3458 | NameOut += '['; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 3459 | assert (CD && "Missing container decl in GetNameForMethod"); |
| 3460 | NameOut += CD->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3461 | if (const ObjCCategoryImplDecl *CID = |
Fariborz Jahanian | 1e9aef3 | 2009-04-16 18:34:20 +0000 | [diff] [blame] | 3462 | dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) { |
| 3463 | NameOut += '('; |
| 3464 | NameOut += CID->getNameAsString(); |
| 3465 | NameOut+= ')'; |
| 3466 | } |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 3467 | NameOut += ' '; |
| 3468 | NameOut += D->getSelector().getAsString(); |
| 3469 | NameOut += ']'; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 3470 | } |
| 3471 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3472 | void CGObjCMac::FinishModule() { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3473 | EmitModuleInfo(); |
| 3474 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3475 | // Emit the dummy bodies for any protocols which were referenced but |
| 3476 | // never defined. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3477 | for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 3478 | I = Protocols.begin(), e = Protocols.end(); I != e; ++I) { |
| 3479 | if (I->second->hasInitializer()) |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3480 | continue; |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3481 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3482 | std::vector<llvm::Constant*> Values(5); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3483 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 3484 | Values[1] = GetClassName(I->first); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3485 | Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3486 | Values[3] = Values[4] = |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 3487 | llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 3488 | I->second->setLinkage(llvm::GlobalValue::InternalLinkage); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 3489 | I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy, |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3490 | Values)); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 3491 | CGM.AddUsedGlobal(I->second); |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3492 | } |
| 3493 | |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3494 | // Add assembler directives to add lazy undefined symbol references |
| 3495 | // for classes which are referenced but not defined. This is |
| 3496 | // important for correct linker interaction. |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame^] | 3497 | // |
| 3498 | // FIXME: It would be nice if we had an LLVM construct for this. |
| 3499 | if (!LazySymbols.empty() || !DefinedSymbols.empty()) { |
| 3500 | llvm::SmallString<256> Asm; |
| 3501 | Asm += CGM.getModule().getModuleInlineAsm(); |
| 3502 | if (!Asm.empty() && Asm.back() != '\n') |
| 3503 | Asm += '\n'; |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3504 | |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame^] | 3505 | llvm::raw_svector_ostream OS(Asm); |
| 3506 | for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(), |
| 3507 | e = LazySymbols.end(); I != e; ++I) |
| 3508 | OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n"; |
| 3509 | for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(), |
| 3510 | e = DefinedSymbols.end(); I != e; ++I) |
| 3511 | OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n" |
| 3512 | << "\t.globl .objc_class_name_" << (*I)->getName() << "\n"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3513 | |
Daniel Dunbar | 3306349 | 2009-09-07 00:20:42 +0000 | [diff] [blame^] | 3514 | CGM.getModule().setModuleInlineAsm(OS.str()); |
Daniel Dunbar | 242d4dc | 2008-08-25 06:02:07 +0000 | [diff] [blame] | 3515 | } |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3516 | } |
| 3517 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3518 | CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3519 | : CGObjCCommonMac(cgm), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3520 | ObjCTypes(cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3521 | { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3522 | ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3523 | ObjCABI = 2; |
| 3524 | } |
| 3525 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 3526 | /* *** */ |
| 3527 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3528 | ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3529 | : VMContext(cgm.getLLVMContext()), CGM(cgm) |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3530 | { |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3531 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 3532 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3533 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3534 | ShortTy = Types.ConvertType(Ctx.ShortTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3535 | IntTy = Types.ConvertType(Ctx.IntTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3536 | LongTy = Types.ConvertType(Ctx.LongTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 3537 | LongLongTy = Types.ConvertType(Ctx.LongLongTy); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3538 | Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3539 | |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3540 | ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3541 | PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy); |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3542 | SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3543 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3544 | // FIXME: It would be nice to unify this with the opaque type, so that the IR |
| 3545 | // comes out a bit cleaner. |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3546 | const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3547 | ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3548 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3549 | // I'm not sure I like this. The implicit coordination is a bit |
| 3550 | // gross. We should solve this in a reasonable fashion because this |
| 3551 | // is a pretty common task (match some runtime data structure with |
| 3552 | // an LLVM data structure). |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3553 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3554 | // FIXME: This is leaked. |
| 3555 | // FIXME: Merge with rewriter code? |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3556 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3557 | // struct _objc_super { |
| 3558 | // id self; |
| 3559 | // Class cls; |
| 3560 | // } |
| 3561 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 3562 | SourceLocation(), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3563 | &Ctx.Idents.get("_objc_super")); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3564 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 3565 | Ctx.getObjCIdType(), 0, 0, false)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3566 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 3567 | Ctx.getObjCClassType(), 0, 0, false)); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3568 | RD->completeDefinition(Ctx); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3569 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3570 | SuperCTy = Ctx.getTagDeclType(RD); |
| 3571 | SuperPtrCTy = Ctx.getPointerType(SuperCTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3572 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3573 | SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3574 | SuperPtrTy = llvm::PointerType::getUnqual(SuperTy); |
| 3575 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3576 | // struct _prop_t { |
| 3577 | // char *name; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3578 | // char *attributes; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3579 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3580 | PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3581 | CGM.getModule().addTypeName("struct._prop_t", |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3582 | PropertyTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3583 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3584 | // struct _prop_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3585 | // uint32_t entsize; // sizeof(struct _prop_t) |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3586 | // uint32_t count_of_properties; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3587 | // struct _prop_t prop_list[count_of_properties]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3588 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3589 | PropertyListTy = llvm::StructType::get(VMContext, IntTy, |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3590 | IntTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3591 | llvm::ArrayType::get(PropertyTy, 0), |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3592 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3593 | CGM.getModule().addTypeName("struct._prop_list_t", |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3594 | PropertyListTy); |
| 3595 | // struct _prop_list_t * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3596 | PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3597 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3598 | // struct _objc_method { |
| 3599 | // SEL _cmd; |
| 3600 | // char *method_type; |
| 3601 | // char *_imp; |
| 3602 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3603 | MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy, |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3604 | Int8PtrTy, |
| 3605 | Int8PtrTy, |
| 3606 | NULL); |
| 3607 | CGM.getModule().addTypeName("struct._objc_method", MethodTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3608 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3609 | // struct _objc_cache * |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 3610 | CacheTy = llvm::OpaqueType::get(VMContext); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3611 | CGM.getModule().addTypeName("struct._objc_cache", CacheTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3612 | CachePtrTy = llvm::PointerType::getUnqual(CacheTy); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3613 | } |
Daniel Dunbar | 4e2d7d0 | 2008-08-12 06:48:42 +0000 | [diff] [blame] | 3614 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3615 | ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3616 | : ObjCCommonTypesHelper(cgm) |
| 3617 | { |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3618 | // struct _objc_method_description { |
| 3619 | // SEL name; |
| 3620 | // char *types; |
| 3621 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3622 | MethodDescriptionTy = |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3623 | llvm::StructType::get(VMContext, SelectorPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3624 | Int8PtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3625 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3626 | CGM.getModule().addTypeName("struct._objc_method_description", |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3627 | MethodDescriptionTy); |
| 3628 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3629 | // struct _objc_method_description_list { |
| 3630 | // int count; |
| 3631 | // struct _objc_method_description[1]; |
| 3632 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3633 | MethodDescriptionListTy = |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3634 | llvm::StructType::get(VMContext, IntTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3635 | llvm::ArrayType::get(MethodDescriptionTy, 0), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3636 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3637 | CGM.getModule().addTypeName("struct._objc_method_description_list", |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3638 | MethodDescriptionListTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3639 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3640 | // struct _objc_method_description_list * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3641 | MethodDescriptionListPtrTy = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3642 | llvm::PointerType::getUnqual(MethodDescriptionListTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3643 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3644 | // Protocol description structures |
| 3645 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3646 | // struct _objc_protocol_extension { |
| 3647 | // uint32_t size; // sizeof(struct _objc_protocol_extension) |
| 3648 | // struct _objc_method_description_list *optional_instance_methods; |
| 3649 | // struct _objc_method_description_list *optional_class_methods; |
| 3650 | // struct _objc_property_list *instance_properties; |
| 3651 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3652 | ProtocolExtensionTy = |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3653 | llvm::StructType::get(VMContext, IntTy, |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3654 | MethodDescriptionListPtrTy, |
| 3655 | MethodDescriptionListPtrTy, |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3656 | PropertyListPtrTy, |
| 3657 | NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3658 | CGM.getModule().addTypeName("struct._objc_protocol_extension", |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3659 | ProtocolExtensionTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3660 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3661 | // struct _objc_protocol_extension * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3662 | ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3663 | |
Daniel Dunbar | 0c0e7a6 | 2008-10-29 22:36:39 +0000 | [diff] [blame] | 3664 | // Handle recursive construction of Protocol and ProtocolList types |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3665 | |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 3666 | llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext); |
| 3667 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3668 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3669 | const llvm::Type *T = |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3670 | llvm::StructType::get(VMContext, |
| 3671 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3672 | LongTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3673 | llvm::ArrayType::get(ProtocolTyHolder, 0), |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3674 | NULL); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3675 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T); |
| 3676 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3677 | // struct _objc_protocol { |
| 3678 | // struct _objc_protocol_extension *isa; |
| 3679 | // char *protocol_name; |
| 3680 | // struct _objc_protocol **_objc_protocol_list; |
| 3681 | // struct _objc_method_description_list *instance_methods; |
| 3682 | // struct _objc_method_description_list *class_methods; |
| 3683 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3684 | T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3685 | Int8PtrTy, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3686 | llvm::PointerType::getUnqual(ProtocolListTyHolder), |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3687 | MethodDescriptionListPtrTy, |
| 3688 | MethodDescriptionListPtrTy, |
| 3689 | NULL); |
| 3690 | cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T); |
| 3691 | |
| 3692 | ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3693 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3694 | ProtocolListTy); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3695 | // struct _objc_protocol_list * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3696 | ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy); |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 3697 | |
| 3698 | ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get()); |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3699 | CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3700 | ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3701 | |
| 3702 | // Class description structures |
| 3703 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3704 | // struct _objc_ivar { |
| 3705 | // char *ivar_name; |
| 3706 | // char *ivar_type; |
| 3707 | // int ivar_offset; |
| 3708 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3709 | IvarTy = llvm::StructType::get(VMContext, Int8PtrTy, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3710 | Int8PtrTy, |
| 3711 | IntTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3712 | NULL); |
| 3713 | CGM.getModule().addTypeName("struct._objc_ivar", IvarTy); |
| 3714 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3715 | // struct _objc_ivar_list * |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 3716 | IvarListTy = llvm::OpaqueType::get(VMContext); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3717 | CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3718 | IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3719 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3720 | // struct _objc_method_list * |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 3721 | MethodListTy = llvm::OpaqueType::get(VMContext); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3722 | CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3723 | MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3724 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3725 | // struct _objc_class_extension * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3726 | ClassExtensionTy = |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3727 | llvm::StructType::get(VMContext, IntTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3728 | Int8PtrTy, |
| 3729 | PropertyListPtrTy, |
| 3730 | NULL); |
| 3731 | CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3732 | ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3733 | |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 3734 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3735 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3736 | // struct _objc_class { |
| 3737 | // Class isa; |
| 3738 | // Class super_class; |
| 3739 | // char *name; |
| 3740 | // long version; |
| 3741 | // long info; |
| 3742 | // long instance_size; |
| 3743 | // struct _objc_ivar_list *ivars; |
| 3744 | // struct _objc_method_list *methods; |
| 3745 | // struct _objc_cache *cache; |
| 3746 | // struct _objc_protocol_list *protocols; |
| 3747 | // char *ivar_layout; |
| 3748 | // struct _objc_class_ext *ext; |
| 3749 | // }; |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3750 | T = llvm::StructType::get(VMContext, |
| 3751 | llvm::PointerType::getUnqual(ClassTyHolder), |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3752 | llvm::PointerType::getUnqual(ClassTyHolder), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3753 | Int8PtrTy, |
| 3754 | LongTy, |
| 3755 | LongTy, |
| 3756 | LongTy, |
| 3757 | IvarListPtrTy, |
| 3758 | MethodListPtrTy, |
| 3759 | CachePtrTy, |
| 3760 | ProtocolListPtrTy, |
| 3761 | Int8PtrTy, |
| 3762 | ClassExtensionPtrTy, |
| 3763 | NULL); |
| 3764 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3765 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3766 | ClassTy = cast<llvm::StructType>(ClassTyHolder.get()); |
| 3767 | CGM.getModule().addTypeName("struct._objc_class", ClassTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3768 | ClassPtrTy = llvm::PointerType::getUnqual(ClassTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3769 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3770 | // struct _objc_category { |
| 3771 | // char *category_name; |
| 3772 | // char *class_name; |
| 3773 | // struct _objc_method_list *instance_method; |
| 3774 | // struct _objc_method_list *class_method; |
| 3775 | // uint32_t size; // sizeof(struct _objc_category) |
| 3776 | // struct _objc_property_list *instance_properties;// category's @property |
| 3777 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3778 | CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy, |
Daniel Dunbar | 86e253a | 2008-08-22 20:34:54 +0000 | [diff] [blame] | 3779 | Int8PtrTy, |
| 3780 | MethodListPtrTy, |
| 3781 | MethodListPtrTy, |
| 3782 | ProtocolListPtrTy, |
| 3783 | IntTy, |
| 3784 | PropertyListPtrTy, |
| 3785 | NULL); |
| 3786 | CGM.getModule().addTypeName("struct._objc_category", CategoryTy); |
| 3787 | |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3788 | // Global metadata structures |
| 3789 | |
Fariborz Jahanian | 10a4231 | 2009-01-21 00:39:53 +0000 | [diff] [blame] | 3790 | // struct _objc_symtab { |
| 3791 | // long sel_ref_cnt; |
| 3792 | // SEL *refs; |
| 3793 | // short cls_def_cnt; |
| 3794 | // short cat_def_cnt; |
| 3795 | // char *defs[cls_def_cnt + cat_def_cnt]; |
| 3796 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3797 | SymtabTy = llvm::StructType::get(VMContext, LongTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3798 | SelectorPtrTy, |
| 3799 | ShortTy, |
| 3800 | ShortTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3801 | llvm::ArrayType::get(Int8PtrTy, 0), |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3802 | NULL); |
| 3803 | CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3804 | SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy); |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3805 | |
Fariborz Jahanian | db28686 | 2009-01-22 00:37:21 +0000 | [diff] [blame] | 3806 | // struct _objc_module { |
| 3807 | // long version; |
| 3808 | // long size; // sizeof(struct _objc_module) |
| 3809 | // char *name; |
| 3810 | // struct _objc_symtab* symtab; |
| 3811 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3812 | ModuleTy = |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3813 | llvm::StructType::get(VMContext, LongTy, |
Daniel Dunbar | 27f9d77 | 2008-08-21 04:36:09 +0000 | [diff] [blame] | 3814 | LongTy, |
| 3815 | Int8PtrTy, |
| 3816 | SymtabPtrTy, |
| 3817 | NULL); |
| 3818 | CGM.getModule().addTypeName("struct._objc_module", ModuleTy); |
Daniel Dunbar | 14c80b7 | 2008-08-23 09:25:55 +0000 | [diff] [blame] | 3819 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3820 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 3821 | // FIXME: This is the size of the setjmp buffer and should be target |
| 3822 | // specific. 18 is what's used on 32-bit X86. |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3823 | uint64_t SetJmpBufferSize = 18; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3824 | |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3825 | // Exceptions |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3826 | const llvm::Type *StackPtrTy = llvm::ArrayType::get( |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3827 | llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)), 4); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3828 | |
| 3829 | ExceptionDataTy = |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 3830 | llvm::StructType::get(VMContext, llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext), |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3831 | SetJmpBufferSize), |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3832 | StackPtrTy, NULL); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3833 | CGM.getModule().addTypeName("struct._objc_exception_data", |
Anders Carlsson | 124526b | 2008-09-09 10:10:21 +0000 | [diff] [blame] | 3834 | ExceptionDataTy); |
| 3835 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 3836 | } |
| 3837 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3838 | ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm) |
| 3839 | : ObjCCommonTypesHelper(cgm) |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 3840 | { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3841 | // struct _method_list_t { |
| 3842 | // uint32_t entsize; // sizeof(struct _objc_method) |
| 3843 | // uint32_t method_count; |
| 3844 | // struct _objc_method method_list[method_count]; |
| 3845 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3846 | MethodListnfABITy = llvm::StructType::get(VMContext, IntTy, |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3847 | IntTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3848 | llvm::ArrayType::get(MethodTy, 0), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3849 | NULL); |
| 3850 | CGM.getModule().addTypeName("struct.__method_list_t", |
| 3851 | MethodListnfABITy); |
| 3852 | // struct method_list_t * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3853 | MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3854 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3855 | // struct _protocol_t { |
| 3856 | // id isa; // NULL |
| 3857 | // const char * const protocol_name; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3858 | // const struct _protocol_list_t * protocol_list; // super protocols |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3859 | // const struct method_list_t * const instance_methods; |
| 3860 | // const struct method_list_t * const class_methods; |
| 3861 | // const struct method_list_t *optionalInstanceMethods; |
| 3862 | // const struct method_list_t *optionalClassMethods; |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3863 | // const struct _prop_list_t * properties; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3864 | // const uint32_t size; // sizeof(struct _protocol_t) |
| 3865 | // const uint32_t flags; // = 0 |
| 3866 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3867 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3868 | // Holder for struct _protocol_list_t * |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 3869 | llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3870 | |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3871 | ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy, |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3872 | Int8PtrTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3873 | llvm::PointerType::getUnqual( |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3874 | ProtocolListTyHolder), |
| 3875 | MethodListnfABIPtrTy, |
| 3876 | MethodListnfABIPtrTy, |
| 3877 | MethodListnfABIPtrTy, |
| 3878 | MethodListnfABIPtrTy, |
| 3879 | PropertyListPtrTy, |
| 3880 | IntTy, |
| 3881 | IntTy, |
| 3882 | NULL); |
| 3883 | CGM.getModule().addTypeName("struct._protocol_t", |
| 3884 | ProtocolnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3885 | |
| 3886 | // struct _protocol_t* |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3887 | ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3888 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 3889 | // struct _protocol_list_t { |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3890 | // long protocol_count; // Note, this is 32/64 bit |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3891 | // struct _protocol_t *[protocol_count]; |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3892 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3893 | ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3894 | llvm::ArrayType::get( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3895 | ProtocolnfABIPtrTy, 0), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3896 | NULL); |
| 3897 | CGM.getModule().addTypeName("struct._objc_protocol_list", |
| 3898 | ProtocolListnfABITy); |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 3899 | cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3900 | ProtocolListnfABITy); |
| 3901 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3902 | // struct _objc_protocol_list* |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3903 | ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3904 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3905 | // struct _ivar_t { |
| 3906 | // unsigned long int *offset; // pointer to ivar offset location |
| 3907 | // char *name; |
| 3908 | // char *type; |
| 3909 | // uint32_t alignment; |
| 3910 | // uint32_t size; |
| 3911 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3912 | IvarnfABITy = llvm::StructType::get(VMContext, |
| 3913 | llvm::PointerType::getUnqual(LongTy), |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3914 | Int8PtrTy, |
| 3915 | Int8PtrTy, |
| 3916 | IntTy, |
| 3917 | IntTy, |
| 3918 | NULL); |
| 3919 | CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3920 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3921 | // struct _ivar_list_t { |
| 3922 | // uint32 entsize; // sizeof(struct _ivar_t) |
| 3923 | // uint32 count; |
| 3924 | // struct _iver_t list[count]; |
| 3925 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3926 | IvarListnfABITy = llvm::StructType::get(VMContext, IntTy, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3927 | IntTy, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3928 | llvm::ArrayType::get( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3929 | IvarnfABITy, 0), |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 3930 | NULL); |
| 3931 | CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3932 | |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3933 | IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3934 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3935 | // struct _class_ro_t { |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 3936 | // uint32_t const flags; |
| 3937 | // uint32_t const instanceStart; |
| 3938 | // uint32_t const instanceSize; |
| 3939 | // uint32_t const reserved; // only when building for 64bit targets |
| 3940 | // const uint8_t * const ivarLayout; |
| 3941 | // const char *const name; |
| 3942 | // const struct _method_list_t * const baseMethods; |
| 3943 | // const struct _objc_protocol_list *const baseProtocols; |
| 3944 | // const struct _ivar_list_t *const ivars; |
| 3945 | // const uint8_t * const weakIvarLayout; |
| 3946 | // const struct _prop_list_t * const properties; |
| 3947 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3948 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3949 | // FIXME. Add 'reserved' field in 64bit abi mode! |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3950 | ClassRonfABITy = llvm::StructType::get(VMContext, IntTy, |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3951 | IntTy, |
| 3952 | IntTy, |
| 3953 | Int8PtrTy, |
| 3954 | Int8PtrTy, |
| 3955 | MethodListnfABIPtrTy, |
| 3956 | ProtocolListnfABIPtrTy, |
| 3957 | IvarListnfABIPtrTy, |
| 3958 | Int8PtrTy, |
| 3959 | PropertyListPtrTy, |
| 3960 | NULL); |
| 3961 | CGM.getModule().addTypeName("struct._class_ro_t", |
| 3962 | ClassRonfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3963 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3964 | // ImpnfABITy - LLVM for id (*)(id, SEL, ...) |
| 3965 | std::vector<const llvm::Type*> Params; |
| 3966 | Params.push_back(ObjectPtrTy); |
| 3967 | Params.push_back(SelectorPtrTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3968 | ImpnfABITy = llvm::PointerType::getUnqual( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3969 | llvm::FunctionType::get(ObjectPtrTy, Params, false)); |
| 3970 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3971 | // struct _class_t { |
| 3972 | // struct _class_t *isa; |
| 3973 | // struct _class_t * const superclass; |
| 3974 | // void *cache; |
| 3975 | // IMP *vtable; |
| 3976 | // struct class_ro_t *ro; |
| 3977 | // } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3978 | |
Owen Anderson | 8c8f69e | 2009-08-13 23:27:53 +0000 | [diff] [blame] | 3979 | llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 3980 | ClassnfABITy = |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 3981 | llvm::StructType::get(VMContext, |
| 3982 | llvm::PointerType::getUnqual(ClassTyHolder), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3983 | llvm::PointerType::getUnqual(ClassTyHolder), |
| 3984 | CachePtrTy, |
| 3985 | llvm::PointerType::getUnqual(ImpnfABITy), |
| 3986 | llvm::PointerType::getUnqual(ClassRonfABITy), |
| 3987 | NULL); |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3988 | CGM.getModule().addTypeName("struct._class_t", ClassnfABITy); |
| 3989 | |
| 3990 | cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3991 | ClassnfABITy); |
| 3992 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 3993 | // LLVM for struct _class_t * |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 3994 | ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 3995 | |
Fariborz Jahanian | d55b6fc | 2009-01-23 01:46:23 +0000 | [diff] [blame] | 3996 | // struct _category_t { |
| 3997 | // const char * const name; |
| 3998 | // struct _class_t *const cls; |
| 3999 | // const struct _method_list_t * const instance_methods; |
| 4000 | // const struct _method_list_t * const class_methods; |
| 4001 | // const struct _protocol_list_t * const protocols; |
| 4002 | // const struct _prop_list_t * const properties; |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 4003 | // } |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4004 | CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy, |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4005 | ClassnfABIPtrTy, |
Fariborz Jahanian | 45c2ba0 | 2009-01-23 17:41:22 +0000 | [diff] [blame] | 4006 | MethodListnfABIPtrTy, |
| 4007 | MethodListnfABIPtrTy, |
| 4008 | ProtocolListnfABIPtrTy, |
| 4009 | PropertyListPtrTy, |
| 4010 | NULL); |
| 4011 | CGM.getModule().addTypeName("struct._category_t", CategorynfABITy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4012 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4013 | // New types for nonfragile abi messaging. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4014 | CodeGen::CodeGenTypes &Types = CGM.getTypes(); |
| 4015 | ASTContext &Ctx = CGM.getContext(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4016 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4017 | // MessageRefTy - LLVM for: |
| 4018 | // struct _message_ref_t { |
| 4019 | // IMP messenger; |
| 4020 | // SEL name; |
| 4021 | // }; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4022 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4023 | // First the clang type for struct _message_ref_t |
| 4024 | RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0, |
| 4025 | SourceLocation(), |
| 4026 | &Ctx.Idents.get("_message_ref_t")); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4027 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4028 | Ctx.VoidPtrTy, 0, 0, false)); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4029 | RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0, |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 4030 | Ctx.getObjCSelType(), 0, 0, false)); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4031 | RD->completeDefinition(Ctx); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4032 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 4033 | MessageRefCTy = Ctx.getTagDeclType(RD); |
| 4034 | MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy); |
| 4035 | MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4036 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4037 | // MessageRefPtrTy - LLVM for struct _message_ref_t* |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4038 | MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4039 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4040 | // SuperMessageRefTy - LLVM for: |
| 4041 | // struct _super_message_ref_t { |
| 4042 | // SUPER_IMP messenger; |
| 4043 | // SEL name; |
| 4044 | // }; |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4045 | SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy, |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4046 | SelectorPtrTy, |
| 4047 | NULL); |
| 4048 | CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4049 | |
Fariborz Jahanian | 2e4672b | 2009-02-03 23:49:23 +0000 | [diff] [blame] | 4050 | // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t* |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4051 | SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy); |
| 4052 | |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4053 | |
| 4054 | // struct objc_typeinfo { |
| 4055 | // const void** vtable; // objc_ehtype_vtable + 2 |
| 4056 | // const char* name; // c++ typeinfo string |
| 4057 | // Class cls; |
| 4058 | // }; |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4059 | EHTypeTy = llvm::StructType::get(VMContext, |
| 4060 | llvm::PointerType::getUnqual(Int8PtrTy), |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 4061 | Int8PtrTy, |
| 4062 | ClassnfABIPtrTy, |
| 4063 | NULL); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 4064 | CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4065 | EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 4066 | } |
| 4067 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4068 | llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4069 | FinishNonFragileABIModule(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4070 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4071 | return NULL; |
| 4072 | } |
| 4073 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4074 | void CGObjCNonFragileABIMac::AddModuleClassList(const |
| 4075 | std::vector<llvm::GlobalValue*> |
| 4076 | &Container, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4077 | const char *SymbolName, |
| 4078 | const char *SectionName) { |
| 4079 | unsigned NumClasses = Container.size(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4080 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4081 | if (!NumClasses) |
| 4082 | return; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4083 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4084 | std::vector<llvm::Constant*> Symbols(NumClasses); |
| 4085 | for (unsigned i=0; i<NumClasses; i++) |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4086 | Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i], |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4087 | ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4088 | llvm::Constant* Init = |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4089 | llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4090 | NumClasses), |
| 4091 | Symbols); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4092 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4093 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4094 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4095 | llvm::GlobalValue::InternalLinkage, |
| 4096 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4097 | SymbolName); |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4098 | GV->setAlignment(8); |
| 4099 | GV->setSection(SectionName); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4100 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4101 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4102 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4103 | void CGObjCNonFragileABIMac::FinishNonFragileABIModule() { |
| 4104 | // nonfragile abi has no module definition. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4105 | |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4106 | // Build list of all implemented class addresses in array |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4107 | // L_OBJC_LABEL_CLASS_$. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4108 | AddModuleClassList(DefinedClasses, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4109 | "\01L_OBJC_LABEL_CLASS_$", |
| 4110 | "__DATA, __objc_classlist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4111 | AddModuleClassList(DefinedNonLazyClasses, |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4112 | "\01L_OBJC_LABEL_NONLAZY_CLASS_$", |
| 4113 | "__DATA, __objc_nlclslist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4114 | |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4115 | // Build list of all implemented category addresses in array |
| 4116 | // L_OBJC_LABEL_CATEGORY_$. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4117 | AddModuleClassList(DefinedCategories, |
Daniel Dunbar | 463b876 | 2009-05-15 21:48:48 +0000 | [diff] [blame] | 4118 | "\01L_OBJC_LABEL_CATEGORY_$", |
| 4119 | "__DATA, __objc_catlist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4120 | AddModuleClassList(DefinedNonLazyCategories, |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4121 | "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$", |
| 4122 | "__DATA, __objc_nlcatlist, regular, no_dead_strip"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4123 | |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4124 | // static int L_OBJC_IMAGE_INFO[2] = { 0, flags }; |
| 4125 | // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0 |
| 4126 | std::vector<llvm::Constant*> Values(2); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4127 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0); |
Fariborz Jahanian | 067986e | 2009-02-24 21:08:09 +0000 | [diff] [blame] | 4128 | unsigned int flags = 0; |
Fariborz Jahanian | 66a5c2c | 2009-02-24 23:34:44 +0000 | [diff] [blame] | 4129 | // FIXME: Fix and continue? |
| 4130 | if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC) |
| 4131 | flags |= eImageInfo_GarbageCollected; |
| 4132 | if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly) |
| 4133 | flags |= eImageInfo_GCOnly; |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4134 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 4135 | llvm::Constant* Init = llvm::ConstantArray::get( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4136 | llvm::ArrayType::get(ObjCTypes.IntTy, 2), |
| 4137 | Values); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4138 | llvm::GlobalVariable *IMGV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4139 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4140 | llvm::GlobalValue::InternalLinkage, |
| 4141 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4142 | "\01L_OBJC_IMAGE_INFO"); |
Fariborz Jahanian | 0f6610e | 2009-01-30 22:07:48 +0000 | [diff] [blame] | 4143 | IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip"); |
Daniel Dunbar | 325f758 | 2009-04-23 08:03:21 +0000 | [diff] [blame] | 4144 | IMGV->setConstant(true); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4145 | CGM.AddUsedGlobal(IMGV); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4146 | } |
| 4147 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4148 | /// LegacyDispatchedSelector - Returns true if SEL is not in the list of |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4149 | /// NonLegacyDispatchMethods; false otherwise. What this means is that |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4150 | /// except for the 19 selectors in the list, we generate 32bit-style |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4151 | /// message dispatch call for all the rest. |
| 4152 | /// |
| 4153 | bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) { |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4154 | if (NonLegacyDispatchMethods.empty()) { |
| 4155 | NonLegacyDispatchMethods.insert(GetNullarySelector("alloc")); |
| 4156 | NonLegacyDispatchMethods.insert(GetNullarySelector("class")); |
| 4157 | NonLegacyDispatchMethods.insert(GetNullarySelector("self")); |
| 4158 | NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped")); |
| 4159 | NonLegacyDispatchMethods.insert(GetNullarySelector("length")); |
| 4160 | NonLegacyDispatchMethods.insert(GetNullarySelector("count")); |
| 4161 | NonLegacyDispatchMethods.insert(GetNullarySelector("retain")); |
| 4162 | NonLegacyDispatchMethods.insert(GetNullarySelector("release")); |
| 4163 | NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease")); |
| 4164 | NonLegacyDispatchMethods.insert(GetNullarySelector("hash")); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4165 | |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4166 | NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone")); |
| 4167 | NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass")); |
| 4168 | NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector")); |
| 4169 | NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey")); |
| 4170 | NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex")); |
| 4171 | NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString")); |
| 4172 | NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual")); |
| 4173 | NonLegacyDispatchMethods.insert(GetUnarySelector("addObject")); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4174 | // "countByEnumeratingWithState:objects:count" |
Fariborz Jahanian | be53be4 | 2009-05-13 16:19:02 +0000 | [diff] [blame] | 4175 | IdentifierInfo *KeyIdents[] = { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4176 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), |
| 4177 | &CGM.getContext().Idents.get("objects"), |
| 4178 | &CGM.getContext().Idents.get("count") |
Fariborz Jahanian | be53be4 | 2009-05-13 16:19:02 +0000 | [diff] [blame] | 4179 | }; |
| 4180 | NonLegacyDispatchMethods.insert( |
| 4181 | CGM.getContext().Selectors.getSelector(3, KeyIdents)); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4182 | } |
Fariborz Jahanian | 4523eb0 | 2009-05-12 20:06:41 +0000 | [diff] [blame] | 4183 | return (NonLegacyDispatchMethods.count(Sel) == 0); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 4184 | } |
| 4185 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4186 | // Metadata flags |
| 4187 | enum MetaDataDlags { |
| 4188 | CLS = 0x0, |
| 4189 | CLS_META = 0x1, |
| 4190 | CLS_ROOT = 0x2, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4191 | OBJC2_CLS_HIDDEN = 0x10, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4192 | CLS_EXCEPTION = 0x20 |
| 4193 | }; |
| 4194 | /// BuildClassRoTInitializer - generate meta-data for: |
| 4195 | /// struct _class_ro_t { |
| 4196 | /// uint32_t const flags; |
| 4197 | /// uint32_t const instanceStart; |
| 4198 | /// uint32_t const instanceSize; |
| 4199 | /// uint32_t const reserved; // only when building for 64bit targets |
| 4200 | /// const uint8_t * const ivarLayout; |
| 4201 | /// const char *const name; |
| 4202 | /// const struct _method_list_t * const baseMethods; |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4203 | /// const struct _protocol_list_t *const baseProtocols; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4204 | /// const struct _ivar_list_t *const ivars; |
| 4205 | /// const uint8_t * const weakIvarLayout; |
| 4206 | /// const struct _prop_list_t * const properties; |
| 4207 | /// } |
| 4208 | /// |
| 4209 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4210 | unsigned flags, |
| 4211 | unsigned InstanceStart, |
| 4212 | unsigned InstanceSize, |
| 4213 | const ObjCImplementationDecl *ID) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4214 | std::string ClassName = ID->getNameAsString(); |
| 4215 | std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets! |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4216 | Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags); |
| 4217 | Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart); |
| 4218 | Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4219 | // FIXME. For 64bit targets add 0 here. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4220 | Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4221 | : BuildIvarLayout(ID, true); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4222 | Values[ 4] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4223 | // const struct _method_list_t * const baseMethods; |
| 4224 | std::vector<llvm::Constant*> Methods; |
| 4225 | std::string MethodListName("\01l_OBJC_$_"); |
| 4226 | if (flags & CLS_META) { |
| 4227 | MethodListName += "CLASS_METHODS_" + ID->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4228 | for (ObjCImplementationDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4229 | i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4230 | // Class methods should always be defined. |
| 4231 | Methods.push_back(GetMethodConstant(*i)); |
| 4232 | } |
| 4233 | } else { |
| 4234 | MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4235 | for (ObjCImplementationDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4236 | i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4237 | // Instance methods should always be defined. |
| 4238 | Methods.push_back(GetMethodConstant(*i)); |
| 4239 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4240 | for (ObjCImplementationDecl::propimpl_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4241 | i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) { |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4242 | ObjCPropertyImplDecl *PID = *i; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4243 | |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4244 | if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){ |
| 4245 | ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4246 | |
Fariborz Jahanian | 939abce | 2009-01-28 22:46:49 +0000 | [diff] [blame] | 4247 | if (ObjCMethodDecl *MD = PD->getGetterMethodDecl()) |
| 4248 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4249 | Methods.push_back(C); |
| 4250 | if (ObjCMethodDecl *MD = PD->getSetterMethodDecl()) |
| 4251 | if (llvm::Constant *C = GetMethodConstant(MD)) |
| 4252 | Methods.push_back(C); |
| 4253 | } |
| 4254 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4255 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4256 | Values[ 5] = EmitMethodList(MethodListName, |
| 4257 | "__DATA, __objc_const", Methods); |
| 4258 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4259 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4260 | assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4261 | Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4262 | + OID->getNameAsString(), |
| 4263 | OID->protocol_begin(), |
| 4264 | OID->protocol_end()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4265 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4266 | if (flags & CLS_META) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4267 | Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4268 | else |
| 4269 | Values[ 7] = EmitIvarList(ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4270 | Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes) |
| 4271 | : BuildIvarLayout(ID, false); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4272 | if (flags & CLS_META) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4273 | Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4274 | else |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4275 | Values[ 9] = |
Chris Lattner | 41f55d3 | 2009-07-28 18:25:06 +0000 | [diff] [blame] | 4276 | EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(), |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4277 | ID, ID->getClassInterface(), ObjCTypes); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4278 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4279 | Values); |
| 4280 | llvm::GlobalVariable *CLASS_RO_GV = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4281 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false, |
| 4282 | llvm::GlobalValue::InternalLinkage, |
| 4283 | Init, |
| 4284 | (flags & CLS_META) ? |
| 4285 | std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName : |
| 4286 | std::string("\01l_OBJC_CLASS_RO_$_")+ClassName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4287 | CLASS_RO_GV->setAlignment( |
| 4288 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4289 | CLASS_RO_GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4290 | return CLASS_RO_GV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4291 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4292 | } |
| 4293 | |
| 4294 | /// BuildClassMetaData - This routine defines that to-level meta-data |
| 4295 | /// for the given ClassName for: |
| 4296 | /// struct _class_t { |
| 4297 | /// struct _class_t *isa; |
| 4298 | /// struct _class_t * const superclass; |
| 4299 | /// void *cache; |
| 4300 | /// IMP *vtable; |
| 4301 | /// struct class_ro_t *ro; |
| 4302 | /// } |
| 4303 | /// |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4304 | llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4305 | std::string &ClassName, |
| 4306 | llvm::Constant *IsAGV, |
| 4307 | llvm::Constant *SuperClassGV, |
| 4308 | llvm::Constant *ClassRoGV, |
| 4309 | bool HiddenVisibility) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4310 | std::vector<llvm::Constant*> Values(5); |
| 4311 | Values[0] = IsAGV; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4312 | Values[1] = SuperClassGV; |
| 4313 | if (!Values[1]) |
| 4314 | Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4315 | Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar |
| 4316 | Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar |
| 4317 | Values[4] = ClassRoGV; // &CLASS_RO_GV |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4318 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy, |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4319 | Values); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4320 | llvm::GlobalVariable *GV = GetClassGlobal(ClassName); |
| 4321 | GV->setInitializer(Init); |
Fariborz Jahanian | dd0db2a | 2009-01-31 01:07:39 +0000 | [diff] [blame] | 4322 | GV->setSection("__DATA, __objc_data"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4323 | GV->setAlignment( |
| 4324 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy)); |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4325 | if (HiddenVisibility) |
| 4326 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4327 | return GV; |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4328 | } |
| 4329 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4330 | bool |
Fariborz Jahanian | ecfbdcb | 2009-05-21 01:03:45 +0000 | [diff] [blame] | 4331 | CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4332 | return OD->getClassMethod(GetNullarySelector("load")) != 0; |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4333 | } |
| 4334 | |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4335 | void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID, |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4336 | uint32_t &InstanceStart, |
| 4337 | uint32_t &InstanceSize) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4338 | const ASTRecordLayout &RL = |
Daniel Dunbar | b4c79e0 | 2009-05-04 21:26:30 +0000 | [diff] [blame] | 4339 | CGM.getContext().getASTObjCImplementationLayout(OID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4340 | |
Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 4341 | // InstanceSize is really instance end. |
Anders Carlsson | 243a685 | 2009-07-18 21:26:44 +0000 | [diff] [blame] | 4342 | InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8; |
Daniel Dunbar | 6e8575b | 2009-05-04 23:23:09 +0000 | [diff] [blame] | 4343 | |
| 4344 | // If there are no fields, the start is the same as the end. |
| 4345 | if (!RL.getFieldCount()) |
| 4346 | InstanceStart = InstanceSize; |
| 4347 | else |
| 4348 | InstanceStart = RL.getFieldOffset(0) / 8; |
Daniel Dunbar | b02532a | 2009-04-19 23:41:48 +0000 | [diff] [blame] | 4349 | } |
| 4350 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4351 | void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) { |
| 4352 | std::string ClassName = ID->getNameAsString(); |
| 4353 | if (!ObjCEmptyCacheVar) { |
| 4354 | ObjCEmptyCacheVar = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4355 | CGM.getModule(), |
| 4356 | ObjCTypes.CacheTy, |
| 4357 | false, |
| 4358 | llvm::GlobalValue::ExternalLinkage, |
| 4359 | 0, |
| 4360 | "_objc_empty_cache"); |
| 4361 | |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4362 | ObjCEmptyVtableVar = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4363 | CGM.getModule(), |
| 4364 | ObjCTypes.ImpnfABITy, |
| 4365 | false, |
| 4366 | llvm::GlobalValue::ExternalLinkage, |
| 4367 | 0, |
| 4368 | "_objc_empty_vtable"); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4369 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4370 | assert(ID->getClassInterface() && |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4371 | "CGObjCNonFragileABIMac::GenerateClass - class is 0"); |
Daniel Dunbar | 6c1aac8 | 2009-04-20 20:18:54 +0000 | [diff] [blame] | 4372 | // FIXME: Is this correct (that meta class size is never computed)? |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4373 | uint32_t InstanceStart = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4374 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4375 | uint32_t InstanceSize = InstanceStart; |
| 4376 | uint32_t flags = CLS_META; |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4377 | std::string ObjCMetaClassName(getMetaclassSymbolPrefix()); |
| 4378 | std::string ObjCClassName(getClassSymbolPrefix()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4379 | |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4380 | llvm::GlobalVariable *SuperClassGV, *IsAGV; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4381 | |
| 4382 | bool classIsHidden = |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4383 | CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4384 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4385 | flags |= OBJC2_CLS_HIDDEN; |
| 4386 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4387 | // class is root |
| 4388 | flags |= CLS_ROOT; |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4389 | SuperClassGV = GetClassGlobal(ObjCClassName + ClassName); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4390 | IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4391 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4392 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4393 | const ObjCInterfaceDecl *Root = ID->getClassInterface(); |
| 4394 | while (const ObjCInterfaceDecl *Super = Root->getSuperClass()) |
| 4395 | Root = Super; |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4396 | IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString()); |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4397 | // work on super class metadata symbol. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4398 | std::string SuperClassName = |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4399 | ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 4400 | SuperClassGV = GetClassGlobal(SuperClassName); |
Fariborz Jahanian | 058a1b7 | 2009-01-24 20:21:50 +0000 | [diff] [blame] | 4401 | } |
| 4402 | llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags, |
| 4403 | InstanceStart, |
| 4404 | InstanceSize,ID); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4405 | std::string TClassName = ObjCMetaClassName + ClassName; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4406 | llvm::GlobalVariable *MetaTClass = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4407 | BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV, |
| 4408 | classIsHidden); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4409 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4410 | // Metadata for the class |
| 4411 | flags = CLS; |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4412 | if (classIsHidden) |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4413 | flags |= OBJC2_CLS_HIDDEN; |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4414 | |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 4415 | if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface())) |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4416 | flags |= CLS_EXCEPTION; |
| 4417 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4418 | if (!ID->getClassInterface()->getSuperClass()) { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4419 | flags |= CLS_ROOT; |
| 4420 | SuperClassGV = 0; |
Chris Lattner | b7b58b1 | 2009-04-19 06:02:28 +0000 | [diff] [blame] | 4421 | } else { |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4422 | // Has a root. Current class is not a root. |
Fariborz Jahanian | fab98c4 | 2009-02-26 18:23:47 +0000 | [diff] [blame] | 4423 | std::string RootClassName = |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4424 | ID->getClassInterface()->getSuperClass()->getNameAsString(); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4425 | SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4426 | } |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4427 | GetClassSizeInfo(ID, InstanceStart, InstanceSize); |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4428 | CLASS_RO_GV = BuildClassRoTInitializer(flags, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4429 | InstanceStart, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4430 | InstanceSize, |
Fariborz Jahanian | f6a077e | 2009-01-24 23:43:01 +0000 | [diff] [blame] | 4431 | ID); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4432 | |
Fariborz Jahanian | 84394a5 | 2009-01-24 21:21:53 +0000 | [diff] [blame] | 4433 | TClassName = ObjCClassName + ClassName; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4434 | llvm::GlobalVariable *ClassMD = |
Fariborz Jahanian | cf55516 | 2009-01-31 00:59:10 +0000 | [diff] [blame] | 4435 | BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV, |
| 4436 | classIsHidden); |
Fariborz Jahanian | f87a0cc | 2009-01-30 20:55:31 +0000 | [diff] [blame] | 4437 | DefinedClasses.push_back(ClassMD); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4438 | |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4439 | // Determine if this class is also "non-lazy". |
| 4440 | if (ImplementationIsNonLazy(ID)) |
| 4441 | DefinedNonLazyClasses.push_back(ClassMD); |
| 4442 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 4443 | // Force the definition of the EHType if necessary. |
| 4444 | if (flags & CLS_EXCEPTION) |
| 4445 | GetInterfaceEHType(ID->getClassInterface(), true); |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 4446 | } |
| 4447 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4448 | /// GenerateProtocolRef - This routine is called to generate code for |
| 4449 | /// a protocol reference expression; as in: |
| 4450 | /// @code |
| 4451 | /// @protocol(Proto1); |
| 4452 | /// @endcode |
| 4453 | /// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1 |
| 4454 | /// which will hold address of the protocol meta-data. |
| 4455 | /// |
| 4456 | llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4457 | const ObjCProtocolDecl *PD) { |
| 4458 | |
Fariborz Jahanian | 960cd06 | 2009-04-10 18:47:34 +0000 | [diff] [blame] | 4459 | // This routine is called for @protocol only. So, we must build definition |
| 4460 | // of protocol's meta-data (not a reference to it!) |
| 4461 | // |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4462 | llvm::Constant *Init = |
| 4463 | llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD), |
| 4464 | ObjCTypes.ExternalProtocolPtrTy); |
| 4465 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4466 | std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_"); |
| 4467 | ProtocolName += PD->getNameAsCString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4468 | |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4469 | llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName); |
| 4470 | if (PTGV) |
| 4471 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4472 | PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4473 | CGM.getModule(), |
| 4474 | Init->getType(), false, |
| 4475 | llvm::GlobalValue::WeakAnyLinkage, |
| 4476 | Init, |
| 4477 | ProtocolName); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4478 | PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip"); |
| 4479 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4480 | CGM.AddUsedGlobal(PTGV); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4481 | return Builder.CreateLoad(PTGV, false, "tmp"); |
| 4482 | } |
| 4483 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4484 | /// GenerateCategory - Build metadata for a category implementation. |
| 4485 | /// struct _category_t { |
| 4486 | /// const char * const name; |
| 4487 | /// struct _class_t *const cls; |
| 4488 | /// const struct _method_list_t * const instance_methods; |
| 4489 | /// const struct _method_list_t * const class_methods; |
| 4490 | /// const struct _protocol_list_t * const protocols; |
| 4491 | /// const struct _prop_list_t * const properties; |
| 4492 | /// } |
| 4493 | /// |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4494 | void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) { |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4495 | const ObjCInterfaceDecl *Interface = OCD->getClassInterface(); |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4496 | const char *Prefix = "\01l_OBJC_$_CATEGORY_"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4497 | std::string ExtCatName(Prefix + Interface->getNameAsString()+ |
| 4498 | "_$_" + OCD->getNameAsString()); |
| 4499 | std::string ExtClassName(getClassSymbolPrefix() + |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 4500 | Interface->getNameAsString()); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4501 | |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4502 | std::vector<llvm::Constant*> Values(6); |
| 4503 | Values[0] = GetClassName(OCD->getIdentifier()); |
| 4504 | // meta-class entry symbol |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 4505 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4506 | Values[1] = ClassGV; |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4507 | std::vector<llvm::Constant*> Methods; |
| 4508 | std::string MethodListName(Prefix); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4509 | MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() + |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4510 | "_$_" + OCD->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4511 | |
| 4512 | for (ObjCCategoryImplDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4513 | i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4514 | // Instance methods should always be defined. |
| 4515 | Methods.push_back(GetMethodConstant(*i)); |
| 4516 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4517 | |
| 4518 | Values[2] = EmitMethodList(MethodListName, |
| 4519 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4520 | Methods); |
| 4521 | |
| 4522 | MethodListName = Prefix; |
| 4523 | MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" + |
| 4524 | OCD->getNameAsString(); |
| 4525 | Methods.clear(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4526 | for (ObjCCategoryImplDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4527 | i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4528 | // Class methods should always be defined. |
| 4529 | Methods.push_back(GetMethodConstant(*i)); |
| 4530 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4531 | |
| 4532 | Values[3] = EmitMethodList(MethodListName, |
| 4533 | "__DATA, __objc_const", |
Fariborz Jahanian | f6317dd | 2009-01-26 22:58:07 +0000 | [diff] [blame] | 4534 | Methods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4535 | const ObjCCategoryDecl *Category = |
Fariborz Jahanian | 5de14dc | 2009-01-28 22:18:42 +0000 | [diff] [blame] | 4536 | Interface->FindCategoryDeclaration(OCD->getIdentifier()); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4537 | if (Category) { |
| 4538 | std::string ExtName(Interface->getNameAsString() + "_$_" + |
| 4539 | OCD->getNameAsString()); |
| 4540 | Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_" |
| 4541 | + Interface->getNameAsString() + "_$_" |
| 4542 | + Category->getNameAsString(), |
| 4543 | Category->protocol_begin(), |
| 4544 | Category->protocol_end()); |
| 4545 | Values[5] = |
| 4546 | EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName, |
| 4547 | OCD, Category, ObjCTypes); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 4548 | } else { |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4549 | Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
| 4550 | Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy); |
Fariborz Jahanian | 943ed6f | 2009-02-13 17:52:22 +0000 | [diff] [blame] | 4551 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4552 | |
| 4553 | llvm::Constant *Init = |
| 4554 | llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4555 | Values); |
| 4556 | llvm::GlobalVariable *GCATV |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4557 | = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy, |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4558 | false, |
| 4559 | llvm::GlobalValue::InternalLinkage, |
| 4560 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4561 | ExtCatName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4562 | GCATV->setAlignment( |
| 4563 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy)); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4564 | GCATV->setSection("__DATA, __objc_const"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4565 | CGM.AddUsedGlobal(GCATV); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4566 | DefinedCategories.push_back(GCATV); |
Daniel Dunbar | 74d4b12 | 2009-05-15 22:33:15 +0000 | [diff] [blame] | 4567 | |
| 4568 | // Determine if this category is also "non-lazy". |
| 4569 | if (ImplementationIsNonLazy(OCD)) |
| 4570 | DefinedNonLazyCategories.push_back(GCATV); |
Fariborz Jahanian | eb062d9 | 2009-01-26 18:32:24 +0000 | [diff] [blame] | 4571 | } |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4572 | |
| 4573 | /// GetMethodConstant - Return a struct objc_method constant for the |
| 4574 | /// given method if it has been defined. The result is null if the |
| 4575 | /// method has not been defined. The return value has type MethodPtrTy. |
| 4576 | llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4577 | const ObjCMethodDecl *MD) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4578 | // FIXME: Use DenseMap::lookup |
| 4579 | llvm::Function *Fn = MethodDefinitions[MD]; |
| 4580 | if (!Fn) |
| 4581 | return 0; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4582 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4583 | std::vector<llvm::Constant*> Method(3); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4584 | Method[0] = |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4585 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4586 | ObjCTypes.SelectorPtrTy); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4587 | Method[1] = GetMethodVarType(MD); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4588 | Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4589 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4590 | } |
| 4591 | |
| 4592 | /// EmitMethodList - Build meta-data for method declarations |
| 4593 | /// struct _method_list_t { |
| 4594 | /// uint32_t entsize; // sizeof(struct _objc_method) |
| 4595 | /// uint32_t method_count; |
| 4596 | /// struct _objc_method method_list[method_count]; |
| 4597 | /// } |
| 4598 | /// |
| 4599 | llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4600 | const std::string &Name, |
| 4601 | const char *Section, |
| 4602 | const ConstantVector &Methods) { |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4603 | // Return null for empty list. |
| 4604 | if (Methods.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4605 | return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4606 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4607 | std::vector<llvm::Constant*> Values(3); |
| 4608 | // sizeof(struct _objc_method) |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4609 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4610 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4611 | // method_count |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4612 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4613 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4614 | Methods.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 4615 | Values[2] = llvm::ConstantArray::get(AT, Methods); |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4616 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4617 | |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4618 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4619 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4620 | llvm::GlobalValue::InternalLinkage, |
| 4621 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4622 | Name); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4623 | GV->setAlignment( |
| 4624 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4625 | GV->setSection(Section); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4626 | CGM.AddUsedGlobal(GV); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4627 | return llvm::ConstantExpr::getBitCast(GV, |
Fariborz Jahanian | 493dab7 | 2009-01-26 21:38:32 +0000 | [diff] [blame] | 4628 | ObjCTypes.MethodListnfABIPtrTy); |
| 4629 | } |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4630 | |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4631 | /// ObjCIvarOffsetVariable - Returns the ivar offset variable for |
| 4632 | /// the given ivar. |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4633 | llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4634 | const ObjCInterfaceDecl *ID, |
| 4635 | const ObjCIvarDecl *Ivar) { |
Daniel Dunbar | a81419d | 2009-05-05 00:36:57 +0000 | [diff] [blame] | 4636 | // FIXME: We shouldn't need to do this lookup. |
| 4637 | unsigned Index; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4638 | const ObjCInterfaceDecl *Container = |
Daniel Dunbar | a81419d | 2009-05-05 00:36:57 +0000 | [diff] [blame] | 4639 | FindIvarInterface(CGM.getContext(), ID, Ivar, Index); |
| 4640 | assert(Container && "Unable to find ivar container!"); |
| 4641 | std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() + |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 4642 | '.' + Ivar->getNameAsString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4643 | llvm::GlobalVariable *IvarOffsetGV = |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4644 | CGM.getModule().getGlobalVariable(Name); |
| 4645 | if (!IvarOffsetGV) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4646 | IvarOffsetGV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4647 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy, |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4648 | false, |
| 4649 | llvm::GlobalValue::ExternalLinkage, |
| 4650 | 0, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4651 | Name); |
Fariborz Jahanian | ed157d3 | 2009-02-10 20:21:06 +0000 | [diff] [blame] | 4652 | return IvarOffsetGV; |
| 4653 | } |
| 4654 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4655 | llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4656 | const ObjCInterfaceDecl *ID, |
| 4657 | const ObjCIvarDecl *Ivar, |
| 4658 | unsigned long int Offset) { |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4659 | llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4660 | IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy, |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4661 | Offset)); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4662 | IvarOffsetGV->setAlignment( |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4663 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy)); |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4664 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 4665 | // FIXME: This matches gcc, but shouldn't the visibility be set on the use as |
| 4666 | // well (i.e., in ObjCIvarOffsetVariable). |
Daniel Dunbar | 737c502 | 2009-04-19 00:44:02 +0000 | [diff] [blame] | 4667 | if (Ivar->getAccessControl() == ObjCIvarDecl::Private || |
| 4668 | Ivar->getAccessControl() == ObjCIvarDecl::Package || |
| 4669 | CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden) |
Fariborz Jahanian | 2fa5a27 | 2009-01-28 01:36:42 +0000 | [diff] [blame] | 4670 | IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 4671 | else |
Fariborz Jahanian | 77c9fd2 | 2009-04-06 18:30:00 +0000 | [diff] [blame] | 4672 | IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4673 | IvarOffsetGV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4674 | return IvarOffsetGV; |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4675 | } |
| 4676 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4677 | /// EmitIvarList - Emit the ivar list for the given |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 4678 | /// implementation. The return value has type |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4679 | /// IvarListnfABIPtrTy. |
| 4680 | /// struct _ivar_t { |
| 4681 | /// unsigned long int *offset; // pointer to ivar offset location |
| 4682 | /// char *name; |
| 4683 | /// char *type; |
| 4684 | /// uint32_t alignment; |
| 4685 | /// uint32_t size; |
| 4686 | /// } |
| 4687 | /// struct _ivar_list_t { |
| 4688 | /// uint32 entsize; // sizeof(struct _ivar_t) |
| 4689 | /// uint32 count; |
| 4690 | /// struct _iver_t list[count]; |
| 4691 | /// } |
| 4692 | /// |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4693 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4694 | llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4695 | const ObjCImplementationDecl *ID) { |
| 4696 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4697 | std::vector<llvm::Constant*> Ivars, Ivar(5); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4698 | |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4699 | const ObjCInterfaceDecl *OID = ID->getClassInterface(); |
| 4700 | assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4701 | |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4702 | // FIXME. Consolidate this with similar code in GenerateClass. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4703 | |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4704 | // Collect declared and synthesized ivars in a small vector. |
Fariborz Jahanian | 1819188 | 2009-03-31 18:11:23 +0000 | [diff] [blame] | 4705 | llvm::SmallVector<ObjCIvarDecl*, 16> OIvars; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 4706 | CGM.getContext().ShallowCollectObjCIvars(OID, OIvars); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4707 | |
Daniel Dunbar | 3e5f0d8 | 2009-04-20 06:54:31 +0000 | [diff] [blame] | 4708 | for (unsigned i = 0, e = OIvars.size(); i != e; ++i) { |
| 4709 | ObjCIvarDecl *IVD = OIvars[i]; |
Fariborz Jahanian | 8e6ac1d | 2009-06-04 01:19:09 +0000 | [diff] [blame] | 4710 | // Ignore unnamed bit-fields. |
| 4711 | if (!IVD->getDeclName()) |
| 4712 | continue; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4713 | Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD, |
Daniel Dunbar | 9f89f2b | 2009-05-03 12:57:56 +0000 | [diff] [blame] | 4714 | ComputeIvarBaseOffset(CGM, ID, IVD)); |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4715 | Ivar[1] = GetMethodVarName(IVD->getIdentifier()); |
| 4716 | Ivar[2] = GetMethodVarType(IVD); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4717 | const llvm::Type *FieldTy = |
Daniel Dunbar | 3fea0c0 | 2009-04-22 08:22:17 +0000 | [diff] [blame] | 4718 | CGM.getTypes().ConvertTypeForMem(IVD->getType()); |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4719 | unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4720 | unsigned Align = CGM.getContext().getPreferredTypeAlign( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4721 | IVD->getType().getTypePtr()) >> 3; |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4722 | Align = llvm::Log2_32(Align); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4723 | Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align); |
Daniel Dunbar | 91636d6 | 2009-04-20 00:33:43 +0000 | [diff] [blame] | 4724 | // NOTE. Size of a bitfield does not match gcc's, because of the |
| 4725 | // way bitfields are treated special in each. But I am told that |
| 4726 | // 'size' for bitfield ivars is ignored by the runtime so it does |
| 4727 | // not matter. If it matters, there is enough info to get the |
| 4728 | // bitfield right! |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4729 | Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4730 | Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar)); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4731 | } |
| 4732 | // Return null for empty list. |
| 4733 | if (Ivars.empty()) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4734 | return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4735 | std::vector<llvm::Constant*> Values(3); |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4736 | unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4737 | Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
| 4738 | Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size()); |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4739 | llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4740 | Ivars.size()); |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 4741 | Values[2] = llvm::ConstantArray::get(AT, Ivars); |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4742 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values); |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4743 | const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_"; |
| 4744 | llvm::GlobalVariable *GV = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4745 | new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | 98abf4b | 2009-01-27 19:38:51 +0000 | [diff] [blame] | 4746 | llvm::GlobalValue::InternalLinkage, |
| 4747 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4748 | Prefix + OID->getNameAsString()); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4749 | GV->setAlignment( |
| 4750 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Fariborz Jahanian | 1bf0afb | 2009-01-28 01:05:23 +0000 | [diff] [blame] | 4751 | GV->setSection("__DATA, __objc_const"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4752 | |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4753 | CGM.AddUsedGlobal(GV); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 4754 | return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4755 | } |
| 4756 | |
| 4757 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4758 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4759 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4760 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4761 | if (!Entry) { |
| 4762 | // We use the initializer as a marker of whether this is a forward |
| 4763 | // reference or not. At module finalization we add the empty |
| 4764 | // contents for protocols which were referenced but never defined. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4765 | Entry = |
| 4766 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false, |
| 4767 | llvm::GlobalValue::ExternalLinkage, |
| 4768 | 0, |
| 4769 | "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString()); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4770 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4771 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4772 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4773 | return Entry; |
| 4774 | } |
| 4775 | |
| 4776 | /// GetOrEmitProtocol - Generate the protocol meta-data: |
| 4777 | /// @code |
| 4778 | /// struct _protocol_t { |
| 4779 | /// id isa; // NULL |
| 4780 | /// const char * const protocol_name; |
| 4781 | /// const struct _protocol_list_t * protocol_list; // super protocols |
| 4782 | /// const struct method_list_t * const instance_methods; |
| 4783 | /// const struct method_list_t * const class_methods; |
| 4784 | /// const struct method_list_t *optionalInstanceMethods; |
| 4785 | /// const struct method_list_t *optionalClassMethods; |
| 4786 | /// const struct _prop_list_t * properties; |
| 4787 | /// const uint32_t size; // sizeof(struct _protocol_t) |
| 4788 | /// const uint32_t flags; // = 0 |
| 4789 | /// } |
| 4790 | /// @endcode |
| 4791 | /// |
| 4792 | |
| 4793 | llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4794 | const ObjCProtocolDecl *PD) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4795 | llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4796 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4797 | // Early exit if a defining object has already been generated. |
| 4798 | if (Entry && Entry->hasInitializer()) |
| 4799 | return Entry; |
| 4800 | |
| 4801 | const char *ProtocolName = PD->getNameAsCString(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4802 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4803 | // Construct method lists. |
| 4804 | std::vector<llvm::Constant*> InstanceMethods, ClassMethods; |
| 4805 | std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4806 | for (ObjCProtocolDecl::instmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4807 | i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4808 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4809 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4810 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4811 | OptInstanceMethods.push_back(C); |
| 4812 | } else { |
| 4813 | InstanceMethods.push_back(C); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4814 | } |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4815 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4816 | |
| 4817 | for (ObjCProtocolDecl::classmeth_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4818 | i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4819 | ObjCMethodDecl *MD = *i; |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4820 | llvm::Constant *C = GetMethodDescriptionConstant(MD); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4821 | if (MD->getImplementationControl() == ObjCMethodDecl::Optional) { |
| 4822 | OptClassMethods.push_back(C); |
| 4823 | } else { |
| 4824 | ClassMethods.push_back(C); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4825 | } |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4826 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4827 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4828 | std::vector<llvm::Constant*> Values(10); |
| 4829 | // isa is NULL |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4830 | Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4831 | Values[1] = GetClassName(PD->getIdentifier()); |
| 4832 | Values[2] = EmitProtocolList( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4833 | "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(), |
| 4834 | PD->protocol_begin(), |
| 4835 | PD->protocol_end()); |
| 4836 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4837 | Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4838 | + PD->getNameAsString(), |
| 4839 | "__DATA, __objc_const", |
| 4840 | InstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4841 | Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4842 | + PD->getNameAsString(), |
| 4843 | "__DATA, __objc_const", |
| 4844 | ClassMethods); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4845 | Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4846 | + PD->getNameAsString(), |
| 4847 | "__DATA, __objc_const", |
| 4848 | OptInstanceMethods); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4849 | Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_" |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4850 | + PD->getNameAsString(), |
| 4851 | "__DATA, __objc_const", |
| 4852 | OptClassMethods); |
| 4853 | Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(), |
| 4854 | 0, PD, ObjCTypes); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4855 | uint32_t Size = |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 4856 | CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4857 | Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size); |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4858 | Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4859 | llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4860 | Values); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4861 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4862 | if (Entry) { |
| 4863 | // Already created, fix the linkage and update the initializer. |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 4864 | Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4865 | Entry->setInitializer(Init); |
| 4866 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4867 | Entry = |
| 4868 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false, |
| 4869 | llvm::GlobalValue::WeakAnyLinkage, |
| 4870 | Init, |
| 4871 | std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4872 | Entry->setAlignment( |
| 4873 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy)); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4874 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4875 | } |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4876 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4877 | CGM.AddUsedGlobal(Entry); |
| 4878 | |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4879 | // Use this protocol meta-data to build protocol list table in section |
| 4880 | // __DATA, __objc_protolist |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4881 | llvm::GlobalVariable *PTGV = new llvm::GlobalVariable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4882 | CGM.getModule(), |
| 4883 | ObjCTypes.ProtocolnfABIPtrTy, false, |
| 4884 | llvm::GlobalValue::WeakAnyLinkage, |
| 4885 | Entry, |
| 4886 | std::string("\01l_OBJC_LABEL_PROTOCOL_$_") |
| 4887 | +ProtocolName); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4888 | PTGV->setAlignment( |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4889 | CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy)); |
Daniel Dunbar | 0bf2199 | 2009-04-15 02:56:18 +0000 | [diff] [blame] | 4890 | PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip"); |
Fariborz Jahanian | 8448c2c | 2009-01-29 20:10:59 +0000 | [diff] [blame] | 4891 | PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4892 | CGM.AddUsedGlobal(PTGV); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4893 | return Entry; |
| 4894 | } |
| 4895 | |
| 4896 | /// EmitProtocolList - Generate protocol list meta-data: |
| 4897 | /// @code |
| 4898 | /// struct _protocol_list_t { |
| 4899 | /// long protocol_count; // Note, this is 32/64 bit |
| 4900 | /// struct _protocol_t[protocol_count]; |
| 4901 | /// } |
| 4902 | /// @endcode |
| 4903 | /// |
| 4904 | llvm::Constant * |
| 4905 | CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4906 | ObjCProtocolDecl::protocol_iterator begin, |
| 4907 | ObjCProtocolDecl::protocol_iterator end) { |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4908 | std::vector<llvm::Constant*> ProtocolRefs; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4909 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4910 | // Just return null for empty protocol lists |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4911 | if (begin == end) |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4912 | return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4913 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4914 | // FIXME: We shouldn't need to do this lookup here, should we? |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4915 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true); |
| 4916 | if (GV) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4917 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4918 | ObjCTypes.ProtocolListnfABIPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4919 | |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4920 | for (; begin != end; ++begin) |
| 4921 | ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented??? |
| 4922 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4923 | // This list is null terminated. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4924 | ProtocolRefs.push_back(llvm::Constant::getNullValue( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4925 | ObjCTypes.ProtocolnfABIPtrTy)); |
| 4926 | |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4927 | std::vector<llvm::Constant*> Values(2); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4928 | Values[0] = |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 4929 | llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4930 | Values[1] = |
Owen Anderson | 7db6d83 | 2009-07-28 18:33:04 +0000 | [diff] [blame] | 4931 | llvm::ConstantArray::get( |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 4932 | llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4933 | ProtocolRefs.size()), |
| 4934 | ProtocolRefs); |
| 4935 | |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 4936 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4937 | GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4938 | llvm::GlobalValue::InternalLinkage, |
| 4939 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 4940 | Name); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4941 | GV->setSection("__DATA, __objc_const"); |
Fariborz Jahanian | 09796d6 | 2009-01-31 02:43:27 +0000 | [diff] [blame] | 4942 | GV->setAlignment( |
| 4943 | CGM.getTargetData().getPrefTypeAlignment(Init->getType())); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 4944 | CGM.AddUsedGlobal(GV); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4945 | return llvm::ConstantExpr::getBitCast(GV, |
Daniel Dunbar | 948e258 | 2009-02-15 07:36:20 +0000 | [diff] [blame] | 4946 | ObjCTypes.ProtocolListnfABIPtrTy); |
Fariborz Jahanian | da32009 | 2009-01-29 19:24:30 +0000 | [diff] [blame] | 4947 | } |
| 4948 | |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4949 | /// GetMethodDescriptionConstant - This routine build following meta-data: |
| 4950 | /// struct _objc_method { |
| 4951 | /// SEL _cmd; |
| 4952 | /// char *method_type; |
| 4953 | /// char *_imp; |
| 4954 | /// } |
| 4955 | |
| 4956 | llvm::Constant * |
| 4957 | CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) { |
| 4958 | std::vector<llvm::Constant*> Desc(3); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 4959 | Desc[0] = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4960 | llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()), |
| 4961 | ObjCTypes.SelectorPtrTy); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4962 | Desc[1] = GetMethodVarType(MD); |
Fariborz Jahanian | 8cfd397 | 2009-01-30 18:58:59 +0000 | [diff] [blame] | 4963 | // Protocol methods have no implementation. So, this entry is always NULL. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 4964 | Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 4965 | return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc); |
Fariborz Jahanian | 3819a0b | 2009-01-30 00:46:37 +0000 | [diff] [blame] | 4966 | } |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4967 | |
| 4968 | /// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference. |
| 4969 | /// This code gen. amounts to generating code for: |
| 4970 | /// @code |
| 4971 | /// (type *)((char *)base + _OBJC_IVAR_$_.ivar; |
| 4972 | /// @encode |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4973 | /// |
Fariborz Jahanian | 598d3f6 | 2009-02-03 19:03:09 +0000 | [diff] [blame] | 4974 | LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4975 | CodeGen::CodeGenFunction &CGF, |
| 4976 | QualType ObjectTy, |
| 4977 | llvm::Value *BaseValue, |
| 4978 | const ObjCIvarDecl *Ivar, |
| 4979 | unsigned CVRQualifiers) { |
Daniel Dunbar | 525c9b7 | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 4980 | const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl(); |
Daniel Dunbar | 9777687 | 2009-04-22 07:32:20 +0000 | [diff] [blame] | 4981 | return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers, |
| 4982 | EmitIvarOffset(CGF, ID, Ivar)); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 4983 | } |
| 4984 | |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4985 | llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4986 | CodeGen::CodeGenFunction &CGF, |
| 4987 | const ObjCInterfaceDecl *Interface, |
| 4988 | const ObjCIvarDecl *Ivar) { |
| 4989 | return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar), |
Daniel Dunbar | 5e88bea | 2009-04-19 00:31:15 +0000 | [diff] [blame] | 4990 | false, "ivar"); |
Fariborz Jahanian | f63aa3f | 2009-02-10 19:02:04 +0000 | [diff] [blame] | 4991 | } |
| 4992 | |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 4993 | CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 4994 | CodeGen::CodeGenFunction &CGF, |
| 4995 | QualType ResultType, |
| 4996 | Selector Sel, |
| 4997 | llvm::Value *Receiver, |
| 4998 | QualType Arg0Ty, |
| 4999 | bool IsSuper, |
| 5000 | const CallArgList &CallArgs) { |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5001 | // FIXME. Even though IsSuper is passes. This function doese not handle calls |
| 5002 | // to 'super' receivers. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5003 | CodeGenTypes &Types = CGM.getTypes(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5004 | llvm::Value *Arg0 = Receiver; |
| 5005 | if (!IsSuper) |
| 5006 | Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5007 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5008 | // Find the message function name. |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5009 | // FIXME. This is too much work to get the ABI-specific result type needed to |
| 5010 | // find the message name. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5011 | const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, |
| 5012 | llvm::SmallVector<QualType, 16>()); |
Fariborz Jahanian | 70b51c7 | 2009-04-30 23:08:58 +0000 | [diff] [blame] | 5013 | llvm::Constant *Fn = 0; |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5014 | std::string Name("\01l_"); |
| 5015 | if (CGM.ReturnTypeUsesSret(FnInfo)) { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5016 | #if 0 |
| 5017 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5018 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5019 | Fn = ObjCTypes.getMessageSendIdStretFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5020 | // FIXME. Is there a better way of getting these names. |
| 5021 | // They are available in RuntimeFunctions vector pair. |
| 5022 | Name += "objc_msgSendId_stret_fixup"; |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5023 | } else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5024 | #endif |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5025 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5026 | Fn = ObjCTypes.getMessageSendSuper2StretFixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5027 | Name += "objc_msgSendSuper2_stret_fixup"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5028 | } else { |
| 5029 | Fn = ObjCTypes.getMessageSendStretFixupFn(); |
| 5030 | Name += "objc_msgSend_stret_fixup"; |
| 5031 | } |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5032 | } else if (!IsSuper && ResultType->isFloatingType()) { |
Daniel Dunbar | c0183e8 | 2009-06-26 18:32:06 +0000 | [diff] [blame] | 5033 | if (ResultType->isSpecificBuiltinType(BuiltinType::LongDouble)) { |
| 5034 | Fn = ObjCTypes.getMessageSendFpretFixupFn(); |
| 5035 | Name += "objc_msgSend_fpret_fixup"; |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5036 | } else { |
Daniel Dunbar | c0183e8 | 2009-06-26 18:32:06 +0000 | [diff] [blame] | 5037 | Fn = ObjCTypes.getMessageSendFixupFn(); |
| 5038 | Name += "objc_msgSend_fixup"; |
Fariborz Jahanian | 5b2bad0 | 2009-04-30 16:31:11 +0000 | [diff] [blame] | 5039 | } |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5040 | } else { |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5041 | #if 0 |
| 5042 | // unlike what is documented. gcc never generates this API!! |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5043 | if (Receiver->getType() == ObjCTypes.ObjectPtrTy) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5044 | Fn = ObjCTypes.getMessageSendIdFixupFn(); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5045 | Name += "objc_msgSendId_fixup"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5046 | } else |
Fariborz Jahanian | c170852 | 2009-02-05 18:00:27 +0000 | [diff] [blame] | 5047 | #endif |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5048 | if (IsSuper) { |
Chris Lattner | 1c02f86 | 2009-04-22 02:53:24 +0000 | [diff] [blame] | 5049 | Fn = ObjCTypes.getMessageSendSuper2FixupFn(); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5050 | Name += "objc_msgSendSuper2_fixup"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5051 | } else { |
| 5052 | Fn = ObjCTypes.getMessageSendFixupFn(); |
| 5053 | Name += "objc_msgSend_fixup"; |
| 5054 | } |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5055 | } |
Fariborz Jahanian | 70b51c7 | 2009-04-30 23:08:58 +0000 | [diff] [blame] | 5056 | assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5057 | Name += '_'; |
| 5058 | std::string SelName(Sel.getAsString()); |
| 5059 | // Replace all ':' in selector name with '_' ouch! |
| 5060 | for(unsigned i = 0; i < SelName.size(); i++) |
| 5061 | if (SelName[i] == ':') |
| 5062 | SelName[i] = '_'; |
| 5063 | Name += SelName; |
| 5064 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5065 | if (!GV) { |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5066 | // Build message ref table entry. |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5067 | std::vector<llvm::Constant*> Values(2); |
| 5068 | Values[0] = Fn; |
| 5069 | Values[1] = GetMethodVarName(Sel); |
Owen Anderson | 47a434f | 2009-08-05 23:18:46 +0000 | [diff] [blame] | 5070 | llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values); |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5071 | GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false, |
Mike Stump | 286acbd | 2009-03-07 16:33:28 +0000 | [diff] [blame] | 5072 | llvm::GlobalValue::WeakAnyLinkage, |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5073 | Init, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5074 | Name); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5075 | GV->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | f59c1a6 | 2009-04-15 19:04:46 +0000 | [diff] [blame] | 5076 | GV->setAlignment(16); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5077 | GV->setSection("__DATA, __objc_msgrefs, coalesced"); |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5078 | } |
| 5079 | llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5080 | |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5081 | CallArgList ActualArgs; |
| 5082 | ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5083 | ActualArgs.push_back(std::make_pair(RValue::get(Arg1), |
Fariborz Jahanian | 83a8a75 | 2009-02-04 20:42:28 +0000 | [diff] [blame] | 5084 | ObjCTypes.MessageRefCPtrTy)); |
| 5085 | ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end()); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5086 | const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs); |
| 5087 | llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0); |
| 5088 | Callee = CGF.Builder.CreateLoad(Callee); |
Fariborz Jahanian | 3ab75bd | 2009-02-14 21:25:36 +0000 | [diff] [blame] | 5089 | const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5090 | Callee = CGF.Builder.CreateBitCast(Callee, |
Owen Anderson | 96e0fc7 | 2009-07-29 22:16:19 +0000 | [diff] [blame] | 5091 | llvm::PointerType::getUnqual(FTy)); |
Fariborz Jahanian | ef16378 | 2009-02-05 01:13:09 +0000 | [diff] [blame] | 5092 | return CGF.EmitCall(FnInfo1, Callee, ActualArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5093 | } |
| 5094 | |
| 5095 | /// Generate code for a message send expression in the nonfragile abi. |
| 5096 | CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5097 | CodeGen::CodeGenFunction &CGF, |
| 5098 | QualType ResultType, |
| 5099 | Selector Sel, |
| 5100 | llvm::Value *Receiver, |
| 5101 | bool IsClassMessage, |
| 5102 | const CallArgList &CallArgs, |
| 5103 | const ObjCMethodDecl *Method) { |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5104 | return LegacyDispatchedSelector(Sel) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5105 | ? EmitLegacyMessageSend(CGF, ResultType, EmitSelector(CGF.Builder, Sel), |
| 5106 | Receiver, CGF.getContext().getObjCIdType(), |
| 5107 | false, CallArgs, ObjCTypes) |
| 5108 | : EmitMessageSend(CGF, ResultType, Sel, |
| 5109 | Receiver, CGF.getContext().getObjCIdType(), |
| 5110 | false, CallArgs); |
Fariborz Jahanian | 4655112 | 2009-02-04 00:22:57 +0000 | [diff] [blame] | 5111 | } |
| 5112 | |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5113 | llvm::GlobalVariable * |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5114 | CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) { |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5115 | llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name); |
| 5116 | |
Daniel Dunbar | dfff230 | 2009-03-02 05:18:14 +0000 | [diff] [blame] | 5117 | if (!GV) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5118 | GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5119 | false, llvm::GlobalValue::ExternalLinkage, |
| 5120 | 0, Name); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5121 | } |
| 5122 | |
| 5123 | return GV; |
| 5124 | } |
| 5125 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5126 | llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder, |
| 5127 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5128 | llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5129 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5130 | if (!Entry) { |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5131 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | 5a7379a | 2009-03-01 04:40:10 +0000 | [diff] [blame] | 5132 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5133 | Entry = |
| 5134 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5135 | false, llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5136 | ClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5137 | "\01L_OBJC_CLASSLIST_REFERENCES_$_"); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5138 | Entry->setAlignment( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5139 | CGM.getTargetData().getPrefTypeAlignment( |
| 5140 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5141 | Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5142 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5143 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5144 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5145 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5146 | } |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5147 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5148 | llvm::Value * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5149 | CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder, |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5150 | const ObjCInterfaceDecl *ID) { |
| 5151 | llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5152 | |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5153 | if (!Entry) { |
| 5154 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
| 5155 | llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5156 | Entry = |
| 5157 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5158 | false, llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5159 | ClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5160 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5161 | Entry->setAlignment( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5162 | CGM.getTargetData().getPrefTypeAlignment( |
| 5163 | ObjCTypes.ClassnfABIPtrTy)); |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5164 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5165 | CGM.AddUsedGlobal(Entry); |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5166 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5167 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5168 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5169 | } |
| 5170 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5171 | /// EmitMetaClassRef - Return a Value * of the address of _class_t |
| 5172 | /// meta-data |
| 5173 | /// |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5174 | llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder, |
| 5175 | const ObjCInterfaceDecl *ID) { |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5176 | llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()]; |
| 5177 | if (Entry) |
| 5178 | return Builder.CreateLoad(Entry, false, "tmp"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5179 | |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5180 | std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5181 | llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5182 | Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5183 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false, |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5184 | llvm::GlobalValue::InternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5185 | MetaClassGV, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5186 | "\01L_OBJC_CLASSLIST_SUP_REFS_$_"); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5187 | Entry->setAlignment( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5188 | CGM.getTargetData().getPrefTypeAlignment( |
| 5189 | ObjCTypes.ClassnfABIPtrTy)); |
| 5190 | |
Daniel Dunbar | 33af70f | 2009-04-15 19:03:14 +0000 | [diff] [blame] | 5191 | Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5192 | CGM.AddUsedGlobal(Entry); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5193 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5194 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5195 | } |
| 5196 | |
Fariborz Jahanian | 0e81f4b | 2009-02-05 20:41:40 +0000 | [diff] [blame] | 5197 | /// GetClass - Return a reference to the class for the given interface |
| 5198 | /// decl. |
| 5199 | llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder, |
| 5200 | const ObjCInterfaceDecl *ID) { |
| 5201 | return EmitClassRef(Builder, ID); |
| 5202 | } |
| 5203 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5204 | /// Generates a message send where the super is the receiver. This is |
| 5205 | /// a message send to self with special delivery semantics indicating |
| 5206 | /// which class's method should be called. |
| 5207 | CodeGen::RValue |
| 5208 | CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5209 | QualType ResultType, |
| 5210 | Selector Sel, |
| 5211 | const ObjCInterfaceDecl *Class, |
| 5212 | bool isCategoryImpl, |
| 5213 | llvm::Value *Receiver, |
| 5214 | bool IsClassMessage, |
| 5215 | const CodeGen::CallArgList &CallArgs) { |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5216 | // ... |
| 5217 | // Create and init a super structure; this is a (receiver, class) |
| 5218 | // pair we will pass to objc_msgSendSuper. |
| 5219 | llvm::Value *ObjCSuper = |
| 5220 | CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5221 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5222 | llvm::Value *ReceiverAsObject = |
| 5223 | CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy); |
| 5224 | CGF.Builder.CreateStore(ReceiverAsObject, |
| 5225 | CGF.Builder.CreateStructGEP(ObjCSuper, 0)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5226 | |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5227 | // If this is a class message the metaclass is passed as the target. |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5228 | llvm::Value *Target; |
| 5229 | if (IsClassMessage) { |
| 5230 | if (isCategoryImpl) { |
| 5231 | // Message sent to "super' in a class method defined in |
| 5232 | // a category implementation. |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5233 | Target = EmitClassRef(CGF.Builder, Class); |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5234 | Target = CGF.Builder.CreateStructGEP(Target, 0); |
| 5235 | Target = CGF.Builder.CreateLoad(Target); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5236 | } else |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 5237 | Target = EmitMetaClassRef(CGF.Builder, Class); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5238 | } else |
Daniel Dunbar | 1139452 | 2009-04-18 08:51:00 +0000 | [diff] [blame] | 5239 | Target = EmitSuperClassRef(CGF.Builder, Class); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5240 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5241 | // FIXME: We shouldn't need to do this cast, rectify the ASTContext and |
| 5242 | // ObjCTypes types. |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5243 | const llvm::Type *ClassTy = |
| 5244 | CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType()); |
| 5245 | Target = CGF.Builder.CreateBitCast(Target, ClassTy); |
| 5246 | CGF.Builder.CreateStore(Target, |
| 5247 | CGF.Builder.CreateStructGEP(ObjCSuper, 1)); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5248 | |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5249 | return (LegacyDispatchedSelector(Sel)) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5250 | ? EmitLegacyMessageSend(CGF, ResultType,EmitSelector(CGF.Builder, Sel), |
| 5251 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5252 | true, CallArgs, |
| 5253 | ObjCTypes) |
| 5254 | : EmitMessageSend(CGF, ResultType, Sel, |
| 5255 | ObjCSuper, ObjCTypes.SuperPtrCTy, |
| 5256 | true, CallArgs); |
Fariborz Jahanian | 7a06aae | 2009-02-06 20:09:23 +0000 | [diff] [blame] | 5257 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5258 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5259 | llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder, |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5260 | Selector Sel) { |
| 5261 | llvm::GlobalVariable *&Entry = SelectorReferences[Sel]; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5262 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5263 | if (!Entry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5264 | llvm::Constant *Casted = |
| 5265 | llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel), |
| 5266 | ObjCTypes.SelectorPtrTy); |
| 5267 | Entry = |
| 5268 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false, |
| 5269 | llvm::GlobalValue::InternalLinkage, |
| 5270 | Casted, "\01L_OBJC_SELECTOR_REFERENCES_"); |
Fariborz Jahanian | d0f8a8d | 2009-05-11 19:25:47 +0000 | [diff] [blame] | 5271 | Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip"); |
Chris Lattner | ad64e02 | 2009-07-17 23:57:13 +0000 | [diff] [blame] | 5272 | CGM.AddUsedGlobal(Entry); |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5273 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5274 | |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5275 | return Builder.CreateLoad(Entry, false, "tmp"); |
| 5276 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5277 | /// EmitObjCIvarAssign - Code gen for assigning to a __strong object. |
| 5278 | /// objc_assign_ivar (id src, id *dst) |
| 5279 | /// |
| 5280 | void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5281 | llvm::Value *src, llvm::Value *dst) |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5282 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5283 | const llvm::Type * SrcTy = src->getType(); |
| 5284 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5285 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5286 | assert(Size <= 8 && "does not support size > 8"); |
| 5287 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5288 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5289 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5290 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5291 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5292 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5293 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5294 | src, dst, "assignivar"); |
| 5295 | return; |
| 5296 | } |
| 5297 | |
| 5298 | /// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object. |
| 5299 | /// objc_assign_strongCast (id src, id *dst) |
| 5300 | /// |
| 5301 | void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5302 | CodeGen::CodeGenFunction &CGF, |
| 5303 | llvm::Value *src, llvm::Value *dst) |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5304 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5305 | const llvm::Type * SrcTy = src->getType(); |
| 5306 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5307 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5308 | assert(Size <= 8 && "does not support size > 8"); |
| 5309 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5310 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5311 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5312 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5313 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5314 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5315 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5316 | src, dst, "weakassign"); |
| 5317 | return; |
| 5318 | } |
| 5319 | |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 5320 | void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5321 | CodeGen::CodeGenFunction &CGF, |
| 5322 | llvm::Value *DestPtr, |
| 5323 | llvm::Value *SrcPtr, |
Fariborz Jahanian | 08c3213 | 2009-08-31 19:33:16 +0000 | [diff] [blame] | 5324 | QualType Ty) { |
| 5325 | // Get size info for this aggregate. |
| 5326 | std::pair<uint64_t, unsigned> TypeInfo = CGM.getContext().getTypeInfo(Ty); |
| 5327 | unsigned long size = TypeInfo.second/8; |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 5328 | SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy); |
| 5329 | DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 5330 | llvm::Value *N = llvm::ConstantInt::get(ObjCTypes.LongTy, size); |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 5331 | CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(), |
| 5332 | DestPtr, SrcPtr, N); |
| 5333 | return; |
| 5334 | } |
| 5335 | |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5336 | /// EmitObjCWeakRead - Code gen for loading value of a __weak |
| 5337 | /// object: objc_read_weak (id *src) |
| 5338 | /// |
| 5339 | llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead( |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5340 | CodeGen::CodeGenFunction &CGF, |
| 5341 | llvm::Value *AddrWeakObj) |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5342 | { |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5343 | const llvm::Type* DestTy = |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5344 | cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType(); |
| 5345 | AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 72db6c3 | 2009-04-22 02:44:54 +0000 | [diff] [blame] | 5346 | llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5347 | AddrWeakObj, "weakread"); |
Eli Friedman | 8339b35 | 2009-03-07 03:57:15 +0000 | [diff] [blame] | 5348 | read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy); |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5349 | return read_weak; |
| 5350 | } |
| 5351 | |
| 5352 | /// EmitObjCWeakAssign - Code gen for assigning to a __weak object. |
| 5353 | /// objc_assign_weak (id src, id *dst) |
| 5354 | /// |
| 5355 | void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5356 | llvm::Value *src, llvm::Value *dst) |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5357 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5358 | const llvm::Type * SrcTy = src->getType(); |
| 5359 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5360 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5361 | assert(Size <= 8 && "does not support size > 8"); |
| 5362 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5363 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5364 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5365 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5366 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5367 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | 96508e1 | 2009-04-17 22:12:36 +0000 | [diff] [blame] | 5368 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5369 | src, dst, "weakassign"); |
| 5370 | return; |
| 5371 | } |
| 5372 | |
| 5373 | /// EmitObjCGlobalAssign - Code gen for assigning to a __strong object. |
| 5374 | /// objc_assign_global (id src, id *dst) |
| 5375 | /// |
| 5376 | void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5377 | llvm::Value *src, llvm::Value *dst) |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5378 | { |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5379 | const llvm::Type * SrcTy = src->getType(); |
| 5380 | if (!isa<llvm::PointerType>(SrcTy)) { |
Duncan Sands | 9408c45 | 2009-05-09 07:08:47 +0000 | [diff] [blame] | 5381 | unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy); |
Fariborz Jahanian | 0a855d0 | 2009-03-23 19:10:40 +0000 | [diff] [blame] | 5382 | assert(Size <= 8 && "does not support size > 8"); |
| 5383 | src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy) |
| 5384 | : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy)); |
Fariborz Jahanian | 3b8a652 | 2009-03-13 00:42:52 +0000 | [diff] [blame] | 5385 | src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy); |
| 5386 | } |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5387 | src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy); |
| 5388 | dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5389 | CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(), |
Fariborz Jahanian | 6948aea | 2009-02-16 22:52:32 +0000 | [diff] [blame] | 5390 | src, dst, "globalassign"); |
| 5391 | return; |
| 5392 | } |
Fariborz Jahanian | 26cc89f | 2009-02-11 20:51:17 +0000 | [diff] [blame] | 5393 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5394 | void |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5395 | CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, |
| 5396 | const Stmt &S) { |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5397 | bool isTry = isa<ObjCAtTryStmt>(S); |
| 5398 | llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try"); |
| 5399 | llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest(); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5400 | llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5401 | llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5402 | llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw"); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5403 | llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end"); |
| 5404 | |
| 5405 | // For @synchronized, call objc_sync_enter(sync.expr). The |
| 5406 | // evaluation of the expression must occur before we enter the |
| 5407 | // @synchronized. We can safely avoid a temp here because jumps into |
| 5408 | // @synchronized are illegal & this will dominate uses. |
| 5409 | llvm::Value *SyncArg = 0; |
| 5410 | if (!isTry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5411 | SyncArg = |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5412 | CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr()); |
| 5413 | SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5414 | CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5415 | } |
| 5416 | |
| 5417 | // Push an EH context entry, used for handling rethrows and jumps |
| 5418 | // through finally. |
| 5419 | CGF.PushCleanupBlock(FinallyBlock); |
| 5420 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5421 | CGF.setInvokeDest(TryHandler); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5422 | |
| 5423 | CGF.EmitBlock(TryBlock); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5424 | CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody() |
| 5425 | : cast<ObjCAtSynchronizedStmt>(S).getSynchBody()); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5426 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5427 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5428 | // Emit the exception handler. |
| 5429 | |
| 5430 | CGF.EmitBlock(TryHandler); |
| 5431 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5432 | llvm::Value *llvm_eh_exception = |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5433 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5434 | llvm::Value *llvm_eh_selector_i64 = |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5435 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5436 | llvm::Value *llvm_eh_typeid_for_i64 = |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5437 | CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64); |
| 5438 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5439 | llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow"); |
| 5440 | |
| 5441 | llvm::SmallVector<llvm::Value*, 8> SelectorArgs; |
| 5442 | SelectorArgs.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5443 | SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr()); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5444 | |
| 5445 | // Construct the lists of (type, catch body) to handle. |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5446 | llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5447 | bool HasCatchAll = false; |
| 5448 | if (isTry) { |
| 5449 | if (const ObjCAtCatchStmt* CatchStmt = |
| 5450 | cast<ObjCAtTryStmt>(S).getCatchStmts()) { |
| 5451 | for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5452 | const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl(); |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5453 | Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody())); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5454 | |
| 5455 | // catch(...) always matches. |
Steve Naroff | 7ba138a | 2009-03-03 19:52:17 +0000 | [diff] [blame] | 5456 | if (!CatchDecl) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5457 | // Use i8* null here to signal this is a catch all, not a cleanup. |
Owen Anderson | c9c88b4 | 2009-07-31 20:28:54 +0000 | [diff] [blame] | 5458 | llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5459 | SelectorArgs.push_back(Null); |
| 5460 | HasCatchAll = true; |
| 5461 | break; |
| 5462 | } |
| 5463 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5464 | if (CatchDecl->getType()->isObjCIdType() || |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5465 | CatchDecl->getType()->isObjCQualifiedIdType()) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5466 | llvm::Value *IDEHType = |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5467 | CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id"); |
| 5468 | if (!IDEHType) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5469 | IDEHType = |
| 5470 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, |
| 5471 | false, |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5472 | llvm::GlobalValue::ExternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5473 | 0, "OBJC_EHTYPE_id"); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5474 | SelectorArgs.push_back(IDEHType); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 5475 | } else { |
Fariborz Jahanian | 99438a7 | 2009-07-27 23:12:41 +0000 | [diff] [blame] | 5476 | // All other types should be Objective-C interface pointer types. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5477 | const ObjCObjectPointerType *PT = |
Fariborz Jahanian | 99438a7 | 2009-07-27 23:12:41 +0000 | [diff] [blame] | 5478 | CatchDecl->getType()->getAsObjCObjectPointerType(); |
| 5479 | assert(PT && "Invalid @catch type."); |
| 5480 | const ObjCInterfaceType *IT = PT->getInterfaceType(); |
| 5481 | assert(IT && "Invalid @catch type."); |
| 5482 | llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false); |
| 5483 | SelectorArgs.push_back(EHType); |
| 5484 | } |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5485 | } |
| 5486 | } |
| 5487 | } |
| 5488 | |
| 5489 | // We use a cleanup unless there was already a catch all. |
| 5490 | if (!HasCatchAll) { |
Daniel Dunbar | e3d25ab | 2009-08-03 21:20:07 +0000 | [diff] [blame] | 5491 | // Even though this is a cleanup, treat it as a catch all to avoid the C++ |
| 5492 | // personality behavior of terminating the process if only cleanups are |
| 5493 | // found in the exception handling stack. |
| 5494 | SelectorArgs.push_back(llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy)); |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5495 | Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5496 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5497 | |
| 5498 | llvm::Value *Selector = |
| 5499 | CGF.Builder.CreateCall(llvm_eh_selector_i64, |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5500 | SelectorArgs.begin(), SelectorArgs.end(), |
| 5501 | "selector"); |
| 5502 | for (unsigned i = 0, e = Handlers.size(); i != e; ++i) { |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5503 | const ParmVarDecl *CatchParam = Handlers[i].first; |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5504 | const Stmt *CatchBody = Handlers[i].second; |
| 5505 | |
| 5506 | llvm::BasicBlock *Next = 0; |
| 5507 | |
| 5508 | // The last handler always matches. |
| 5509 | if (i + 1 != e) { |
| 5510 | assert(CatchParam && "Only last handler can be a catch all."); |
| 5511 | |
| 5512 | llvm::BasicBlock *Match = CGF.createBasicBlock("match"); |
| 5513 | Next = CGF.createBasicBlock("catch.next"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5514 | llvm::Value *Id = |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5515 | CGF.Builder.CreateCall(llvm_eh_typeid_for_i64, |
| 5516 | CGF.Builder.CreateBitCast(SelectorArgs[i+2], |
| 5517 | ObjCTypes.Int8PtrTy)); |
| 5518 | CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id), |
| 5519 | Match, Next); |
| 5520 | |
| 5521 | CGF.EmitBlock(Match); |
| 5522 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5523 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5524 | if (CatchBody) { |
| 5525 | llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end"); |
| 5526 | llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler"); |
| 5527 | |
| 5528 | // Cleanups must call objc_end_catch. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5529 | // |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 5530 | // FIXME: It seems incorrect for objc_begin_catch to be inside this |
| 5531 | // context, but this matches gcc. |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5532 | CGF.PushCleanupBlock(MatchEnd); |
| 5533 | CGF.setInvokeDest(MatchHandler); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5534 | |
| 5535 | llvm::Value *ExcObject = |
Chris Lattner | 8a56911 | 2009-04-22 02:15:23 +0000 | [diff] [blame] | 5536 | CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5537 | |
| 5538 | // Bind the catch parameter if it exists. |
| 5539 | if (CatchParam) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5540 | ExcObject = |
| 5541 | CGF.Builder.CreateBitCast(ExcObject, |
Daniel Dunbar | ede8de9 | 2009-03-06 00:01:21 +0000 | [diff] [blame] | 5542 | CGF.ConvertType(CatchParam->getType())); |
| 5543 | // CatchParam is a ParmVarDecl because of the grammar |
| 5544 | // construction used to handle this, but for codegen purposes |
| 5545 | // we treat this as a local decl. |
| 5546 | CGF.EmitLocalBlockVarDecl(*CatchParam); |
| 5547 | CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam)); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5548 | } |
| 5549 | |
| 5550 | CGF.ObjCEHValueStack.push_back(ExcObject); |
| 5551 | CGF.EmitStmt(CatchBody); |
| 5552 | CGF.ObjCEHValueStack.pop_back(); |
| 5553 | |
| 5554 | CGF.EmitBranchThroughCleanup(FinallyEnd); |
| 5555 | |
| 5556 | CGF.EmitBlock(MatchHandler); |
| 5557 | |
| 5558 | llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5559 | // We are required to emit this call to satisfy LLVM, even |
| 5560 | // though we don't use the result. |
| 5561 | llvm::SmallVector<llvm::Value*, 8> Args; |
| 5562 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5563 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 5564 | Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5565 | 0)); |
| 5566 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5567 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5568 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5569 | |
| 5570 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5571 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5572 | CGF.EmitBlock(MatchEnd); |
| 5573 | |
| 5574 | // Unfortunately, we also have to generate another EH frame here |
| 5575 | // in case this throws. |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5576 | llvm::BasicBlock *MatchEndHandler = |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5577 | CGF.createBasicBlock("match.end.handler"); |
| 5578 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5579 | CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5580 | Cont, MatchEndHandler, |
| 5581 | Args.begin(), Args.begin()); |
| 5582 | |
| 5583 | CGF.EmitBlock(Cont); |
| 5584 | if (Info.SwitchBlock) |
| 5585 | CGF.EmitBlock(Info.SwitchBlock); |
| 5586 | if (Info.EndBlock) |
| 5587 | CGF.EmitBlock(Info.EndBlock); |
| 5588 | |
| 5589 | CGF.EmitBlock(MatchEndHandler); |
| 5590 | Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc"); |
| 5591 | // We are required to emit this call to satisfy LLVM, even |
| 5592 | // though we don't use the result. |
| 5593 | Args.clear(); |
| 5594 | Args.push_back(Exc); |
Chris Lattner | b02e53b | 2009-04-06 16:53:45 +0000 | [diff] [blame] | 5595 | Args.push_back(ObjCTypes.getEHPersonalityPtr()); |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 5596 | Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5597 | 0)); |
| 5598 | CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end()); |
| 5599 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5600 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5601 | |
| 5602 | if (Next) |
| 5603 | CGF.EmitBlock(Next); |
| 5604 | } else { |
| 5605 | assert(!Next && "catchup should be last handler."); |
| 5606 | |
| 5607 | CGF.Builder.CreateStore(Exc, RethrowPtr); |
| 5608 | CGF.EmitBranchThroughCleanup(FinallyRethrow); |
| 5609 | } |
| 5610 | } |
| 5611 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5612 | // Pop the cleanup entry, the @finally is outside this cleanup |
| 5613 | // scope. |
| 5614 | CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock(); |
| 5615 | CGF.setInvokeDest(PrevLandingPad); |
| 5616 | |
| 5617 | CGF.EmitBlock(FinallyBlock); |
| 5618 | |
| 5619 | if (isTry) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5620 | if (const ObjCAtFinallyStmt* FinallyStmt = |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5621 | cast<ObjCAtTryStmt>(S).getFinallyStmt()) |
| 5622 | CGF.EmitStmt(FinallyStmt->getFinallyBody()); |
| 5623 | } else { |
| 5624 | // Emit 'objc_sync_exit(expr)' as finally's sole statement for |
| 5625 | // @synchronized. |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5626 | CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5627 | } |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5628 | |
| 5629 | if (Info.SwitchBlock) |
| 5630 | CGF.EmitBlock(Info.SwitchBlock); |
| 5631 | if (Info.EndBlock) |
| 5632 | CGF.EmitBlock(Info.EndBlock); |
| 5633 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5634 | // Branch around the rethrow code. |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5635 | CGF.EmitBranch(FinallyEnd); |
| 5636 | |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5637 | CGF.EmitBlock(FinallyRethrow); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5638 | CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5639 | CGF.Builder.CreateLoad(RethrowPtr)); |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5640 | CGF.Builder.CreateUnreachable(); |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5641 | |
Daniel Dunbar | 8ecbaf2 | 2009-02-24 07:47:38 +0000 | [diff] [blame] | 5642 | CGF.EmitBlock(FinallyEnd); |
| 5643 | } |
| 5644 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5645 | /// EmitThrowStmt - Generate code for a throw statement. |
| 5646 | void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF, |
| 5647 | const ObjCAtThrowStmt &S) { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5648 | llvm::Value *Exception; |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5649 | if (const Expr *ThrowExpr = S.getThrowExpr()) { |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5650 | Exception = CGF.EmitScalarExpr(ThrowExpr); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5651 | } else { |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5652 | assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) && |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5653 | "Unexpected rethrow outside @catch block."); |
| 5654 | Exception = CGF.ObjCEHValueStack.back(); |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5655 | } |
| 5656 | |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5657 | llvm::Value *ExceptionAsObject = |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5658 | CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp"); |
| 5659 | llvm::BasicBlock *InvokeDest = CGF.getInvokeDest(); |
| 5660 | if (InvokeDest) { |
| 5661 | llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont"); |
Chris Lattner | bbccd61 | 2009-04-22 02:38:11 +0000 | [diff] [blame] | 5662 | CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(), |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5663 | Cont, InvokeDest, |
| 5664 | &ExceptionAsObject, &ExceptionAsObject + 1); |
| 5665 | CGF.EmitBlock(Cont); |
| 5666 | } else |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5667 | CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject); |
Daniel Dunbar | 4ff3684 | 2009-03-02 06:08:11 +0000 | [diff] [blame] | 5668 | CGF.Builder.CreateUnreachable(); |
| 5669 | |
Anders Carlsson | f57c5b2 | 2009-02-16 22:59:18 +0000 | [diff] [blame] | 5670 | // Clear the insertion point to indicate we are in unreachable code. |
| 5671 | CGF.Builder.ClearInsertionPoint(); |
| 5672 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5673 | |
| 5674 | llvm::Value * |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5675 | CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5676 | bool ForDefinition) { |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5677 | llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()]; |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5678 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5679 | // If we don't need a definition, return the entry if found or check |
| 5680 | // if we use an external reference. |
| 5681 | if (!ForDefinition) { |
| 5682 | if (Entry) |
| 5683 | return Entry; |
Daniel Dunbar | 7e075cb | 2009-04-07 06:43:45 +0000 | [diff] [blame] | 5684 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5685 | // If this type (or a super class) has the __objc_exception__ |
| 5686 | // attribute, emit an external reference. |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 5687 | if (hasObjCExceptionAttribute(CGM.getContext(), ID)) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5688 | return Entry = |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5689 | new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5690 | llvm::GlobalValue::ExternalLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5691 | 0, |
| 5692 | (std::string("OBJC_EHTYPE_$_") + |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5693 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5694 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5695 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5696 | // Otherwise we need to either make a new entry or fill in the |
| 5697 | // initializer. |
| 5698 | assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition"); |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5699 | std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString()); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5700 | std::string VTableName = "objc_ehtype_vtable"; |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5701 | llvm::GlobalVariable *VTableGV = |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5702 | CGM.getModule().getGlobalVariable(VTableName); |
| 5703 | if (!VTableGV) |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5704 | VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy, |
| 5705 | false, |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5706 | llvm::GlobalValue::ExternalLinkage, |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5707 | 0, VTableName); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5708 | |
Owen Anderson | 0032b27 | 2009-08-13 21:57:51 +0000 | [diff] [blame] | 5709 | llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5710 | |
| 5711 | std::vector<llvm::Constant*> Values(3); |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 5712 | Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5713 | Values[1] = GetClassName(ID->getIdentifier()); |
Fariborz Jahanian | 0f90294 | 2009-04-14 18:41:56 +0000 | [diff] [blame] | 5714 | Values[2] = GetClassGlobal(ClassName); |
Owen Anderson | a1cf15f | 2009-07-14 23:10:40 +0000 | [diff] [blame] | 5715 | llvm::Constant *Init = |
Owen Anderson | 08e2524 | 2009-07-27 22:29:56 +0000 | [diff] [blame] | 5716 | llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values); |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5717 | |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5718 | if (Entry) { |
| 5719 | Entry->setInitializer(Init); |
| 5720 | } else { |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5721 | Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false, |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5722 | llvm::GlobalValue::WeakAnyLinkage, |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5723 | Init, |
| 5724 | (std::string("OBJC_EHTYPE_$_") + |
Owen Anderson | 1c431b3 | 2009-07-08 19:05:04 +0000 | [diff] [blame] | 5725 | ID->getIdentifier()->getName())); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5726 | } |
| 5727 | |
Daniel Dunbar | 04d4078 | 2009-04-14 06:00:08 +0000 | [diff] [blame] | 5728 | if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden) |
Daniel Dunbar | 6ab187a | 2009-04-07 05:48:37 +0000 | [diff] [blame] | 5729 | Entry->setVisibility(llvm::GlobalValue::HiddenVisibility); |
Daniel Dunbar | 8158a2f | 2009-04-08 04:21:03 +0000 | [diff] [blame] | 5730 | Entry->setAlignment(8); |
| 5731 | |
| 5732 | if (ForDefinition) { |
| 5733 | Entry->setSection("__DATA,__objc_const"); |
| 5734 | Entry->setLinkage(llvm::GlobalValue::ExternalLinkage); |
| 5735 | } else { |
| 5736 | Entry->setSection("__DATA,__datacoal_nt,coalesced"); |
| 5737 | } |
Daniel Dunbar | e588b99 | 2009-03-01 04:46:24 +0000 | [diff] [blame] | 5738 | |
| 5739 | return Entry; |
| 5740 | } |
Daniel Dunbar | 6bff251 | 2009-08-03 17:06:42 +0000 | [diff] [blame] | 5741 | |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 5742 | /* *** */ |
| 5743 | |
Daniel Dunbar | 6efc0c5 | 2008-08-13 03:21:16 +0000 | [diff] [blame] | 5744 | CodeGen::CGObjCRuntime * |
| 5745 | CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Daniel Dunbar | c17a4d3 | 2008-08-11 02:45:11 +0000 | [diff] [blame] | 5746 | return new CGObjCMac(CGM); |
| 5747 | } |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5748 | |
| 5749 | CodeGen::CGObjCRuntime * |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 5750 | CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) { |
Fariborz Jahanian | aa23b57 | 2009-01-23 23:53:38 +0000 | [diff] [blame] | 5751 | return new CGObjCNonFragileABIMac(CGM); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 5752 | } |