blob: 864b4ebe1f1e71b8c8d3407360eb22a926ec12fb [file] [log] [blame]
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001//===------- 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//
Chris Lattner57540c52011-04-15 05:22:18 +000010// This provides Objective-C code generation targeting the Apple runtime.
Daniel Dunbar303e2c22008-08-11 02:45:11 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000015
Daniel Dunbar034299e2010-03-31 01:09:11 +000016#include "CGRecordLayout.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000017#include "CodeGenModule.h"
Daniel Dunbara94ecd22008-08-16 03:19:19 +000018#include "CodeGenFunction.h"
John McCallad7c5c12011-02-08 08:22:06 +000019#include "CGBlocks.h"
John McCalled1ae862011-01-28 11:13:47 +000020#include "CGCleanup.h"
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000021#include "clang/AST/ASTContext.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000022#include "clang/AST/Decl.h"
Daniel Dunbarb036db82008-08-13 03:21:16 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000025#include "clang/AST/StmtObjC.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000026#include "clang/Basic/LangOptions.h"
Chandler Carruth85098242010-06-15 23:19:56 +000027#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000028
John McCall42227ed2010-07-31 23:20:56 +000029#include "llvm/InlineAsm.h"
30#include "llvm/IntrinsicInst.h"
Owen Andersonae86c192009-07-13 04:10:07 +000031#include "llvm/LLVMContext.h"
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000032#include "llvm/Module.h"
Daniel Dunbarc475d422008-10-29 22:36:39 +000033#include "llvm/ADT/DenseSet.h"
Daniel Dunbard027a922009-09-07 00:20:42 +000034#include "llvm/ADT/SetVector.h"
35#include "llvm/ADT/SmallString.h"
Fariborz Jahanian751c1e72009-12-12 21:26:21 +000036#include "llvm/ADT/SmallPtrSet.h"
John McCall5c08ab92010-07-13 22:12:14 +000037#include "llvm/Support/CallSite.h"
Daniel Dunbard027a922009-09-07 00:20:42 +000038#include "llvm/Support/raw_ostream.h"
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +000039#include "llvm/Target/TargetData.h"
Torok Edwindb714922009-08-24 13:25:12 +000040#include <cstdio>
Daniel Dunbar303e2c22008-08-11 02:45:11 +000041
42using namespace clang;
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000043using namespace CodeGen;
Daniel Dunbar303e2c22008-08-11 02:45:11 +000044
45namespace {
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000046
Daniel Dunbar59e476b2009-08-03 17:06:42 +000047// FIXME: We should find a nicer way to make the labels for metadata, string
48// concatenation is lame.
Daniel Dunbarb036db82008-08-13 03:21:16 +000049
Fariborz Jahanian279eda62009-01-21 22:04:16 +000050class ObjCCommonTypesHelper {
Owen Anderson170229f2009-07-14 23:10:40 +000051protected:
52 llvm::LLVMContext &VMContext;
Daniel Dunbar59e476b2009-08-03 17:06:42 +000053
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000054private:
John McCall9dc0db22011-05-15 01:53:33 +000055 // The types of these functions don't really matter because we
56 // should always bitcast before calling them.
57
58 /// id objc_msgSend (id, SEL, ...)
59 ///
60 /// The default messenger, used for sends whose ABI is unchanged from
61 /// the all-integer/pointer case.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000062 llvm::Constant *getMessageSendFn() const {
John McCall31168b02011-06-15 23:02:42 +000063 // Add the non-lazy-bind attribute, since objc_msgSend is likely to
64 // be called a lot.
Chris Lattnera5f58b02011-07-09 17:41:47 +000065 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +000066 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
67 params, true),
John McCall31168b02011-06-15 23:02:42 +000068 "objc_msgSend",
69 llvm::Attribute::NonLazyBind);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000070 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +000071
John McCall9dc0db22011-05-15 01:53:33 +000072 /// void objc_msgSend_stret (id, SEL, ...)
73 ///
74 /// The messenger used when the return value is an aggregate returned
75 /// by indirect reference in the first argument, and therefore the
76 /// self and selector parameters are shifted over by one.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000077 llvm::Constant *getMessageSendStretFn() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +000078 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +000079 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy,
80 params, true),
81 "objc_msgSend_stret");
Daniel Dunbar59e476b2009-08-03 17:06:42 +000082
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000083 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +000084
John McCall9dc0db22011-05-15 01:53:33 +000085 /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
86 ///
87 /// The messenger used when the return value is returned on the x87
88 /// floating-point stack; without a special entrypoint, the nil case
89 /// would be unbalanced.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000090 llvm::Constant *getMessageSendFpretFn() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +000091 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
Chris Lattnerece04092012-02-07 00:39:47 +000092 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.DoubleTy,
93 params, true),
John McCall9dc0db22011-05-15 01:53:33 +000094 "objc_msgSend_fpret");
Daniel Dunbar59e476b2009-08-03 17:06:42 +000095
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +000096 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +000097
Anders Carlsson2f1a6c32011-10-31 16:27:11 +000098 /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...)
99 ///
100 /// The messenger used when the return value is returned in two values on the
101 /// x87 floating point stack; without a special entrypoint, the nil case
102 /// would be unbalanced. Only used on 64-bit X86.
103 llvm::Constant *getMessageSendFp2retFn() const {
104 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
105 llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext);
106 llvm::Type *resultType =
107 llvm::StructType::get(longDoubleType, longDoubleType, NULL);
108
109 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType,
110 params, true),
111 "objc_msgSend_fp2ret");
112 }
113
John McCall9dc0db22011-05-15 01:53:33 +0000114 /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
115 ///
116 /// The messenger used for super calls, which have different dispatch
117 /// semantics. The class passed is the superclass of the current
118 /// class.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000119 llvm::Constant *getMessageSendSuperFn() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000120 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000121 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000122 params, true),
123 "objc_msgSendSuper");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000124 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000125
John McCall9dc0db22011-05-15 01:53:33 +0000126 /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
127 ///
128 /// A slightly different messenger used for super calls. The class
129 /// passed is the current class.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000130 llvm::Constant *getMessageSendSuperFn2() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000131 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000132 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000133 params, true),
134 "objc_msgSendSuper2");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000135 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000136
John McCall9dc0db22011-05-15 01:53:33 +0000137 /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super,
138 /// SEL op, ...)
139 ///
140 /// The messenger used for super calls which return an aggregate indirectly.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000141 llvm::Constant *getMessageSendSuperStretFn() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000142 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000143 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000144 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000145 "objc_msgSendSuper_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000146 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000147
John McCall9dc0db22011-05-15 01:53:33 +0000148 /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
149 /// SEL op, ...)
150 ///
151 /// objc_msgSendSuper_stret with the super2 semantics.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000152 llvm::Constant *getMessageSendSuperStretFn2() const {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000153 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000154 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000155 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000156 "objc_msgSendSuper2_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000157 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000158
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000159 llvm::Constant *getMessageSendSuperFpretFn() const {
160 // There is no objc_msgSendSuper_fpret? How can that work?
161 return getMessageSendSuperFn();
162 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000163
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000164 llvm::Constant *getMessageSendSuperFpretFn2() const {
165 // There is no objc_msgSendSuper_fpret? How can that work?
166 return getMessageSendSuperFn2();
167 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000168
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000169protected:
170 CodeGen::CodeGenModule &CGM;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000171
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000172public:
Chris Lattnera5f58b02011-07-09 17:41:47 +0000173 llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000174 llvm::Type *Int8PtrTy, *Int8PtrPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000175
Daniel Dunbar5d715592008-08-12 05:28:47 +0000176 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
Chris Lattnera5f58b02011-07-09 17:41:47 +0000177 llvm::Type *ObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000178
Fariborz Jahanian406b1172008-11-18 20:18:11 +0000179 /// PtrObjectPtrTy - LLVM type for id *
Chris Lattnera5f58b02011-07-09 17:41:47 +0000180 llvm::Type *PtrObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000181
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000182 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Chris Lattnera5f58b02011-07-09 17:41:47 +0000183 llvm::Type *SelectorPtrTy;
Douglas Gregor020de322012-01-17 18:36:30 +0000184
185private:
Daniel Dunbarb036db82008-08-13 03:21:16 +0000186 /// ProtocolPtrTy - LLVM type for external protocol handles
187 /// (typeof(Protocol))
Chris Lattnera5f58b02011-07-09 17:41:47 +0000188 llvm::Type *ExternalProtocolPtrTy;
Douglas Gregor020de322012-01-17 18:36:30 +0000189
190public:
191 llvm::Type *getExternalProtocolPtrTy() {
192 if (!ExternalProtocolPtrTy) {
193 // FIXME: It would be nice to unify this with the opaque type, so that the
194 // IR comes out a bit cleaner.
195 CodeGen::CodeGenTypes &Types = CGM.getTypes();
196 ASTContext &Ctx = CGM.getContext();
197 llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
198 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
199 }
200
201 return ExternalProtocolPtrTy;
202 }
203
Daniel Dunbarc722b852008-08-30 03:02:31 +0000204 // SuperCTy - clang type for struct objc_super.
205 QualType SuperCTy;
206 // SuperPtrCTy - clang type for struct objc_super *.
207 QualType SuperPtrCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000208
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000209 /// SuperTy - LLVM type for struct objc_super.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000210 llvm::StructType *SuperTy;
Daniel Dunbar97ff50d2008-08-23 09:25:55 +0000211 /// SuperPtrTy - LLVM type for struct objc_super *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000212 llvm::Type *SuperPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000213
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000214 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
215 /// in GCC parlance).
Chris Lattnera5f58b02011-07-09 17:41:47 +0000216 llvm::StructType *PropertyTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000217
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000218 /// PropertyListTy - LLVM type for struct objc_property_list
219 /// (_prop_list_t in GCC parlance).
Chris Lattnera5f58b02011-07-09 17:41:47 +0000220 llvm::StructType *PropertyListTy;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000221 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000222 llvm::Type *PropertyListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000223
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000224 // MethodTy - LLVM type for struct objc_method.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000225 llvm::StructType *MethodTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000226
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000227 /// CacheTy - LLVM type for struct objc_cache.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000228 llvm::Type *CacheTy;
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000229 /// CachePtrTy - LLVM type for struct objc_cache *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000230 llvm::Type *CachePtrTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000231
Chris Lattnerce8754e2009-04-22 02:44:54 +0000232 llvm::Constant *getGetPropertyFn() {
233 CodeGen::CodeGenTypes &Types = CGM.getTypes();
234 ASTContext &Ctx = CGM.getContext();
235 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000236 SmallVector<CanQualType,4> Params;
John McCall2da83a32010-02-26 00:48:12 +0000237 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
238 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000239 Params.push_back(IdType);
240 Params.push_back(SelType);
David Chisnall08a45252011-03-22 20:03:13 +0000241 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000242 Params.push_back(Ctx.BoolTy);
Chris Lattner2192fe52011-07-18 04:24:23 +0000243 llvm::FunctionType *FTy =
John McCall8dda7b22012-07-07 06:41:13 +0000244 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(IdType, Params,
245 FunctionType::ExtInfo(),
246 RequiredArgs::All));
Chris Lattnerce8754e2009-04-22 02:44:54 +0000247 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
248 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000249
Chris Lattnerce8754e2009-04-22 02:44:54 +0000250 llvm::Constant *getSetPropertyFn() {
251 CodeGen::CodeGenTypes &Types = CGM.getTypes();
252 ASTContext &Ctx = CGM.getContext();
253 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000254 SmallVector<CanQualType,6> Params;
John McCall2da83a32010-02-26 00:48:12 +0000255 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
256 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000257 Params.push_back(IdType);
258 Params.push_back(SelType);
David Chisnall08a45252011-03-22 20:03:13 +0000259 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000260 Params.push_back(IdType);
261 Params.push_back(Ctx.BoolTy);
262 Params.push_back(Ctx.BoolTy);
Chris Lattner2192fe52011-07-18 04:24:23 +0000263 llvm::FunctionType *FTy =
John McCall8dda7b22012-07-07 06:41:13 +0000264 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
265 FunctionType::ExtInfo(),
266 RequiredArgs::All));
Chris Lattnerce8754e2009-04-22 02:44:54 +0000267 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
268 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000269
Ted Kremeneke65b0862012-03-06 20:05:56 +0000270 llvm::Constant *getOptimizedSetPropertyFn(bool atomic, bool copy) {
271 CodeGen::CodeGenTypes &Types = CGM.getTypes();
272 ASTContext &Ctx = CGM.getContext();
273 // void objc_setProperty_atomic(id self, SEL _cmd,
274 // id newValue, ptrdiff_t offset);
275 // void objc_setProperty_nonatomic(id self, SEL _cmd,
276 // id newValue, ptrdiff_t offset);
277 // void objc_setProperty_atomic_copy(id self, SEL _cmd,
278 // id newValue, ptrdiff_t offset);
279 // void objc_setProperty_nonatomic_copy(id self, SEL _cmd,
280 // id newValue, ptrdiff_t offset);
281
282 SmallVector<CanQualType,4> Params;
283 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
284 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
285 Params.push_back(IdType);
286 Params.push_back(SelType);
287 Params.push_back(IdType);
288 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
289 llvm::FunctionType *FTy =
John McCall8dda7b22012-07-07 06:41:13 +0000290 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
291 FunctionType::ExtInfo(),
292 RequiredArgs::All));
Ted Kremeneke65b0862012-03-06 20:05:56 +0000293 const char *name;
294 if (atomic && copy)
295 name = "objc_setProperty_atomic_copy";
296 else if (atomic && !copy)
297 name = "objc_setProperty_atomic";
298 else if (!atomic && copy)
299 name = "objc_setProperty_nonatomic_copy";
300 else
301 name = "objc_setProperty_nonatomic";
302
303 return CGM.CreateRuntimeFunction(FTy, name);
304 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000305
306 llvm::Constant *getCopyStructFn() {
307 CodeGen::CodeGenTypes &Types = CGM.getTypes();
308 ASTContext &Ctx = CGM.getContext();
309 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000310 SmallVector<CanQualType,5> Params;
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000311 Params.push_back(Ctx.VoidPtrTy);
312 Params.push_back(Ctx.VoidPtrTy);
313 Params.push_back(Ctx.LongTy);
314 Params.push_back(Ctx.BoolTy);
315 Params.push_back(Ctx.BoolTy);
Chris Lattner2192fe52011-07-18 04:24:23 +0000316 llvm::FunctionType *FTy =
John McCall8dda7b22012-07-07 06:41:13 +0000317 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
318 FunctionType::ExtInfo(),
319 RequiredArgs::All));
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000320 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
321 }
322
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +0000323 /// This routine declares and returns address of:
324 /// void objc_copyCppObjectAtomic(
325 /// void *dest, const void *src,
326 /// void (*copyHelper) (void *dest, const void *source));
327 llvm::Constant *getCppAtomicObjectFunction() {
328 CodeGen::CodeGenTypes &Types = CGM.getTypes();
329 ASTContext &Ctx = CGM.getContext();
330 /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper);
331 SmallVector<CanQualType,3> Params;
332 Params.push_back(Ctx.VoidPtrTy);
333 Params.push_back(Ctx.VoidPtrTy);
334 Params.push_back(Ctx.VoidPtrTy);
335 llvm::FunctionType *FTy =
John McCall8dda7b22012-07-07 06:41:13 +0000336 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
337 FunctionType::ExtInfo(),
338 RequiredArgs::All));
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +0000339 return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic");
340 }
341
Chris Lattnerce8754e2009-04-22 02:44:54 +0000342 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000343 CodeGen::CodeGenTypes &Types = CGM.getTypes();
344 ASTContext &Ctx = CGM.getContext();
Chris Lattnerce8754e2009-04-22 02:44:54 +0000345 // void objc_enumerationMutation (id)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000346 SmallVector<CanQualType,1> Params;
John McCall2da83a32010-02-26 00:48:12 +0000347 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Chris Lattner2192fe52011-07-18 04:24:23 +0000348 llvm::FunctionType *FTy =
John McCall8dda7b22012-07-07 06:41:13 +0000349 Types.GetFunctionType(Types.arrangeLLVMFunctionInfo(Ctx.VoidTy, Params,
John McCalla729c622012-02-17 03:33:10 +0000350 FunctionType::ExtInfo(),
351 RequiredArgs::All));
Chris Lattnerce8754e2009-04-22 02:44:54 +0000352 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
353 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000354
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000355 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattnerce8754e2009-04-22 02:44:54 +0000356 llvm::Constant *getGcReadWeakFn() {
357 // id objc_read_weak (id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000358 llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000359 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000360 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000361 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000362 }
363
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000364 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000365 llvm::Constant *getGcAssignWeakFn() {
366 // id objc_assign_weak (id, id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000367 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000368 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000369 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000370 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
371 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000372
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000373 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000374 llvm::Constant *getGcAssignGlobalFn() {
375 // id objc_assign_global(id, id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000376 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000377 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000378 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000379 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
380 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000381
Fariborz Jahanian217af242010-07-20 20:30:03 +0000382 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
383 llvm::Constant *getGcAssignThreadLocalFn() {
384 // id objc_assign_threadlocal(id src, id * dest)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000385 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Fariborz Jahanian217af242010-07-20 20:30:03 +0000386 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000387 llvm::FunctionType::get(ObjectPtrTy, args, false);
Fariborz Jahanian217af242010-07-20 20:30:03 +0000388 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
389 }
390
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000391 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000392 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000393 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000394 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
395 CGM.PtrDiffTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000396 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000397 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000398 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
399 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000400
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000401 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
402 llvm::Constant *GcMemmoveCollectableFn() {
403 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000404 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
John McCall9dc0db22011-05-15 01:53:33 +0000405 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000406 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
407 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000408
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000409 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000410 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian217af242010-07-20 20:30:03 +0000411 // id objc_assign_strongCast(id, id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000412 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000413 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000414 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000415 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
416 }
Anders Carlsson9ab53d12009-02-16 22:59:18 +0000417
418 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000419 llvm::Constant *getExceptionThrowFn() {
420 // void objc_exception_throw(id)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000421 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattner0a696a422009-04-22 02:38:11 +0000422 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000423 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000424 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
425 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000426
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000427 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
428 llvm::Constant *getExceptionRethrowFn() {
429 // void objc_exception_rethrow(void)
John McCall9dc0db22011-05-15 01:53:33 +0000430 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000431 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
432 }
433
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000434 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerdcceee72009-04-06 16:53:45 +0000435 llvm::Constant *getSyncEnterFn() {
436 // void objc_sync_enter (id)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000437 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerdcceee72009-04-06 16:53:45 +0000438 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000439 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerdcceee72009-04-06 16:53:45 +0000440 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
441 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000442
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000443 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000444 llvm::Constant *getSyncExitFn() {
445 // void objc_sync_exit (id)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000446 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattner0a696a422009-04-22 02:38:11 +0000447 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000448 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000449 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
450 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000451
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000452 llvm::Constant *getSendFn(bool IsSuper) const {
453 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
454 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000455
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000456 llvm::Constant *getSendFn2(bool IsSuper) const {
457 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
458 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000459
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000460 llvm::Constant *getSendStretFn(bool IsSuper) const {
461 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
462 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000463
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000464 llvm::Constant *getSendStretFn2(bool IsSuper) const {
465 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
466 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000467
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000468 llvm::Constant *getSendFpretFn(bool IsSuper) const {
469 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
470 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000471
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000472 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
473 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
474 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000475
Anders Carlsson2f1a6c32011-10-31 16:27:11 +0000476 llvm::Constant *getSendFp2retFn(bool IsSuper) const {
477 return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn();
478 }
479
480 llvm::Constant *getSendFp2RetFn2(bool IsSuper) const {
481 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn();
482 }
483
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000484 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
485 ~ObjCCommonTypesHelper(){}
486};
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000487
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000488/// ObjCTypesHelper - Helper class that encapsulates lazy
489/// construction of varies types used during ObjC generation.
490class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000491public:
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000492 /// SymtabTy - LLVM type for struct objc_symtab.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000493 llvm::StructType *SymtabTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000494 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000495 llvm::Type *SymtabPtrTy;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000496 /// ModuleTy - LLVM type for struct objc_module.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000497 llvm::StructType *ModuleTy;
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000498
Daniel Dunbarb036db82008-08-13 03:21:16 +0000499 /// ProtocolTy - LLVM type for struct objc_protocol.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000500 llvm::StructType *ProtocolTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000501 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000502 llvm::Type *ProtocolPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000503 /// ProtocolExtensionTy - LLVM type for struct
504 /// objc_protocol_extension.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000505 llvm::StructType *ProtocolExtensionTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000506 /// ProtocolExtensionTy - LLVM type for struct
507 /// objc_protocol_extension *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000508 llvm::Type *ProtocolExtensionPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000509 /// MethodDescriptionTy - LLVM type for struct
510 /// objc_method_description.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000511 llvm::StructType *MethodDescriptionTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000512 /// MethodDescriptionListTy - LLVM type for struct
513 /// objc_method_description_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000514 llvm::StructType *MethodDescriptionListTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000515 /// MethodDescriptionListPtrTy - LLVM type for struct
516 /// objc_method_description_list *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000517 llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000518 /// ProtocolListTy - LLVM type for struct objc_property_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000519 llvm::StructType *ProtocolListTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000520 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000521 llvm::Type *ProtocolListPtrTy;
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000522 /// CategoryTy - LLVM type for struct objc_category.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000523 llvm::StructType *CategoryTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000524 /// ClassTy - LLVM type for struct objc_class.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000525 llvm::StructType *ClassTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000526 /// ClassPtrTy - LLVM type for struct objc_class *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000527 llvm::Type *ClassPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000528 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000529 llvm::StructType *ClassExtensionTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000530 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000531 llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000532 // IvarTy - LLVM type for struct objc_ivar.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000533 llvm::StructType *IvarTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000534 /// IvarListTy - LLVM type for struct objc_ivar_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000535 llvm::Type *IvarListTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000536 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000537 llvm::Type *IvarListPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000538 /// MethodListTy - LLVM type for struct objc_method_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000539 llvm::Type *MethodListTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000540 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000541 llvm::Type *MethodListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000542
Anders Carlsson9ff22482008-09-09 10:10:21 +0000543 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000544 llvm::Type *ExceptionDataTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000545
Anders Carlsson9ff22482008-09-09 10:10:21 +0000546 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000547 llvm::Constant *getExceptionTryEnterFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000548 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000549 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000550 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000551 "objc_exception_try_enter");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000552 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000553
554 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000555 llvm::Constant *getExceptionTryExitFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000556 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000557 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000558 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000559 "objc_exception_try_exit");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000560 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000561
562 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000563 llvm::Constant *getExceptionExtractFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000564 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000565 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000566 params, false),
Chris Lattnerc6406db2009-04-22 02:26:14 +0000567 "objc_exception_extract");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000568 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000569
Anders Carlsson9ff22482008-09-09 10:10:21 +0000570 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000571 llvm::Constant *getExceptionMatchFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000572 llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000573 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000574 llvm::FunctionType::get(CGM.Int32Ty, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000575 "objc_exception_match");
576
Chris Lattnerc6406db2009-04-22 02:26:14 +0000577 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000578
Anders Carlsson9ff22482008-09-09 10:10:21 +0000579 /// SetJmpFn - LLVM _setjmp function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000580 llvm::Constant *getSetJmpFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000581 // This is specifically the prototype for x86.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000582 llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
John McCall9dc0db22011-05-15 01:53:33 +0000583 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
584 params, false),
Bill Wendlingbcefeae2011-11-29 00:10:10 +0000585 "_setjmp",
586 llvm::Attribute::ReturnsTwice);
Chris Lattnerc6406db2009-04-22 02:26:14 +0000587 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000588
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000589public:
590 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000591 ~ObjCTypesHelper() {}
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000592};
593
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000594/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000595/// modern abi
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000596class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000597public:
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000598
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000599 // MethodListnfABITy - LLVM for struct _method_list_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000600 llvm::StructType *MethodListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000601
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000602 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000603 llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000604
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000605 // ProtocolnfABITy = LLVM for struct _protocol_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000606 llvm::StructType *ProtocolnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000607
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000608 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000609 llvm::Type *ProtocolnfABIPtrTy;
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000610
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000611 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
Chris Lattnera5f58b02011-07-09 17:41:47 +0000612 llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000613
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000614 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000615 llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000616
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000617 // ClassnfABITy - LLVM for struct _class_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000618 llvm::StructType *ClassnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000619
Fariborz Jahanian71394042009-01-23 23:53:38 +0000620 // ClassnfABIPtrTy - LLVM for struct _class_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000621 llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000622
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000623 // IvarnfABITy - LLVM for struct _ivar_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000624 llvm::StructType *IvarnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000625
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000626 // IvarListnfABITy - LLVM for struct _ivar_list_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000627 llvm::StructType *IvarListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000628
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000629 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000630 llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000631
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000632 // ClassRonfABITy - LLVM for struct _class_ro_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000633 llvm::StructType *ClassRonfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000634
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000635 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000636 llvm::Type *ImpnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000637
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000638 // CategorynfABITy - LLVM for struct _category_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000639 llvm::StructType *CategorynfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000640
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000641 // New types for nonfragile abi messaging.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000642
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000643 // MessageRefTy - LLVM for:
644 // struct _message_ref_t {
645 // IMP messenger;
646 // SEL name;
647 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +0000648 llvm::StructType *MessageRefTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000649 // MessageRefCTy - clang type for struct _message_ref_t
650 QualType MessageRefCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000651
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000652 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000653 llvm::Type *MessageRefPtrTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000654 // MessageRefCPtrTy - clang type for struct _message_ref_t*
655 QualType MessageRefCPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000656
Fariborz Jahanian4e87c832009-02-05 01:13:09 +0000657 // MessengerTy - Type of the messenger (shown as IMP above)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000658 llvm::FunctionType *MessengerTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000659
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000660 // SuperMessageRefTy - LLVM for:
661 // struct _super_message_ref_t {
662 // SUPER_IMP messenger;
663 // SEL name;
664 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +0000665 llvm::StructType *SuperMessageRefTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000666
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000667 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000668 llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000669
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000670 llvm::Constant *getMessageSendFixupFn() {
671 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000672 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000673 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000674 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000675 "objc_msgSend_fixup");
676 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000677
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000678 llvm::Constant *getMessageSendFpretFixupFn() {
679 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000680 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000681 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000682 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000683 "objc_msgSend_fpret_fixup");
684 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000685
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000686 llvm::Constant *getMessageSendStretFixupFn() {
687 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000688 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000689 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000690 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000691 "objc_msgSend_stret_fixup");
692 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000693
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000694 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000695 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000696 // struct _super_message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000697 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000698 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000699 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000700 "objc_msgSendSuper2_fixup");
701 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000702
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000703 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000704 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000705 // struct _super_message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000706 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000707 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000708 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000709 "objc_msgSendSuper2_stret_fixup");
710 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000711
Chris Lattnera7c00b42009-04-22 02:15:23 +0000712 llvm::Constant *getObjCEndCatchFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000713 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000714 "objc_end_catch");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000715
Chris Lattnera7c00b42009-04-22 02:15:23 +0000716 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000717
Chris Lattnera7c00b42009-04-22 02:15:23 +0000718 llvm::Constant *getObjCBeginCatchFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000719 llvm::Type *params[] = { Int8PtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000720 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000721 params, false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000722 "objc_begin_catch");
723 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +0000724
Chris Lattnera5f58b02011-07-09 17:41:47 +0000725 llvm::StructType *EHTypeTy;
726 llvm::Type *EHTypePtrTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000727
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000728 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
729 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000730};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000731
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000732class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000733public:
734 // FIXME - accessibility
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000735 class GC_IVAR {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +0000736 public:
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000737 unsigned ivar_bytepos;
738 unsigned ivar_size;
739 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000740 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar22b0ada2009-04-23 01:29:05 +0000741
742 // Allow sorting based on byte pos.
743 bool operator<(const GC_IVAR &b) const {
744 return ivar_bytepos < b.ivar_bytepos;
745 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000746 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000747
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000748 class SKIP_SCAN {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000749 public:
750 unsigned skip;
751 unsigned scan;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000752 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000753 : skip(_skip), scan(_scan) {}
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000754 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000755
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000756protected:
Owen Andersonae86c192009-07-13 04:10:07 +0000757 llvm::LLVMContext &VMContext;
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000758 // FIXME! May not be needing this after all.
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000759 unsigned ObjCABI;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000760
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000761 // gc ivar layout bitmap calculation helper caches.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000762 SmallVector<GC_IVAR, 16> SkipIvars;
763 SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000764
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000765 /// LazySymbols - Symbols to generate a lazy reference for. See
766 /// DefinedSymbols and FinishModule().
Daniel Dunbard027a922009-09-07 00:20:42 +0000767 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000768
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000769 /// DefinedSymbols - External symbols which are defined by this
770 /// module. The symbols in this list and LazySymbols are used to add
771 /// special linker symbols which ensure that Objective-C modules are
772 /// linked properly.
Daniel Dunbard027a922009-09-07 00:20:42 +0000773 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000774
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000775 /// ClassNames - uniqued class names.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000776 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000777
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000778 /// MethodVarNames - uniqued method variable names.
779 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000780
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +0000781 /// DefinedCategoryNames - list of category names in form Class_Category.
782 llvm::SetVector<std::string> DefinedCategoryNames;
783
Daniel Dunbarb036db82008-08-13 03:21:16 +0000784 /// MethodVarTypes - uniqued method type signatures. We have to use
785 /// a StringMap here because have no other unique reference.
786 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000787
Daniel Dunbar3c76cb52008-08-26 21:51:14 +0000788 /// MethodDefinitions - map of methods which have been defined in
789 /// this translation unit.
790 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000791
Daniel Dunbar80a840b2008-08-23 00:19:03 +0000792 /// PropertyNames - uniqued method variable names.
793 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000794
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000795 /// ClassReferences - uniqued class references.
796 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000797
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000798 /// SelectorReferences - uniqued selector references.
799 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000800
Daniel Dunbarb036db82008-08-13 03:21:16 +0000801 /// Protocols - Protocols for which an objc_protocol structure has
802 /// been emitted. Forward declarations are handled by creating an
803 /// empty structure whose initializer is filled in when/if defined.
804 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000805
Daniel Dunbarc475d422008-10-29 22:36:39 +0000806 /// DefinedProtocols - Protocols which have actually been
807 /// defined. We should not need this, see FIXME in GenerateProtocol.
808 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000809
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000810 /// DefinedClasses - List of defined classes.
Bill Wendling8392a472012-02-07 09:40:07 +0000811 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedClasses;
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000812
813 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
Bill Wendling8392a472012-02-07 09:40:07 +0000814 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyClasses;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000815
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000816 /// DefinedCategories - List of defined categories.
Bill Wendling8392a472012-02-07 09:40:07 +0000817 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000818
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000819 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
Bill Wendling8392a472012-02-07 09:40:07 +0000820 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000821
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000822 /// GetNameForMethod - Return a name for the given method.
823 /// \param[out] NameOut - The return value.
824 void GetNameForMethod(const ObjCMethodDecl *OMD,
825 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000826 SmallVectorImpl<char> &NameOut);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000827
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000828 /// GetMethodVarName - Return a unique constant for the given
829 /// selector's name. The return value has type char *.
830 llvm::Constant *GetMethodVarName(Selector Sel);
831 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000832
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000833 /// GetMethodVarType - Return a unique constant for the given
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000834 /// method's type encoding string. The return value has type char *.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000835
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000836 // FIXME: This is a horrible name.
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000837 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D,
838 bool Extended = false);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000839 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000840
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000841 /// GetPropertyName - Return a unique constant for the given
842 /// name. The return value has type char *.
843 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000844
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000845 // FIXME: This can be dropped once string functions are unified.
846 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
847 const Decl *Container);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000848
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +0000849 /// GetClassName - Return a unique constant for the given selector's
850 /// name. The return value has type char *.
851 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000852
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +0000853 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
854
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000855 /// BuildIvarLayout - Builds ivar layout bitmap for the class
856 /// implementation for the __strong or __weak case.
857 ///
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000858 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
859 bool ForStrongLayout);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +0000860
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +0000861 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000862
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000863 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000864 unsigned int BytePos, bool ForStrongLayout,
865 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000866 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000867 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000868 const RecordDecl *RD,
Bill Wendlingf1a3fca2012-02-22 09:30:11 +0000869 ArrayRef<const FieldDecl*> RecFields,
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000870 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000871 bool &HasUnion);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000872
Fariborz Jahanian01dff422009-03-05 19:17:31 +0000873 /// GetIvarLayoutName - Returns a unique constant for the given
874 /// ivar layout bitmap.
875 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
876 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000877
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000878 /// EmitPropertyList - Emit the given property list. The return
879 /// value has type PropertyListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000880 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000881 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000882 const ObjCContainerDecl *OCD,
883 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000884
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000885 /// EmitProtocolMethodTypes - Generate the array of extended method type
886 /// strings. The return value has type Int8PtrPtrTy.
887 llvm::Constant *EmitProtocolMethodTypes(Twine Name,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +0000888 ArrayRef<llvm::Constant*> MethodTypes,
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000889 const ObjCCommonTypesHelper &ObjCTypes);
890
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000891 /// PushProtocolProperties - Push protocol's property on the input stack.
Bill Wendlinga515b582012-02-09 22:16:49 +0000892 void PushProtocolProperties(
893 llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
894 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
895 const Decl *Container,
896 const ObjCProtocolDecl *PROTO,
897 const ObjCCommonTypesHelper &ObjCTypes);
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000898
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000899 /// GetProtocolRef - Return a reference to the internal protocol
900 /// description, creating an empty one if it has not been
901 /// defined. The return value has type ProtocolPtrTy.
902 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanian67726212009-03-08 20:18:37 +0000903
Daniel Dunbar30c65362009-03-09 20:09:19 +0000904 /// CreateMetadataVar - Create a global variable with internal
905 /// linkage for use by the Objective-C runtime.
906 ///
907 /// This is a convenience wrapper which not only creates the
908 /// variable, but also sets the section and alignment and adds the
Chris Lattnerf56501c2009-07-17 23:57:13 +0000909 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000910 ///
911 /// \param Name - The variable name.
912 /// \param Init - The variable initializer; this is also used to
913 /// define the type of the variable.
914 /// \param Section - The section the variable should go into, or 0.
915 /// \param Align - The alignment for the variable, or 0.
916 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar4527d302009-04-14 17:42:51 +0000917 /// "llvm.used".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000918 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +0000919 llvm::Constant *Init,
920 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000921 unsigned Align,
922 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +0000923
John McCall9e8bb002011-05-14 03:10:52 +0000924 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
925 ReturnValueSlot Return,
926 QualType ResultType,
927 llvm::Value *Sel,
928 llvm::Value *Arg0,
929 QualType Arg0Ty,
930 bool IsSuper,
931 const CallArgList &CallArgs,
932 const ObjCMethodDecl *OMD,
933 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000934
Daniel Dunbar5e639272010-04-25 20:39:01 +0000935 /// EmitImageInfo - Emit the image info marker used to encode some module
936 /// level information.
937 void EmitImageInfo();
938
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000939public:
Owen Andersonae86c192009-07-13 04:10:07 +0000940 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
John McCalla729c622012-02-17 03:33:10 +0000941 CGObjCRuntime(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000942
David Chisnall481e3a82010-01-23 02:40:42 +0000943 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Ted Kremeneke65b0862012-03-06 20:05:56 +0000944
Fariborz Jahanian99113fd2009-01-26 21:38:32 +0000945 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
946 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000947
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000948 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000949
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000950 /// GetOrEmitProtocol - Get the protocol object for the given
951 /// declaration, emitting it if necessary. The return value has type
952 /// ProtocolPtrTy.
953 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000954
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000955 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
956 /// object for the given declaration, emitting it if needed. These
957 /// forward references will be filled in with empty bodies if no
958 /// definition is seen. The return value has type ProtocolPtrTy.
959 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall351762c2011-02-07 10:33:21 +0000960 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
961 const CGBlockInfo &blockInfo);
Fariborz Jahanianc05349e2010-08-04 16:57:49 +0000962
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000963};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000964
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000965class CGObjCMac : public CGObjCCommonMac {
966private:
967 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000968
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000969 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000970 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000971 void EmitModuleInfo();
972
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000973 /// EmitModuleSymols - Emit module symbols, the list of defined
974 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000975 llvm::Constant *EmitModuleSymbols();
976
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000977 /// FinishModule - Write out global data structures at the end of
978 /// processing a translation unit.
979 void FinishModule();
Daniel Dunbarb036db82008-08-13 03:21:16 +0000980
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000981 /// EmitClassExtension - Generate the class extension structure used
982 /// to store the weak ivar layout and properties. The return value
983 /// has type ClassExtensionPtrTy.
984 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
985
986 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
987 /// for the given class.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000988 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000989 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +0000990
John McCall31168b02011-06-15 23:02:42 +0000991 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
992 IdentifierInfo *II);
993
994 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
995
Fariborz Jahanianeb80c982009-11-12 20:14:24 +0000996 /// EmitSuperClassRef - Emits reference to class's main metadata class.
997 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000998
999 /// EmitIvarList - Emit the ivar list for the given
1000 /// implementation. If ForClass is true the list of class ivars
1001 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1002 /// interface ivars will be emitted. The return value has type
1003 /// IvarListPtrTy.
1004 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00001005 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001006
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001007 /// EmitMetaClass - Emit a forward reference to the class structure
1008 /// for the metaclass of the given interface. The return value has
1009 /// type ClassPtrTy.
1010 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1011
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001012 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001013 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001014 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1015 llvm::Constant *Protocols,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001016 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001017
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001018 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001019
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001020 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001021
1022 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +00001023 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001024 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00001025 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001026 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001027
1028 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001029 /// method declarations.
Daniel Dunbarb036db82008-08-13 03:21:16 +00001030 /// - TypeName: The name for the type containing the methods.
1031 /// - IsProtocol: True iff these methods are for a protocol.
1032 /// - ClassMethds: True iff these are class methods.
1033 /// - Required: When true, only "required" methods are
1034 /// listed. Similarly, when false only "optional" methods are
1035 /// listed. For classes this should always be true.
1036 /// - begin, end: The method list to output.
1037 ///
1038 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001039 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001040 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001041 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001042
Daniel Dunbarc475d422008-10-29 22:36:39 +00001043 /// GetOrEmitProtocol - Get the protocol object for the given
1044 /// declaration, emitting it if necessary. The return value has type
1045 /// ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001046 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001047
1048 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1049 /// object for the given declaration, emitting it if needed. These
1050 /// forward references will be filled in with empty bodies if no
1051 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001052 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001053
Daniel Dunbarb036db82008-08-13 03:21:16 +00001054 /// EmitProtocolExtension - Generate the protocol extension
1055 /// structure used to store optional instance and class methods, and
1056 /// protocol properties. The return value has type
1057 /// ProtocolExtensionPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001058 llvm::Constant *
1059 EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001060 ArrayRef<llvm::Constant*> OptInstanceMethods,
1061 ArrayRef<llvm::Constant*> OptClassMethods,
1062 ArrayRef<llvm::Constant*> MethodTypesExt);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001063
1064 /// EmitProtocolList - Generate the list of referenced
1065 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001066 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001067 ObjCProtocolDecl::protocol_iterator begin,
1068 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001069
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001070 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1071 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001072 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1073 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001074
1075public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001076 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001077
Fariborz Jahanian71394042009-01-23 23:53:38 +00001078 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001079
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001080 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001081 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001082 QualType ResultType,
1083 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001084 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001085 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001086 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001087 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001088
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001089 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001090 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001091 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001092 QualType ResultType,
1093 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001094 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001095 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001096 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001097 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001098 const CallArgList &CallArgs,
1099 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001100
Daniel Dunbarcb463852008-11-01 01:53:16 +00001101 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001102 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001103
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001104 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1105 bool lval = false);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001106
1107 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1108 /// untyped one.
1109 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1110 const ObjCMethodDecl *Method);
1111
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001112 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001113
Daniel Dunbar92992502008-08-15 22:20:32 +00001114 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001115
Daniel Dunbar92992502008-08-15 22:20:32 +00001116 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001117
Daniel Dunbarf3d3b012012-02-28 15:36:15 +00001118 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall92d436b2012-01-31 18:59:20 +00001119
Daniel Dunbarcb463852008-11-01 01:53:16 +00001120 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001121 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001122
Chris Lattnerd4808922009-03-22 21:03:39 +00001123 virtual llvm::Constant *GetPropertyGetFunction();
1124 virtual llvm::Constant *GetPropertySetFunction();
Ted Kremeneke65b0862012-03-06 20:05:56 +00001125 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1126 bool copy);
David Chisnall168b80f2010-12-26 22:13:16 +00001127 virtual llvm::Constant *GetGetStructFunction();
1128 virtual llvm::Constant *GetSetStructFunction();
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00001129 virtual llvm::Constant *GetCppAtomicObjectFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001130 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001131
John McCallbd309292010-07-06 01:34:17 +00001132 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1133 const ObjCAtTryStmt &S);
1134 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1135 const ObjCAtSynchronizedStmt &S);
1136 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001137 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1138 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001139 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001140 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001141 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001142 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001143 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001144 llvm::Value *src, llvm::Value *dest,
1145 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001146 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001147 llvm::Value *src, llvm::Value *dest,
1148 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001149 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1150 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001151 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1152 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001153 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001154
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001155 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1156 QualType ObjectTy,
1157 llvm::Value *BaseValue,
1158 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001159 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001160 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001161 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001162 const ObjCIvarDecl *Ivar);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001163
1164 /// GetClassGlobal - Return the global variable for the Objective-C
1165 /// class of the given name.
1166 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikie83d382b2011-09-23 05:06:16 +00001167 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001168 }
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001169};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001170
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001171class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001172private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001173 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001174 llvm::GlobalVariable* ObjCEmptyCacheVar;
1175 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001176
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001177 /// SuperClassReferences - uniqued super class references.
1178 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001179
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001180 /// MetaClassReferences - uniqued meta class references.
1181 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001182
1183 /// EHTypeReferences - uniqued class ehtype references.
1184 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001185
John McCall9e8bb002011-05-14 03:10:52 +00001186 /// VTableDispatchMethods - List of methods for which we generate
1187 /// vtable-based message dispatch.
1188 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001189
Fariborz Jahanian67260552009-11-17 21:37:35 +00001190 /// DefinedMetaClasses - List of defined meta-classes.
1191 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1192
John McCall9e8bb002011-05-14 03:10:52 +00001193 /// isVTableDispatchedSelector - Returns true if SEL is a
1194 /// vtable-based selector.
1195 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001196
Fariborz Jahanian71394042009-01-23 23:53:38 +00001197 /// FinishNonFragileABIModule - Write out global data structures at the end of
1198 /// processing a translation unit.
1199 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001200
Daniel Dunbar19573e72009-05-15 21:48:48 +00001201 /// AddModuleClassList - Add the given list of class pointers to the
1202 /// module with the provided symbol and section names.
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001203 void AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00001204 const char *SymbolName,
1205 const char *SectionName);
1206
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001207 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1208 unsigned InstanceStart,
1209 unsigned InstanceSize,
1210 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001211 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001212 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001213 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001214 llvm::Constant *ClassRoGV,
1215 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001216
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001217 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001218
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001219 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001220
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001221 /// EmitMethodList - Emit the method list for the given
1222 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001223 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001224 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001225 ArrayRef<llvm::Constant*> Methods);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001226 /// EmitIvarList - Emit the ivar list for the given
1227 /// implementation. If ForClass is true the list of class ivars
1228 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1229 /// interface ivars will be emitted. The return value has type
1230 /// IvarListnfABIPtrTy.
1231 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001232
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001233 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001234 const ObjCIvarDecl *Ivar,
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00001235 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001236
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001237 /// GetOrEmitProtocol - Get the protocol object for the given
1238 /// declaration, emitting it if necessary. The return value has type
1239 /// ProtocolPtrTy.
1240 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001241
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001242 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1243 /// object for the given declaration, emitting it if needed. These
1244 /// forward references will be filled in with empty bodies if no
1245 /// definition is seen. The return value has type ProtocolPtrTy.
1246 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001247
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001248 /// EmitProtocolList - Generate the list of referenced
1249 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001250 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001251 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001252 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001253
John McCall9e8bb002011-05-14 03:10:52 +00001254 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1255 ReturnValueSlot Return,
1256 QualType ResultType,
1257 Selector Sel,
1258 llvm::Value *Receiver,
1259 QualType Arg0Ty,
1260 bool IsSuper,
1261 const CallArgList &CallArgs,
1262 const ObjCMethodDecl *Method);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001263
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001264 /// GetClassGlobal - Return the global variable for the Objective-C
1265 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001266 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001267
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001268 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001269 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001270 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001271 const ObjCInterfaceDecl *ID);
John McCall31168b02011-06-15 23:02:42 +00001272
1273 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1274 IdentifierInfo *II);
1275
1276 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001277
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001278 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1279 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001280 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1281 const ObjCInterfaceDecl *ID);
1282
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001283 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1284 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001285 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001286 const ObjCInterfaceDecl *ID);
1287
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001288 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1289 /// the given ivar.
1290 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001291 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001292 const ObjCInterfaceDecl *ID,
1293 const ObjCIvarDecl *Ivar);
1294
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001295 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1296 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001297 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1298 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001299
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001300 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001301 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001302 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001303 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001304
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001305 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001306 return "OBJC_METACLASS_$_";
1307 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001308
Daniel Dunbar15894b72009-04-07 05:48:37 +00001309 const char *getClassSymbolPrefix() const {
1310 return "OBJC_CLASS_$_";
1311 }
1312
Daniel Dunbar961202372009-05-03 12:57:56 +00001313 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001314 uint32_t &InstanceStart,
1315 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001316
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001317 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001318 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001319 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1320 return CGM.getContext().Selectors.getSelector(0, &II);
1321 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001322
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001323 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001324 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1325 return CGM.getContext().Selectors.getSelector(1, &II);
1326 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001327
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001328 /// ImplementationIsNonLazy - Check whether the given category or
1329 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001330 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001331
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001332public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001333 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001334 // FIXME. All stubs for now!
1335 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001336
Fariborz Jahanian71394042009-01-23 23:53:38 +00001337 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001338 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001339 QualType ResultType,
1340 Selector Sel,
1341 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001342 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001343 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001344 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001345
1346 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001347 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001348 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001349 QualType ResultType,
1350 Selector Sel,
1351 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001352 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001353 llvm::Value *Receiver,
1354 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001355 const CallArgList &CallArgs,
1356 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001357
Fariborz Jahanian71394042009-01-23 23:53:38 +00001358 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001359 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001360
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001361 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1362 bool lvalue = false)
1363 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001364
1365 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1366 /// untyped one.
1367 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1368 const ObjCMethodDecl *Method)
1369 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001370
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001371 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001372
Fariborz Jahanian71394042009-01-23 23:53:38 +00001373 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
David Chisnall92d436b2012-01-31 18:59:20 +00001374
Daniel Dunbarf3d3b012012-02-28 15:36:15 +00001375 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {}
David Chisnall92d436b2012-01-31 18:59:20 +00001376
Fariborz Jahanian71394042009-01-23 23:53:38 +00001377 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001378 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001379
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001380 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001381
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001382 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001383 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001384 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001385 virtual llvm::Constant *GetPropertySetFunction() {
1386 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001387 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001388
Ted Kremeneke65b0862012-03-06 20:05:56 +00001389 virtual llvm::Constant *GetOptimizedPropertySetFunction(bool atomic,
1390 bool copy) {
1391 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
1392 }
1393
David Chisnall168b80f2010-12-26 22:13:16 +00001394 virtual llvm::Constant *GetSetStructFunction() {
1395 return ObjCTypes.getCopyStructFn();
1396 }
1397 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001398 return ObjCTypes.getCopyStructFn();
1399 }
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00001400 virtual llvm::Constant *GetCppAtomicObjectFunction() {
1401 return ObjCTypes.getCppAtomicObjectFunction();
1402 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001403
Chris Lattnerd4808922009-03-22 21:03:39 +00001404 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001405 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001406 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001407
John McCallbd309292010-07-06 01:34:17 +00001408 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1409 const ObjCAtTryStmt &S);
1410 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1411 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001412 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001413 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001414 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001415 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001416 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001417 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001418 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001419 llvm::Value *src, llvm::Value *dest,
1420 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001421 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001422 llvm::Value *src, llvm::Value *dest,
1423 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001424 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001425 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001426 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1427 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001428 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001429 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1430 QualType ObjectTy,
1431 llvm::Value *BaseValue,
1432 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001433 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001434 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001435 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001436 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001437};
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001438
1439/// A helper class for performing the null-initialization of a return
1440/// value.
1441struct NullReturnState {
1442 llvm::BasicBlock *NullBB;
1443 llvm::BasicBlock *callBB;
1444 NullReturnState() : NullBB(0), callBB(0) {}
1445
1446 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1447 // Make blocks for the null-init and call edges.
1448 NullBB = CGF.createBasicBlock("msgSend.nullinit");
1449 callBB = CGF.createBasicBlock("msgSend.call");
1450
1451 // Check for a null receiver and, if there is one, jump to the
1452 // null-init test.
1453 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1454 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1455
1456 // Otherwise, start performing the call.
1457 CGF.EmitBlock(callBB);
1458 }
1459
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001460 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType,
1461 const CallArgList &CallArgs,
1462 const ObjCMethodDecl *Method) {
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001463 if (!NullBB) return result;
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001464
1465 llvm::Value *NullInitPtr = 0;
1466 if (result.isScalar() && !resultType->isVoidType()) {
1467 NullInitPtr = CGF.CreateTempAlloca(result.getScalarVal()->getType());
1468 CGF.Builder.CreateStore(result.getScalarVal(), NullInitPtr);
1469 }
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001470
1471 // Finish the call path.
1472 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1473 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1474
1475 // Emit the null-init block and perform the null-initialization there.
1476 CGF.EmitBlock(NullBB);
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001477
1478 // Release consumed arguments along the null-receiver path.
1479 if (Method) {
1480 CallArgList::const_iterator I = CallArgs.begin();
1481 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1482 e = Method->param_end(); i != e; ++i, ++I) {
1483 const ParmVarDecl *ParamDecl = (*i);
1484 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1485 RValue RV = I->RV;
1486 assert(RV.isScalar() &&
1487 "NullReturnState::complete - arg not on object");
1488 CGF.EmitARCRelease(RV.getScalarVal(), true);
1489 }
1490 }
1491 }
1492
1493 if (result.isScalar()) {
1494 if (NullInitPtr)
1495 CGF.EmitNullInitialization(NullInitPtr, resultType);
1496 // Jump to the continuation block.
1497 CGF.EmitBlock(contBB);
1498 return NullInitPtr ? RValue::get(CGF.Builder.CreateLoad(NullInitPtr))
1499 : result;
1500 }
1501
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001502 if (!resultType->isAnyComplexType()) {
1503 assert(result.isAggregate() && "null init of non-aggregate result?");
1504 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1505 // Jump to the continuation block.
1506 CGF.EmitBlock(contBB);
1507 return result;
1508 }
1509
1510 // _Complex type
1511 // FIXME. Now easy to handle any other scalar type whose result is returned
1512 // in memory due to ABI limitations.
1513 CGF.EmitBlock(contBB);
1514 CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal();
1515 llvm::Type *MemberType = CallCV.first->getType();
1516 llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType);
1517 // Create phi instruction for scalar complex value.
1518 llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2);
1519 PHIReal->addIncoming(ZeroCV, NullBB);
1520 PHIReal->addIncoming(CallCV.first, callBB);
1521 llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2);
1522 PHIImag->addIncoming(ZeroCV, NullBB);
1523 PHIImag->addIncoming(CallCV.second, callBB);
1524 return RValue::getComplex(PHIReal, PHIImag);
1525 }
1526};
1527
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001528} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001529
1530/* *** Helper Functions *** */
1531
1532/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001533static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001534 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001535 unsigned idx0,
1536 unsigned idx1) {
1537 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001538 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1539 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001540 };
Jay Foaded8db7d2011-07-21 14:31:17 +00001541 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001542}
1543
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001544/// hasObjCExceptionAttribute - Return true if this class or any super
1545/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001546static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001547 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001548 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001549 return true;
1550 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001551 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001552 return false;
1553}
1554
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001555/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001556
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001557CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001558 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001559 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001560 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001561}
1562
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001563/// GetClass - Return a reference to the class for the given interface
1564/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001565llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001566 const ObjCInterfaceDecl *ID) {
1567 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001568}
1569
1570/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001571llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1572 bool lval) {
1573 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001574}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001575llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001576 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001577 return EmitSelector(Builder, Method->getSelector());
1578}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001579
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001580llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001581 if (T->isObjCIdType() ||
1582 T->isObjCQualifiedIdType()) {
1583 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001584 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001585 }
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001586 if (T->isObjCClassType() ||
1587 T->isObjCQualifiedClassType()) {
1588 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001589 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001590 }
1591 if (T->isObjCObjectPointerType())
1592 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1593
John McCall2ca705e2010-07-24 00:37:23 +00001594 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
John McCall2ca705e2010-07-24 00:37:23 +00001595}
1596
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001597/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001598/*
1599 struct __builtin_CFString {
1600 const int *isa; // point to __CFConstantStringClassReference
1601 int flags;
1602 const char *str;
1603 long length;
1604 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001605*/
1606
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001607/// or Generate a constant NSString object.
1608/*
1609 struct __builtin_NSString {
1610 const int *isa; // point to __NSConstantStringClassReference
1611 const char *str;
1612 unsigned int length;
1613 };
1614*/
1615
Fariborz Jahanian71394042009-01-23 23:53:38 +00001616llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001617 const StringLiteral *SL) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001618 return (CGM.getLangOpts().NoConstantCFStrings == 0 ?
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001619 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian50c925f2010-10-19 17:19:29 +00001620 CGM.GetAddrOfConstantString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001621}
1622
Ted Kremeneke65b0862012-03-06 20:05:56 +00001623enum {
1624 kCFTaggedObjectID_Integer = (1 << 1) + 1
1625};
1626
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001627/// Generates a message send where the super is the receiver. This is
1628/// a message send to self with special delivery semantics indicating
1629/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001630CodeGen::RValue
1631CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001632 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001633 QualType ResultType,
1634 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001635 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001636 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001637 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001638 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001639 const CodeGen::CallArgList &CallArgs,
1640 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001641 // Create and init a super structure; this is a (receiver, class)
1642 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001643 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00001644 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001645 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001646 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001647 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001648 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001649
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001650 // If this is a class message the metaclass is passed as the target.
1651 llvm::Value *Target;
1652 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001653 if (isCategoryImpl) {
1654 // Message sent to 'super' in a class method defined in a category
1655 // implementation requires an odd treatment.
1656 // If we are in a class method, we must retrieve the
1657 // _metaclass_ for the current class, pointed at by
1658 // the class's "isa" pointer. The following assumes that
1659 // isa" is the first ivar in a class (which it must be).
1660 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1661 Target = CGF.Builder.CreateStructGEP(Target, 0);
1662 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001663 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001664 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1665 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1666 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1667 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001668 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001669 }
1670 else if (isCategoryImpl)
1671 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1672 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001673 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1674 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1675 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001676 }
Mike Stump18bb9282009-05-16 07:57:57 +00001677 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1678 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00001679 llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001680 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001681 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001682 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001683 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall9e8bb002011-05-14 03:10:52 +00001684 return EmitMessageSend(CGF, Return, ResultType,
1685 EmitSelector(CGF.Builder, Sel),
1686 ObjCSuper, ObjCTypes.SuperPtrCTy,
1687 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001688}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001689
1690/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001691CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001692 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001693 QualType ResultType,
1694 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001695 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001696 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001697 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001698 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00001699 return EmitMessageSend(CGF, Return, ResultType,
1700 EmitSelector(CGF.Builder, Sel),
1701 Receiver, CGF.getContext().getObjCIdType(),
1702 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001703}
1704
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001705CodeGen::RValue
John McCall9e8bb002011-05-14 03:10:52 +00001706CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1707 ReturnValueSlot Return,
1708 QualType ResultType,
1709 llvm::Value *Sel,
1710 llvm::Value *Arg0,
1711 QualType Arg0Ty,
1712 bool IsSuper,
1713 const CallArgList &CallArgs,
1714 const ObjCMethodDecl *Method,
1715 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001716 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001717 if (!IsSuper)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00001718 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman43dca6a2011-05-02 17:57:46 +00001719 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1720 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCall31168b02011-06-15 23:02:42 +00001721 ActualArgs.addFrom(CallArgs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001722
John McCalla729c622012-02-17 03:33:10 +00001723 // If we're calling a method, use the formal signature.
1724 MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001725
Anders Carlsson280e61f12010-06-21 20:59:55 +00001726 if (Method)
1727 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1728 CGM.getContext().getCanonicalType(ResultType) &&
1729 "Result type mismatch!");
1730
John McCall5880fb82011-05-14 21:12:11 +00001731 NullReturnState nullReturn;
1732
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001733 llvm::Constant *Fn = NULL;
John McCalla729c622012-02-17 03:33:10 +00001734 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001735 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001736 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001737 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001738 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1739 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1740 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlsson2f1a6c32011-10-31 16:27:11 +00001741 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1742 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1743 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001744 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001745 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001746 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001747 }
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001748
1749 bool requiresnullCheck = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00001750 if (CGM.getLangOpts().ObjCAutoRefCount && Method)
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001751 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1752 e = Method->param_end(); i != e; ++i) {
1753 const ParmVarDecl *ParamDecl = (*i);
1754 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1755 if (!nullReturn.NullBB)
1756 nullReturn.init(CGF, Arg0);
1757 requiresnullCheck = true;
1758 break;
1759 }
1760 }
1761
John McCalla729c622012-02-17 03:33:10 +00001762 Fn = llvm::ConstantExpr::getBitCast(Fn, MSI.MessengerType);
1763 RValue rvalue = CGF.EmitCall(MSI.CallInfo, Fn, Return, ActualArgs);
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001764 return nullReturn.complete(CGF, rvalue, ResultType, CallArgs,
1765 requiresnullCheck ? Method : 0);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001766}
1767
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001768static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1769 if (FQT.isObjCGCStrong())
1770 return Qualifiers::Strong;
1771
John McCall31168b02011-06-15 23:02:42 +00001772 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001773 return Qualifiers::Weak;
1774
Fariborz Jahanian430b35e2012-02-16 00:15:02 +00001775 // check for __unsafe_unretained
1776 if (FQT.getObjCLifetime() == Qualifiers::OCL_ExplicitNone)
1777 return Qualifiers::GCNone;
1778
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001779 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1780 return Qualifiers::Strong;
1781
1782 if (const PointerType *PT = FQT->getAs<PointerType>())
1783 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1784
1785 return Qualifiers::GCNone;
1786}
1787
John McCall351762c2011-02-07 10:33:21 +00001788llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1789 const CGBlockInfo &blockInfo) {
Chris Lattnerece04092012-02-07 00:39:47 +00001790 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
John McCall351762c2011-02-07 10:33:21 +00001791
David Blaikiebbafb8a2012-03-11 07:00:24 +00001792 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
1793 !CGM.getLangOpts().ObjCAutoRefCount)
John McCall351762c2011-02-07 10:33:21 +00001794 return nullPtr;
1795
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001796 bool hasUnion = false;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001797 SkipIvars.clear();
1798 IvarsInfo.clear();
Douglas Gregore8bbc122011-09-02 00:18:52 +00001799 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1800 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001801
Fariborz Jahaniancfddabf2010-09-09 00:21:45 +00001802 // __isa is the first field in block descriptor and must assume by runtime's
1803 // convention that it is GC'able.
1804 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall351762c2011-02-07 10:33:21 +00001805
1806 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1807
1808 // Calculate the basic layout of the block structure.
1809 const llvm::StructLayout *layout =
1810 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1811
1812 // Ignore the optional 'this' capture: C++ objects are not assumed
1813 // to be GC'ed.
1814
1815 // Walk the captured variables.
1816 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1817 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1818 const VarDecl *variable = ci->getVariable();
1819 QualType type = variable->getType();
1820
1821 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1822
1823 // Ignore constant captures.
1824 if (capture.isConstant()) continue;
1825
1826 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1827
1828 // __block variables are passed by their descriptor address.
1829 if (ci->isByRef()) {
1830 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanian933c6722010-09-11 01:27:29 +00001831 continue;
John McCall351762c2011-02-07 10:33:21 +00001832 }
1833
1834 assert(!type->isArrayType() && "array variable should not be caught");
1835 if (const RecordType *record = type->getAs<RecordType>()) {
1836 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001837 continue;
1838 }
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001839
John McCall351762c2011-02-07 10:33:21 +00001840 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1841 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1842
1843 if (GCAttr == Qualifiers::Strong)
1844 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1845 fieldSize / WordSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001846 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall351762c2011-02-07 10:33:21 +00001847 SkipIvars.push_back(GC_IVAR(fieldOffset,
1848 fieldSize / ByteSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001849 }
1850
1851 if (IvarsInfo.empty())
John McCall351762c2011-02-07 10:33:21 +00001852 return nullPtr;
1853
1854 // Sort on byte position; captures might not be allocated in order,
1855 // and unions can do funny things.
1856 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1857 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001858
1859 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00001860 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001861 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001862 printf("\n block variable layout for block: ");
1863 const unsigned char *s = (unsigned char*)BitMap.c_str();
Bill Wendling53136852012-02-07 09:06:01 +00001864 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001865 if (!(s[i] & 0xf0))
1866 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1867 else
1868 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1869 printf("\n");
1870 }
1871
1872 return C;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001873}
1874
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001875llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001876 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001877 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001878 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001879 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1880
Owen Andersonade90fd2009-07-29 18:54:39 +00001881 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Douglas Gregor020de322012-01-17 18:36:30 +00001882 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001883}
1884
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001885void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00001886 // FIXME: We shouldn't need this, the protocol decl should contain enough
1887 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00001888 DefinedProtocols.insert(PD->getIdentifier());
1889
1890 // If we have generated a forward reference to this protocol, emit
1891 // it now. Otherwise do nothing, the protocol objects are lazily
1892 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001893 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001894 GetOrEmitProtocol(PD);
1895}
1896
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001897llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001898 if (DefinedProtocols.count(PD->getIdentifier()))
1899 return GetOrEmitProtocol(PD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001900
Daniel Dunbarc475d422008-10-29 22:36:39 +00001901 return GetOrEmitProtocolRef(PD);
1902}
1903
Daniel Dunbarb036db82008-08-13 03:21:16 +00001904/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001905// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1906struct _objc_protocol {
1907struct _objc_protocol_extension *isa;
1908char *protocol_name;
1909struct _objc_protocol_list *protocol_list;
1910struct _objc__method_prototype_list *instance_methods;
1911struct _objc__method_prototype_list *class_methods
1912};
Daniel Dunbarb036db82008-08-13 03:21:16 +00001913
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001914See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001915*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00001916llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
John McCallf9582a72012-03-30 21:29:05 +00001917 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbarc475d422008-10-29 22:36:39 +00001918
1919 // Early exit if a defining object has already been generated.
1920 if (Entry && Entry->hasInitializer())
1921 return Entry;
1922
Douglas Gregora715bff2012-01-01 19:51:50 +00001923 // Use the protocol definition, if there is one.
1924 if (const ObjCProtocolDecl *Def = PD->getDefinition())
1925 PD = Def;
1926
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001927 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001928 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001929 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1930
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001931 // Construct method lists.
1932 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1933 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001934 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001935 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001936 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001937 ObjCMethodDecl *MD = *i;
1938 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001939 if (!C)
1940 return GetOrEmitProtocolRef(PD);
1941
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001942 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1943 OptInstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001944 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001945 } else {
1946 InstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001947 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001948 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001949 }
1950
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001951 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001952 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001953 ObjCMethodDecl *MD = *i;
1954 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001955 if (!C)
1956 return GetOrEmitProtocolRef(PD);
1957
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001958 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1959 OptClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001960 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001961 } else {
1962 ClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001963 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001964 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001965 }
1966
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001967 MethodTypesExt.insert(MethodTypesExt.end(),
1968 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
1969
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001970 llvm::Constant *Values[] = {
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001971 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
1972 MethodTypesExt),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001973 GetClassName(PD->getIdentifier()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001974 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00001975 PD->protocol_begin(),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001976 PD->protocol_end()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001977 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001978 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001979 InstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001980 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001981 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001982 ClassMethods)
1983 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00001984 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001985 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001986
Daniel Dunbarb036db82008-08-13 03:21:16 +00001987 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001988 // Already created, fix the linkage and update the initializer.
1989 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001990 Entry->setInitializer(Init);
1991 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001992 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001993 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001994 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001995 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001996 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001997 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001998 // FIXME: Is this necessary? Why only for protocol?
1999 Entry->setAlignment(4);
John McCallf9582a72012-03-30 21:29:05 +00002000
2001 Protocols[PD->getIdentifier()] = Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002002 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00002003 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00002004
2005 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002006}
2007
Daniel Dunbarc475d422008-10-29 22:36:39 +00002008llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002009 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
2010
2011 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00002012 // We use the initializer as a marker of whether this is a forward
2013 // reference or not. At module finalization we add the empty
2014 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002015 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002016 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00002017 llvm::GlobalValue::ExternalLinkage,
2018 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002019 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00002020 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00002021 // FIXME: Is this necessary? Why only for protocol?
2022 Entry->setAlignment(4);
2023 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002024
Daniel Dunbarb036db82008-08-13 03:21:16 +00002025 return Entry;
2026}
2027
2028/*
2029 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002030 uint32_t size;
2031 struct objc_method_description_list *optional_instance_methods;
2032 struct objc_method_description_list *optional_class_methods;
2033 struct objc_property_list *instance_properties;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002034 const char ** extendedMethodTypes;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002035 };
2036*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002037llvm::Constant *
2038CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002039 ArrayRef<llvm::Constant*> OptInstanceMethods,
2040 ArrayRef<llvm::Constant*> OptClassMethods,
2041 ArrayRef<llvm::Constant*> MethodTypesExt) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002042 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002043 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002044 llvm::Constant *Values[] = {
2045 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002046 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002047 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002048 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002049 OptInstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002050 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002051 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002052 OptClassMethods),
2053 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002054 ObjCTypes),
2055 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
2056 MethodTypesExt, ObjCTypes)
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002057 };
Daniel Dunbarb036db82008-08-13 03:21:16 +00002058
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002059 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002060 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002061 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002062 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002063
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002064 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002065 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002066
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002067 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002068 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002069 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002070 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002071}
2072
2073/*
2074 struct objc_protocol_list {
Bill Wendlinga515b582012-02-09 22:16:49 +00002075 struct objc_protocol_list *next;
2076 long count;
2077 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002078 };
2079*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00002080llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002081CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00002082 ObjCProtocolDecl::protocol_iterator begin,
2083 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendlinga515b582012-02-09 22:16:49 +00002084 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002085
Daniel Dunbardec75f82008-08-21 21:57:41 +00002086 for (; begin != end; ++begin)
2087 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00002088
2089 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002090 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002091 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002092
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002093 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00002094 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00002095
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002096 llvm::Constant *Values[3];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002097 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00002098 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002099 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002100 ProtocolRefs.size() - 1);
2101 Values[2] =
2102 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
2103 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00002104 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002105
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002106 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002107 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002108 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002109 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00002110 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002111}
2112
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002113void CGObjCCommonMac::
2114PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet,
Bill Wendlinga515b582012-02-09 22:16:49 +00002115 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002116 const Decl *Container,
2117 const ObjCProtocolDecl *PROTO,
2118 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002119 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
2120 E = PROTO->protocol_end(); P != E; ++P)
2121 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2122 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2123 E = PROTO->prop_end(); I != E; ++I) {
David Blaikie40ed2972012-06-06 20:45:41 +00002124 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002125 if (!PropertySet.insert(PD->getIdentifier()))
2126 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002127 llvm::Constant *Prop[] = {
2128 GetPropertyName(PD->getIdentifier()),
2129 GetPropertyTypeString(PD, Container)
2130 };
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002131 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2132 }
2133}
2134
Daniel Dunbarb036db82008-08-13 03:21:16 +00002135/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002136 struct _objc_property {
Bill Wendlinga515b582012-02-09 22:16:49 +00002137 const char * const name;
2138 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002139 };
2140
2141 struct _objc_property_list {
Bill Wendlinga515b582012-02-09 22:16:49 +00002142 uint32_t entsize; // sizeof (struct _objc_property)
2143 uint32_t prop_count;
2144 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002145 };
2146*/
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002147llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002148 const Decl *Container,
2149 const ObjCContainerDecl *OCD,
2150 const ObjCCommonTypesHelper &ObjCTypes) {
Bill Wendlinga515b582012-02-09 22:16:49 +00002151 llvm::SmallVector<llvm::Constant*, 16> Properties;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002152 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002153 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2154 E = OCD->prop_end(); I != E; ++I) {
David Blaikie40ed2972012-06-06 20:45:41 +00002155 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002156 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002157 llvm::Constant *Prop[] = {
2158 GetPropertyName(PD->getIdentifier()),
2159 GetPropertyTypeString(PD, Container)
2160 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002161 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002162 Prop));
2163 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002164 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002165 for (ObjCInterfaceDecl::all_protocol_iterator
2166 P = OID->all_referenced_protocol_begin(),
2167 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002168 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2169 ObjCTypes);
2170 }
2171 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2172 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2173 E = CD->protocol_end(); P != E; ++P)
2174 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2175 ObjCTypes);
2176 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002177
2178 // Return null for empty list.
2179 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002180 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002181
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002182 unsigned PropertySize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002183 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002184 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002185 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2186 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002187 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002188 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002189 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002190 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002191
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002192 llvm::GlobalVariable *GV =
2193 CreateMetadataVar(Name, Init,
2194 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002195 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002196 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002197 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002198 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002199}
2200
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002201llvm::Constant *
2202CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2203 ArrayRef<llvm::Constant*> MethodTypes,
2204 const ObjCCommonTypesHelper &ObjCTypes) {
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002205 // Return null for empty list.
2206 if (MethodTypes.empty())
2207 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2208
2209 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2210 MethodTypes.size());
2211 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2212
2213 llvm::GlobalVariable *GV =
2214 CreateMetadataVar(Name, Init,
2215 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2216 (ObjCABI == 2) ? 8 : 4,
2217 true);
2218 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2219}
2220
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002221/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00002222 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002223 int count;
2224 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002225 };
2226*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002227llvm::Constant *
2228CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002229 llvm::Constant *Desc[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002230 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002231 ObjCTypes.SelectorPtrTy),
2232 GetMethodVarType(MD)
2233 };
Douglas Gregora9d84932011-05-27 01:19:52 +00002234 if (!Desc[1])
2235 return 0;
2236
Owen Anderson0e0189d2009-07-27 22:29:56 +00002237 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002238 Desc);
2239}
Daniel Dunbarb036db82008-08-13 03:21:16 +00002240
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002241llvm::Constant *
2242CGObjCMac::EmitMethodDescList(Twine Name, const char *Section,
2243 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002244 // Return null for empty list.
2245 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002246 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002247
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002248 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002249 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002250 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002251 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002252 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002253 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002254
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002255 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002256 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002257 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002258}
2259
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002260/*
2261 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002262 char *category_name;
2263 char *class_name;
2264 struct _objc_method_list *instance_methods;
2265 struct _objc_method_list *class_methods;
2266 struct _objc_protocol_list *protocols;
2267 uint32_t size; // <rdar://4585769>
2268 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002269 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002270*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002271void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002272 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002273
Mike Stump18bb9282009-05-16 07:57:57 +00002274 // FIXME: This is poor design, the OCD should have a pointer to the category
2275 // decl. Additionally, note that Category can be null for the @implementation
2276 // w/o an @interface case. Sema should just create one for us as it does for
2277 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002278 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002279 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002280 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002281
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002282 SmallString<256> ExtName;
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002283 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2284 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002285
Bill Wendlinga515b582012-02-09 22:16:49 +00002286 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002287 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002288 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002289 // Instance methods should always be defined.
2290 InstanceMethods.push_back(GetMethodConstant(*i));
2291 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002292 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002293 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002294 // Class methods should always be defined.
2295 ClassMethods.push_back(GetMethodConstant(*i));
2296 }
2297
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002298 llvm::Constant *Values[7];
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002299 Values[0] = GetClassName(OCD->getIdentifier());
2300 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002301 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002302 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002303 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002304 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002305 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002306 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002307 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002308 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002309 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002310 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002311 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002312 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002313 Category->protocol_begin(),
2314 Category->protocol_end());
2315 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002316 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002317 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002318 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002319
2320 // If there is no category @interface then there can be no properties.
2321 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002322 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002323 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002324 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002325 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002326 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002327
Owen Anderson0e0189d2009-07-27 22:29:56 +00002328 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002329 Values);
2330
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002331 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002332 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002333 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002334 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002335 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002336 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002337 // method definition entries must be clear for next implementation.
2338 MethodDefinitions.clear();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002339}
2340
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002341// FIXME: Get from somewhere?
2342enum ClassFlags {
2343 eClassFlags_Factory = 0x00001,
2344 eClassFlags_Meta = 0x00002,
2345 // <rdr://5142207>
2346 eClassFlags_HasCXXStructors = 0x02000,
2347 eClassFlags_Hidden = 0x20000,
2348 eClassFlags_ABI2_Hidden = 0x00010,
2349 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2350};
2351
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002352/*
2353 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002354 Class isa;
2355 Class super_class;
2356 const char *name;
2357 long version;
2358 long info;
2359 long instance_size;
2360 struct _objc_ivar_list *ivars;
2361 struct _objc_method_list *methods;
2362 struct _objc_cache *cache;
2363 struct _objc_protocol_list *protocols;
2364 // Objective-C 1.0 extensions (<rdr://4585769>)
2365 const char *ivar_layout;
2366 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002367 };
2368
2369 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002370*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002371void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002372 DefinedSymbols.insert(ID->getIdentifier());
2373
Chris Lattner86d7d912008-11-24 03:54:41 +00002374 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002375 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002376 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002377 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002378 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002379 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002380 Interface->all_referenced_protocol_begin(),
2381 Interface->all_referenced_protocol_end());
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002382 unsigned Flags = eClassFlags_Factory;
John McCall31168b02011-06-15 23:02:42 +00002383 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00002384 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002385 unsigned Size =
Ken Dyckc8ae5502011-02-09 01:59:34 +00002386 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002387
2388 // FIXME: Set CXX-structors flag.
John McCall457a04e2010-10-22 21:05:15 +00002389 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002390 Flags |= eClassFlags_Hidden;
2391
Bill Wendlinga515b582012-02-09 22:16:49 +00002392 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002393 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002394 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002395 // Instance methods should always be defined.
2396 InstanceMethods.push_back(GetMethodConstant(*i));
2397 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002398 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002399 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002400 // Class methods should always be defined.
2401 ClassMethods.push_back(GetMethodConstant(*i));
2402 }
2403
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002404 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002405 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +00002406 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002407
2408 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2409 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2410
2411 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2412 if (llvm::Constant *C = GetMethodConstant(MD))
2413 InstanceMethods.push_back(C);
2414 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2415 if (llvm::Constant *C = GetMethodConstant(MD))
2416 InstanceMethods.push_back(C);
2417 }
2418 }
2419
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002420 llvm::Constant *Values[12];
Daniel Dunbarccf61832009-05-03 08:56:52 +00002421 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002422 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002423 // Record a reference to the super class.
2424 LazySymbols.insert(Super->getIdentifier());
2425
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002426 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002427 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002428 ObjCTypes.ClassPtrTy);
2429 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002430 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002431 }
2432 Values[ 2] = GetClassName(ID->getIdentifier());
2433 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002434 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2435 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2436 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002437 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002438 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002439 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002440 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002441 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002442 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002443 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002444 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002445 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002446 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002447 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002448 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002449 std::string Name("\01L_OBJC_CLASS_");
2450 Name += ClassName;
2451 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2452 // Check for a forward reference.
2453 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2454 if (GV) {
2455 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2456 "Forward metaclass reference has incorrect type.");
2457 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2458 GV->setInitializer(Init);
2459 GV->setSection(Section);
2460 GV->setAlignment(4);
2461 CGM.AddUsedGlobal(GV);
2462 }
2463 else
2464 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002465 DefinedClasses.push_back(GV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002466 // method definition entries must be clear for next implementation.
2467 MethodDefinitions.clear();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002468}
2469
2470llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2471 llvm::Constant *Protocols,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002472 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002473 unsigned Flags = eClassFlags_Meta;
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002474 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002475
John McCall457a04e2010-10-22 21:05:15 +00002476 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002477 Flags |= eClassFlags_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002478
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002479 llvm::Constant *Values[12];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002480 // The isa for the metaclass is the root of the hierarchy.
2481 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2482 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2483 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002484 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002485 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002486 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002487 // The super class for the metaclass is emitted as the name of the
2488 // super class. The runtime fixes this up to point to the
2489 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002490 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002491 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002492 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002493 ObjCTypes.ClassPtrTy);
2494 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002495 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002496 }
2497 Values[ 2] = GetClassName(ID->getIdentifier());
2498 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002499 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2500 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2501 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002502 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002503 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002504 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002505 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002506 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002507 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002508 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002509 Values[ 9] = Protocols;
2510 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002511 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002512 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002513 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002514 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002515 Values);
2516
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002517 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner86d7d912008-11-24 03:54:41 +00002518 Name += ID->getNameAsCString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002519
2520 // Check for a forward reference.
2521 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2522 if (GV) {
2523 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2524 "Forward metaclass reference has incorrect type.");
2525 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2526 GV->setInitializer(Init);
2527 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002528 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002529 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002530 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002531 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002532 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002533 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002534 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002535
2536 return GV;
2537}
2538
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002539llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002540 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002541
Mike Stump18bb9282009-05-16 07:57:57 +00002542 // FIXME: Should we look these up somewhere other than the module. Its a bit
2543 // silly since we only generate these while processing an implementation, so
2544 // exactly one pointer would work if know when we entered/exitted an
2545 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002546
2547 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00002548 // Previously, metaclass with internal linkage may have been defined.
2549 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002550 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2551 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002552 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2553 "Forward metaclass reference has incorrect type.");
2554 return GV;
2555 } else {
2556 // Generate as an external reference to keep a consistent
2557 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00002558 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002559 llvm::GlobalValue::ExternalLinkage,
2560 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002561 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002562 }
2563}
2564
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002565llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2566 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2567
2568 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2569 true)) {
2570 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2571 "Forward class metadata reference has incorrect type.");
2572 return GV;
2573 } else {
2574 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2575 llvm::GlobalValue::ExternalLinkage,
2576 0,
2577 Name);
2578 }
2579}
2580
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002581/*
2582 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002583 uint32_t size;
2584 const char *weak_ivar_layout;
2585 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002586 };
2587*/
2588llvm::Constant *
2589CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002590 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002591 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002592
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002593 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002594 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002595 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002596 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002597 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002598
2599 // Return null if no extension bits are used.
2600 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002601 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002602
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002603 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002604 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002605 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002606 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002607 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002608}
2609
2610/*
2611 struct objc_ivar {
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00002612 char *ivar_name;
2613 char *ivar_type;
2614 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002615 };
2616
2617 struct objc_ivar_list {
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00002618 int ivar_count;
2619 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002620 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002621*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002622llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002623 bool ForClass) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002624 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002625
2626 // When emitting the root class GCC emits ivar entries for the
2627 // actual class structure. It is not clear if we need to follow this
2628 // behavior; for now lets try and get away with not doing it. If so,
2629 // the cleanest solution would be to make up an ObjCInterfaceDecl
2630 // for the class.
2631 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00002632 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002633
Jordy Rosea91768e2011-07-22 02:08:32 +00002634 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002635
Jordy Rosea91768e2011-07-22 02:08:32 +00002636 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00002637 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002638 // Ignore unnamed bit-fields.
2639 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002640 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002641 llvm::Constant *Ivar[] = {
2642 GetMethodVarName(IVD->getIdentifier()),
2643 GetMethodVarType(IVD),
2644 llvm::ConstantInt::get(ObjCTypes.IntTy,
2645 ComputeIvarBaseOffset(CGM, OID, IVD))
2646 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002647 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002648 }
2649
2650 // Return null for empty list.
2651 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002652 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002653
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002654 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002655 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002656 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002657 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002658 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002659 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002660
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002661 llvm::GlobalVariable *GV;
2662 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002663 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002664 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002665 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002666 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002667 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002668 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002669 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002670 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002671}
2672
2673/*
2674 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002675 SEL method_name;
2676 char *method_types;
2677 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002678 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002679
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002680 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002681 struct objc_method_list *obsolete;
2682 int count;
2683 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002684 };
2685*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002686
2687/// GetMethodConstant - Return a struct objc_method constant for the
2688/// given method if it has been defined. The result is null if the
2689/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002690llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00002691 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002692 if (!Fn)
2693 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002694
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002695 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00002696 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002697 ObjCTypes.SelectorPtrTy),
2698 GetMethodVarType(MD),
2699 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
2700 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002701 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002702}
2703
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002704llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002705 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002706 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002707 // Return null for empty list.
2708 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002709 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002710
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002711 llvm::Constant *Values[3];
Owen Anderson0b75f232009-07-31 20:28:54 +00002712 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002713 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002714 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002715 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002716 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002717 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002718
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002719 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002720 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002721}
2722
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002723llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002724 const ObjCContainerDecl *CD) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002725 SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002726 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002727
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002728 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2192fe52011-07-18 04:24:23 +00002729 llvm::FunctionType *MethodTy =
John McCalla729c622012-02-17 03:33:10 +00002730 Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002731 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002732 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002733 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002734 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002735 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002736 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002737
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002738 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002739}
2740
Daniel Dunbar30c65362009-03-09 20:09:19 +00002741llvm::GlobalVariable *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002742CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002743 llvm::Constant *Init,
2744 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002745 unsigned Align,
2746 bool AddToUsed) {
Chris Lattner2192fe52011-07-18 04:24:23 +00002747 llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002748 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002749 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002750 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002751 if (Section)
2752 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002753 if (Align)
2754 GV->setAlignment(Align);
2755 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002756 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002757 return GV;
2758}
2759
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002760llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002761 // Abuse this interface function as a place to finalize.
2762 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002763 return NULL;
2764}
2765
Chris Lattnerd4808922009-03-22 21:03:39 +00002766llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002767 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002768}
2769
Chris Lattnerd4808922009-03-22 21:03:39 +00002770llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002771 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002772}
2773
Ted Kremeneke65b0862012-03-06 20:05:56 +00002774llvm::Constant *CGObjCMac::GetOptimizedPropertySetFunction(bool atomic,
2775 bool copy) {
2776 return ObjCTypes.getOptimizedSetPropertyFn(atomic, copy);
2777}
2778
David Chisnall168b80f2010-12-26 22:13:16 +00002779llvm::Constant *CGObjCMac::GetGetStructFunction() {
2780 return ObjCTypes.getCopyStructFn();
2781}
2782llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002783 return ObjCTypes.getCopyStructFn();
2784}
2785
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00002786llvm::Constant *CGObjCMac::GetCppAtomicObjectFunction() {
2787 return ObjCTypes.getCppAtomicObjectFunction();
2788}
2789
Chris Lattnerd4808922009-03-22 21:03:39 +00002790llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002791 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002792}
2793
John McCallbd309292010-07-06 01:34:17 +00002794void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2795 return EmitTryOrSynchronizedStmt(CGF, S);
2796}
2797
2798void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2799 const ObjCAtSynchronizedStmt &S) {
2800 return EmitTryOrSynchronizedStmt(CGF, S);
2801}
2802
John McCall65bea082010-07-21 06:59:36 +00002803namespace {
John McCallcda666c2010-07-21 07:22:38 +00002804 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00002805 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00002806 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00002807 llvm::Value *CallTryExitVar;
2808 llvm::Value *ExceptionData;
2809 ObjCTypesHelper &ObjCTypes;
2810 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00002811 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00002812 llvm::Value *CallTryExitVar,
2813 llvm::Value *ExceptionData,
2814 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00002815 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00002816 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2817
John McCall30317fd2011-07-12 20:27:29 +00002818 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall65bea082010-07-21 06:59:36 +00002819 // Check whether we need to call objc_exception_try_exit.
2820 // In optimized code, this branch will always be folded.
2821 llvm::BasicBlock *FinallyCallExit =
2822 CGF.createBasicBlock("finally.call_exit");
2823 llvm::BasicBlock *FinallyNoCallExit =
2824 CGF.createBasicBlock("finally.no_call_exit");
2825 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2826 FinallyCallExit, FinallyNoCallExit);
2827
2828 CGF.EmitBlock(FinallyCallExit);
2829 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2830 ->setDoesNotThrow();
2831
2832 CGF.EmitBlock(FinallyNoCallExit);
2833
2834 if (isa<ObjCAtTryStmt>(S)) {
2835 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCallcebe0ca2010-08-11 00:16:14 +00002836 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2837 // Save the current cleanup destination in case there's
2838 // control flow inside the finally statement.
2839 llvm::Value *CurCleanupDest =
2840 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2841
John McCall65bea082010-07-21 06:59:36 +00002842 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2843
John McCallcebe0ca2010-08-11 00:16:14 +00002844 if (CGF.HaveInsertPoint()) {
2845 CGF.Builder.CreateStore(CurCleanupDest,
2846 CGF.getNormalCleanupDestSlot());
2847 } else {
2848 // Currently, the end of the cleanup must always exist.
2849 CGF.EnsureInsertPoint();
2850 }
2851 }
John McCall65bea082010-07-21 06:59:36 +00002852 } else {
2853 // Emit objc_sync_exit(expr); as finally's sole statement for
2854 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00002855 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00002856 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2857 ->setDoesNotThrow();
2858 }
2859 }
2860 };
John McCall42227ed2010-07-31 23:20:56 +00002861
2862 class FragileHazards {
2863 CodeGenFunction &CGF;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002864 SmallVector<llvm::Value*, 20> Locals;
John McCall42227ed2010-07-31 23:20:56 +00002865 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2866
2867 llvm::InlineAsm *ReadHazard;
2868 llvm::InlineAsm *WriteHazard;
2869
2870 llvm::FunctionType *GetAsmFnType();
2871
2872 void collectLocals();
2873 void emitReadHazard(CGBuilderTy &Builder);
2874
2875 public:
2876 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00002877
John McCall42227ed2010-07-31 23:20:56 +00002878 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00002879 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00002880 };
2881}
2882
2883/// Create the fragile-ABI read and write hazards based on the current
2884/// state of the function, which is presumed to be immediately prior
2885/// to a @try block. These hazards are used to maintain correct
2886/// semantics in the face of optimization and the fragile ABI's
2887/// cavalier use of setjmp/longjmp.
2888FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2889 collectLocals();
2890
2891 if (Locals.empty()) return;
2892
2893 // Collect all the blocks in the function.
2894 for (llvm::Function::iterator
2895 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2896 BlocksBeforeTry.insert(&*I);
2897
2898 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2899
2900 // Create a read hazard for the allocas. This inhibits dead-store
2901 // optimizations and forces the values to memory. This hazard is
2902 // inserted before any 'throwing' calls in the protected scope to
2903 // reflect the possibility that the variables might be read from the
2904 // catch block if the call throws.
2905 {
2906 std::string Constraint;
2907 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2908 if (I) Constraint += ',';
2909 Constraint += "*m";
2910 }
2911
2912 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2913 }
2914
2915 // Create a write hazard for the allocas. This inhibits folding
2916 // loads across the hazard. This hazard is inserted at the
2917 // beginning of the catch path to reflect the possibility that the
2918 // variables might have been written within the protected scope.
2919 {
2920 std::string Constraint;
2921 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2922 if (I) Constraint += ',';
2923 Constraint += "=*m";
2924 }
2925
2926 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2927 }
2928}
2929
2930/// Emit a write hazard at the current location.
2931void FragileHazards::emitWriteHazard() {
2932 if (Locals.empty()) return;
2933
Jay Foad5bd375a2011-07-15 08:37:34 +00002934 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall42227ed2010-07-31 23:20:56 +00002935}
2936
John McCall42227ed2010-07-31 23:20:56 +00002937void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2938 assert(!Locals.empty());
Jay Foad5bd375a2011-07-15 08:37:34 +00002939 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall42227ed2010-07-31 23:20:56 +00002940}
2941
2942/// Emit read hazards in all the protected blocks, i.e. all the blocks
2943/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00002944void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00002945 if (Locals.empty()) return;
2946
2947 CGBuilderTy Builder(CGF.getLLVMContext());
2948
2949 // Iterate through all blocks, skipping those prior to the try.
2950 for (llvm::Function::iterator
2951 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2952 llvm::BasicBlock &BB = *FI;
2953 if (BlocksBeforeTry.count(&BB)) continue;
2954
2955 // Walk through all the calls in the block.
2956 for (llvm::BasicBlock::iterator
2957 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2958 llvm::Instruction &I = *BI;
2959
2960 // Ignore instructions that aren't non-intrinsic calls.
2961 // These are the only calls that can possibly call longjmp.
2962 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2963 if (isa<llvm::IntrinsicInst>(I))
2964 continue;
2965
2966 // Ignore call sites marked nounwind. This may be questionable,
2967 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2968 llvm::CallSite CS(&I);
2969 if (CS.doesNotThrow()) continue;
2970
John McCall2dd7d442010-08-04 05:59:32 +00002971 // Insert a read hazard before the call. This will ensure that
2972 // any writes to the locals are performed before making the
2973 // call. If the call throws, then this is sufficient to
2974 // guarantee correctness as long as it doesn't also write to any
2975 // locals.
John McCall42227ed2010-07-31 23:20:56 +00002976 Builder.SetInsertPoint(&BB, BI);
2977 emitReadHazard(Builder);
2978 }
2979 }
2980}
2981
2982static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2983 if (V) S.insert(V);
2984}
2985
2986void FragileHazards::collectLocals() {
2987 // Compute a set of allocas to ignore.
2988 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2989 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2990 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall42227ed2010-07-31 23:20:56 +00002991
2992 // Collect all the allocas currently in the function. This is
2993 // probably way too aggressive.
2994 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2995 for (llvm::BasicBlock::iterator
2996 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2997 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2998 Locals.push_back(&*I);
2999}
3000
3001llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003002 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall9dc0db22011-05-15 01:53:33 +00003003 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
3004 tys[i] = Locals[i]->getType();
3005 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCall65bea082010-07-21 06:59:36 +00003006}
3007
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003008/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003009
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003010 Objective-C setjmp-longjmp (sjlj) Exception Handling
3011 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003012
John McCallbd309292010-07-06 01:34:17 +00003013 A catch buffer is a setjmp buffer plus:
3014 - a pointer to the exception that was caught
3015 - a pointer to the previous exception data buffer
3016 - two pointers of reserved storage
3017 Therefore catch buffers form a stack, with a pointer to the top
3018 of the stack kept in thread-local storage.
3019
3020 objc_exception_try_enter pushes a catch buffer onto the EH stack.
3021 objc_exception_try_exit pops the given catch buffer, which is
3022 required to be the top of the EH stack.
3023 objc_exception_throw pops the top of the EH stack, writes the
3024 thrown exception into the appropriate field, and longjmps
3025 to the setjmp buffer. It crashes the process (with a printf
3026 and an abort()) if there are no catch buffers on the stack.
3027 objc_exception_extract just reads the exception pointer out of the
3028 catch buffer.
3029
3030 There's no reason an implementation couldn't use a light-weight
3031 setjmp here --- something like __builtin_setjmp, but API-compatible
3032 with the heavyweight setjmp. This will be more important if we ever
3033 want to implement correct ObjC/C++ exception interactions for the
3034 fragile ABI.
3035
3036 Note that for this use of setjmp/longjmp to be correct, we may need
3037 to mark some local variables volatile: if a non-volatile local
3038 variable is modified between the setjmp and the longjmp, it has
3039 indeterminate value. For the purposes of LLVM IR, it may be
3040 sufficient to make loads and stores within the @try (to variables
3041 declared outside the @try) volatile. This is necessary for
3042 optimized correctness, but is not currently being done; this is
3043 being tracked as rdar://problem/8160285
3044
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003045 The basic framework for a @try-catch-finally is as follows:
3046 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003047 objc_exception_data d;
3048 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00003049 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003050
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003051 objc_exception_try_enter(&d);
3052 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003053 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003054 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003055 // exception path
3056 id _caught = objc_exception_extract(&d);
3057
3058 // enter new try scope for handlers
3059 if (!setjmp(d.jmp_buf)) {
3060 ... match exception and execute catch blocks ...
3061
3062 // fell off end, rethrow.
3063 _rethrow = _caught;
3064 ... jump-through-finally to finally_rethrow ...
3065 } else {
3066 // exception in catch block
3067 _rethrow = objc_exception_extract(&d);
3068 _call_try_exit = false;
3069 ... jump-through-finally to finally_rethrow ...
3070 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003071 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003072 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003073
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003074 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00003075 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003076 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00003077
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003078 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003079 ... dispatch to finally destination ...
3080
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003081 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003082 objc_exception_throw(_rethrow);
3083
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003084 finally_end:
3085 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003086
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003087 This framework differs slightly from the one gcc uses, in that gcc
3088 uses _rethrow to determine if objc_exception_try_exit should be called
3089 and if the object should be rethrown. This breaks in the face of
3090 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003091
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003092 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003093
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003094 - If there are no catch blocks, then we avoid emitting the second
3095 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003096
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003097 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
3098 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003099
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003100 - FIXME: If there is no @finally block we can do a few more
3101 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003102
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003103 Rethrows and Jumps-Through-Finally
3104 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003105
John McCallbd309292010-07-06 01:34:17 +00003106 '@throw;' is supported by pushing the currently-caught exception
3107 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003108
John McCallbd309292010-07-06 01:34:17 +00003109 Branches through the @finally block are handled with an ordinary
3110 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
3111 exceptions are not compatible with C++ exceptions, and this is
3112 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003113
John McCallbd309292010-07-06 01:34:17 +00003114 @synchronized(expr) { stmt; } is emitted as if it were:
3115 id synch_value = expr;
3116 objc_sync_enter(synch_value);
3117 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003118*/
3119
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00003120void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
3121 const Stmt &S) {
3122 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00003123
3124 // A destination for the fall-through edges of the catch handlers to
3125 // jump to.
3126 CodeGenFunction::JumpDest FinallyEnd =
3127 CGF.getJumpDestInCurrentScope("finally.end");
3128
3129 // A destination for the rethrow edge of the catch handlers to jump
3130 // to.
3131 CodeGenFunction::JumpDest FinallyRethrow =
3132 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003133
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003134 // For @synchronized, call objc_sync_enter(sync.expr). The
3135 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00003136 // @synchronized. We can't avoid a temp here because we need the
3137 // value to be preserved. If the backend ever does liveness
3138 // correctly after setjmp, this will be unnecessary.
3139 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003140 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00003141 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003142 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
3143 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00003144 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
3145 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00003146
3147 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3148 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003149 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003150
John McCall2dd7d442010-08-04 05:59:32 +00003151 // Allocate memory for the setjmp buffer. This needs to be kept
3152 // live throughout the try and catch blocks.
3153 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3154 "exceptiondata.ptr");
3155
John McCall42227ed2010-07-31 23:20:56 +00003156 // Create the fragile hazards. Note that this will not capture any
3157 // of the allocas required for exception processing, but will
3158 // capture the current basic block (which extends all the way to the
3159 // setjmp call) as "before the @try".
3160 FragileHazards Hazards(CGF);
3161
John McCallbd309292010-07-06 01:34:17 +00003162 // Create a flag indicating whether the cleanup needs to call
3163 // objc_exception_try_exit. This is true except when
3164 // - no catches match and we're branching through the cleanup
3165 // just to rethrow the exception, or
3166 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00003167 // The setjmp-safety rule here is that we should always store to this
3168 // variable in a place that dominates the branch through the cleanup
3169 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00003170 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00003171 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003172
John McCall9916e3f2010-10-04 23:42:51 +00003173 // A slot containing the exception to rethrow. Only needed when we
3174 // have both a @catch and a @finally.
3175 llvm::Value *PropagatingExnVar = 0;
3176
John McCallbd309292010-07-06 01:34:17 +00003177 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00003178 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00003179 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00003180 CallTryExitVar,
3181 ExceptionData,
3182 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00003183
3184 // Enter a try block:
3185 // - Call objc_exception_try_enter to push ExceptionData on top of
3186 // the EH stack.
3187 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3188 ->setDoesNotThrow();
3189
3190 // - Call setjmp on the exception data buffer.
3191 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3192 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3193 llvm::Value *SetJmpBuffer =
Jay Foad040dd822011-07-22 08:16:57 +00003194 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallbd309292010-07-06 01:34:17 +00003195 llvm::CallInst *SetJmpResult =
3196 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3197 SetJmpResult->setDoesNotThrow();
Bill Wendlingbd26cf92011-12-19 23:53:28 +00003198 SetJmpResult->setCanReturnTwice();
John McCallbd309292010-07-06 01:34:17 +00003199
3200 // If setjmp returned 0, enter the protected block; otherwise,
3201 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00003202 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3203 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00003204 llvm::Value *DidCatch =
John McCallcebe0ca2010-08-11 00:16:14 +00003205 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3206 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003207
John McCallbd309292010-07-06 01:34:17 +00003208 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003209 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00003210 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003211 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00003212 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00003213
3214 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003215
John McCallbd309292010-07-06 01:34:17 +00003216 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00003217 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003218
John McCall42227ed2010-07-31 23:20:56 +00003219 // Don't optimize loads of the in-scope locals across this point.
3220 Hazards.emitWriteHazard();
3221
John McCallbd309292010-07-06 01:34:17 +00003222 // For a @synchronized (or a @try with no catches), just branch
3223 // through the cleanup to the rethrow block.
3224 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3225 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00003226 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003227 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00003228
3229 // Otherwise, we have to match against the caught exceptions.
3230 } else {
John McCall2dd7d442010-08-04 05:59:32 +00003231 // Retrieve the exception object. We may emit multiple blocks but
3232 // nothing can cross this so the value is already in SSA form.
3233 llvm::CallInst *Caught =
3234 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3235 ExceptionData, "caught");
3236 Caught->setDoesNotThrow();
3237
John McCallbd309292010-07-06 01:34:17 +00003238 // Push the exception to rethrow onto the EH value stack for the
3239 // benefit of any @throws in the handlers.
3240 CGF.ObjCEHValueStack.push_back(Caught);
3241
Douglas Gregor96c79492010-04-23 22:50:49 +00003242 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003243
John McCall2dd7d442010-08-04 05:59:32 +00003244 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00003245
John McCall2dd7d442010-08-04 05:59:32 +00003246 llvm::BasicBlock *CatchBlock = 0;
3247 llvm::BasicBlock *CatchHandler = 0;
3248 if (HasFinally) {
John McCall9916e3f2010-10-04 23:42:51 +00003249 // Save the currently-propagating exception before
3250 // objc_exception_try_enter clears the exception slot.
3251 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3252 "propagating_exception");
3253 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3254
John McCall2dd7d442010-08-04 05:59:32 +00003255 // Enter a new exception try block (in case a @catch block
3256 // throws an exception).
3257 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3258 ->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003259
John McCall2dd7d442010-08-04 05:59:32 +00003260 llvm::CallInst *SetJmpResult =
3261 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3262 "setjmp.result");
3263 SetJmpResult->setDoesNotThrow();
Bill Wendlingbd26cf92011-12-19 23:53:28 +00003264 SetJmpResult->setCanReturnTwice();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003265
John McCall2dd7d442010-08-04 05:59:32 +00003266 llvm::Value *Threw =
3267 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3268
3269 CatchBlock = CGF.createBasicBlock("catch");
3270 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3271 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3272
3273 CGF.EmitBlock(CatchBlock);
3274 }
3275
3276 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003277
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003278 // Handle catch list. As a special case we check if everything is
3279 // matched and avoid generating code for falling off the end if
3280 // so.
3281 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003282 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3283 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003284
Douglas Gregor46a572b2010-04-26 16:46:50 +00003285 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003286 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003287
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003288 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003289 if (!CatchParam) {
3290 AllMatched = true;
3291 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003292 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003293
John McCallbd309292010-07-06 01:34:17 +00003294 // catch(id e) always matches under this ABI, since only
3295 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003296 // FIXME: For the time being we also match id<X>; this should
3297 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003298 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003299 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003300 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003301
John McCallbd309292010-07-06 01:34:17 +00003302 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003303 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003304 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3305
Anders Carlsson9396a892008-09-11 09:15:33 +00003306 if (CatchParam) {
John McCall1c9c3fd2010-10-15 04:57:14 +00003307 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003308 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003309
3310 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003311 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003312 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003313
Anders Carlsson9396a892008-09-11 09:15:33 +00003314 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003315
3316 // The scope of the catch variable ends right here.
3317 CatchVarCleanups.ForceCleanup();
3318
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003319 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003320 break;
3321 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003322
Steve Naroff7cae42b2009-07-10 23:34:53 +00003323 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00003324 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00003325
3326 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00003327 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3328 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003329
3330 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00003331 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003332
John McCallbd309292010-07-06 01:34:17 +00003333 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00003334 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3335 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00003336 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003337
John McCallbd309292010-07-06 01:34:17 +00003338 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3339 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003340
3341 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00003342 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003343
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003344 // Emit the @catch block.
3345 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00003346
3347 // Collect any cleanups for the catch variable. The scope lasts until
3348 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00003349 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00003350
John McCall1c9c3fd2010-10-15 04:57:14 +00003351 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003352 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003353
John McCallbd309292010-07-06 01:34:17 +00003354 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003355 llvm::Value *Tmp =
3356 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003357 CGF.ConvertType(CatchParam->getType()));
Steve Naroff371b8fb2009-03-03 19:52:17 +00003358 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003359
Anders Carlsson9396a892008-09-11 09:15:33 +00003360 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003361
3362 // We're done with the catch variable.
3363 CatchVarCleanups.ForceCleanup();
3364
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003365 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003366
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003367 CGF.EmitBlock(NextCatchBlock);
3368 }
3369
John McCallbd309292010-07-06 01:34:17 +00003370 CGF.ObjCEHValueStack.pop_back();
3371
John McCall2dd7d442010-08-04 05:59:32 +00003372 // If nothing wanted anything to do with the caught exception,
3373 // kill the extract call.
3374 if (Caught->use_empty())
3375 Caught->eraseFromParent();
3376
3377 if (!AllMatched)
3378 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3379
3380 if (HasFinally) {
3381 // Emit the exception handler for the @catch blocks.
3382 CGF.EmitBlock(CatchHandler);
3383
3384 // In theory we might now need a write hazard, but actually it's
3385 // unnecessary because there's no local-accessing code between
3386 // the try's write hazard and here.
3387 //Hazards.emitWriteHazard();
3388
John McCall9916e3f2010-10-04 23:42:51 +00003389 // Extract the new exception and save it to the
3390 // propagating-exception slot.
3391 assert(PropagatingExnVar);
3392 llvm::CallInst *NewCaught =
3393 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3394 ExceptionData, "caught");
3395 NewCaught->setDoesNotThrow();
3396 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3397
John McCall2dd7d442010-08-04 05:59:32 +00003398 // Don't pop the catch handler; the throw already did.
3399 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003400 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003401 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003402 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003403
John McCall42227ed2010-07-31 23:20:56 +00003404 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00003405 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003406
John McCallbd309292010-07-06 01:34:17 +00003407 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00003408 CGF.Builder.restoreIP(TryFallthroughIP);
3409 if (CGF.HaveInsertPoint())
3410 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00003411 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00003412 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003413
John McCallbd309292010-07-06 01:34:17 +00003414 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00003415 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00003416 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00003417 if (CGF.HaveInsertPoint()) {
John McCall9916e3f2010-10-04 23:42:51 +00003418 // If we have a propagating-exception variable, check it.
3419 llvm::Value *PropagatingExn;
3420 if (PropagatingExnVar) {
3421 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall2dd7d442010-08-04 05:59:32 +00003422
John McCall9916e3f2010-10-04 23:42:51 +00003423 // Otherwise, just look in the buffer for the exception to throw.
3424 } else {
3425 llvm::CallInst *Caught =
3426 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3427 ExceptionData);
3428 Caught->setDoesNotThrow();
3429 PropagatingExn = Caught;
3430 }
3431
3432 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallbd309292010-07-06 01:34:17 +00003433 ->setDoesNotThrow();
3434 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00003435 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003436
John McCall42227ed2010-07-31 23:20:56 +00003437 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003438}
3439
3440void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003441 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00003442 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003443
Anders Carlssone005aa12008-09-09 16:16:55 +00003444 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00003445 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003446 ExceptionAsObject =
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003447 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlssone005aa12008-09-09 16:16:55 +00003448 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003449 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003450 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00003451 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00003452 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003453
John McCallbd309292010-07-06 01:34:17 +00003454 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3455 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003456 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003457
3458 // Clear the insertion point to indicate we are in unreachable code.
3459 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003460}
3461
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003462/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003463/// object: objc_read_weak (id *src)
3464///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003465llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003466 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003467 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003468 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3469 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3470 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00003471 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003472 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00003473 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003474 return read_weak;
3475}
3476
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003477/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3478/// objc_assign_weak (id src, id *dst)
3479///
3480void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003481 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003482 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003483 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003484 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003485 assert(Size <= 8 && "does not support size > 8");
3486 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003487 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003488 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3489 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003490 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3491 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003492 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003493 src, dst, "weakassign");
3494 return;
3495}
3496
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003497/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3498/// objc_assign_global (id src, id *dst)
3499///
3500void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00003501 llvm::Value *src, llvm::Value *dst,
3502 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003503 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003504 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003505 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003506 assert(Size <= 8 && "does not support size > 8");
3507 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003508 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003509 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3510 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003511 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3512 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00003513 if (!threadlocal)
3514 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3515 src, dst, "globalassign");
3516 else
3517 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3518 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003519 return;
3520}
3521
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003522/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003523/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003524///
3525void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003526 llvm::Value *src, llvm::Value *dst,
3527 llvm::Value *ivarOffset) {
3528 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2192fe52011-07-18 04:24:23 +00003529 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003530 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003531 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003532 assert(Size <= 8 && "does not support size > 8");
3533 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003534 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003535 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3536 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003537 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3538 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003539 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3540 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003541 return;
3542}
3543
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003544/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3545/// objc_assign_strongCast (id src, id *dst)
3546///
3547void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003548 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003549 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003550 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003551 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003552 assert(Size <= 8 && "does not support size > 8");
3553 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003554 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003555 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3556 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003557 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3558 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003559 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003560 src, dst, "weakassign");
3561 return;
3562}
3563
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003564void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003565 llvm::Value *DestPtr,
3566 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003567 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003568 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3569 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003570 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003571 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003572 return;
3573}
3574
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003575/// EmitObjCValueForIvar - Code Gen for ivar reference.
3576///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003577LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3578 QualType ObjectTy,
3579 llvm::Value *BaseValue,
3580 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003581 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003582 const ObjCInterfaceDecl *ID =
3583 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003584 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3585 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003586}
3587
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003588llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003589 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003590 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003591 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003592 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003593 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3594 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003595}
3596
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003597/* *** Private Interface *** */
3598
3599/// EmitImageInfo - Emit the image info marker used to encode some module
3600/// level information.
3601///
3602/// See: <rdr://4810609&4810587&4810587>
3603/// struct IMAGE_INFO {
3604/// unsigned version;
3605/// unsigned flags;
3606/// };
3607enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00003608 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003609 eImageInfo_GarbageCollected = (1 << 1),
3610 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003611 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3612
Daniel Dunbar5e639272010-04-25 20:39:01 +00003613 // A flag indicating that the module has no instances of a @synthesize of a
3614 // superclass variable. <rdar://problem/6803242>
Bill Wendling1e60a2c2012-04-24 11:04:57 +00003615 eImageInfo_CorrectedSynthesize = (1 << 4),
3616 eImageInfo_ImageIsSimulated = (1 << 5)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003617};
3618
Daniel Dunbar5e639272010-04-25 20:39:01 +00003619void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003620 unsigned version = 0; // Version is unused?
Bill Wendlingb6f795e2012-02-16 01:13:30 +00003621 const char *Section = (ObjCABI == 1) ?
3622 "__OBJC, __image_info,regular" :
3623 "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003624
Bill Wendlingb6f795e2012-02-16 01:13:30 +00003625 // Generate module-level named metadata to convey this information to the
3626 // linker and code-gen.
3627 llvm::Module &Mod = CGM.getModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003628
Bill Wendlingb6f795e2012-02-16 01:13:30 +00003629 // Add the ObjC ABI version to the module flags.
3630 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI);
3631 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version",
3632 version);
3633 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section",
3634 llvm::MDString::get(VMContext,Section));
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003635
David Blaikiebbafb8a2012-03-11 07:00:24 +00003636 if (CGM.getLangOpts().getGC() == LangOptions::NonGC) {
Bill Wendlingb6f795e2012-02-16 01:13:30 +00003637 // Non-GC overrides those files which specify GC.
3638 Mod.addModuleFlag(llvm::Module::Override,
3639 "Objective-C Garbage Collection", (uint32_t)0);
3640 } else {
3641 // Add the ObjC garbage collection value.
3642 Mod.addModuleFlag(llvm::Module::Error,
3643 "Objective-C Garbage Collection",
3644 eImageInfo_GarbageCollected);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003645
David Blaikiebbafb8a2012-03-11 07:00:24 +00003646 if (CGM.getLangOpts().getGC() == LangOptions::GCOnly) {
Bill Wendlingb6f795e2012-02-16 01:13:30 +00003647 // Add the ObjC GC Only value.
3648 Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only",
3649 eImageInfo_GCOnly);
3650
3651 // Require that GC be specified and set to eImageInfo_GarbageCollected.
3652 llvm::Value *Ops[2] = {
3653 llvm::MDString::get(VMContext, "Objective-C Garbage Collection"),
3654 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
3655 eImageInfo_GarbageCollected)
3656 };
3657 Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only",
3658 llvm::MDNode::get(VMContext, Ops));
3659 }
3660 }
Bill Wendling1e60a2c2012-04-24 11:04:57 +00003661
3662 // Indicate whether we're compiling this to run on a simulator.
3663 const llvm::Triple &Triple = CGM.getTarget().getTriple();
3664 if (Triple.getOS() == llvm::Triple::IOS &&
3665 (Triple.getArch() == llvm::Triple::x86 ||
3666 Triple.getArch() == llvm::Triple::x86_64))
3667 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Is Simulated",
3668 eImageInfo_ImageIsSimulated);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003669}
3670
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003671// struct objc_module {
3672// unsigned long version;
3673// unsigned long size;
3674// const char *name;
3675// Symtab symtab;
3676// };
3677
3678// FIXME: Get from somewhere
3679static const int ModuleVersion = 7;
3680
3681void CGObjCMac::EmitModuleInfo() {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003682 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003683
Benjamin Kramer22d24c22011-10-15 12:20:02 +00003684 llvm::Constant *Values[] = {
3685 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
3686 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
3687 // This used to be the filename, now it is unused. <rdr://4327263>
3688 GetClassName(&CGM.getContext().Idents.get("")),
3689 EmitModuleSymbols()
3690 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003691 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003692 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003693 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003694 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003695}
3696
3697llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003698 unsigned NumClasses = DefinedClasses.size();
3699 unsigned NumCategories = DefinedCategories.size();
3700
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003701 // Return null if no symbols were defined.
3702 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003703 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003704
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003705 llvm::Constant *Values[5];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003706 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003707 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003708 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3709 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003710
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003711 // The runtime expects exactly the list of defined classes followed
3712 // by the list of defined categories, in a single array.
Chris Lattner3def9ae2012-02-06 22:16:34 +00003713 SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003714 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003715 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003716 ObjCTypes.Int8PtrTy);
3717 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003718 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003719 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003720 ObjCTypes.Int8PtrTy);
3721
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003722 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003723 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner3def9ae2012-02-06 22:16:34 +00003724 Symbols.size()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003725 Symbols);
3726
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003727 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003728
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003729 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003730 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3731 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003732 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003733 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003734}
3735
John McCall31168b02011-06-15 23:02:42 +00003736llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3737 IdentifierInfo *II) {
3738 LazySymbols.insert(II);
3739
3740 llvm::GlobalVariable *&Entry = ClassReferences[II];
3741
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003742 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003743 llvm::Constant *Casted =
John McCall31168b02011-06-15 23:02:42 +00003744 llvm::ConstantExpr::getBitCast(GetClassName(II),
3745 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003746 Entry =
John McCall31168b02011-06-15 23:02:42 +00003747 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3748 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3749 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003750 }
John McCall31168b02011-06-15 23:02:42 +00003751
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003752 return Builder.CreateLoad(Entry);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003753}
3754
John McCall31168b02011-06-15 23:02:42 +00003755llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3756 const ObjCInterfaceDecl *ID) {
3757 return EmitClassRefFromId(Builder, ID->getIdentifier());
3758}
3759
3760llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3761 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3762 return EmitClassRefFromId(Builder, II);
3763}
3764
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003765llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3766 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003767 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003768
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003769 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003770 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003771 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003772 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003773 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003774 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3775 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003776 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003777 }
3778
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003779 if (lvalue)
3780 return Entry;
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003781 return Builder.CreateLoad(Entry);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003782}
3783
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003784llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003785 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003786
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003787 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003788 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner9c818332012-02-05 02:30:40 +00003789 llvm::ConstantDataArray::getString(VMContext,
3790 Ident->getNameStart()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003791 ((ObjCABI == 2) ?
3792 "__TEXT,__objc_classname,cstring_literals" :
3793 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003794 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003795
Owen Anderson170229f2009-07-14 23:10:40 +00003796 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003797}
3798
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003799llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3800 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3801 I = MethodDefinitions.find(MD);
3802 if (I != MethodDefinitions.end())
3803 return I->second;
3804
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003805 return NULL;
3806}
3807
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003808/// GetIvarLayoutName - Returns a unique constant for the given
3809/// ivar layout bitmap.
3810llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003811 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003812 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003813}
3814
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003815void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003816 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003817 bool ForStrongLayout,
3818 bool &HasUnion) {
3819 const RecordDecl *RD = RT->getDecl();
3820 // FIXME - Use iterator.
David Blaikie2d7c57e2012-04-30 02:36:29 +00003821 SmallVector<const FieldDecl*, 16> Fields;
3822 for (RecordDecl::field_iterator i = RD->field_begin(),
3823 e = RD->field_end(); i != e; ++i)
David Blaikie40ed2972012-06-06 20:45:41 +00003824 Fields.push_back(*i);
Chris Lattner2192fe52011-07-18 04:24:23 +00003825 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003826 const llvm::StructLayout *RecLayout =
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003827 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003828
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003829 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3830 ForStrongLayout, HasUnion);
3831}
3832
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003833void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003834 const llvm::StructLayout *Layout,
3835 const RecordDecl *RD,
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00003836 ArrayRef<const FieldDecl*> RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003837 unsigned int BytePos, bool ForStrongLayout,
3838 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003839 bool IsUnion = (RD && RD->isUnion());
3840 uint64_t MaxUnionIvarSize = 0;
3841 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosea91768e2011-07-22 02:08:32 +00003842 const FieldDecl *MaxField = 0;
3843 const FieldDecl *MaxSkippedField = 0;
3844 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003845 uint64_t MaxFieldOffset = 0;
3846 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003847 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCall31168b02011-06-15 23:02:42 +00003848 uint64_t FirstFieldDelta = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003849
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003850 if (RecFields.empty())
3851 return;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003852 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
3853 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003854 if (!RD && CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003855 const FieldDecl *FirstField = RecFields[0];
John McCall31168b02011-06-15 23:02:42 +00003856 FirstFieldDelta =
3857 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3858 }
3859
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003860 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003861 const FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003862 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003863 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00003864 // Note that 'i' here is actually the field index inside RD of Field,
3865 // although this dependency is hidden.
3866 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCall31168b02011-06-15 23:02:42 +00003867 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003868 } else
John McCall31168b02011-06-15 23:02:42 +00003869 FieldOffset =
3870 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003871
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003872 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003873 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003874 LastFieldBitfieldOrUnnamed = Field;
3875 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003876 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003877 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003878
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003879 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003880 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003881 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003882 if (FQT->isUnionType())
3883 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003884
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003885 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003886 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003887 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003888 continue;
3889 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003890
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003891 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003892 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003893 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003894 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003895 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003896 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003897 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3898 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003899 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003900 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003901 FQT = CArray->getElementType();
3902 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003903
3904 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003905 "layout for array of unions not supported");
Fariborz Jahanian9a7d57d2011-01-03 19:23:18 +00003906 if (FQT->isRecordType() && ElCount) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003907 int OldIndex = IvarsInfo.size() - 1;
3908 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003909
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003910 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003911 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003912 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003913
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003914 // Replicate layout information for each array element. Note that
3915 // one element is already done.
3916 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003917 for (int FirstIndex = IvarsInfo.size() - 1,
3918 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003919 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003920 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3921 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3922 IvarsInfo[i].ivar_size));
3923 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3924 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3925 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003926 }
3927 continue;
3928 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003929 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003930 // At this point, we are done with Record/Union and array there of.
3931 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00003932 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003933
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003934 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00003935 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3936 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003937 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003938 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003939 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003940 MaxUnionIvarSize = UnionIvarSize;
3941 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003942 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003943 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003944 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003945 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003946 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003947 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003948 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00003949 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3950 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003951 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00003952 // FIXME: Why the asymmetry? We divide by word size in bits on other
3953 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003954 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003955 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003956 MaxSkippedUnionIvarSize = UnionIvarSize;
3957 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003958 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003959 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003960 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003961 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003962 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003963 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003964 }
3965 }
3966 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003967
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003968 if (LastFieldBitfieldOrUnnamed) {
3969 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3970 // Last field was a bitfield. Must update skip info.
Richard Smithcaf33902011-10-10 18:28:20 +00003971 uint64_t BitFieldSize
3972 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003973 GC_IVAR skivar;
3974 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3975 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3976 + ((BitFieldSize % ByteSizeInBits) != 0);
3977 SkipIvars.push_back(skivar);
3978 } else {
3979 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3980 // Last field was unnamed. Must update skip info.
3981 unsigned FieldSize
3982 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3983 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3984 FieldSize / ByteSizeInBits));
3985 }
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003986 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003987
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003988 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003989 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003990 MaxUnionIvarSize));
3991 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00003992 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003993 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003994}
3995
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003996/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3997/// the computations and returning the layout bitmap (for ivar or blocks) in
3998/// the given argument BitMap string container. Routine reads
3999/// two containers, IvarsInfo and SkipIvars which are assumed to be
4000/// filled already by the caller.
Chris Lattner9c818332012-02-05 02:30:40 +00004001llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004002 unsigned int WordsToScan, WordsToSkip;
Chris Lattnerece04092012-02-07 00:39:47 +00004003 llvm::Type *PtrTy = CGM.Int8PtrTy;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004004
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004005 // Build the string of skip/scan nibbles
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004006 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004007 unsigned int WordSize =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004008 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004009 if (IvarsInfo[0].ivar_bytepos == 0) {
4010 WordsToSkip = 0;
4011 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004012 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004013 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
4014 WordsToScan = IvarsInfo[0].ivar_size;
4015 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004016 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004017 unsigned int TailPrevGCObjC =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004018 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004019 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004020 // consecutive 'scanned' object pointers.
4021 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004022 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004023 // Skip over 'gc'able object pointer which lay over each other.
4024 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
4025 continue;
4026 // Must skip over 1 or more words. We save current skip/scan values
4027 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004028 SKIP_SCAN SkScan;
4029 SkScan.skip = WordsToSkip;
4030 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004031 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004032
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004033 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004034 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
4035 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004036 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004037 WordsToSkip = 0;
4038 WordsToScan = IvarsInfo[i].ivar_size;
4039 }
4040 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004041 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004042 SKIP_SCAN SkScan;
4043 SkScan.skip = WordsToSkip;
4044 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004045 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004046 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004047
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004048 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004049 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004050 int LastByteSkipped =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004051 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004052 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004053 int LastByteScanned =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004054 IvarsInfo[LastIndex].ivar_bytepos +
4055 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004056 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00004057 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004058 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00004059 SKIP_SCAN SkScan;
4060 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
4061 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004062 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004063 }
4064 }
4065 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
4066 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004067 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004068 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004069 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
4070 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
4071 // 0xM0 followed by 0x0N detected.
4072 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
4073 for (int j = i+1; j < SkipScan; j++)
4074 SkipScanIvars[j] = SkipScanIvars[j+1];
4075 --SkipScan;
4076 }
4077 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004078
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004079 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004080 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004081 unsigned char byte;
4082 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
4083 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
4084 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
4085 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004086
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004087 // first skip big.
4088 for (unsigned int ix = 0; ix < skip_big; ix++)
4089 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004090
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004091 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004092 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004093 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004094 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004095 byte |= 0xf;
4096 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004097 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004098 byte |= scan_small;
4099 scan_small = 0;
4100 }
4101 BitMap += byte;
4102 }
4103 // next scan big
4104 for (unsigned int ix = 0; ix < scan_big; ix++)
4105 BitMap += (unsigned char)(0x0f);
4106 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004107 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004108 byte = scan_small;
4109 BitMap += byte;
4110 }
4111 }
4112 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00004113 unsigned char zero = 0;
4114 BitMap += zero;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004115
4116 llvm::GlobalVariable * Entry =
4117 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner9c818332012-02-05 02:30:40 +00004118 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004119 ((ObjCABI == 2) ?
4120 "__TEXT,__objc_classname,cstring_literals" :
4121 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004122 1, true);
4123 return getConstantGEP(VMContext, Entry, 0, 0);
4124}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004125
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004126/// BuildIvarLayout - Builds ivar layout bitmap for the class
4127/// implementation for the __strong or __weak case.
4128/// The layout map displays which words in ivar list must be skipped
4129/// and which must be scanned by GC (see below). String is built of bytes.
4130/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
4131/// of words to skip and right nibble is count of words to scan. So, each
4132/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
4133/// represented by a 0x00 byte which also ends the string.
4134/// 1. when ForStrongLayout is true, following ivars are scanned:
4135/// - id, Class
4136/// - object *
4137/// - __strong anything
4138///
4139/// 2. When ForStrongLayout is false, following ivars are scanned:
4140/// - __weak anything
4141///
4142llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
4143 const ObjCImplementationDecl *OMD,
4144 bool ForStrongLayout) {
4145 bool hasUnion = false;
4146
Chris Lattnerece04092012-02-07 00:39:47 +00004147 llvm::Type *PtrTy = CGM.Int8PtrTy;
David Blaikiebbafb8a2012-03-11 07:00:24 +00004148 if (CGM.getLangOpts().getGC() == LangOptions::NonGC &&
4149 !CGM.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004150 return llvm::Constant::getNullValue(PtrTy);
4151
Jordy Rosea91768e2011-07-22 02:08:32 +00004152 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
4153 SmallVector<const FieldDecl*, 32> RecFields;
David Blaikiebbafb8a2012-03-11 07:00:24 +00004154 if (CGM.getLangOpts().ObjCAutoRefCount) {
Jordy Rosea91768e2011-07-22 02:08:32 +00004155 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00004156 IVD; IVD = IVD->getNextIvar())
4157 RecFields.push_back(cast<FieldDecl>(IVD));
4158 }
4159 else {
Jordy Rosea91768e2011-07-22 02:08:32 +00004160 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCall31168b02011-06-15 23:02:42 +00004161 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004162
Jordy Rosea91768e2011-07-22 02:08:32 +00004163 // FIXME: This is not ideal; we shouldn't have to do this copy.
4164 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00004165 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004166
4167 if (RecFields.empty())
4168 return llvm::Constant::getNullValue(PtrTy);
4169
4170 SkipIvars.clear();
4171 IvarsInfo.clear();
4172
4173 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
4174 if (IvarsInfo.empty())
4175 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004176 // Sort on byte position in case we encounterred a union nested in
4177 // the ivar list.
4178 if (hasUnion && !IvarsInfo.empty())
4179 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4180 if (hasUnion && !SkipIvars.empty())
4181 std::sort(SkipIvars.begin(), SkipIvars.end());
4182
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004183 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004184 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004185
David Blaikiebbafb8a2012-03-11 07:00:24 +00004186 if (CGM.getLangOpts().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004187 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00004188 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar56df9772010-08-17 22:39:59 +00004189 OMD->getClassInterface()->getName().data());
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00004190 const unsigned char *s = (unsigned char*)BitMap.c_str();
Bill Wendling53136852012-02-07 09:06:01 +00004191 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00004192 if (!(s[i] & 0xf0))
4193 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4194 else
4195 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4196 printf("\n");
4197 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004198 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00004199}
4200
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004201llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004202 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4203
Chris Lattner3def9ae2012-02-06 22:16:34 +00004204 // FIXME: Avoid std::string in "Sel.getAsString()"
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004205 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004206 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Chris Lattner9c818332012-02-05 02:30:40 +00004207 llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004208 ((ObjCABI == 2) ?
4209 "__TEXT,__objc_methname,cstring_literals" :
4210 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004211 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004212
Owen Anderson170229f2009-07-14 23:10:40 +00004213 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004214}
4215
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004216// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004217llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004218 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4219}
4220
Daniel Dunbarf5c18462009-04-20 06:54:31 +00004221llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004222 std::string TypeStr;
4223 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4224
4225 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00004226
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004227 if (!Entry)
4228 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner9c818332012-02-05 02:30:40 +00004229 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004230 ((ObjCABI == 2) ?
4231 "__TEXT,__objc_methtype,cstring_literals" :
4232 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004233 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004234
Owen Anderson170229f2009-07-14 23:10:40 +00004235 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004236}
4237
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004238llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4239 bool Extended) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004240 std::string TypeStr;
Bill Wendlinge22bef72012-02-09 22:45:21 +00004241 if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended))
Douglas Gregora9d84932011-05-27 01:19:52 +00004242 return 0;
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004243
4244 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4245
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004246 if (!Entry)
4247 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner9c818332012-02-05 02:30:40 +00004248 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004249 ((ObjCABI == 2) ?
4250 "__TEXT,__objc_methtype,cstring_literals" :
4251 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004252 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004253
Owen Anderson170229f2009-07-14 23:10:40 +00004254 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004255}
4256
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004257// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004258llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004259 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004260
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004261 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004262 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Chris Lattner9c818332012-02-05 02:30:40 +00004263 llvm::ConstantDataArray::getString(VMContext,
4264 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004265 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004266 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004267
Owen Anderson170229f2009-07-14 23:10:40 +00004268 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004269}
4270
4271// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00004272// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004273llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004274CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4275 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00004276 std::string TypeStr;
4277 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004278 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4279}
4280
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004281void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004282 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004283 SmallVectorImpl<char> &Name) {
Daniel Dunbard2386812009-10-19 01:21:19 +00004284 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00004285 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00004286 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4287 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004288 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00004289 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer2f569922012-02-07 11:57:45 +00004290 OS << '(' << *CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00004291 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00004292}
4293
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004294void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004295 EmitModuleInfo();
4296
Daniel Dunbarc475d422008-10-29 22:36:39 +00004297 // Emit the dummy bodies for any protocols which were referenced but
4298 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004299 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00004300 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4301 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00004302 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004303
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004304 llvm::Constant *Values[5];
Owen Anderson0b75f232009-07-31 20:28:54 +00004305 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004306 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00004307 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004308 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00004309 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004310 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004311 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00004312 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00004313 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004314 }
4315
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004316 // Add assembler directives to add lazy undefined symbol references
4317 // for classes which are referenced but not defined. This is
4318 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00004319 //
4320 // FIXME: It would be nice if we had an LLVM construct for this.
4321 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004322 SmallString<256> Asm;
Daniel Dunbard027a922009-09-07 00:20:42 +00004323 Asm += CGM.getModule().getModuleInlineAsm();
4324 if (!Asm.empty() && Asm.back() != '\n')
4325 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004326
Daniel Dunbard027a922009-09-07 00:20:42 +00004327 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00004328 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4329 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00004330 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4331 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00004332 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004333 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00004334 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004335 }
4336
Bill Wendling53136852012-02-07 09:06:01 +00004337 for (size_t i = 0, e = DefinedCategoryNames.size(); i < e; ++i) {
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004338 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4339 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4340 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00004341
Daniel Dunbard027a922009-09-07 00:20:42 +00004342 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004343 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004344}
4345
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004346CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004347 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00004348 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004349 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004350 ObjCABI = 2;
4351}
4352
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004353/* *** */
4354
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004355ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Douglas Gregora95f9aa2012-01-17 23:38:32 +00004356 : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0)
4357{
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004358 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4359 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004360
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004361 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004362 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004363 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004364 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Chris Lattnerece04092012-02-07 00:39:47 +00004365 Int8PtrTy = CGM.Int8PtrTy;
4366 Int8PtrPtrTy = CGM.Int8PtrPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004367
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004368 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004369 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004370 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004371
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004372 // I'm not sure I like this. The implicit coordination is a bit
4373 // gross. We should solve this in a reasonable fashion because this
4374 // is a pretty common task (match some runtime data structure with
4375 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004376
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004377 // FIXME: This is leaked.
4378 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004379
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004380 // struct _objc_super {
4381 // id self;
4382 // Class cls;
4383 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00004384 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004385 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004386 SourceLocation(), SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004387 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004388 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith2b013182012-06-10 03:12:00 +00004389 Ctx.getObjCIdType(), 0, 0, false, ICIS_NoInit));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004390 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith2b013182012-06-10 03:12:00 +00004391 Ctx.getObjCClassType(), 0, 0, false,
4392 ICIS_NoInit));
Douglas Gregord5058122010-02-11 01:19:42 +00004393 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004394
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004395 SuperCTy = Ctx.getTagDeclType(RD);
4396 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004397
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004398 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004399 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4400
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004401 // struct _prop_t {
4402 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004403 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004404 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004405 PropertyTy = llvm::StructType::create("struct._prop_t",
4406 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004407
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004408 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004409 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004410 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004411 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004412 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004413 PropertyListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004414 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4415 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004416 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004417 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004418
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004419 // struct _objc_method {
4420 // SEL _cmd;
4421 // char *method_type;
4422 // char *_imp;
4423 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004424 MethodTy = llvm::StructType::create("struct._objc_method",
4425 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
4426 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004427
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004428 // struct _objc_cache *
Chris Lattner5ec04a52011-08-12 17:43:31 +00004429 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004430 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00004431
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004432}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004433
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004434ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004435 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004436 // struct _objc_method_description {
4437 // SEL name;
4438 // char *types;
4439 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004440 MethodDescriptionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004441 llvm::StructType::create("struct._objc_method_description",
4442 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004443
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004444 // struct _objc_method_description_list {
4445 // int count;
4446 // struct _objc_method_description[1];
4447 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004448 MethodDescriptionListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004449 llvm::StructType::create("struct._objc_method_description_list",
4450 IntTy,
4451 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004452
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004453 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004454 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004455 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004456
Daniel Dunbarb036db82008-08-13 03:21:16 +00004457 // Protocol description structures
4458
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004459 // struct _objc_protocol_extension {
4460 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4461 // struct _objc_method_description_list *optional_instance_methods;
4462 // struct _objc_method_description_list *optional_class_methods;
4463 // struct _objc_property_list *instance_properties;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004464 // const char ** extendedMethodTypes;
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004465 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004466 ProtocolExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004467 llvm::StructType::create("struct._objc_protocol_extension",
4468 IntTy, MethodDescriptionListPtrTy,
4469 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004470 Int8PtrPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004471
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004472 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004473 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004474
Daniel Dunbarc475d422008-10-29 22:36:39 +00004475 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00004476
Chris Lattnera5f58b02011-07-09 17:41:47 +00004477 ProtocolTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004478 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbarb036db82008-08-13 03:21:16 +00004479
Chris Lattnera5f58b02011-07-09 17:41:47 +00004480 ProtocolListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004481 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattnera5f58b02011-07-09 17:41:47 +00004482 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004483 LongTy,
Chris Lattnera5f58b02011-07-09 17:41:47 +00004484 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004485 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004486
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004487 // struct _objc_protocol {
4488 // struct _objc_protocol_extension *isa;
4489 // char *protocol_name;
4490 // struct _objc_protocol **_objc_protocol_list;
4491 // struct _objc_method_description_list *instance_methods;
4492 // struct _objc_method_description_list *class_methods;
4493 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004494 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
4495 llvm::PointerType::getUnqual(ProtocolListTy),
4496 MethodDescriptionListPtrTy,
4497 MethodDescriptionListPtrTy,
4498 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004499
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004500 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004501 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004502
Owen Anderson9793f0e2009-07-29 22:16:19 +00004503 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004504
4505 // Class description structures
4506
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004507 // struct _objc_ivar {
4508 // char *ivar_name;
4509 // char *ivar_type;
4510 // int ivar_offset;
4511 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004512 IvarTy = llvm::StructType::create("struct._objc_ivar",
4513 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004514
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004515 // struct _objc_ivar_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004516 IvarListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004517 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004518 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004519
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004520 // struct _objc_method_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004521 MethodListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004522 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004523 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004524
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004525 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004526 ClassExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004527 llvm::StructType::create("struct._objc_class_extension",
4528 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004529 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004530
Chris Lattner5ec04a52011-08-12 17:43:31 +00004531 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004532
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004533 // struct _objc_class {
4534 // Class isa;
4535 // Class super_class;
4536 // char *name;
4537 // long version;
4538 // long info;
4539 // long instance_size;
4540 // struct _objc_ivar_list *ivars;
4541 // struct _objc_method_list *methods;
4542 // struct _objc_cache *cache;
4543 // struct _objc_protocol_list *protocols;
4544 // char *ivar_layout;
4545 // struct _objc_class_ext *ext;
4546 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004547 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
4548 llvm::PointerType::getUnqual(ClassTy),
4549 Int8PtrTy,
4550 LongTy,
4551 LongTy,
4552 LongTy,
4553 IvarListPtrTy,
4554 MethodListPtrTy,
4555 CachePtrTy,
4556 ProtocolListPtrTy,
4557 Int8PtrTy,
4558 ClassExtensionPtrTy,
4559 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004560
Owen Anderson9793f0e2009-07-29 22:16:19 +00004561 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004562
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004563 // struct _objc_category {
4564 // char *category_name;
4565 // char *class_name;
4566 // struct _objc_method_list *instance_method;
4567 // struct _objc_method_list *class_method;
4568 // uint32_t size; // sizeof(struct _objc_category)
4569 // struct _objc_property_list *instance_properties;// category's @property
4570 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004571 CategoryTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004572 llvm::StructType::create("struct._objc_category",
4573 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4574 MethodListPtrTy, ProtocolListPtrTy,
4575 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004576
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004577 // Global metadata structures
4578
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004579 // struct _objc_symtab {
4580 // long sel_ref_cnt;
4581 // SEL *refs;
4582 // short cls_def_cnt;
4583 // short cat_def_cnt;
4584 // char *defs[cls_def_cnt + cat_def_cnt];
4585 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004586 SymtabTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004587 llvm::StructType::create("struct._objc_symtab",
4588 LongTy, SelectorPtrTy, ShortTy, ShortTy,
4589 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004590 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004591
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00004592 // struct _objc_module {
4593 // long version;
4594 // long size; // sizeof(struct _objc_module)
4595 // char *name;
4596 // struct _objc_symtab* symtab;
4597 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004598 ModuleTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004599 llvm::StructType::create("struct._objc_module",
4600 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004601
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004602
Mike Stump18bb9282009-05-16 07:57:57 +00004603 // FIXME: This is the size of the setjmp buffer and should be target
4604 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00004605 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004606
Anders Carlsson9ff22482008-09-09 10:10:21 +00004607 // Exceptions
Chris Lattnerece04092012-02-07 00:39:47 +00004608 llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004609
4610 ExceptionDataTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004611 llvm::StructType::create("struct._objc_exception_data",
Chris Lattnerece04092012-02-07 00:39:47 +00004612 llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize),
4613 StackPtrTy, NULL);
Anders Carlsson9ff22482008-09-09 10:10:21 +00004614
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004615}
4616
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004617ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004618 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004619 // struct _method_list_t {
4620 // uint32_t entsize; // sizeof(struct _objc_method)
4621 // uint32_t method_count;
4622 // struct _objc_method method_list[method_count];
4623 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004624 MethodListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004625 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
4626 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004627 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004628 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004629
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004630 // struct _protocol_t {
4631 // id isa; // NULL
4632 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004633 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004634 // const struct method_list_t * const instance_methods;
4635 // const struct method_list_t * const class_methods;
4636 // const struct method_list_t *optionalInstanceMethods;
4637 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004638 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004639 // const uint32_t size; // sizeof(struct _protocol_t)
4640 // const uint32_t flags; // = 0
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004641 // const char ** extendedMethodTypes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004642 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004643
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004644 // Holder for struct _protocol_list_t *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004645 ProtocolListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004646 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004647
Chris Lattnera5f58b02011-07-09 17:41:47 +00004648 ProtocolnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004649 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
4650 llvm::PointerType::getUnqual(ProtocolListnfABITy),
4651 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4652 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004653 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
4654 NULL);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004655
4656 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004657 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004658
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004659 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004660 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004661 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004662 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004663 ProtocolListnfABITy->setBody(LongTy,
4664 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
4665 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004666
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004667 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004668 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004669
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004670 // struct _ivar_t {
4671 // unsigned long int *offset; // pointer to ivar offset location
4672 // char *name;
4673 // char *type;
4674 // uint32_t alignment;
4675 // uint32_t size;
4676 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004677 IvarnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004678 llvm::StructType::create("struct._ivar_t",
4679 llvm::PointerType::getUnqual(LongTy),
4680 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004681
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004682 // struct _ivar_list_t {
4683 // uint32 entsize; // sizeof(struct _ivar_t)
4684 // uint32 count;
4685 // struct _iver_t list[count];
4686 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004687 IvarListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004688 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
4689 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004690
Owen Anderson9793f0e2009-07-29 22:16:19 +00004691 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004692
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004693 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004694 // uint32_t const flags;
4695 // uint32_t const instanceStart;
4696 // uint32_t const instanceSize;
4697 // uint32_t const reserved; // only when building for 64bit targets
4698 // const uint8_t * const ivarLayout;
4699 // const char *const name;
4700 // const struct _method_list_t * const baseMethods;
4701 // const struct _objc_protocol_list *const baseProtocols;
4702 // const struct _ivar_list_t *const ivars;
4703 // const uint8_t * const weakIvarLayout;
4704 // const struct _prop_list_t * const properties;
4705 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004706
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004707 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattner5ec04a52011-08-12 17:43:31 +00004708 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
4709 IntTy, IntTy, IntTy, Int8PtrTy,
4710 Int8PtrTy, MethodListnfABIPtrTy,
4711 ProtocolListnfABIPtrTy,
4712 IvarListnfABIPtrTy,
4713 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004714
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004715 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +00004716 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +00004717 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4718 ->getPointerTo();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004719
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004720 // struct _class_t {
4721 // struct _class_t *isa;
4722 // struct _class_t * const superclass;
4723 // void *cache;
4724 // IMP *vtable;
4725 // struct class_ro_t *ro;
4726 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004727
Chris Lattner5ec04a52011-08-12 17:43:31 +00004728 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattnera5f58b02011-07-09 17:41:47 +00004729 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
4730 llvm::PointerType::getUnqual(ClassnfABITy),
4731 CachePtrTy,
4732 llvm::PointerType::getUnqual(ImpnfABITy),
4733 llvm::PointerType::getUnqual(ClassRonfABITy),
4734 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004735
Fariborz Jahanian71394042009-01-23 23:53:38 +00004736 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004737 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004738
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004739 // struct _category_t {
4740 // const char * const name;
4741 // struct _class_t *const cls;
4742 // const struct _method_list_t * const instance_methods;
4743 // const struct _method_list_t * const class_methods;
4744 // const struct _protocol_list_t * const protocols;
4745 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004746 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004747 CategorynfABITy = llvm::StructType::create("struct._category_t",
4748 Int8PtrTy, ClassnfABIPtrTy,
4749 MethodListnfABIPtrTy,
4750 MethodListnfABIPtrTy,
4751 ProtocolListnfABIPtrTy,
4752 PropertyListPtrTy,
4753 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004754
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004755 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004756 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4757 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004758
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004759 // MessageRefTy - LLVM for:
4760 // struct _message_ref_t {
4761 // IMP messenger;
4762 // SEL name;
4763 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004764
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004765 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004766 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004767 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004768 SourceLocation(), SourceLocation(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004769 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004770 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith2b013182012-06-10 03:12:00 +00004771 Ctx.VoidPtrTy, 0, 0, false, ICIS_NoInit));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004772 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith2b013182012-06-10 03:12:00 +00004773 Ctx.getObjCSelType(), 0, 0, false,
4774 ICIS_NoInit));
Douglas Gregord5058122010-02-11 01:19:42 +00004775 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004776
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004777 MessageRefCTy = Ctx.getTagDeclType(RD);
4778 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4779 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004780
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004781 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004782 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004783
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004784 // SuperMessageRefTy - LLVM for:
4785 // struct _super_message_ref_t {
4786 // SUPER_IMP messenger;
4787 // SEL name;
4788 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004789 SuperMessageRefTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004790 llvm::StructType::create("struct._super_message_ref_t",
4791 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004792
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004793 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004794 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00004795
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004796
4797 // struct objc_typeinfo {
4798 // const void** vtable; // objc_ehtype_vtable + 2
4799 // const char* name; // c++ typeinfo string
4800 // Class cls;
4801 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004802 EHTypeTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004803 llvm::StructType::create("struct._objc_typeinfo",
4804 llvm::PointerType::getUnqual(Int8PtrTy),
4805 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004806 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004807}
4808
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004809llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004810 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004811
Fariborz Jahanian71394042009-01-23 23:53:38 +00004812 return NULL;
4813}
4814
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00004815void CGObjCNonFragileABIMac::
4816AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
4817 const char *SymbolName,
4818 const char *SectionName) {
Daniel Dunbar19573e72009-05-15 21:48:48 +00004819 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004820
Daniel Dunbar19573e72009-05-15 21:48:48 +00004821 if (!NumClasses)
4822 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004823
Chris Lattner3def9ae2012-02-06 22:16:34 +00004824 SmallVector<llvm::Constant*, 8> Symbols(NumClasses);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004825 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004826 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004827 ObjCTypes.Int8PtrTy);
Chris Lattner3def9ae2012-02-06 22:16:34 +00004828 llvm::Constant *Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004829 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner3def9ae2012-02-06 22:16:34 +00004830 Symbols.size()),
Daniel Dunbar19573e72009-05-15 21:48:48 +00004831 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004832
Daniel Dunbar19573e72009-05-15 21:48:48 +00004833 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004834 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004835 llvm::GlobalValue::InternalLinkage,
4836 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004837 SymbolName);
Daniel Dunbar710cb202010-04-25 20:39:32 +00004838 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004839 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004840 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004841}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004842
Fariborz Jahanian71394042009-01-23 23:53:38 +00004843void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4844 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004845
Daniel Dunbar19573e72009-05-15 21:48:48 +00004846 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004847 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004848 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004849 "\01L_OBJC_LABEL_CLASS_$",
4850 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004851
Bill Wendling53136852012-02-07 09:06:01 +00004852 for (unsigned i = 0, e = DefinedClasses.size(); i < e; i++) {
Fariborz Jahanian67260552009-11-17 21:37:35 +00004853 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4854 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4855 continue;
4856 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004857 }
4858
Bill Wendling53136852012-02-07 09:06:01 +00004859 for (unsigned i = 0, e = DefinedMetaClasses.size(); i < e; i++) {
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004860 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4861 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4862 continue;
4863 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4864 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00004865
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004866 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004867 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4868 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004869
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004870 // Build list of all implemented category addresses in array
4871 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004872 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004873 "\01L_OBJC_LABEL_CATEGORY_$",
4874 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004875 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004876 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4877 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004878
Daniel Dunbar5e639272010-04-25 20:39:01 +00004879 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004880}
4881
John McCall9e8bb002011-05-14 03:10:52 +00004882/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4883/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004884/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004885/// message dispatch call for all the rest.
John McCall9e8bb002011-05-14 03:10:52 +00004886bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4887 // At various points we've experimented with using vtable-based
4888 // dispatch for all methods.
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004889 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004890 case CodeGenOptions::Legacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004891 return false;
John McCall9e8bb002011-05-14 03:10:52 +00004892 case CodeGenOptions::NonLegacy:
4893 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004894 case CodeGenOptions::Mixed:
4895 break;
4896 }
4897
4898 // If so, see whether this selector is in the white-list of things which must
4899 // use the new dispatch convention. We lazily build a dense set for this.
John McCall9e8bb002011-05-14 03:10:52 +00004900 if (VTableDispatchMethods.empty()) {
4901 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4902 VTableDispatchMethods.insert(GetNullarySelector("class"));
4903 VTableDispatchMethods.insert(GetNullarySelector("self"));
4904 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4905 VTableDispatchMethods.insert(GetNullarySelector("length"));
4906 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004907
John McCall9e8bb002011-05-14 03:10:52 +00004908 // These are vtable-based if GC is disabled.
4909 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004910 if (CGM.getLangOpts().getGC() != LangOptions::GCOnly) {
John McCall9e8bb002011-05-14 03:10:52 +00004911 VTableDispatchMethods.insert(GetNullarySelector("retain"));
4912 VTableDispatchMethods.insert(GetNullarySelector("release"));
4913 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4914 }
4915
4916 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4917 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4918 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4919 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4920 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4921 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4922 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4923
4924 // These are vtable-based if GC is enabled.
4925 // Optimistically use vtable dispatch for hybrid compiles.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004926 if (CGM.getLangOpts().getGC() != LangOptions::NonGC) {
John McCall9e8bb002011-05-14 03:10:52 +00004927 VTableDispatchMethods.insert(GetNullarySelector("hash"));
4928 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4929
4930 // "countByEnumeratingWithState:objects:count"
4931 IdentifierInfo *KeyIdents[] = {
4932 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4933 &CGM.getContext().Idents.get("objects"),
4934 &CGM.getContext().Idents.get("count")
4935 };
4936 VTableDispatchMethods.insert(
4937 CGM.getContext().Selectors.getSelector(3, KeyIdents));
4938 }
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004939 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004940
John McCall9e8bb002011-05-14 03:10:52 +00004941 return VTableDispatchMethods.count(Sel);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004942}
4943
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004944// Metadata flags
4945enum MetaDataDlags {
4946 CLS = 0x0,
4947 CLS_META = 0x1,
4948 CLS_ROOT = 0x2,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004949 OBJC2_CLS_HIDDEN = 0x10,
John McCall31168b02011-06-15 23:02:42 +00004950 CLS_EXCEPTION = 0x20,
4951
4952 /// (Obsolete) ARC-specific: this class has a .release_ivars method
4953 CLS_HAS_IVAR_RELEASER = 0x40,
4954 /// class was compiled with -fobjc-arr
4955 CLS_COMPILED_BY_ARC = 0x80 // (1<<7)
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004956};
4957/// BuildClassRoTInitializer - generate meta-data for:
4958/// struct _class_ro_t {
4959/// uint32_t const flags;
4960/// uint32_t const instanceStart;
4961/// uint32_t const instanceSize;
4962/// uint32_t const reserved; // only when building for 64bit targets
4963/// const uint8_t * const ivarLayout;
4964/// const char *const name;
4965/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004966/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004967/// const struct _ivar_list_t *const ivars;
4968/// const uint8_t * const weakIvarLayout;
4969/// const struct _prop_list_t * const properties;
4970/// }
4971///
4972llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004973 unsigned flags,
4974 unsigned InstanceStart,
4975 unsigned InstanceSize,
4976 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004977 std::string ClassName = ID->getNameAsString();
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004978 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCall31168b02011-06-15 23:02:42 +00004979
David Blaikiebbafb8a2012-03-11 07:00:24 +00004980 if (CGM.getLangOpts().ObjCAutoRefCount)
John McCall31168b02011-06-15 23:02:42 +00004981 flags |= CLS_COMPILED_BY_ARC;
4982
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004983 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4984 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4985 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004986 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004987 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4988 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004989 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004990 // const struct _method_list_t * const baseMethods;
4991 std::vector<llvm::Constant*> Methods;
4992 std::string MethodListName("\01l_OBJC_$_");
4993 if (flags & CLS_META) {
4994 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004995 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004996 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004997 // Class methods should always be defined.
4998 Methods.push_back(GetMethodConstant(*i));
4999 }
5000 } else {
5001 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005002 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005003 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005004 // Instance methods should always be defined.
5005 Methods.push_back(GetMethodConstant(*i));
5006 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005007 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005008 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +00005009 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005010
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00005011 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
5012 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005013
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00005014 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
5015 if (llvm::Constant *C = GetMethodConstant(MD))
5016 Methods.push_back(C);
5017 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
5018 if (llvm::Constant *C = GetMethodConstant(MD))
5019 Methods.push_back(C);
5020 }
5021 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005022 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005023 Values[ 5] = EmitMethodList(MethodListName,
5024 "__DATA, __objc_const", Methods);
5025
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005026 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5027 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005028 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005029 + OID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00005030 OID->all_referenced_protocol_begin(),
5031 OID->all_referenced_protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005032
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005033 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00005034 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005035 else
5036 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005037 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
5038 : BuildIvarLayout(ID, false);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005039 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00005040 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005041 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005042 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
5043 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005044 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005045 Values);
5046 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005047 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
5048 llvm::GlobalValue::InternalLinkage,
5049 Init,
5050 (flags & CLS_META) ?
5051 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
5052 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005053 CLASS_RO_GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005054 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005055 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005056 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005057
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005058}
5059
5060/// BuildClassMetaData - This routine defines that to-level meta-data
5061/// for the given ClassName for:
5062/// struct _class_t {
5063/// struct _class_t *isa;
5064/// struct _class_t * const superclass;
5065/// void *cache;
5066/// IMP *vtable;
5067/// struct class_ro_t *ro;
5068/// }
5069///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005070llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005071 std::string &ClassName,
5072 llvm::Constant *IsAGV,
5073 llvm::Constant *SuperClassGV,
5074 llvm::Constant *ClassRoGV,
5075 bool HiddenVisibility) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005076 llvm::Constant *Values[] = {
5077 IsAGV,
5078 SuperClassGV,
5079 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
5080 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
5081 ClassRoGV // &CLASS_RO_GV
5082 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005083 if (!Values[1])
5084 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005085 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005086 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005087 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
5088 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00005089 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005090 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005091 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00005092 if (HiddenVisibility)
5093 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005094 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005095}
5096
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005097bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00005098CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005099 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005100}
5101
Daniel Dunbar961202372009-05-03 12:57:56 +00005102void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00005103 uint32_t &InstanceStart,
5104 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005105 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00005106 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005107
Daniel Dunbar9b042e02009-05-04 23:23:09 +00005108 // InstanceSize is really instance end.
Ken Dyckd5090c12011-02-11 02:20:09 +00005109 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar9b042e02009-05-04 23:23:09 +00005110
5111 // If there are no fields, the start is the same as the end.
5112 if (!RL.getFieldCount())
5113 InstanceStart = InstanceSize;
5114 else
Ken Dyckc5ca8762011-04-14 00:43:09 +00005115 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbar554fd792009-04-19 23:41:48 +00005116}
5117
Fariborz Jahanian71394042009-01-23 23:53:38 +00005118void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
5119 std::string ClassName = ID->getNameAsString();
5120 if (!ObjCEmptyCacheVar) {
5121 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005122 CGM.getModule(),
5123 ObjCTypes.CacheTy,
5124 false,
5125 llvm::GlobalValue::ExternalLinkage,
5126 0,
5127 "_objc_empty_cache");
5128
Fariborz Jahanian71394042009-01-23 23:53:38 +00005129 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005130 CGM.getModule(),
5131 ObjCTypes.ImpnfABITy,
5132 false,
5133 llvm::GlobalValue::ExternalLinkage,
5134 0,
5135 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00005136 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005137 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005138 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00005139 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005140 uint32_t InstanceStart =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005141 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005142 uint32_t InstanceSize = InstanceStart;
5143 uint32_t flags = CLS_META;
Daniel Dunbar15894b72009-04-07 05:48:37 +00005144 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
5145 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005146
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005147 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005148
5149 bool classIsHidden =
John McCall457a04e2010-10-22 21:05:15 +00005150 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005151 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005152 flags |= OBJC2_CLS_HIDDEN;
John McCall31168b02011-06-15 23:02:42 +00005153 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00005154 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005155 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005156 // class is root
5157 flags |= CLS_ROOT;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005158 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005159 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005160 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005161 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005162 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5163 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5164 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005165 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005166 if (Root->isWeakImported())
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005167 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005168 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005169 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005170 ObjCMetaClassName +
5171 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005172 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005173 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00005174 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005175 }
5176 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5177 InstanceStart,
5178 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005179 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005180 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005181 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5182 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00005183 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00005184
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005185 // Metadata for the class
5186 flags = CLS;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005187 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005188 flags |= OBJC2_CLS_HIDDEN;
John McCall31168b02011-06-15 23:02:42 +00005189 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00005190 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005191
Douglas Gregor78bd61f2009-06-18 16:11:24 +00005192 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005193 flags |= CLS_EXCEPTION;
5194
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005195 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005196 flags |= CLS_ROOT;
5197 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00005198 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005199 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005200 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005201 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005202 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005203 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00005204 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005205 }
Daniel Dunbar961202372009-05-03 12:57:56 +00005206 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005207 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005208 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005209 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005210 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005211
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005212 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005213 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005214 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5215 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005216 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005217
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005218 // Determine if this class is also "non-lazy".
5219 if (ImplementationIsNonLazy(ID))
5220 DefinedNonLazyClasses.push_back(ClassMD);
5221
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005222 // Force the definition of the EHType if necessary.
5223 if (flags & CLS_EXCEPTION)
5224 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005225 // Make sure method definition entries are all clear for next implementation.
5226 MethodDefinitions.clear();
Fariborz Jahanian71394042009-01-23 23:53:38 +00005227}
5228
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005229/// GenerateProtocolRef - This routine is called to generate code for
5230/// a protocol reference expression; as in:
5231/// @code
5232/// @protocol(Proto1);
5233/// @endcode
5234/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5235/// which will hold address of the protocol meta-data.
5236///
5237llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005238 const ObjCProtocolDecl *PD) {
5239
Fariborz Jahanian464423d2009-04-10 18:47:34 +00005240 // This routine is called for @protocol only. So, we must build definition
5241 // of protocol's meta-data (not a reference to it!)
5242 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005243 llvm::Constant *Init =
5244 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Douglas Gregor020de322012-01-17 18:36:30 +00005245 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005246
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005247 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar56df9772010-08-17 22:39:59 +00005248 ProtocolName += PD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005249
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005250 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5251 if (PTGV)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005252 return Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005253 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005254 CGM.getModule(),
5255 Init->getType(), false,
5256 llvm::GlobalValue::WeakAnyLinkage,
5257 Init,
5258 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005259 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5260 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005261 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005262 return Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005263}
5264
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005265/// GenerateCategory - Build metadata for a category implementation.
5266/// struct _category_t {
5267/// const char * const name;
5268/// struct _class_t *const cls;
5269/// const struct _method_list_t * const instance_methods;
5270/// const struct _method_list_t * const class_methods;
5271/// const struct _protocol_list_t * const protocols;
5272/// const struct _prop_list_t * const properties;
5273/// }
5274///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005275void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005276 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005277 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005278 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5279 "_$_" + OCD->getNameAsString());
5280 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005281 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005282
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005283 llvm::Constant *Values[6];
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005284 Values[0] = GetClassName(OCD->getIdentifier());
5285 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005286 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005287 if (Interface->isWeakImported())
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005288 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5289
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005290 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005291 std::vector<llvm::Constant*> Methods;
5292 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005293 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005294 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005295
5296 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005297 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005298 // Instance methods should always be defined.
5299 Methods.push_back(GetMethodConstant(*i));
5300 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005301
5302 Values[2] = EmitMethodList(MethodListName,
5303 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005304 Methods);
5305
5306 MethodListName = Prefix;
5307 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5308 OCD->getNameAsString();
5309 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005310 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005311 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005312 // Class methods should always be defined.
5313 Methods.push_back(GetMethodConstant(*i));
5314 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005315
5316 Values[3] = EmitMethodList(MethodListName,
5317 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005318 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005319 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005320 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005321 if (Category) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00005322 SmallString<256> ExtName;
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005323 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5324 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005325 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005326 + Interface->getName() + "_$_"
5327 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005328 Category->protocol_begin(),
5329 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005330 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5331 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00005332 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00005333 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5334 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005335 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005336
5337 llvm::Constant *Init =
5338 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005339 Values);
5340 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005341 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005342 false,
5343 llvm::GlobalValue::InternalLinkage,
5344 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005345 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005346 GCATV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005347 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005348 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005349 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005350 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005351
5352 // Determine if this category is also "non-lazy".
5353 if (ImplementationIsNonLazy(OCD))
5354 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005355 // method definition entries must be clear for next implementation.
5356 MethodDefinitions.clear();
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005357}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005358
5359/// GetMethodConstant - Return a struct objc_method constant for the
5360/// given method if it has been defined. The result is null if the
5361/// method has not been defined. The return value has type MethodPtrTy.
5362llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005363 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00005364 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005365 if (!Fn)
5366 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005367
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005368 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00005369 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005370 ObjCTypes.SelectorPtrTy),
5371 GetMethodVarType(MD),
5372 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5373 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00005374 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005375}
5376
5377/// EmitMethodList - Build meta-data for method declarations
5378/// struct _method_list_t {
5379/// uint32_t entsize; // sizeof(struct _objc_method)
5380/// uint32_t method_count;
5381/// struct _objc_method method_list[method_count];
5382/// }
5383///
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00005384llvm::Constant *
5385CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
5386 const char *Section,
5387 ArrayRef<llvm::Constant*> Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005388 // Return null for empty list.
5389 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005390 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005391
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005392 llvm::Constant *Values[3];
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005393 // sizeof(struct _objc_method)
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005394 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005395 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005396 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005397 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005398 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005399 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005400 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005401 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005402
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005403 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005404 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005405 llvm::GlobalValue::InternalLinkage, Init, Name);
5406 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005407 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005408 CGM.AddUsedGlobal(GV);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005409 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005410}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005411
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005412/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5413/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005414llvm::GlobalVariable *
5415CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5416 const ObjCIvarDecl *Ivar) {
5417 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00005418 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00005419 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005420 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005421 CGM.getModule().getGlobalVariable(Name);
5422 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005423 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005424 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005425 false,
5426 llvm::GlobalValue::ExternalLinkage,
5427 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005428 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005429 return IvarOffsetGV;
5430}
5431
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005432llvm::Constant *
5433CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5434 const ObjCIvarDecl *Ivar,
5435 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005436 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005437 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005438 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005439 IvarOffsetGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005440 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005441
Mike Stump18bb9282009-05-16 07:57:57 +00005442 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5443 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005444 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5445 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall457a04e2010-10-22 21:05:15 +00005446 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00005447 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005448 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00005449 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendlingf7d45982011-05-04 21:37:25 +00005450 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005451 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005452}
5453
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005454/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005455/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005456/// IvarListnfABIPtrTy.
5457/// struct _ivar_t {
5458/// unsigned long int *offset; // pointer to ivar offset location
5459/// char *name;
5460/// char *type;
5461/// uint32_t alignment;
5462/// uint32_t size;
5463/// }
5464/// struct _ivar_list_t {
5465/// uint32 entsize; // sizeof(struct _ivar_t)
5466/// uint32 count;
5467/// struct _iver_t list[count];
5468/// }
5469///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005470
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005471llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005472 const ObjCImplementationDecl *ID) {
5473
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005474 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005475
Jordy Rosea91768e2011-07-22 02:08:32 +00005476 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005477 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005478
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005479 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005480
Jordy Rosea91768e2011-07-22 02:08:32 +00005481 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00005482 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005483 // Ignore unnamed bit-fields.
5484 if (!IVD->getDeclName())
5485 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005486 llvm::Constant *Ivar[5];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005487 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005488 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005489 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5490 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2192fe52011-07-18 04:24:23 +00005491 llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005492 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005493 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005494 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005495 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005496 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005497 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00005498 // NOTE. Size of a bitfield does not match gcc's, because of the
5499 // way bitfields are treated special in each. But I am told that
5500 // 'size' for bitfield ivars is ignored by the runtime so it does
5501 // not matter. If it matters, there is enough info to get the
5502 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005503 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005504 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005505 }
5506 // Return null for empty list.
5507 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005508 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005509
5510 llvm::Constant *Values[3];
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005511 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005512 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5513 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005514 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005515 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005516 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005517 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005518 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5519 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005520 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005521 llvm::GlobalValue::InternalLinkage,
5522 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005523 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005524 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005525 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005526 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005527
Chris Lattnerf56501c2009-07-17 23:57:13 +00005528 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005529 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005530}
5531
5532llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005533 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005534 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005535
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005536 if (!Entry) {
5537 // We use the initializer as a marker of whether this is a forward
5538 // reference or not. At module finalization we add the empty
5539 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005540 Entry =
5541 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5542 llvm::GlobalValue::ExternalLinkage,
5543 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005544 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005545 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005546 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005547
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005548 return Entry;
5549}
5550
5551/// GetOrEmitProtocol - Generate the protocol meta-data:
5552/// @code
5553/// struct _protocol_t {
5554/// id isa; // NULL
5555/// const char * const protocol_name;
5556/// const struct _protocol_list_t * protocol_list; // super protocols
5557/// const struct method_list_t * const instance_methods;
5558/// const struct method_list_t * const class_methods;
5559/// const struct method_list_t *optionalInstanceMethods;
5560/// const struct method_list_t *optionalClassMethods;
5561/// const struct _prop_list_t * properties;
5562/// const uint32_t size; // sizeof(struct _protocol_t)
5563/// const uint32_t flags; // = 0
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005564/// const char ** extendedMethodTypes;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005565/// }
5566/// @endcode
5567///
5568
5569llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005570 const ObjCProtocolDecl *PD) {
John McCallf9582a72012-03-30 21:29:05 +00005571 llvm::GlobalVariable *Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005572
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005573 // Early exit if a defining object has already been generated.
5574 if (Entry && Entry->hasInitializer())
5575 return Entry;
5576
Douglas Gregora715bff2012-01-01 19:51:50 +00005577 // Use the protocol definition, if there is one.
5578 if (const ObjCProtocolDecl *Def = PD->getDefinition())
5579 PD = Def;
5580
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005581 // Construct method lists.
5582 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5583 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005584 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005585 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005586 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005587 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005588 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005589 if (!C)
5590 return GetOrEmitProtocolRef(PD);
5591
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005592 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5593 OptInstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005594 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005595 } else {
5596 InstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005597 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005598 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005599 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005600
5601 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005602 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005603 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005604 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005605 if (!C)
5606 return GetOrEmitProtocolRef(PD);
5607
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005608 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5609 OptClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005610 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005611 } else {
5612 ClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005613 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005614 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005615 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005616
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005617 MethodTypesExt.insert(MethodTypesExt.end(),
5618 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
5619
5620 llvm::Constant *Values[11];
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005621 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005622 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005623 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005624 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5625 PD->protocol_begin(),
5626 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005627
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005628 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005629 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005630 "__DATA, __objc_const",
5631 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005632 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005633 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005634 "__DATA, __objc_const",
5635 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005636 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005637 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005638 "__DATA, __objc_const",
5639 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005640 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005641 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005642 "__DATA, __objc_const",
5643 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005644 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005645 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005646 uint32_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005647 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005648 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005649 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005650 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
5651 + PD->getName(),
5652 MethodTypesExt, ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005653 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005654 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005655
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005656 if (Entry) {
5657 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005658 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005659 Entry->setInitializer(Init);
5660 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005661 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005662 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5663 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5664 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005665 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005666 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005667 Entry->setSection("__DATA,__datacoal_nt,coalesced");
John McCallf9582a72012-03-30 21:29:05 +00005668
5669 Protocols[PD->getIdentifier()] = Entry;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005670 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005671 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005672 CGM.AddUsedGlobal(Entry);
5673
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005674 // Use this protocol meta-data to build protocol list table in section
5675 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005676 llvm::GlobalVariable *PTGV =
5677 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5678 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5679 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005680 PTGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005681 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005682 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005683 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005684 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005685 return Entry;
5686}
5687
5688/// EmitProtocolList - Generate protocol list meta-data:
5689/// @code
5690/// struct _protocol_list_t {
5691/// long protocol_count; // Note, this is 32/64 bit
5692/// struct _protocol_t[protocol_count];
5693/// }
5694/// @endcode
5695///
5696llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005697CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005698 ObjCProtocolDecl::protocol_iterator begin,
5699 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendlinga515b582012-02-09 22:16:49 +00005700 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005701
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005702 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005703 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005704 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005705
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005706 // FIXME: We shouldn't need to do this lookup here, should we?
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00005707 SmallString<256> TmpName;
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005708 Name.toVector(TmpName);
5709 llvm::GlobalVariable *GV =
5710 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005711 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005712 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005713
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005714 for (; begin != end; ++begin)
5715 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5716
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005717 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005718 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005719 ObjCTypes.ProtocolnfABIPtrTy));
5720
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005721 llvm::Constant *Values[2];
Owen Anderson170229f2009-07-14 23:10:40 +00005722 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005723 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005724 Values[1] =
Bill Wendlinga515b582012-02-09 22:16:49 +00005725 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
5726 ProtocolRefs.size()),
5727 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005728
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005729 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005730 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005731 llvm::GlobalValue::InternalLinkage,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005732 Init, Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005733 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005734 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005735 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005736 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005737 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005738 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005739}
5740
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005741/// GetMethodDescriptionConstant - This routine build following meta-data:
5742/// struct _objc_method {
5743/// SEL _cmd;
5744/// char *method_type;
5745/// char *_imp;
5746/// }
5747
5748llvm::Constant *
5749CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005750 llvm::Constant *Desc[3];
Owen Anderson170229f2009-07-14 23:10:40 +00005751 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005752 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5753 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005754 Desc[1] = GetMethodVarType(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005755 if (!Desc[1])
5756 return 0;
5757
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005758 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005759 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005760 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005761}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005762
5763/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5764/// This code gen. amounts to generating code for:
5765/// @code
5766/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5767/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005768///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005769LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00005770 CodeGen::CodeGenFunction &CGF,
5771 QualType ObjectTy,
5772 llvm::Value *BaseValue,
5773 const ObjCIvarDecl *Ivar,
5774 unsigned CVRQualifiers) {
5775 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Fariborz Jahaniancaabf1b2012-02-20 22:42:22 +00005776 llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar);
5777 if (llvm::LoadInst *LI = dyn_cast<llvm::LoadInst>(Offset))
5778 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
5779 llvm::MDNode::get(VMContext,
5780 ArrayRef<llvm::Value*>()));
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00005781 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
Fariborz Jahaniancaabf1b2012-02-20 22:42:22 +00005782 Offset);
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005783}
5784
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005785llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005786 CodeGen::CodeGenFunction &CGF,
5787 const ObjCInterfaceDecl *Interface,
5788 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005789 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005790}
5791
John McCall234eac82011-05-13 23:16:18 +00005792static void appendSelectorForMessageRefTable(std::string &buffer,
5793 Selector selector) {
5794 if (selector.isUnarySelector()) {
5795 buffer += selector.getNameForSlot(0);
5796 return;
5797 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005798
John McCall234eac82011-05-13 23:16:18 +00005799 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5800 buffer += selector.getNameForSlot(i);
5801 buffer += '_';
5802 }
5803}
5804
John McCall9e8bb002011-05-14 03:10:52 +00005805/// Emit a "v-table" message send. We emit a weak hidden-visibility
5806/// struct, initially containing the selector pointer and a pointer to
5807/// a "fixup" variant of the appropriate objc_msgSend. To call, we
5808/// load and call the function pointer, passing the address of the
5809/// struct as the second parameter. The runtime determines whether
5810/// the selector is currently emitted using vtable dispatch; if so, it
5811/// substitutes a stub function which simply tail-calls through the
5812/// appropriate vtable slot, and if not, it substitues a stub function
5813/// which tail-calls objc_msgSend. Both stubs adjust the selector
5814/// argument to correctly point to the selector.
5815RValue
5816CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5817 ReturnValueSlot returnSlot,
5818 QualType resultType,
5819 Selector selector,
5820 llvm::Value *arg0,
5821 QualType arg0Type,
5822 bool isSuper,
5823 const CallArgList &formalArgs,
5824 const ObjCMethodDecl *method) {
John McCall234eac82011-05-13 23:16:18 +00005825 // Compute the actual arguments.
5826 CallArgList args;
5827
John McCall9e8bb002011-05-14 03:10:52 +00005828 // First argument: the receiver / super-call structure.
John McCall234eac82011-05-13 23:16:18 +00005829 if (!isSuper)
John McCall9e8bb002011-05-14 03:10:52 +00005830 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5831 args.add(RValue::get(arg0), arg0Type);
John McCall234eac82011-05-13 23:16:18 +00005832
John McCall9e8bb002011-05-14 03:10:52 +00005833 // Second argument: a pointer to the message ref structure. Leave
5834 // the actual argument value blank for now.
John McCall234eac82011-05-13 23:16:18 +00005835 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5836
5837 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5838
John McCalla729c622012-02-17 03:33:10 +00005839 MessageSendInfo MSI = getMessageSendInfo(method, resultType, args);
John McCall234eac82011-05-13 23:16:18 +00005840
John McCall5880fb82011-05-14 21:12:11 +00005841 NullReturnState nullReturn;
5842
John McCall9e8bb002011-05-14 03:10:52 +00005843 // Find the function to call and the mangled name for the message
5844 // ref structure. Using a different mangled name wouldn't actually
5845 // be a problem; it would just be a waste.
5846 //
5847 // The runtime currently never uses vtable dispatch for anything
5848 // except normal, non-super message-sends.
5849 // FIXME: don't use this for that.
John McCall234eac82011-05-13 23:16:18 +00005850 llvm::Constant *fn = 0;
5851 std::string messageRefName("\01l_");
John McCalla729c622012-02-17 03:33:10 +00005852 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
John McCall234eac82011-05-13 23:16:18 +00005853 if (isSuper) {
5854 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5855 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005856 } else {
John McCall5880fb82011-05-14 21:12:11 +00005857 nullReturn.init(CGF, arg0);
John McCall234eac82011-05-13 23:16:18 +00005858 fn = ObjCTypes.getMessageSendStretFixupFn();
5859 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005860 }
John McCall234eac82011-05-13 23:16:18 +00005861 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5862 fn = ObjCTypes.getMessageSendFpretFixupFn();
5863 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005864 } else {
John McCall234eac82011-05-13 23:16:18 +00005865 if (isSuper) {
5866 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5867 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005868 } else {
John McCall234eac82011-05-13 23:16:18 +00005869 fn = ObjCTypes.getMessageSendFixupFn();
5870 messageRefName += "objc_msgSend_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005871 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005872 }
John McCall234eac82011-05-13 23:16:18 +00005873 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5874 messageRefName += '_';
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005875
John McCall234eac82011-05-13 23:16:18 +00005876 // Append the selector name, except use underscores anywhere we
5877 // would have used colons.
5878 appendSelectorForMessageRefTable(messageRefName, selector);
5879
5880 llvm::GlobalVariable *messageRef
5881 = CGM.getModule().getGlobalVariable(messageRefName);
5882 if (!messageRef) {
John McCall9e8bb002011-05-14 03:10:52 +00005883 // Build the message ref structure.
John McCall234eac82011-05-13 23:16:18 +00005884 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005885 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCall234eac82011-05-13 23:16:18 +00005886 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5887 init->getType(),
5888 /*constant*/ false,
5889 llvm::GlobalValue::WeakAnyLinkage,
5890 init,
5891 messageRefName);
5892 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5893 messageRef->setAlignment(16);
5894 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5895 }
Fariborz Jahanianc93fa982012-01-30 23:39:30 +00005896
5897 bool requiresnullCheck = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00005898 if (CGM.getLangOpts().ObjCAutoRefCount && method)
Fariborz Jahanianc93fa982012-01-30 23:39:30 +00005899 for (ObjCMethodDecl::param_const_iterator i = method->param_begin(),
5900 e = method->param_end(); i != e; ++i) {
5901 const ParmVarDecl *ParamDecl = (*i);
5902 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
5903 if (!nullReturn.NullBB)
5904 nullReturn.init(CGF, arg0);
5905 requiresnullCheck = true;
5906 break;
5907 }
5908 }
5909
John McCall234eac82011-05-13 23:16:18 +00005910 llvm::Value *mref =
5911 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5912
John McCall9e8bb002011-05-14 03:10:52 +00005913 // Update the message ref argument.
John McCall234eac82011-05-13 23:16:18 +00005914 args[1].RV = RValue::get(mref);
5915
5916 // Load the function to call from the message ref table.
5917 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5918 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5919
John McCalla729c622012-02-17 03:33:10 +00005920 callee = CGF.Builder.CreateBitCast(callee, MSI.MessengerType);
John McCall234eac82011-05-13 23:16:18 +00005921
John McCalla729c622012-02-17 03:33:10 +00005922 RValue result = CGF.EmitCall(MSI.CallInfo, callee, returnSlot, args);
Fariborz Jahanianc93fa982012-01-30 23:39:30 +00005923 return nullReturn.complete(CGF, result, resultType, formalArgs,
5924 requiresnullCheck ? method : 0);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005925}
5926
5927/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005928CodeGen::RValue
5929CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005930 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005931 QualType ResultType,
5932 Selector Sel,
5933 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005934 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00005935 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005936 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00005937 return isVTableDispatchedSelector(Sel)
5938 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005939 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00005940 false, CallArgs, Method)
5941 : EmitMessageSend(CGF, Return, ResultType,
5942 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005943 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00005944 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005945}
5946
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005947llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005948CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005949 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5950
Daniel Dunbara6468342009-03-02 05:18:14 +00005951 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005952 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005953 false, llvm::GlobalValue::ExternalLinkage,
5954 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005955 }
5956
5957 return GV;
5958}
5959
John McCall31168b02011-06-15 23:02:42 +00005960llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
5961 IdentifierInfo *II) {
5962 llvm::GlobalVariable *&Entry = ClassReferences[II];
5963
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005964 if (!Entry) {
John McCall31168b02011-06-15 23:02:42 +00005965 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005966 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005967 Entry =
John McCall31168b02011-06-15 23:02:42 +00005968 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5969 false, llvm::GlobalValue::InternalLinkage,
5970 ClassGV,
5971 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005972 Entry->setAlignment(
John McCall31168b02011-06-15 23:02:42 +00005973 CGM.getTargetData().getABITypeAlignment(
5974 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005975 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005976 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005977 }
John McCall31168b02011-06-15 23:02:42 +00005978
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005979 return Builder.CreateLoad(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005980}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005981
John McCall31168b02011-06-15 23:02:42 +00005982llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5983 const ObjCInterfaceDecl *ID) {
5984 return EmitClassRefFromId(Builder, ID->getIdentifier());
5985}
5986
5987llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
5988 CGBuilderTy &Builder) {
5989 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
5990 return EmitClassRefFromId(Builder, II);
5991}
5992
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005993llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005994CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005995 const ObjCInterfaceDecl *ID) {
5996 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005997
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005998 if (!Entry) {
5999 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6000 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006001 Entry =
6002 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006003 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006004 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006005 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006006 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00006007 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006008 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006009 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006010 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006011 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006012
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006013 return Builder.CreateLoad(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006014}
6015
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006016/// EmitMetaClassRef - Return a Value * of the address of _class_t
6017/// meta-data
6018///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006019llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
6020 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006021 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
6022 if (Entry)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006023 return Builder.CreateLoad(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006024
Daniel Dunbar15894b72009-04-07 05:48:37 +00006025 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00006026 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006027 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006028 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006029 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006030 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006031 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006032 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00006033 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006034 ObjCTypes.ClassnfABIPtrTy));
6035
Daniel Dunbare60aa052009-04-15 19:03:14 +00006036 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006037 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006038
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006039 return Builder.CreateLoad(Entry);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006040}
6041
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006042/// GetClass - Return a reference to the class for the given interface
6043/// decl.
6044llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
6045 const ObjCInterfaceDecl *ID) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00006046 if (ID->isWeakImported()) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00006047 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
6048 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
6049 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
6050 }
6051
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00006052 return EmitClassRef(Builder, ID);
6053}
6054
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006055/// Generates a message send where the super is the receiver. This is
6056/// a message send to self with special delivery semantics indicating
6057/// which class's method should be called.
6058CodeGen::RValue
6059CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00006060 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006061 QualType ResultType,
6062 Selector Sel,
6063 const ObjCInterfaceDecl *Class,
6064 bool isCategoryImpl,
6065 llvm::Value *Receiver,
6066 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00006067 const CodeGen::CallArgList &CallArgs,
6068 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006069 // ...
6070 // Create and init a super structure; this is a (receiver, class)
6071 // pair we will pass to objc_msgSendSuper.
6072 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00006073 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006074
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006075 llvm::Value *ReceiverAsObject =
6076 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
6077 CGF.Builder.CreateStore(ReceiverAsObject,
6078 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006079
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006080 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00006081 llvm::Value *Target;
6082 if (IsClassMessage) {
6083 if (isCategoryImpl) {
6084 // Message sent to "super' in a class method defined in
6085 // a category implementation.
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006086 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00006087 Target = CGF.Builder.CreateStructGEP(Target, 0);
6088 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00006089 } else
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00006090 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stump658fe022009-07-30 22:28:39 +00006091 } else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006092 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006093
Mike Stump18bb9282009-05-16 07:57:57 +00006094 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
6095 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00006096 llvm::Type *ClassTy =
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006097 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
6098 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
6099 CGF.Builder.CreateStore(Target,
6100 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006101
John McCall9e8bb002011-05-14 03:10:52 +00006102 return (isVTableDispatchedSelector(Sel))
6103 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006104 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00006105 true, CallArgs, Method)
6106 : EmitMessageSend(CGF, Return, ResultType,
6107 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006108 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00006109 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006110}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006111
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006112llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00006113 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006114 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006115
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006116 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006117 llvm::Constant *Casted =
6118 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
6119 ObjCTypes.SelectorPtrTy);
6120 Entry =
6121 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
6122 llvm::GlobalValue::InternalLinkage,
6123 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00006124 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006125 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006126 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006127
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00006128 if (lval)
6129 return Entry;
Pete Cooper9d605512011-11-10 21:45:06 +00006130 llvm::LoadInst* LI = Builder.CreateLoad(Entry);
6131
6132 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6133 llvm::MDNode::get(VMContext,
6134 ArrayRef<llvm::Value*>()));
6135 return LI;
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006136}
Fariborz Jahanian06292952009-02-16 22:52:32 +00006137/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00006138/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00006139///
6140void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006141 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00006142 llvm::Value *dst,
6143 llvm::Value *ivarOffset) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006144 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006145 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00006146 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006147 assert(Size <= 8 && "does not support size > 8");
6148 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6149 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006150 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6151 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006152 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6153 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00006154 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
6155 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00006156 return;
6157}
6158
6159/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
6160/// objc_assign_strongCast (id src, id *dst)
6161///
6162void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006163 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006164 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006165 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006166 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00006167 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006168 assert(Size <= 8 && "does not support size > 8");
6169 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006170 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006171 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6172 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006173 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6174 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00006175 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006176 src, dst, "weakassign");
6177 return;
6178}
6179
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006180void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006181 CodeGen::CodeGenFunction &CGF,
6182 llvm::Value *DestPtr,
6183 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00006184 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006185 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6186 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006187 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00006188 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006189 return;
6190}
6191
Fariborz Jahanian06292952009-02-16 22:52:32 +00006192/// EmitObjCWeakRead - Code gen for loading value of a __weak
6193/// object: objc_read_weak (id *src)
6194///
6195llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006196 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006197 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006198 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006199 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6200 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00006201 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006202 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00006203 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00006204 return read_weak;
6205}
6206
6207/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6208/// objc_assign_weak (id src, id *dst)
6209///
6210void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006211 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006212 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006213 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00006214 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006215 assert(Size <= 8 && "does not support size > 8");
6216 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6217 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006218 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6219 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006220 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6221 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00006222 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006223 src, dst, "weakassign");
6224 return;
6225}
6226
6227/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6228/// objc_assign_global (id src, id *dst)
6229///
6230void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00006231 llvm::Value *src, llvm::Value *dst,
6232 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006233 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006234 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00006235 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006236 assert(Size <= 8 && "does not support size > 8");
6237 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6238 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006239 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6240 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006241 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6242 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00006243 if (!threadlocal)
6244 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6245 src, dst, "globalassign");
6246 else
6247 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6248 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00006249 return;
6250}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006251
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006252void
John McCallbd309292010-07-06 01:34:17 +00006253CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6254 const ObjCAtSynchronizedStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006255 EmitAtSynchronizedStmt(CGF, S,
6256 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6257 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallbd309292010-07-06 01:34:17 +00006258}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006259
John McCall2ca705e2010-07-24 00:37:23 +00006260llvm::Constant *
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00006261CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall2ca705e2010-07-24 00:37:23 +00006262 // There's a particular fixed type info for 'id'.
6263 if (T->isObjCIdType() ||
6264 T->isObjCQualifiedIdType()) {
6265 llvm::Constant *IDEHType =
6266 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6267 if (!IDEHType)
6268 IDEHType =
6269 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6270 false,
6271 llvm::GlobalValue::ExternalLinkage,
6272 0, "OBJC_EHTYPE_id");
6273 return IDEHType;
6274 }
6275
6276 // All other types should be Objective-C interface pointer types.
6277 const ObjCObjectPointerType *PT =
6278 T->getAs<ObjCObjectPointerType>();
6279 assert(PT && "Invalid @catch type.");
6280 const ObjCInterfaceType *IT = PT->getInterfaceType();
6281 assert(IT && "Invalid @catch type.");
6282 return GetInterfaceEHType(IT->getDecl(), false);
6283}
6284
John McCallbd309292010-07-06 01:34:17 +00006285void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6286 const ObjCAtTryStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006287 EmitTryCatchStmt(CGF, S,
6288 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6289 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6290 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006291}
6292
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006293/// EmitThrowStmt - Generate code for a throw statement.
6294void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6295 const ObjCAtThrowStmt &S) {
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006296 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00006297 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006298 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad5bd375a2011-07-15 08:37:34 +00006299 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall17afe452010-10-16 08:21:07 +00006300 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006301 } else {
Jay Foad5bd375a2011-07-15 08:37:34 +00006302 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall17afe452010-10-16 08:21:07 +00006303 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006304 }
6305
John McCall17afe452010-10-16 08:21:07 +00006306 CGF.Builder.CreateUnreachable();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006307 CGF.Builder.ClearInsertionPoint();
6308}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006309
John McCall2ca705e2010-07-24 00:37:23 +00006310llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006311CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006312 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006313 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006314
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006315 // If we don't need a definition, return the entry if found or check
6316 // if we use an external reference.
6317 if (!ForDefinition) {
6318 if (Entry)
6319 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00006320
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006321 // If this type (or a super class) has the __objc_exception__
6322 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00006323 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006324 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006325 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006326 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006327 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006328 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006329 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006330 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006331
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006332 // Otherwise we need to either make a new entry or fill in the
6333 // initializer.
6334 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00006335 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006336 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006337 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006338 CGM.getModule().getGlobalVariable(VTableName);
6339 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006340 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6341 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006342 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006343 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006344
Chris Lattnerece04092012-02-07 00:39:47 +00006345 llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006346
Benjamin Kramer22d24c22011-10-15 12:20:02 +00006347 llvm::Constant *Values[] = {
6348 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6349 GetClassName(ID->getIdentifier()),
6350 GetClassGlobal(ClassName)
6351 };
Owen Anderson170229f2009-07-14 23:10:40 +00006352 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00006353 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006354
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006355 if (Entry) {
6356 Entry->setInitializer(Init);
6357 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00006358 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006359 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006360 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006361 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006362 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006363 }
6364
David Blaikiebbafb8a2012-03-11 07:00:24 +00006365 if (CGM.getLangOpts().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar15894b72009-04-07 05:48:37 +00006366 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar710cb202010-04-25 20:39:32 +00006367 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6368 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006369
6370 if (ForDefinition) {
6371 Entry->setSection("__DATA,__objc_const");
6372 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6373 } else {
6374 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6375 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006376
6377 return Entry;
6378}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006379
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006380/* *** */
6381
Daniel Dunbarb036db82008-08-13 03:21:16 +00006382CodeGen::CGObjCRuntime *
6383CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
John McCall5fb5df92012-06-20 06:18:46 +00006384 switch (CGM.getLangOpts().ObjCRuntime.getKind()) {
6385 case ObjCRuntime::FragileMacOSX:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006386 return new CGObjCMac(CGM);
John McCall5fb5df92012-06-20 06:18:46 +00006387
6388 case ObjCRuntime::MacOSX:
6389 case ObjCRuntime::iOS:
6390 return new CGObjCNonFragileABIMac(CGM);
6391
David Chisnallb601c962012-07-03 20:49:52 +00006392 case ObjCRuntime::GNUstep:
6393 case ObjCRuntime::GCC:
John McCall775086e2012-07-12 02:07:58 +00006394 case ObjCRuntime::ObjFW:
John McCall5fb5df92012-06-20 06:18:46 +00006395 llvm_unreachable("these runtimes are not Mac runtimes");
6396 }
6397 llvm_unreachable("bad runtime");
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006398}