blob: db910d9f3aa2290295adc344aaeb202d6ae7826f [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 McCalla729c622012-02-17 03:33:10 +0000244 Types.GetFunctionType(Types.arrangeFunctionType(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 McCalla729c622012-02-17 03:33:10 +0000264 Types.GetFunctionType(Types.arrangeFunctionType(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
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000270
271 llvm::Constant *getCopyStructFn() {
272 CodeGen::CodeGenTypes &Types = CGM.getTypes();
273 ASTContext &Ctx = CGM.getContext();
274 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000275 SmallVector<CanQualType,5> Params;
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000276 Params.push_back(Ctx.VoidPtrTy);
277 Params.push_back(Ctx.VoidPtrTy);
278 Params.push_back(Ctx.LongTy);
279 Params.push_back(Ctx.BoolTy);
280 Params.push_back(Ctx.BoolTy);
Chris Lattner2192fe52011-07-18 04:24:23 +0000281 llvm::FunctionType *FTy =
John McCalla729c622012-02-17 03:33:10 +0000282 Types.GetFunctionType(Types.arrangeFunctionType(Ctx.VoidTy, Params,
283 FunctionType::ExtInfo(),
284 RequiredArgs::All));
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000285 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
286 }
287
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +0000288 /// This routine declares and returns address of:
289 /// void objc_copyCppObjectAtomic(
290 /// void *dest, const void *src,
291 /// void (*copyHelper) (void *dest, const void *source));
292 llvm::Constant *getCppAtomicObjectFunction() {
293 CodeGen::CodeGenTypes &Types = CGM.getTypes();
294 ASTContext &Ctx = CGM.getContext();
295 /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper);
296 SmallVector<CanQualType,3> Params;
297 Params.push_back(Ctx.VoidPtrTy);
298 Params.push_back(Ctx.VoidPtrTy);
299 Params.push_back(Ctx.VoidPtrTy);
300 llvm::FunctionType *FTy =
John McCalla729c622012-02-17 03:33:10 +0000301 Types.GetFunctionType(Types.arrangeFunctionType(Ctx.VoidTy, Params,
302 FunctionType::ExtInfo(),
303 RequiredArgs::All));
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +0000304 return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic");
305 }
306
Chris Lattnerce8754e2009-04-22 02:44:54 +0000307 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000308 CodeGen::CodeGenTypes &Types = CGM.getTypes();
309 ASTContext &Ctx = CGM.getContext();
Chris Lattnerce8754e2009-04-22 02:44:54 +0000310 // void objc_enumerationMutation (id)
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000311 SmallVector<CanQualType,1> Params;
John McCall2da83a32010-02-26 00:48:12 +0000312 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Chris Lattner2192fe52011-07-18 04:24:23 +0000313 llvm::FunctionType *FTy =
John McCalla729c622012-02-17 03:33:10 +0000314 Types.GetFunctionType(Types.arrangeFunctionType(Ctx.VoidTy, Params,
315 FunctionType::ExtInfo(),
316 RequiredArgs::All));
Chris Lattnerce8754e2009-04-22 02:44:54 +0000317 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
318 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000319
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000320 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattnerce8754e2009-04-22 02:44:54 +0000321 llvm::Constant *getGcReadWeakFn() {
322 // id objc_read_weak (id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000323 llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000324 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000325 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000326 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000327 }
328
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000329 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000330 llvm::Constant *getGcAssignWeakFn() {
331 // id objc_assign_weak (id, id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000332 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000333 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000334 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000335 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
336 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000337
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000338 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000339 llvm::Constant *getGcAssignGlobalFn() {
340 // id objc_assign_global(id, id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000341 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000342 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000343 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000344 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
345 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000346
Fariborz Jahanian217af242010-07-20 20:30:03 +0000347 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
348 llvm::Constant *getGcAssignThreadLocalFn() {
349 // id objc_assign_threadlocal(id src, id * dest)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000350 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Fariborz Jahanian217af242010-07-20 20:30:03 +0000351 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000352 llvm::FunctionType::get(ObjectPtrTy, args, false);
Fariborz Jahanian217af242010-07-20 20:30:03 +0000353 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
354 }
355
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000356 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000357 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000358 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000359 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
360 CGM.PtrDiffTy };
Owen Anderson170229f2009-07-14 23:10:40 +0000361 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000362 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000363 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
364 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000365
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000366 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
367 llvm::Constant *GcMemmoveCollectableFn() {
368 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000369 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
John McCall9dc0db22011-05-15 01:53:33 +0000370 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000371 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
372 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000373
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000374 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000375 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian217af242010-07-20 20:30:03 +0000376 // id objc_assign_strongCast(id, id *)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000377 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000378 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000379 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000380 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
381 }
Anders Carlsson9ab53d12009-02-16 22:59:18 +0000382
383 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000384 llvm::Constant *getExceptionThrowFn() {
385 // void objc_exception_throw(id)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000386 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattner0a696a422009-04-22 02:38:11 +0000387 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000388 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000389 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
390 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000391
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000392 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
393 llvm::Constant *getExceptionRethrowFn() {
394 // void objc_exception_rethrow(void)
John McCall9dc0db22011-05-15 01:53:33 +0000395 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000396 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
397 }
398
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000399 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerdcceee72009-04-06 16:53:45 +0000400 llvm::Constant *getSyncEnterFn() {
401 // void objc_sync_enter (id)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000402 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerdcceee72009-04-06 16:53:45 +0000403 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000404 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerdcceee72009-04-06 16:53:45 +0000405 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
406 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000407
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000408 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000409 llvm::Constant *getSyncExitFn() {
410 // void objc_sync_exit (id)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000411 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattner0a696a422009-04-22 02:38:11 +0000412 llvm::FunctionType *FTy =
John McCall9dc0db22011-05-15 01:53:33 +0000413 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000414 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
415 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000416
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000417 llvm::Constant *getSendFn(bool IsSuper) const {
418 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
419 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000420
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000421 llvm::Constant *getSendFn2(bool IsSuper) const {
422 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
423 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000424
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000425 llvm::Constant *getSendStretFn(bool IsSuper) const {
426 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
427 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000428
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000429 llvm::Constant *getSendStretFn2(bool IsSuper) const {
430 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
431 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000432
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000433 llvm::Constant *getSendFpretFn(bool IsSuper) const {
434 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
435 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000436
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000437 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
438 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
439 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000440
Anders Carlsson2f1a6c32011-10-31 16:27:11 +0000441 llvm::Constant *getSendFp2retFn(bool IsSuper) const {
442 return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn();
443 }
444
445 llvm::Constant *getSendFp2RetFn2(bool IsSuper) const {
446 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn();
447 }
448
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000449 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
450 ~ObjCCommonTypesHelper(){}
451};
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000452
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000453/// ObjCTypesHelper - Helper class that encapsulates lazy
454/// construction of varies types used during ObjC generation.
455class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000456public:
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000457 /// SymtabTy - LLVM type for struct objc_symtab.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000458 llvm::StructType *SymtabTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000459 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000460 llvm::Type *SymtabPtrTy;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000461 /// ModuleTy - LLVM type for struct objc_module.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000462 llvm::StructType *ModuleTy;
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000463
Daniel Dunbarb036db82008-08-13 03:21:16 +0000464 /// ProtocolTy - LLVM type for struct objc_protocol.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000465 llvm::StructType *ProtocolTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000466 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000467 llvm::Type *ProtocolPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000468 /// ProtocolExtensionTy - LLVM type for struct
469 /// objc_protocol_extension.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000470 llvm::StructType *ProtocolExtensionTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000471 /// ProtocolExtensionTy - LLVM type for struct
472 /// objc_protocol_extension *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000473 llvm::Type *ProtocolExtensionPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000474 /// MethodDescriptionTy - LLVM type for struct
475 /// objc_method_description.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000476 llvm::StructType *MethodDescriptionTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000477 /// MethodDescriptionListTy - LLVM type for struct
478 /// objc_method_description_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000479 llvm::StructType *MethodDescriptionListTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000480 /// MethodDescriptionListPtrTy - LLVM type for struct
481 /// objc_method_description_list *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000482 llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000483 /// ProtocolListTy - LLVM type for struct objc_property_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000484 llvm::StructType *ProtocolListTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000485 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000486 llvm::Type *ProtocolListPtrTy;
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000487 /// CategoryTy - LLVM type for struct objc_category.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000488 llvm::StructType *CategoryTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000489 /// ClassTy - LLVM type for struct objc_class.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000490 llvm::StructType *ClassTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000491 /// ClassPtrTy - LLVM type for struct objc_class *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000492 llvm::Type *ClassPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000493 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000494 llvm::StructType *ClassExtensionTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000495 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000496 llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000497 // IvarTy - LLVM type for struct objc_ivar.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000498 llvm::StructType *IvarTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000499 /// IvarListTy - LLVM type for struct objc_ivar_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000500 llvm::Type *IvarListTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000501 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000502 llvm::Type *IvarListPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000503 /// MethodListTy - LLVM type for struct objc_method_list.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000504 llvm::Type *MethodListTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000505 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000506 llvm::Type *MethodListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000507
Anders Carlsson9ff22482008-09-09 10:10:21 +0000508 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000509 llvm::Type *ExceptionDataTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000510
Anders Carlsson9ff22482008-09-09 10:10:21 +0000511 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000512 llvm::Constant *getExceptionTryEnterFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000513 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000514 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000515 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000516 "objc_exception_try_enter");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000517 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000518
519 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000520 llvm::Constant *getExceptionTryExitFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000521 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson170229f2009-07-14 23:10:40 +0000522 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000523 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000524 "objc_exception_try_exit");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000525 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000526
527 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000528 llvm::Constant *getExceptionExtractFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000529 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000530 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000531 params, false),
Chris Lattnerc6406db2009-04-22 02:26:14 +0000532 "objc_exception_extract");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000533 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000534
Anders Carlsson9ff22482008-09-09 10:10:21 +0000535 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000536 llvm::Constant *getExceptionMatchFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000537 llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000538 return CGM.CreateRuntimeFunction(
John McCall9dc0db22011-05-15 01:53:33 +0000539 llvm::FunctionType::get(CGM.Int32Ty, params, false),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000540 "objc_exception_match");
541
Chris Lattnerc6406db2009-04-22 02:26:14 +0000542 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000543
Anders Carlsson9ff22482008-09-09 10:10:21 +0000544 /// SetJmpFn - LLVM _setjmp function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000545 llvm::Constant *getSetJmpFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000546 // This is specifically the prototype for x86.
Chris Lattnera5f58b02011-07-09 17:41:47 +0000547 llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
John McCall9dc0db22011-05-15 01:53:33 +0000548 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
549 params, false),
Bill Wendlingbcefeae2011-11-29 00:10:10 +0000550 "_setjmp",
551 llvm::Attribute::ReturnsTwice);
Chris Lattnerc6406db2009-04-22 02:26:14 +0000552 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000553
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000554public:
555 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000556 ~ObjCTypesHelper() {}
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000557};
558
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000559/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000560/// modern abi
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000561class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000562public:
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000563
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000564 // MethodListnfABITy - LLVM for struct _method_list_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000565 llvm::StructType *MethodListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000566
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000567 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000568 llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000569
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000570 // ProtocolnfABITy = LLVM for struct _protocol_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000571 llvm::StructType *ProtocolnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000572
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000573 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000574 llvm::Type *ProtocolnfABIPtrTy;
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000575
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000576 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
Chris Lattnera5f58b02011-07-09 17:41:47 +0000577 llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000578
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000579 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000580 llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000581
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000582 // ClassnfABITy - LLVM for struct _class_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000583 llvm::StructType *ClassnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000584
Fariborz Jahanian71394042009-01-23 23:53:38 +0000585 // ClassnfABIPtrTy - LLVM for struct _class_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000586 llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000587
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000588 // IvarnfABITy - LLVM for struct _ivar_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000589 llvm::StructType *IvarnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000590
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000591 // IvarListnfABITy - LLVM for struct _ivar_list_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000592 llvm::StructType *IvarListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000593
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000594 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000595 llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000596
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000597 // ClassRonfABITy - LLVM for struct _class_ro_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000598 llvm::StructType *ClassRonfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000599
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000600 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000601 llvm::Type *ImpnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000602
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000603 // CategorynfABITy - LLVM for struct _category_t
Chris Lattnera5f58b02011-07-09 17:41:47 +0000604 llvm::StructType *CategorynfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000605
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000606 // New types for nonfragile abi messaging.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000607
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000608 // MessageRefTy - LLVM for:
609 // struct _message_ref_t {
610 // IMP messenger;
611 // SEL name;
612 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +0000613 llvm::StructType *MessageRefTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000614 // MessageRefCTy - clang type for struct _message_ref_t
615 QualType MessageRefCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000616
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000617 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000618 llvm::Type *MessageRefPtrTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000619 // MessageRefCPtrTy - clang type for struct _message_ref_t*
620 QualType MessageRefCPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000621
Fariborz Jahanian4e87c832009-02-05 01:13:09 +0000622 // MessengerTy - Type of the messenger (shown as IMP above)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000623 llvm::FunctionType *MessengerTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000624
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000625 // SuperMessageRefTy - LLVM for:
626 // struct _super_message_ref_t {
627 // SUPER_IMP messenger;
628 // SEL name;
629 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +0000630 llvm::StructType *SuperMessageRefTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000631
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000632 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Chris Lattnera5f58b02011-07-09 17:41:47 +0000633 llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000634
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000635 llvm::Constant *getMessageSendFixupFn() {
636 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000637 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000638 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000639 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000640 "objc_msgSend_fixup");
641 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000642
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000643 llvm::Constant *getMessageSendFpretFixupFn() {
644 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000645 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000646 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000647 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000648 "objc_msgSend_fpret_fixup");
649 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000650
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000651 llvm::Constant *getMessageSendStretFixupFn() {
652 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000653 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000654 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000655 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000656 "objc_msgSend_stret_fixup");
657 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000658
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000659 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000660 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000661 // struct _super_message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000662 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000663 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000664 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000665 "objc_msgSendSuper2_fixup");
666 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000667
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000668 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000669 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000670 // struct _super_message_ref_t*, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +0000671 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000672 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000673 params, true),
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000674 "objc_msgSendSuper2_stret_fixup");
675 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000676
Chris Lattnera7c00b42009-04-22 02:15:23 +0000677 llvm::Constant *getObjCEndCatchFn() {
John McCall9dc0db22011-05-15 01:53:33 +0000678 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000679 "objc_end_catch");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000680
Chris Lattnera7c00b42009-04-22 02:15:23 +0000681 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000682
Chris Lattnera7c00b42009-04-22 02:15:23 +0000683 llvm::Constant *getObjCBeginCatchFn() {
Chris Lattnera5f58b02011-07-09 17:41:47 +0000684 llvm::Type *params[] = { Int8PtrTy };
Owen Anderson9793f0e2009-07-29 22:16:19 +0000685 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall9dc0db22011-05-15 01:53:33 +0000686 params, false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000687 "objc_begin_catch");
688 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +0000689
Chris Lattnera5f58b02011-07-09 17:41:47 +0000690 llvm::StructType *EHTypeTy;
691 llvm::Type *EHTypePtrTy;
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +0000692
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000693 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
694 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000695};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000696
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000697class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000698public:
699 // FIXME - accessibility
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000700 class GC_IVAR {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +0000701 public:
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000702 unsigned ivar_bytepos;
703 unsigned ivar_size;
704 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000705 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar22b0ada2009-04-23 01:29:05 +0000706
707 // Allow sorting based on byte pos.
708 bool operator<(const GC_IVAR &b) const {
709 return ivar_bytepos < b.ivar_bytepos;
710 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000711 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000712
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000713 class SKIP_SCAN {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000714 public:
715 unsigned skip;
716 unsigned scan;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000717 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000718 : skip(_skip), scan(_scan) {}
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000719 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000720
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000721protected:
Owen Andersonae86c192009-07-13 04:10:07 +0000722 llvm::LLVMContext &VMContext;
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000723 // FIXME! May not be needing this after all.
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000724 unsigned ObjCABI;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000725
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000726 // gc ivar layout bitmap calculation helper caches.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000727 SmallVector<GC_IVAR, 16> SkipIvars;
728 SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000729
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000730 /// LazySymbols - Symbols to generate a lazy reference for. See
731 /// DefinedSymbols and FinishModule().
Daniel Dunbard027a922009-09-07 00:20:42 +0000732 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000733
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000734 /// DefinedSymbols - External symbols which are defined by this
735 /// module. The symbols in this list and LazySymbols are used to add
736 /// special linker symbols which ensure that Objective-C modules are
737 /// linked properly.
Daniel Dunbard027a922009-09-07 00:20:42 +0000738 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000739
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000740 /// ClassNames - uniqued class names.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000741 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000742
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000743 /// MethodVarNames - uniqued method variable names.
744 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000745
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +0000746 /// DefinedCategoryNames - list of category names in form Class_Category.
747 llvm::SetVector<std::string> DefinedCategoryNames;
748
Daniel Dunbarb036db82008-08-13 03:21:16 +0000749 /// MethodVarTypes - uniqued method type signatures. We have to use
750 /// a StringMap here because have no other unique reference.
751 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000752
Daniel Dunbar3c76cb52008-08-26 21:51:14 +0000753 /// MethodDefinitions - map of methods which have been defined in
754 /// this translation unit.
755 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000756
Daniel Dunbar80a840b2008-08-23 00:19:03 +0000757 /// PropertyNames - uniqued method variable names.
758 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000759
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000760 /// ClassReferences - uniqued class references.
761 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000762
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000763 /// SelectorReferences - uniqued selector references.
764 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000765
Daniel Dunbarb036db82008-08-13 03:21:16 +0000766 /// Protocols - Protocols for which an objc_protocol structure has
767 /// been emitted. Forward declarations are handled by creating an
768 /// empty structure whose initializer is filled in when/if defined.
769 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000770
Daniel Dunbarc475d422008-10-29 22:36:39 +0000771 /// DefinedProtocols - Protocols which have actually been
772 /// defined. We should not need this, see FIXME in GenerateProtocol.
773 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000774
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000775 /// DefinedClasses - List of defined classes.
Bill Wendling8392a472012-02-07 09:40:07 +0000776 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedClasses;
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000777
778 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
Bill Wendling8392a472012-02-07 09:40:07 +0000779 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyClasses;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000780
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000781 /// DefinedCategories - List of defined categories.
Bill Wendling8392a472012-02-07 09:40:07 +0000782 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000783
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000784 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
Bill Wendling8392a472012-02-07 09:40:07 +0000785 llvm::SmallVector<llvm::GlobalValue*, 16> DefinedNonLazyCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000786
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000787 /// GetNameForMethod - Return a name for the given method.
788 /// \param[out] NameOut - The return value.
789 void GetNameForMethod(const ObjCMethodDecl *OMD,
790 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000791 SmallVectorImpl<char> &NameOut);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000792
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000793 /// GetMethodVarName - Return a unique constant for the given
794 /// selector's name. The return value has type char *.
795 llvm::Constant *GetMethodVarName(Selector Sel);
796 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000797
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000798 /// GetMethodVarType - Return a unique constant for the given
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000799 /// method's type encoding string. The return value has type char *.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000800
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000801 // FIXME: This is a horrible name.
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000802 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D,
803 bool Extended = false);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000804 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000805
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000806 /// GetPropertyName - Return a unique constant for the given
807 /// name. The return value has type char *.
808 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000809
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000810 // FIXME: This can be dropped once string functions are unified.
811 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
812 const Decl *Container);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000813
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +0000814 /// GetClassName - Return a unique constant for the given selector's
815 /// name. The return value has type char *.
816 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000817
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +0000818 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
819
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000820 /// BuildIvarLayout - Builds ivar layout bitmap for the class
821 /// implementation for the __strong or __weak case.
822 ///
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000823 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
824 bool ForStrongLayout);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +0000825
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +0000826 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000827
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000828 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000829 unsigned int BytePos, bool ForStrongLayout,
830 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000831 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000832 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000833 const RecordDecl *RD,
Bill Wendlingf1a3fca2012-02-22 09:30:11 +0000834 ArrayRef<const FieldDecl*> RecFields,
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000835 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000836 bool &HasUnion);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000837
Fariborz Jahanian01dff422009-03-05 19:17:31 +0000838 /// GetIvarLayoutName - Returns a unique constant for the given
839 /// ivar layout bitmap.
840 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
841 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000842
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000843 /// EmitPropertyList - Emit the given property list. The return
844 /// value has type PropertyListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000845 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000846 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000847 const ObjCContainerDecl *OCD,
848 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000849
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000850 /// EmitProtocolMethodTypes - Generate the array of extended method type
851 /// strings. The return value has type Int8PtrPtrTy.
852 llvm::Constant *EmitProtocolMethodTypes(Twine Name,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +0000853 ArrayRef<llvm::Constant*> MethodTypes,
Bob Wilson5f4e3a72011-11-30 01:57:58 +0000854 const ObjCCommonTypesHelper &ObjCTypes);
855
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000856 /// PushProtocolProperties - Push protocol's property on the input stack.
Bill Wendlinga515b582012-02-09 22:16:49 +0000857 void PushProtocolProperties(
858 llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
859 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
860 const Decl *Container,
861 const ObjCProtocolDecl *PROTO,
862 const ObjCCommonTypesHelper &ObjCTypes);
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000863
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000864 /// GetProtocolRef - Return a reference to the internal protocol
865 /// description, creating an empty one if it has not been
866 /// defined. The return value has type ProtocolPtrTy.
867 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanian67726212009-03-08 20:18:37 +0000868
Daniel Dunbar30c65362009-03-09 20:09:19 +0000869 /// CreateMetadataVar - Create a global variable with internal
870 /// linkage for use by the Objective-C runtime.
871 ///
872 /// This is a convenience wrapper which not only creates the
873 /// variable, but also sets the section and alignment and adds the
Chris Lattnerf56501c2009-07-17 23:57:13 +0000874 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000875 ///
876 /// \param Name - The variable name.
877 /// \param Init - The variable initializer; this is also used to
878 /// define the type of the variable.
879 /// \param Section - The section the variable should go into, or 0.
880 /// \param Align - The alignment for the variable, or 0.
881 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar4527d302009-04-14 17:42:51 +0000882 /// "llvm.used".
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000883 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +0000884 llvm::Constant *Init,
885 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000886 unsigned Align,
887 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +0000888
John McCall9e8bb002011-05-14 03:10:52 +0000889 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
890 ReturnValueSlot Return,
891 QualType ResultType,
892 llvm::Value *Sel,
893 llvm::Value *Arg0,
894 QualType Arg0Ty,
895 bool IsSuper,
896 const CallArgList &CallArgs,
897 const ObjCMethodDecl *OMD,
898 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000899
Daniel Dunbar5e639272010-04-25 20:39:01 +0000900 /// EmitImageInfo - Emit the image info marker used to encode some module
901 /// level information.
902 void EmitImageInfo();
903
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000904public:
Owen Andersonae86c192009-07-13 04:10:07 +0000905 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
John McCalla729c622012-02-17 03:33:10 +0000906 CGObjCRuntime(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000907
David Chisnall481e3a82010-01-23 02:40:42 +0000908 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000909
Fariborz Jahanian99113fd2009-01-26 21:38:32 +0000910 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
911 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000912
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000913 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000914
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000915 /// GetOrEmitProtocol - Get the protocol object for the given
916 /// declaration, emitting it if necessary. The return value has type
917 /// ProtocolPtrTy.
918 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000919
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000920 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
921 /// object for the given declaration, emitting it if needed. These
922 /// forward references will be filled in with empty bodies if no
923 /// definition is seen. The return value has type ProtocolPtrTy.
924 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall351762c2011-02-07 10:33:21 +0000925 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
926 const CGBlockInfo &blockInfo);
Fariborz Jahanianc05349e2010-08-04 16:57:49 +0000927
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000928};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000929
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000930class CGObjCMac : public CGObjCCommonMac {
931private:
932 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000933
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000934 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000935 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000936 void EmitModuleInfo();
937
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000938 /// EmitModuleSymols - Emit module symbols, the list of defined
939 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000940 llvm::Constant *EmitModuleSymbols();
941
Daniel Dunbar3ad53482008-08-11 21:35:06 +0000942 /// FinishModule - Write out global data structures at the end of
943 /// processing a translation unit.
944 void FinishModule();
Daniel Dunbarb036db82008-08-13 03:21:16 +0000945
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000946 /// EmitClassExtension - Generate the class extension structure used
947 /// to store the weak ivar layout and properties. The return value
948 /// has type ClassExtensionPtrTy.
949 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
950
951 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
952 /// for the given class.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000953 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000954 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +0000955
John McCall31168b02011-06-15 23:02:42 +0000956 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
957 IdentifierInfo *II);
958
959 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
960
Fariborz Jahanianeb80c982009-11-12 20:14:24 +0000961 /// EmitSuperClassRef - Emits reference to class's main metadata class.
962 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000963
964 /// EmitIvarList - Emit the ivar list for the given
965 /// implementation. If ForClass is true the list of class ivars
966 /// (i.e. metaclass ivars) is emitted, otherwise the list of
967 /// interface ivars will be emitted. The return value has type
968 /// IvarListPtrTy.
969 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +0000970 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000971
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000972 /// EmitMetaClass - Emit a forward reference to the class structure
973 /// for the metaclass of the given interface. The return value has
974 /// type ClassPtrTy.
975 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
976
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000977 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +0000978 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000979 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
980 llvm::Constant *Protocols,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +0000981 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000982
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000983 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000984
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000985 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000986
987 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +0000988 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000989 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000990 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +0000991 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000992
993 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000994 /// method declarations.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000995 /// - TypeName: The name for the type containing the methods.
996 /// - IsProtocol: True iff these methods are for a protocol.
997 /// - ClassMethds: True iff these are class methods.
998 /// - Required: When true, only "required" methods are
999 /// listed. Similarly, when false only "optional" methods are
1000 /// listed. For classes this should always be true.
1001 /// - begin, end: The method list to output.
1002 ///
1003 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001004 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001005 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001006 ArrayRef<llvm::Constant*> Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001007
Daniel Dunbarc475d422008-10-29 22:36:39 +00001008 /// GetOrEmitProtocol - Get the protocol object for the given
1009 /// declaration, emitting it if necessary. The return value has type
1010 /// ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001011 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001012
1013 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1014 /// object for the given declaration, emitting it if needed. These
1015 /// forward references will be filled in with empty bodies if no
1016 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001017 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001018
Daniel Dunbarb036db82008-08-13 03:21:16 +00001019 /// EmitProtocolExtension - Generate the protocol extension
1020 /// structure used to store optional instance and class methods, and
1021 /// protocol properties. The return value has type
1022 /// ProtocolExtensionPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001023 llvm::Constant *
1024 EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001025 ArrayRef<llvm::Constant*> OptInstanceMethods,
1026 ArrayRef<llvm::Constant*> OptClassMethods,
1027 ArrayRef<llvm::Constant*> MethodTypesExt);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001028
1029 /// EmitProtocolList - Generate the list of referenced
1030 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001031 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001032 ObjCProtocolDecl::protocol_iterator begin,
1033 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001034
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001035 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1036 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001037 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1038 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001039
1040public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001041 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001042
Fariborz Jahanian71394042009-01-23 23:53:38 +00001043 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001044
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001045 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001046 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001047 QualType ResultType,
1048 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001049 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001050 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001051 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001052 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001053
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001054 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001055 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001056 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001057 QualType ResultType,
1058 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001059 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001060 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001061 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001062 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001063 const CallArgList &CallArgs,
1064 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001065
Daniel Dunbarcb463852008-11-01 01:53:16 +00001066 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001067 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001068
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001069 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1070 bool lval = false);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001071
1072 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1073 /// untyped one.
1074 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1075 const ObjCMethodDecl *Method);
1076
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001077 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001078
Daniel Dunbar92992502008-08-15 22:20:32 +00001079 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001080
Daniel Dunbar92992502008-08-15 22:20:32 +00001081 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001082
David Chisnall92d436b2012-01-31 18:59:20 +00001083 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {};
1084
Daniel Dunbarcb463852008-11-01 01:53:16 +00001085 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001086 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001087
Chris Lattnerd4808922009-03-22 21:03:39 +00001088 virtual llvm::Constant *GetPropertyGetFunction();
1089 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall168b80f2010-12-26 22:13:16 +00001090 virtual llvm::Constant *GetGetStructFunction();
1091 virtual llvm::Constant *GetSetStructFunction();
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00001092 virtual llvm::Constant *GetCppAtomicObjectFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001093 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001094
John McCallbd309292010-07-06 01:34:17 +00001095 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1096 const ObjCAtTryStmt &S);
1097 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1098 const ObjCAtSynchronizedStmt &S);
1099 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001100 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1101 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001102 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001103 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001104 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001105 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001106 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001107 llvm::Value *src, llvm::Value *dest,
1108 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001109 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001110 llvm::Value *src, llvm::Value *dest,
1111 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001112 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1113 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001114 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1115 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001116 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001117
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001118 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1119 QualType ObjectTy,
1120 llvm::Value *BaseValue,
1121 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001122 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001123 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001124 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001125 const ObjCIvarDecl *Ivar);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001126
1127 /// GetClassGlobal - Return the global variable for the Objective-C
1128 /// class of the given name.
1129 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikie83d382b2011-09-23 05:06:16 +00001130 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001131 }
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001132};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001133
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001134class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001135private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001136 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001137 llvm::GlobalVariable* ObjCEmptyCacheVar;
1138 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001139
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001140 /// SuperClassReferences - uniqued super class references.
1141 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001142
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001143 /// MetaClassReferences - uniqued meta class references.
1144 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001145
1146 /// EHTypeReferences - uniqued class ehtype references.
1147 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001148
John McCall9e8bb002011-05-14 03:10:52 +00001149 /// VTableDispatchMethods - List of methods for which we generate
1150 /// vtable-based message dispatch.
1151 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001152
Fariborz Jahanian67260552009-11-17 21:37:35 +00001153 /// DefinedMetaClasses - List of defined meta-classes.
1154 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1155
John McCall9e8bb002011-05-14 03:10:52 +00001156 /// isVTableDispatchedSelector - Returns true if SEL is a
1157 /// vtable-based selector.
1158 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001159
Fariborz Jahanian71394042009-01-23 23:53:38 +00001160 /// FinishNonFragileABIModule - Write out global data structures at the end of
1161 /// processing a translation unit.
1162 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001163
Daniel Dunbar19573e72009-05-15 21:48:48 +00001164 /// AddModuleClassList - Add the given list of class pointers to the
1165 /// module with the provided symbol and section names.
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001166 void AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00001167 const char *SymbolName,
1168 const char *SectionName);
1169
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001170 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1171 unsigned InstanceStart,
1172 unsigned InstanceSize,
1173 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001174 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001175 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001176 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001177 llvm::Constant *ClassRoGV,
1178 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001179
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001180 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001181
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001182 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001183
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001184 /// EmitMethodList - Emit the method list for the given
1185 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001186 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001187 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001188 ArrayRef<llvm::Constant*> Methods);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001189 /// EmitIvarList - Emit the ivar list for the given
1190 /// implementation. If ForClass is true the list of class ivars
1191 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1192 /// interface ivars will be emitted. The return value has type
1193 /// IvarListnfABIPtrTy.
1194 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001195
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001196 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001197 const ObjCIvarDecl *Ivar,
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00001198 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001199
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001200 /// GetOrEmitProtocol - Get the protocol object for the given
1201 /// declaration, emitting it if necessary. The return value has type
1202 /// ProtocolPtrTy.
1203 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001204
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001205 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1206 /// object for the given declaration, emitting it if needed. These
1207 /// forward references will be filled in with empty bodies if no
1208 /// definition is seen. The return value has type ProtocolPtrTy.
1209 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001210
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001211 /// EmitProtocolList - Generate the list of referenced
1212 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001213 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001214 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001215 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001216
John McCall9e8bb002011-05-14 03:10:52 +00001217 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1218 ReturnValueSlot Return,
1219 QualType ResultType,
1220 Selector Sel,
1221 llvm::Value *Receiver,
1222 QualType Arg0Ty,
1223 bool IsSuper,
1224 const CallArgList &CallArgs,
1225 const ObjCMethodDecl *Method);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001226
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001227 /// GetClassGlobal - Return the global variable for the Objective-C
1228 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001229 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian7bd3d1c2011-05-17 22:21:16 +00001230
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001231 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001232 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001233 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001234 const ObjCInterfaceDecl *ID);
John McCall31168b02011-06-15 23:02:42 +00001235
1236 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1237 IdentifierInfo *II);
1238
1239 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001240
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001241 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1242 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001243 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1244 const ObjCInterfaceDecl *ID);
1245
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001246 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1247 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001248 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001249 const ObjCInterfaceDecl *ID);
1250
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001251 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1252 /// the given ivar.
1253 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001254 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001255 const ObjCInterfaceDecl *ID,
1256 const ObjCIvarDecl *Ivar);
1257
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001258 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1259 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001260 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1261 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001262
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001263 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001264 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001265 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001266 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001267
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001268 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001269 return "OBJC_METACLASS_$_";
1270 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001271
Daniel Dunbar15894b72009-04-07 05:48:37 +00001272 const char *getClassSymbolPrefix() const {
1273 return "OBJC_CLASS_$_";
1274 }
1275
Daniel Dunbar961202372009-05-03 12:57:56 +00001276 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001277 uint32_t &InstanceStart,
1278 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001279
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001280 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001281 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001282 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1283 return CGM.getContext().Selectors.getSelector(0, &II);
1284 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001285
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001286 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001287 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1288 return CGM.getContext().Selectors.getSelector(1, &II);
1289 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001290
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001291 /// ImplementationIsNonLazy - Check whether the given category or
1292 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001293 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001294
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001295public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001296 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001297 // FIXME. All stubs for now!
1298 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001299
Fariborz Jahanian71394042009-01-23 23:53:38 +00001300 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001301 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001302 QualType ResultType,
1303 Selector Sel,
1304 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001305 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001306 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001307 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001308
1309 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001310 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001311 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001312 QualType ResultType,
1313 Selector Sel,
1314 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001315 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001316 llvm::Value *Receiver,
1317 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001318 const CallArgList &CallArgs,
1319 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001320
Fariborz Jahanian71394042009-01-23 23:53:38 +00001321 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001322 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001323
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001324 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1325 bool lvalue = false)
1326 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001327
1328 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1329 /// untyped one.
1330 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1331 const ObjCMethodDecl *Method)
1332 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001333
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001334 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001335
Fariborz Jahanian71394042009-01-23 23:53:38 +00001336 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
David Chisnall92d436b2012-01-31 18:59:20 +00001337
1338 virtual void RegisterAlias(const ObjCCompatibleAliasDecl *OAD) {};
1339
Fariborz Jahanian71394042009-01-23 23:53:38 +00001340 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001341 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001342
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001343 virtual llvm::Constant *GetEHType(QualType T);
John McCall2ca705e2010-07-24 00:37:23 +00001344
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001345 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001346 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001347 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001348 virtual llvm::Constant *GetPropertySetFunction() {
1349 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001350 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001351
David Chisnall168b80f2010-12-26 22:13:16 +00001352 virtual llvm::Constant *GetSetStructFunction() {
1353 return ObjCTypes.getCopyStructFn();
1354 }
1355 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001356 return ObjCTypes.getCopyStructFn();
1357 }
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00001358 virtual llvm::Constant *GetCppAtomicObjectFunction() {
1359 return ObjCTypes.getCppAtomicObjectFunction();
1360 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001361
Chris Lattnerd4808922009-03-22 21:03:39 +00001362 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001363 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001364 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001365
John McCallbd309292010-07-06 01:34:17 +00001366 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1367 const ObjCAtTryStmt &S);
1368 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1369 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001370 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001371 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001372 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001373 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001374 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001375 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001376 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001377 llvm::Value *src, llvm::Value *dest,
1378 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001379 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001380 llvm::Value *src, llvm::Value *dest,
1381 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001382 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001383 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001384 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1385 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001386 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001387 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1388 QualType ObjectTy,
1389 llvm::Value *BaseValue,
1390 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001391 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001392 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001393 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001394 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001395};
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001396
1397/// A helper class for performing the null-initialization of a return
1398/// value.
1399struct NullReturnState {
1400 llvm::BasicBlock *NullBB;
1401 llvm::BasicBlock *callBB;
1402 NullReturnState() : NullBB(0), callBB(0) {}
1403
1404 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1405 // Make blocks for the null-init and call edges.
1406 NullBB = CGF.createBasicBlock("msgSend.nullinit");
1407 callBB = CGF.createBasicBlock("msgSend.call");
1408
1409 // Check for a null receiver and, if there is one, jump to the
1410 // null-init test.
1411 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1412 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1413
1414 // Otherwise, start performing the call.
1415 CGF.EmitBlock(callBB);
1416 }
1417
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001418 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType,
1419 const CallArgList &CallArgs,
1420 const ObjCMethodDecl *Method) {
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001421 if (!NullBB) return result;
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001422
1423 llvm::Value *NullInitPtr = 0;
1424 if (result.isScalar() && !resultType->isVoidType()) {
1425 NullInitPtr = CGF.CreateTempAlloca(result.getScalarVal()->getType());
1426 CGF.Builder.CreateStore(result.getScalarVal(), NullInitPtr);
1427 }
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001428
1429 // Finish the call path.
1430 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1431 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1432
1433 // Emit the null-init block and perform the null-initialization there.
1434 CGF.EmitBlock(NullBB);
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001435
1436 // Release consumed arguments along the null-receiver path.
1437 if (Method) {
1438 CallArgList::const_iterator I = CallArgs.begin();
1439 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1440 e = Method->param_end(); i != e; ++i, ++I) {
1441 const ParmVarDecl *ParamDecl = (*i);
1442 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1443 RValue RV = I->RV;
1444 assert(RV.isScalar() &&
1445 "NullReturnState::complete - arg not on object");
1446 CGF.EmitARCRelease(RV.getScalarVal(), true);
1447 }
1448 }
1449 }
1450
1451 if (result.isScalar()) {
1452 if (NullInitPtr)
1453 CGF.EmitNullInitialization(NullInitPtr, resultType);
1454 // Jump to the continuation block.
1455 CGF.EmitBlock(contBB);
1456 return NullInitPtr ? RValue::get(CGF.Builder.CreateLoad(NullInitPtr))
1457 : result;
1458 }
1459
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001460 if (!resultType->isAnyComplexType()) {
1461 assert(result.isAggregate() && "null init of non-aggregate result?");
1462 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1463 // Jump to the continuation block.
1464 CGF.EmitBlock(contBB);
1465 return result;
1466 }
1467
1468 // _Complex type
1469 // FIXME. Now easy to handle any other scalar type whose result is returned
1470 // in memory due to ABI limitations.
1471 CGF.EmitBlock(contBB);
1472 CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal();
1473 llvm::Type *MemberType = CallCV.first->getType();
1474 llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType);
1475 // Create phi instruction for scalar complex value.
1476 llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2);
1477 PHIReal->addIncoming(ZeroCV, NullBB);
1478 PHIReal->addIncoming(CallCV.first, callBB);
1479 llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2);
1480 PHIImag->addIncoming(ZeroCV, NullBB);
1481 PHIImag->addIncoming(CallCV.second, callBB);
1482 return RValue::getComplex(PHIReal, PHIImag);
1483 }
1484};
1485
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001486} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001487
1488/* *** Helper Functions *** */
1489
1490/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001491static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001492 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001493 unsigned idx0,
1494 unsigned idx1) {
1495 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001496 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1497 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001498 };
Jay Foaded8db7d2011-07-21 14:31:17 +00001499 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001500}
1501
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001502/// hasObjCExceptionAttribute - Return true if this class or any super
1503/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001504static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001505 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001506 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001507 return true;
1508 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001509 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001510 return false;
1511}
1512
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001513/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001514
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001515CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001516 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001517 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001518 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001519}
1520
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001521/// GetClass - Return a reference to the class for the given interface
1522/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001523llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001524 const ObjCInterfaceDecl *ID) {
1525 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001526}
1527
1528/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001529llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1530 bool lval) {
1531 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001532}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001533llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001534 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001535 return EmitSelector(Builder, Method->getSelector());
1536}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001537
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001538llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001539 if (T->isObjCIdType() ||
1540 T->isObjCQualifiedIdType()) {
1541 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001542 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00001543 }
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001544 if (T->isObjCClassType() ||
1545 T->isObjCQualifiedClassType()) {
1546 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor97673472011-08-11 20:58:55 +00001547 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00001548 }
1549 if (T->isObjCObjectPointerType())
1550 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1551
John McCall2ca705e2010-07-24 00:37:23 +00001552 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
John McCall2ca705e2010-07-24 00:37:23 +00001553}
1554
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001555/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001556/*
1557 struct __builtin_CFString {
1558 const int *isa; // point to __CFConstantStringClassReference
1559 int flags;
1560 const char *str;
1561 long length;
1562 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001563*/
1564
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001565/// or Generate a constant NSString object.
1566/*
1567 struct __builtin_NSString {
1568 const int *isa; // point to __NSConstantStringClassReference
1569 const char *str;
1570 unsigned int length;
1571 };
1572*/
1573
Fariborz Jahanian71394042009-01-23 23:53:38 +00001574llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001575 const StringLiteral *SL) {
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001576 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1577 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian50c925f2010-10-19 17:19:29 +00001578 CGM.GetAddrOfConstantString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001579}
1580
1581/// Generates a message send where the super is the receiver. This is
1582/// a message send to self with special delivery semantics indicating
1583/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001584CodeGen::RValue
1585CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001586 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001587 QualType ResultType,
1588 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001589 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001590 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001591 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001592 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001593 const CodeGen::CallArgList &CallArgs,
1594 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001595 // Create and init a super structure; this is a (receiver, class)
1596 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001597 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00001598 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001599 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001600 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001601 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001602 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001603
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001604 // If this is a class message the metaclass is passed as the target.
1605 llvm::Value *Target;
1606 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001607 if (isCategoryImpl) {
1608 // Message sent to 'super' in a class method defined in a category
1609 // implementation requires an odd treatment.
1610 // If we are in a class method, we must retrieve the
1611 // _metaclass_ for the current class, pointed at by
1612 // the class's "isa" pointer. The following assumes that
1613 // isa" is the first ivar in a class (which it must be).
1614 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1615 Target = CGF.Builder.CreateStructGEP(Target, 0);
1616 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001617 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001618 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1619 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1620 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1621 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001622 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001623 }
1624 else if (isCategoryImpl)
1625 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1626 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001627 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1628 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1629 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001630 }
Mike Stump18bb9282009-05-16 07:57:57 +00001631 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1632 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00001633 llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001634 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001635 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001636 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001637 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall9e8bb002011-05-14 03:10:52 +00001638 return EmitMessageSend(CGF, Return, ResultType,
1639 EmitSelector(CGF.Builder, Sel),
1640 ObjCSuper, ObjCTypes.SuperPtrCTy,
1641 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001642}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001643
1644/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001645CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001646 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001647 QualType ResultType,
1648 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001649 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001650 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001651 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001652 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00001653 return EmitMessageSend(CGF, Return, ResultType,
1654 EmitSelector(CGF.Builder, Sel),
1655 Receiver, CGF.getContext().getObjCIdType(),
1656 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001657}
1658
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001659CodeGen::RValue
John McCall9e8bb002011-05-14 03:10:52 +00001660CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1661 ReturnValueSlot Return,
1662 QualType ResultType,
1663 llvm::Value *Sel,
1664 llvm::Value *Arg0,
1665 QualType Arg0Ty,
1666 bool IsSuper,
1667 const CallArgList &CallArgs,
1668 const ObjCMethodDecl *Method,
1669 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001670 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001671 if (!IsSuper)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00001672 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman43dca6a2011-05-02 17:57:46 +00001673 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1674 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCall31168b02011-06-15 23:02:42 +00001675 ActualArgs.addFrom(CallArgs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001676
John McCalla729c622012-02-17 03:33:10 +00001677 // If we're calling a method, use the formal signature.
1678 MessageSendInfo MSI = getMessageSendInfo(Method, ResultType, ActualArgs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001679
Anders Carlsson280e61f12010-06-21 20:59:55 +00001680 if (Method)
1681 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1682 CGM.getContext().getCanonicalType(ResultType) &&
1683 "Result type mismatch!");
1684
John McCall5880fb82011-05-14 21:12:11 +00001685 NullReturnState nullReturn;
1686
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001687 llvm::Constant *Fn = NULL;
John McCalla729c622012-02-17 03:33:10 +00001688 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
Fariborz Jahanian715fdd52012-01-29 20:27:13 +00001689 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001690 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001691 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001692 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1693 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1694 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlsson2f1a6c32011-10-31 16:27:11 +00001695 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1696 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1697 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001698 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001699 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001700 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001701 }
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001702
1703 bool requiresnullCheck = false;
1704 if (CGM.getLangOptions().ObjCAutoRefCount && Method)
1705 for (ObjCMethodDecl::param_const_iterator i = Method->param_begin(),
1706 e = Method->param_end(); i != e; ++i) {
1707 const ParmVarDecl *ParamDecl = (*i);
1708 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
1709 if (!nullReturn.NullBB)
1710 nullReturn.init(CGF, Arg0);
1711 requiresnullCheck = true;
1712 break;
1713 }
1714 }
1715
John McCalla729c622012-02-17 03:33:10 +00001716 Fn = llvm::ConstantExpr::getBitCast(Fn, MSI.MessengerType);
1717 RValue rvalue = CGF.EmitCall(MSI.CallInfo, Fn, Return, ActualArgs);
Fariborz Jahanianf2bda692012-01-30 21:40:37 +00001718 return nullReturn.complete(CGF, rvalue, ResultType, CallArgs,
1719 requiresnullCheck ? Method : 0);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001720}
1721
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001722static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1723 if (FQT.isObjCGCStrong())
1724 return Qualifiers::Strong;
1725
John McCall31168b02011-06-15 23:02:42 +00001726 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001727 return Qualifiers::Weak;
1728
Fariborz Jahanian430b35e2012-02-16 00:15:02 +00001729 // check for __unsafe_unretained
1730 if (FQT.getObjCLifetime() == Qualifiers::OCL_ExplicitNone)
1731 return Qualifiers::GCNone;
1732
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001733 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1734 return Qualifiers::Strong;
1735
1736 if (const PointerType *PT = FQT->getAs<PointerType>())
1737 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1738
1739 return Qualifiers::GCNone;
1740}
1741
John McCall351762c2011-02-07 10:33:21 +00001742llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1743 const CGBlockInfo &blockInfo) {
Chris Lattnerece04092012-02-07 00:39:47 +00001744 llvm::Constant *nullPtr = llvm::Constant::getNullValue(CGM.Int8PtrTy);
John McCall351762c2011-02-07 10:33:21 +00001745
Douglas Gregor79a91412011-09-13 17:21:33 +00001746 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCall31168b02011-06-15 23:02:42 +00001747 !CGM.getLangOptions().ObjCAutoRefCount)
John McCall351762c2011-02-07 10:33:21 +00001748 return nullPtr;
1749
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001750 bool hasUnion = false;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001751 SkipIvars.clear();
1752 IvarsInfo.clear();
Douglas Gregore8bbc122011-09-02 00:18:52 +00001753 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1754 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001755
Fariborz Jahaniancfddabf2010-09-09 00:21:45 +00001756 // __isa is the first field in block descriptor and must assume by runtime's
1757 // convention that it is GC'able.
1758 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall351762c2011-02-07 10:33:21 +00001759
1760 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1761
1762 // Calculate the basic layout of the block structure.
1763 const llvm::StructLayout *layout =
1764 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1765
1766 // Ignore the optional 'this' capture: C++ objects are not assumed
1767 // to be GC'ed.
1768
1769 // Walk the captured variables.
1770 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1771 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1772 const VarDecl *variable = ci->getVariable();
1773 QualType type = variable->getType();
1774
1775 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1776
1777 // Ignore constant captures.
1778 if (capture.isConstant()) continue;
1779
1780 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1781
1782 // __block variables are passed by their descriptor address.
1783 if (ci->isByRef()) {
1784 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanian933c6722010-09-11 01:27:29 +00001785 continue;
John McCall351762c2011-02-07 10:33:21 +00001786 }
1787
1788 assert(!type->isArrayType() && "array variable should not be caught");
1789 if (const RecordType *record = type->getAs<RecordType>()) {
1790 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahanian903aba32010-08-05 21:00:25 +00001791 continue;
1792 }
Fariborz Jahanianf95e3582010-08-06 16:28:55 +00001793
John McCall351762c2011-02-07 10:33:21 +00001794 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1795 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1796
1797 if (GCAttr == Qualifiers::Strong)
1798 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1799 fieldSize / WordSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001800 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall351762c2011-02-07 10:33:21 +00001801 SkipIvars.push_back(GC_IVAR(fieldOffset,
1802 fieldSize / ByteSizeInBits));
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001803 }
1804
1805 if (IvarsInfo.empty())
John McCall351762c2011-02-07 10:33:21 +00001806 return nullPtr;
1807
1808 // Sort on byte position; captures might not be allocated in order,
1809 // and unions can do funny things.
1810 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1811 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001812
1813 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00001814 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001815 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1816 printf("\n block variable layout for block: ");
1817 const unsigned char *s = (unsigned char*)BitMap.c_str();
Bill Wendling53136852012-02-07 09:06:01 +00001818 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00001819 if (!(s[i] & 0xf0))
1820 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1821 else
1822 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1823 printf("\n");
1824 }
1825
1826 return C;
Fariborz Jahanianc05349e2010-08-04 16:57:49 +00001827}
1828
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001829llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001830 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001831 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001832 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001833 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1834
Owen Andersonade90fd2009-07-29 18:54:39 +00001835 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Douglas Gregor020de322012-01-17 18:36:30 +00001836 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001837}
1838
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001839void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00001840 // FIXME: We shouldn't need this, the protocol decl should contain enough
1841 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00001842 DefinedProtocols.insert(PD->getIdentifier());
1843
1844 // If we have generated a forward reference to this protocol, emit
1845 // it now. Otherwise do nothing, the protocol objects are lazily
1846 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001847 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001848 GetOrEmitProtocol(PD);
1849}
1850
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001851llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001852 if (DefinedProtocols.count(PD->getIdentifier()))
1853 return GetOrEmitProtocol(PD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001854
Daniel Dunbarc475d422008-10-29 22:36:39 +00001855 return GetOrEmitProtocolRef(PD);
1856}
1857
Daniel Dunbarb036db82008-08-13 03:21:16 +00001858/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001859// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1860struct _objc_protocol {
1861struct _objc_protocol_extension *isa;
1862char *protocol_name;
1863struct _objc_protocol_list *protocol_list;
1864struct _objc__method_prototype_list *instance_methods;
1865struct _objc__method_prototype_list *class_methods
1866};
Daniel Dunbarb036db82008-08-13 03:21:16 +00001867
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001868See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001869*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00001870llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1871 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1872
1873 // Early exit if a defining object has already been generated.
1874 if (Entry && Entry->hasInitializer())
1875 return Entry;
1876
Douglas Gregora715bff2012-01-01 19:51:50 +00001877 // Use the protocol definition, if there is one.
1878 if (const ObjCProtocolDecl *Def = PD->getDefinition())
1879 PD = Def;
1880
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001881 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001882 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001883 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1884
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001885 // Construct method lists.
1886 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1887 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001888 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001889 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001890 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001891 ObjCMethodDecl *MD = *i;
1892 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001893 if (!C)
1894 return GetOrEmitProtocolRef(PD);
1895
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001896 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1897 OptInstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001898 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001899 } else {
1900 InstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001901 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001902 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001903 }
1904
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001905 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001906 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001907 ObjCMethodDecl *MD = *i;
1908 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00001909 if (!C)
1910 return GetOrEmitProtocolRef(PD);
1911
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001912 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1913 OptClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001914 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001915 } else {
1916 ClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001917 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001918 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001919 }
1920
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001921 MethodTypesExt.insert(MethodTypesExt.end(),
1922 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
1923
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001924 llvm::Constant *Values[] = {
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001925 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
1926 MethodTypesExt),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001927 GetClassName(PD->getIdentifier()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001928 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00001929 PD->protocol_begin(),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001930 PD->protocol_end()),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001931 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001932 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001933 InstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001934 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001935 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001936 ClassMethods)
1937 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00001938 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001939 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001940
Daniel Dunbarb036db82008-08-13 03:21:16 +00001941 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001942 // Already created, fix the linkage and update the initializer.
1943 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001944 Entry->setInitializer(Init);
1945 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001946 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001947 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001948 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001949 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001950 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001951 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001952 // FIXME: Is this necessary? Why only for protocol?
1953 Entry->setAlignment(4);
1954 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00001955 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001956
1957 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001958}
1959
Daniel Dunbarc475d422008-10-29 22:36:39 +00001960llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001961 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1962
1963 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001964 // We use the initializer as a marker of whether this is a forward
1965 // reference or not. At module finalization we add the empty
1966 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001967 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001968 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00001969 llvm::GlobalValue::ExternalLinkage,
1970 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001971 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001972 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001973 // FIXME: Is this necessary? Why only for protocol?
1974 Entry->setAlignment(4);
1975 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001976
Daniel Dunbarb036db82008-08-13 03:21:16 +00001977 return Entry;
1978}
1979
1980/*
1981 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001982 uint32_t size;
1983 struct objc_method_description_list *optional_instance_methods;
1984 struct objc_method_description_list *optional_class_methods;
1985 struct objc_property_list *instance_properties;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00001986 const char ** extendedMethodTypes;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001987 };
1988*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001989llvm::Constant *
1990CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00001991 ArrayRef<llvm::Constant*> OptInstanceMethods,
1992 ArrayRef<llvm::Constant*> OptClassMethods,
1993 ArrayRef<llvm::Constant*> MethodTypesExt) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001994 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001995 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer22d24c22011-10-15 12:20:02 +00001996 llvm::Constant *Values[] = {
1997 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001998 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001999 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002000 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002001 OptInstanceMethods),
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002002 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002003 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002004 OptClassMethods),
2005 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002006 ObjCTypes),
2007 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
2008 MethodTypesExt, ObjCTypes)
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002009 };
Daniel Dunbarb036db82008-08-13 03:21:16 +00002010
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002011 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002012 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002013 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002014 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002015
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002016 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002017 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002018
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002019 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002020 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002021 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002022 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002023}
2024
2025/*
2026 struct objc_protocol_list {
Bill Wendlinga515b582012-02-09 22:16:49 +00002027 struct objc_protocol_list *next;
2028 long count;
2029 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002030 };
2031*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00002032llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002033CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00002034 ObjCProtocolDecl::protocol_iterator begin,
2035 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendlinga515b582012-02-09 22:16:49 +00002036 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbarb036db82008-08-13 03:21:16 +00002037
Daniel Dunbardec75f82008-08-21 21:57:41 +00002038 for (; begin != end; ++begin)
2039 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00002040
2041 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002042 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002043 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002044
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002045 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00002046 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00002047
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002048 llvm::Constant *Values[3];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002049 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00002050 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002051 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002052 ProtocolRefs.size() - 1);
2053 Values[2] =
2054 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
2055 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00002056 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002057
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002058 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002059 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002060 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002061 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00002062 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002063}
2064
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002065void CGObjCCommonMac::
2066PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet,
Bill Wendlinga515b582012-02-09 22:16:49 +00002067 llvm::SmallVectorImpl<llvm::Constant*> &Properties,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002068 const Decl *Container,
2069 const ObjCProtocolDecl *PROTO,
2070 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002071 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
2072 E = PROTO->protocol_end(); P != E; ++P)
2073 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2074 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2075 E = PROTO->prop_end(); I != E; ++I) {
2076 const ObjCPropertyDecl *PD = *I;
2077 if (!PropertySet.insert(PD->getIdentifier()))
2078 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002079 llvm::Constant *Prop[] = {
2080 GetPropertyName(PD->getIdentifier()),
2081 GetPropertyTypeString(PD, Container)
2082 };
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002083 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2084 }
2085}
2086
Daniel Dunbarb036db82008-08-13 03:21:16 +00002087/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002088 struct _objc_property {
Bill Wendlinga515b582012-02-09 22:16:49 +00002089 const char * const name;
2090 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002091 };
2092
2093 struct _objc_property_list {
Bill Wendlinga515b582012-02-09 22:16:49 +00002094 uint32_t entsize; // sizeof (struct _objc_property)
2095 uint32_t prop_count;
2096 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002097 };
2098*/
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002099llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002100 const Decl *Container,
2101 const ObjCContainerDecl *OCD,
2102 const ObjCCommonTypesHelper &ObjCTypes) {
Bill Wendlinga515b582012-02-09 22:16:49 +00002103 llvm::SmallVector<llvm::Constant*, 16> Properties;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002104 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002105 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2106 E = OCD->prop_end(); I != E; ++I) {
Steve Naroffba3dc382009-01-11 12:47:58 +00002107 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00002108 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002109 llvm::Constant *Prop[] = {
2110 GetPropertyName(PD->getIdentifier()),
2111 GetPropertyTypeString(PD, Container)
2112 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002113 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002114 Prop));
2115 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002116 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002117 for (ObjCInterfaceDecl::all_protocol_iterator
2118 P = OID->all_referenced_protocol_begin(),
2119 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00002120 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2121 ObjCTypes);
2122 }
2123 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2124 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2125 E = CD->protocol_end(); P != E; ++P)
2126 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2127 ObjCTypes);
2128 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002129
2130 // Return null for empty list.
2131 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002132 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002133
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002134 unsigned PropertySize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002135 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002136 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002137 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2138 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002139 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002140 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002141 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002142 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002143
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002144 llvm::GlobalVariable *GV =
2145 CreateMetadataVar(Name, Init,
2146 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002147 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002148 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002149 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002150 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002151}
2152
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002153llvm::Constant *
2154CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2155 ArrayRef<llvm::Constant*> MethodTypes,
2156 const ObjCCommonTypesHelper &ObjCTypes) {
Bob Wilson5f4e3a72011-11-30 01:57:58 +00002157 // Return null for empty list.
2158 if (MethodTypes.empty())
2159 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2160
2161 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2162 MethodTypes.size());
2163 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2164
2165 llvm::GlobalVariable *GV =
2166 CreateMetadataVar(Name, Init,
2167 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2168 (ObjCABI == 2) ? 8 : 4,
2169 true);
2170 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2171}
2172
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002173/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00002174 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002175 int count;
2176 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00002177 };
2178*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002179llvm::Constant *
2180CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002181 llvm::Constant *Desc[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002182 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002183 ObjCTypes.SelectorPtrTy),
2184 GetMethodVarType(MD)
2185 };
Douglas Gregora9d84932011-05-27 01:19:52 +00002186 if (!Desc[1])
2187 return 0;
2188
Owen Anderson0e0189d2009-07-27 22:29:56 +00002189 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002190 Desc);
2191}
Daniel Dunbarb036db82008-08-13 03:21:16 +00002192
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002193llvm::Constant *
2194CGObjCMac::EmitMethodDescList(Twine Name, const char *Section,
2195 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002196 // Return null for empty list.
2197 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002198 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002199
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002200 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002201 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002202 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002203 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002204 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002205 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002206
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002207 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002208 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002209 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002210}
2211
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002212/*
2213 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002214 char *category_name;
2215 char *class_name;
2216 struct _objc_method_list *instance_methods;
2217 struct _objc_method_list *class_methods;
2218 struct _objc_protocol_list *protocols;
2219 uint32_t size; // <rdar://4585769>
2220 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002221 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002222*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002223void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002224 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002225
Mike Stump18bb9282009-05-16 07:57:57 +00002226 // FIXME: This is poor design, the OCD should have a pointer to the category
2227 // decl. Additionally, note that Category can be null for the @implementation
2228 // w/o an @interface case. Sema should just create one for us as it does for
2229 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002230 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002231 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002232 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002233
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002234 SmallString<256> ExtName;
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002235 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2236 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002237
Bill Wendlinga515b582012-02-09 22:16:49 +00002238 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002239 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002240 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002241 // Instance methods should always be defined.
2242 InstanceMethods.push_back(GetMethodConstant(*i));
2243 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002244 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002245 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002246 // Class methods should always be defined.
2247 ClassMethods.push_back(GetMethodConstant(*i));
2248 }
2249
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002250 llvm::Constant *Values[7];
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002251 Values[0] = GetClassName(OCD->getIdentifier());
2252 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002253 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002254 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002255 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002256 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002257 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002258 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002259 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002260 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002261 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002262 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002263 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002264 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002265 Category->protocol_begin(),
2266 Category->protocol_end());
2267 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002268 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002269 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002270 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002271
2272 // If there is no category @interface then there can be no properties.
2273 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002274 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002275 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002276 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002277 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002278 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002279
Owen Anderson0e0189d2009-07-27 22:29:56 +00002280 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002281 Values);
2282
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002283 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002284 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002285 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002286 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002287 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002288 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002289 // method definition entries must be clear for next implementation.
2290 MethodDefinitions.clear();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002291}
2292
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002293// FIXME: Get from somewhere?
2294enum ClassFlags {
2295 eClassFlags_Factory = 0x00001,
2296 eClassFlags_Meta = 0x00002,
2297 // <rdr://5142207>
2298 eClassFlags_HasCXXStructors = 0x02000,
2299 eClassFlags_Hidden = 0x20000,
2300 eClassFlags_ABI2_Hidden = 0x00010,
2301 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2302};
2303
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002304/*
2305 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002306 Class isa;
2307 Class super_class;
2308 const char *name;
2309 long version;
2310 long info;
2311 long instance_size;
2312 struct _objc_ivar_list *ivars;
2313 struct _objc_method_list *methods;
2314 struct _objc_cache *cache;
2315 struct _objc_protocol_list *protocols;
2316 // Objective-C 1.0 extensions (<rdr://4585769>)
2317 const char *ivar_layout;
2318 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002319 };
2320
2321 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002322*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002323void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002324 DefinedSymbols.insert(ID->getIdentifier());
2325
Chris Lattner86d7d912008-11-24 03:54:41 +00002326 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002327 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002328 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002329 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002330 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002331 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00002332 Interface->all_referenced_protocol_begin(),
2333 Interface->all_referenced_protocol_end());
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002334 unsigned Flags = eClassFlags_Factory;
John McCall31168b02011-06-15 23:02:42 +00002335 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00002336 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002337 unsigned Size =
Ken Dyckc8ae5502011-02-09 01:59:34 +00002338 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002339
2340 // FIXME: Set CXX-structors flag.
John McCall457a04e2010-10-22 21:05:15 +00002341 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002342 Flags |= eClassFlags_Hidden;
2343
Bill Wendlinga515b582012-02-09 22:16:49 +00002344 llvm::SmallVector<llvm::Constant*, 16> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002345 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002346 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002347 // Instance methods should always be defined.
2348 InstanceMethods.push_back(GetMethodConstant(*i));
2349 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002350 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002351 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002352 // Class methods should always be defined.
2353 ClassMethods.push_back(GetMethodConstant(*i));
2354 }
2355
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002356 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002357 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002358 ObjCPropertyImplDecl *PID = *i;
2359
2360 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2361 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2362
2363 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2364 if (llvm::Constant *C = GetMethodConstant(MD))
2365 InstanceMethods.push_back(C);
2366 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2367 if (llvm::Constant *C = GetMethodConstant(MD))
2368 InstanceMethods.push_back(C);
2369 }
2370 }
2371
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002372 llvm::Constant *Values[12];
Daniel Dunbarccf61832009-05-03 08:56:52 +00002373 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002374 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002375 // Record a reference to the super class.
2376 LazySymbols.insert(Super->getIdentifier());
2377
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002378 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002379 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002380 ObjCTypes.ClassPtrTy);
2381 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002382 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002383 }
2384 Values[ 2] = GetClassName(ID->getIdentifier());
2385 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002386 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2387 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2388 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002389 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002390 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002391 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002392 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002393 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002394 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002395 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002396 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002397 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002398 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002399 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002400 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002401 std::string Name("\01L_OBJC_CLASS_");
2402 Name += ClassName;
2403 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2404 // Check for a forward reference.
2405 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2406 if (GV) {
2407 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2408 "Forward metaclass reference has incorrect type.");
2409 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2410 GV->setInitializer(Init);
2411 GV->setSection(Section);
2412 GV->setAlignment(4);
2413 CGM.AddUsedGlobal(GV);
2414 }
2415 else
2416 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002417 DefinedClasses.push_back(GV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00002418 // method definition entries must be clear for next implementation.
2419 MethodDefinitions.clear();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002420}
2421
2422llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2423 llvm::Constant *Protocols,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002424 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002425 unsigned Flags = eClassFlags_Meta;
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002426 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002427
John McCall457a04e2010-10-22 21:05:15 +00002428 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002429 Flags |= eClassFlags_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002430
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002431 llvm::Constant *Values[12];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002432 // The isa for the metaclass is the root of the hierarchy.
2433 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2434 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2435 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002436 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002437 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002438 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002439 // The super class for the metaclass is emitted as the name of the
2440 // super class. The runtime fixes this up to point to the
2441 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002442 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002443 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002444 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002445 ObjCTypes.ClassPtrTy);
2446 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002447 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002448 }
2449 Values[ 2] = GetClassName(ID->getIdentifier());
2450 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002451 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2452 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2453 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002454 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002455 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002456 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002457 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002458 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002459 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002460 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002461 Values[ 9] = Protocols;
2462 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002463 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002464 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002465 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002466 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002467 Values);
2468
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002469 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner86d7d912008-11-24 03:54:41 +00002470 Name += ID->getNameAsCString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002471
2472 // Check for a forward reference.
2473 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2474 if (GV) {
2475 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2476 "Forward metaclass reference has incorrect type.");
2477 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2478 GV->setInitializer(Init);
2479 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002480 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002481 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002482 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002483 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002484 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002485 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002486 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002487
2488 return GV;
2489}
2490
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002491llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002492 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002493
Mike Stump18bb9282009-05-16 07:57:57 +00002494 // FIXME: Should we look these up somewhere other than the module. Its a bit
2495 // silly since we only generate these while processing an implementation, so
2496 // exactly one pointer would work if know when we entered/exitted an
2497 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002498
2499 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00002500 // Previously, metaclass with internal linkage may have been defined.
2501 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002502 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2503 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002504 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2505 "Forward metaclass reference has incorrect type.");
2506 return GV;
2507 } else {
2508 // Generate as an external reference to keep a consistent
2509 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00002510 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002511 llvm::GlobalValue::ExternalLinkage,
2512 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002513 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002514 }
2515}
2516
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002517llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2518 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2519
2520 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2521 true)) {
2522 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2523 "Forward class metadata reference has incorrect type.");
2524 return GV;
2525 } else {
2526 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2527 llvm::GlobalValue::ExternalLinkage,
2528 0,
2529 Name);
2530 }
2531}
2532
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002533/*
2534 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002535 uint32_t size;
2536 const char *weak_ivar_layout;
2537 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002538 };
2539*/
2540llvm::Constant *
2541CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002542 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002543 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002544
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002545 llvm::Constant *Values[3];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002546 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002547 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002548 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002549 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002550
2551 // Return null if no extension bits are used.
2552 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002553 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002554
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002555 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002556 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002557 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002558 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002559 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002560}
2561
2562/*
2563 struct objc_ivar {
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00002564 char *ivar_name;
2565 char *ivar_type;
2566 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002567 };
2568
2569 struct objc_ivar_list {
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00002570 int ivar_count;
2571 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002572 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002573*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002574llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002575 bool ForClass) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002576 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002577
2578 // When emitting the root class GCC emits ivar entries for the
2579 // actual class structure. It is not clear if we need to follow this
2580 // behavior; for now lets try and get away with not doing it. If so,
2581 // the cleanest solution would be to make up an ObjCInterfaceDecl
2582 // for the class.
2583 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00002584 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002585
Jordy Rosea91768e2011-07-22 02:08:32 +00002586 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002587
Jordy Rosea91768e2011-07-22 02:08:32 +00002588 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00002589 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002590 // Ignore unnamed bit-fields.
2591 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002592 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002593 llvm::Constant *Ivar[] = {
2594 GetMethodVarName(IVD->getIdentifier()),
2595 GetMethodVarType(IVD),
2596 llvm::ConstantInt::get(ObjCTypes.IntTy,
2597 ComputeIvarBaseOffset(CGM, OID, IVD))
2598 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002599 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002600 }
2601
2602 // Return null for empty list.
2603 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002604 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002605
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002606 llvm::Constant *Values[2];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002607 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002608 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002609 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002610 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002611 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002612
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002613 llvm::GlobalVariable *GV;
2614 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002615 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002616 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002617 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002618 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002619 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002620 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002621 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002622 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002623}
2624
2625/*
2626 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002627 SEL method_name;
2628 char *method_types;
2629 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002630 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002631
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002632 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002633 struct objc_method_list *obsolete;
2634 int count;
2635 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002636 };
2637*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002638
2639/// GetMethodConstant - Return a struct objc_method constant for the
2640/// given method if it has been defined. The result is null if the
2641/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002642llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00002643 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002644 if (!Fn)
2645 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002646
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002647 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00002648 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00002649 ObjCTypes.SelectorPtrTy),
2650 GetMethodVarType(MD),
2651 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
2652 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00002653 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002654}
2655
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002656llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002657 const char *Section,
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00002658 ArrayRef<llvm::Constant*> Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002659 // Return null for empty list.
2660 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002661 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002662
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002663 llvm::Constant *Values[3];
Owen Anderson0b75f232009-07-31 20:28:54 +00002664 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002665 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002666 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002667 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002668 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002669 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002670
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002671 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00002672 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002673}
2674
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002675llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002676 const ObjCContainerDecl *CD) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00002677 SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002678 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002679
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002680 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2192fe52011-07-18 04:24:23 +00002681 llvm::FunctionType *MethodTy =
John McCalla729c622012-02-17 03:33:10 +00002682 Types.GetFunctionType(Types.arrangeObjCMethodDeclaration(OMD));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002683 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002684 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002685 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002686 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002687 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002688 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002689
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002690 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002691}
2692
Daniel Dunbar30c65362009-03-09 20:09:19 +00002693llvm::GlobalVariable *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002694CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002695 llvm::Constant *Init,
2696 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002697 unsigned Align,
2698 bool AddToUsed) {
Chris Lattner2192fe52011-07-18 04:24:23 +00002699 llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002700 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002701 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002702 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002703 if (Section)
2704 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002705 if (Align)
2706 GV->setAlignment(Align);
2707 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002708 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002709 return GV;
2710}
2711
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002712llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002713 // Abuse this interface function as a place to finalize.
2714 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002715 return NULL;
2716}
2717
Chris Lattnerd4808922009-03-22 21:03:39 +00002718llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002719 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002720}
2721
Chris Lattnerd4808922009-03-22 21:03:39 +00002722llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002723 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002724}
2725
David Chisnall168b80f2010-12-26 22:13:16 +00002726llvm::Constant *CGObjCMac::GetGetStructFunction() {
2727 return ObjCTypes.getCopyStructFn();
2728}
2729llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002730 return ObjCTypes.getCopyStructFn();
2731}
2732
Fariborz Jahanian1e1b5492012-01-06 18:07:23 +00002733llvm::Constant *CGObjCMac::GetCppAtomicObjectFunction() {
2734 return ObjCTypes.getCppAtomicObjectFunction();
2735}
2736
Chris Lattnerd4808922009-03-22 21:03:39 +00002737llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002738 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002739}
2740
John McCallbd309292010-07-06 01:34:17 +00002741void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2742 return EmitTryOrSynchronizedStmt(CGF, S);
2743}
2744
2745void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2746 const ObjCAtSynchronizedStmt &S) {
2747 return EmitTryOrSynchronizedStmt(CGF, S);
2748}
2749
John McCall65bea082010-07-21 06:59:36 +00002750namespace {
John McCallcda666c2010-07-21 07:22:38 +00002751 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00002752 const Stmt &S;
John McCall2dd7d442010-08-04 05:59:32 +00002753 llvm::Value *SyncArgSlot;
John McCall65bea082010-07-21 06:59:36 +00002754 llvm::Value *CallTryExitVar;
2755 llvm::Value *ExceptionData;
2756 ObjCTypesHelper &ObjCTypes;
2757 PerformFragileFinally(const Stmt *S,
John McCall2dd7d442010-08-04 05:59:32 +00002758 llvm::Value *SyncArgSlot,
John McCall65bea082010-07-21 06:59:36 +00002759 llvm::Value *CallTryExitVar,
2760 llvm::Value *ExceptionData,
2761 ObjCTypesHelper *ObjCTypes)
John McCall2dd7d442010-08-04 05:59:32 +00002762 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCall65bea082010-07-21 06:59:36 +00002763 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2764
John McCall30317fd2011-07-12 20:27:29 +00002765 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCall65bea082010-07-21 06:59:36 +00002766 // Check whether we need to call objc_exception_try_exit.
2767 // In optimized code, this branch will always be folded.
2768 llvm::BasicBlock *FinallyCallExit =
2769 CGF.createBasicBlock("finally.call_exit");
2770 llvm::BasicBlock *FinallyNoCallExit =
2771 CGF.createBasicBlock("finally.no_call_exit");
2772 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2773 FinallyCallExit, FinallyNoCallExit);
2774
2775 CGF.EmitBlock(FinallyCallExit);
2776 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2777 ->setDoesNotThrow();
2778
2779 CGF.EmitBlock(FinallyNoCallExit);
2780
2781 if (isa<ObjCAtTryStmt>(S)) {
2782 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCallcebe0ca2010-08-11 00:16:14 +00002783 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2784 // Save the current cleanup destination in case there's
2785 // control flow inside the finally statement.
2786 llvm::Value *CurCleanupDest =
2787 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2788
John McCall65bea082010-07-21 06:59:36 +00002789 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2790
John McCallcebe0ca2010-08-11 00:16:14 +00002791 if (CGF.HaveInsertPoint()) {
2792 CGF.Builder.CreateStore(CurCleanupDest,
2793 CGF.getNormalCleanupDestSlot());
2794 } else {
2795 // Currently, the end of the cleanup must always exist.
2796 CGF.EnsureInsertPoint();
2797 }
2798 }
John McCall65bea082010-07-21 06:59:36 +00002799 } else {
2800 // Emit objc_sync_exit(expr); as finally's sole statement for
2801 // @synchronized.
John McCall2dd7d442010-08-04 05:59:32 +00002802 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCall65bea082010-07-21 06:59:36 +00002803 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2804 ->setDoesNotThrow();
2805 }
2806 }
2807 };
John McCall42227ed2010-07-31 23:20:56 +00002808
2809 class FragileHazards {
2810 CodeGenFunction &CGF;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002811 SmallVector<llvm::Value*, 20> Locals;
John McCall42227ed2010-07-31 23:20:56 +00002812 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2813
2814 llvm::InlineAsm *ReadHazard;
2815 llvm::InlineAsm *WriteHazard;
2816
2817 llvm::FunctionType *GetAsmFnType();
2818
2819 void collectLocals();
2820 void emitReadHazard(CGBuilderTy &Builder);
2821
2822 public:
2823 FragileHazards(CodeGenFunction &CGF);
John McCall2dd7d442010-08-04 05:59:32 +00002824
John McCall42227ed2010-07-31 23:20:56 +00002825 void emitWriteHazard();
John McCall2dd7d442010-08-04 05:59:32 +00002826 void emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00002827 };
2828}
2829
2830/// Create the fragile-ABI read and write hazards based on the current
2831/// state of the function, which is presumed to be immediately prior
2832/// to a @try block. These hazards are used to maintain correct
2833/// semantics in the face of optimization and the fragile ABI's
2834/// cavalier use of setjmp/longjmp.
2835FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2836 collectLocals();
2837
2838 if (Locals.empty()) return;
2839
2840 // Collect all the blocks in the function.
2841 for (llvm::Function::iterator
2842 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2843 BlocksBeforeTry.insert(&*I);
2844
2845 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2846
2847 // Create a read hazard for the allocas. This inhibits dead-store
2848 // optimizations and forces the values to memory. This hazard is
2849 // inserted before any 'throwing' calls in the protected scope to
2850 // reflect the possibility that the variables might be read from the
2851 // catch block if the call throws.
2852 {
2853 std::string Constraint;
2854 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2855 if (I) Constraint += ',';
2856 Constraint += "*m";
2857 }
2858
2859 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2860 }
2861
2862 // Create a write hazard for the allocas. This inhibits folding
2863 // loads across the hazard. This hazard is inserted at the
2864 // beginning of the catch path to reflect the possibility that the
2865 // variables might have been written within the protected scope.
2866 {
2867 std::string Constraint;
2868 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2869 if (I) Constraint += ',';
2870 Constraint += "=*m";
2871 }
2872
2873 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2874 }
2875}
2876
2877/// Emit a write hazard at the current location.
2878void FragileHazards::emitWriteHazard() {
2879 if (Locals.empty()) return;
2880
Jay Foad5bd375a2011-07-15 08:37:34 +00002881 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall42227ed2010-07-31 23:20:56 +00002882}
2883
John McCall42227ed2010-07-31 23:20:56 +00002884void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2885 assert(!Locals.empty());
Jay Foad5bd375a2011-07-15 08:37:34 +00002886 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall42227ed2010-07-31 23:20:56 +00002887}
2888
2889/// Emit read hazards in all the protected blocks, i.e. all the blocks
2890/// which have been inserted since the beginning of the try.
John McCall2dd7d442010-08-04 05:59:32 +00002891void FragileHazards::emitHazardsInNewBlocks() {
John McCall42227ed2010-07-31 23:20:56 +00002892 if (Locals.empty()) return;
2893
2894 CGBuilderTy Builder(CGF.getLLVMContext());
2895
2896 // Iterate through all blocks, skipping those prior to the try.
2897 for (llvm::Function::iterator
2898 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2899 llvm::BasicBlock &BB = *FI;
2900 if (BlocksBeforeTry.count(&BB)) continue;
2901
2902 // Walk through all the calls in the block.
2903 for (llvm::BasicBlock::iterator
2904 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2905 llvm::Instruction &I = *BI;
2906
2907 // Ignore instructions that aren't non-intrinsic calls.
2908 // These are the only calls that can possibly call longjmp.
2909 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2910 if (isa<llvm::IntrinsicInst>(I))
2911 continue;
2912
2913 // Ignore call sites marked nounwind. This may be questionable,
2914 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2915 llvm::CallSite CS(&I);
2916 if (CS.doesNotThrow()) continue;
2917
John McCall2dd7d442010-08-04 05:59:32 +00002918 // Insert a read hazard before the call. This will ensure that
2919 // any writes to the locals are performed before making the
2920 // call. If the call throws, then this is sufficient to
2921 // guarantee correctness as long as it doesn't also write to any
2922 // locals.
John McCall42227ed2010-07-31 23:20:56 +00002923 Builder.SetInsertPoint(&BB, BI);
2924 emitReadHazard(Builder);
2925 }
2926 }
2927}
2928
2929static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2930 if (V) S.insert(V);
2931}
2932
2933void FragileHazards::collectLocals() {
2934 // Compute a set of allocas to ignore.
2935 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2936 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2937 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall42227ed2010-07-31 23:20:56 +00002938
2939 // Collect all the allocas currently in the function. This is
2940 // probably way too aggressive.
2941 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2942 for (llvm::BasicBlock::iterator
2943 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2944 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2945 Locals.push_back(&*I);
2946}
2947
2948llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002949 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall9dc0db22011-05-15 01:53:33 +00002950 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
2951 tys[i] = Locals[i]->getType();
2952 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCall65bea082010-07-21 06:59:36 +00002953}
2954
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002955/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002956
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002957 Objective-C setjmp-longjmp (sjlj) Exception Handling
2958 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002959
John McCallbd309292010-07-06 01:34:17 +00002960 A catch buffer is a setjmp buffer plus:
2961 - a pointer to the exception that was caught
2962 - a pointer to the previous exception data buffer
2963 - two pointers of reserved storage
2964 Therefore catch buffers form a stack, with a pointer to the top
2965 of the stack kept in thread-local storage.
2966
2967 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2968 objc_exception_try_exit pops the given catch buffer, which is
2969 required to be the top of the EH stack.
2970 objc_exception_throw pops the top of the EH stack, writes the
2971 thrown exception into the appropriate field, and longjmps
2972 to the setjmp buffer. It crashes the process (with a printf
2973 and an abort()) if there are no catch buffers on the stack.
2974 objc_exception_extract just reads the exception pointer out of the
2975 catch buffer.
2976
2977 There's no reason an implementation couldn't use a light-weight
2978 setjmp here --- something like __builtin_setjmp, but API-compatible
2979 with the heavyweight setjmp. This will be more important if we ever
2980 want to implement correct ObjC/C++ exception interactions for the
2981 fragile ABI.
2982
2983 Note that for this use of setjmp/longjmp to be correct, we may need
2984 to mark some local variables volatile: if a non-volatile local
2985 variable is modified between the setjmp and the longjmp, it has
2986 indeterminate value. For the purposes of LLVM IR, it may be
2987 sufficient to make loads and stores within the @try (to variables
2988 declared outside the @try) volatile. This is necessary for
2989 optimized correctness, but is not currently being done; this is
2990 being tracked as rdar://problem/8160285
2991
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002992 The basic framework for a @try-catch-finally is as follows:
2993 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002994 objc_exception_data d;
2995 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00002996 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002997
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002998 objc_exception_try_enter(&d);
2999 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003000 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003001 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003002 // exception path
3003 id _caught = objc_exception_extract(&d);
3004
3005 // enter new try scope for handlers
3006 if (!setjmp(d.jmp_buf)) {
3007 ... match exception and execute catch blocks ...
3008
3009 // fell off end, rethrow.
3010 _rethrow = _caught;
3011 ... jump-through-finally to finally_rethrow ...
3012 } else {
3013 // exception in catch block
3014 _rethrow = objc_exception_extract(&d);
3015 _call_try_exit = false;
3016 ... jump-through-finally to finally_rethrow ...
3017 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003018 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003019 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003020
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003021 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00003022 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003023 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00003024
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003025 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003026 ... dispatch to finally destination ...
3027
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003028 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003029 objc_exception_throw(_rethrow);
3030
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003031 finally_end:
3032 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003033
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003034 This framework differs slightly from the one gcc uses, in that gcc
3035 uses _rethrow to determine if objc_exception_try_exit should be called
3036 and if the object should be rethrown. This breaks in the face of
3037 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003038
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003039 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003040
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003041 - If there are no catch blocks, then we avoid emitting the second
3042 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003043
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003044 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
3045 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003046
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003047 - FIXME: If there is no @finally block we can do a few more
3048 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003049
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003050 Rethrows and Jumps-Through-Finally
3051 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003052
John McCallbd309292010-07-06 01:34:17 +00003053 '@throw;' is supported by pushing the currently-caught exception
3054 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003055
John McCallbd309292010-07-06 01:34:17 +00003056 Branches through the @finally block are handled with an ordinary
3057 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
3058 exceptions are not compatible with C++ exceptions, and this is
3059 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003060
John McCallbd309292010-07-06 01:34:17 +00003061 @synchronized(expr) { stmt; } is emitted as if it were:
3062 id synch_value = expr;
3063 objc_sync_enter(synch_value);
3064 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003065*/
3066
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00003067void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
3068 const Stmt &S) {
3069 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00003070
3071 // A destination for the fall-through edges of the catch handlers to
3072 // jump to.
3073 CodeGenFunction::JumpDest FinallyEnd =
3074 CGF.getJumpDestInCurrentScope("finally.end");
3075
3076 // A destination for the rethrow edge of the catch handlers to jump
3077 // to.
3078 CodeGenFunction::JumpDest FinallyRethrow =
3079 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003080
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003081 // For @synchronized, call objc_sync_enter(sync.expr). The
3082 // evaluation of the expression must occur before we enter the
John McCall2dd7d442010-08-04 05:59:32 +00003083 // @synchronized. We can't avoid a temp here because we need the
3084 // value to be preserved. If the backend ever does liveness
3085 // correctly after setjmp, this will be unnecessary.
3086 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003087 if (!isTry) {
John McCall2dd7d442010-08-04 05:59:32 +00003088 llvm::Value *SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003089 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
3090 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00003091 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
3092 ->setDoesNotThrow();
John McCall2dd7d442010-08-04 05:59:32 +00003093
3094 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3095 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar94ceb612009-02-24 01:43:46 +00003096 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003097
John McCall2dd7d442010-08-04 05:59:32 +00003098 // Allocate memory for the setjmp buffer. This needs to be kept
3099 // live throughout the try and catch blocks.
3100 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3101 "exceptiondata.ptr");
3102
John McCall42227ed2010-07-31 23:20:56 +00003103 // Create the fragile hazards. Note that this will not capture any
3104 // of the allocas required for exception processing, but will
3105 // capture the current basic block (which extends all the way to the
3106 // setjmp call) as "before the @try".
3107 FragileHazards Hazards(CGF);
3108
John McCallbd309292010-07-06 01:34:17 +00003109 // Create a flag indicating whether the cleanup needs to call
3110 // objc_exception_try_exit. This is true except when
3111 // - no catches match and we're branching through the cleanup
3112 // just to rethrow the exception, or
3113 // - a catch matched and we're falling out of the catch handler.
John McCall2dd7d442010-08-04 05:59:32 +00003114 // The setjmp-safety rule here is that we should always store to this
3115 // variable in a place that dominates the branch through the cleanup
3116 // without passing through any setjmps.
John McCallbd309292010-07-06 01:34:17 +00003117 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00003118 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003119
John McCall9916e3f2010-10-04 23:42:51 +00003120 // A slot containing the exception to rethrow. Only needed when we
3121 // have both a @catch and a @finally.
3122 llvm::Value *PropagatingExnVar = 0;
3123
John McCallbd309292010-07-06 01:34:17 +00003124 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00003125 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall2dd7d442010-08-04 05:59:32 +00003126 SyncArgSlot,
John McCallcda666c2010-07-21 07:22:38 +00003127 CallTryExitVar,
3128 ExceptionData,
3129 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00003130
3131 // Enter a try block:
3132 // - Call objc_exception_try_enter to push ExceptionData on top of
3133 // the EH stack.
3134 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3135 ->setDoesNotThrow();
3136
3137 // - Call setjmp on the exception data buffer.
3138 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3139 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3140 llvm::Value *SetJmpBuffer =
Jay Foad040dd822011-07-22 08:16:57 +00003141 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallbd309292010-07-06 01:34:17 +00003142 llvm::CallInst *SetJmpResult =
3143 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3144 SetJmpResult->setDoesNotThrow();
Bill Wendlingbd26cf92011-12-19 23:53:28 +00003145 SetJmpResult->setCanReturnTwice();
John McCallbd309292010-07-06 01:34:17 +00003146
3147 // If setjmp returned 0, enter the protected block; otherwise,
3148 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00003149 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3150 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00003151 llvm::Value *DidCatch =
John McCallcebe0ca2010-08-11 00:16:14 +00003152 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3153 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003154
John McCallbd309292010-07-06 01:34:17 +00003155 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003156 CGF.EmitBlock(TryBlock);
John McCall2dd7d442010-08-04 05:59:32 +00003157 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003158 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00003159 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall2dd7d442010-08-04 05:59:32 +00003160
3161 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003162
John McCallbd309292010-07-06 01:34:17 +00003163 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00003164 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003165
John McCall42227ed2010-07-31 23:20:56 +00003166 // Don't optimize loads of the in-scope locals across this point.
3167 Hazards.emitWriteHazard();
3168
John McCallbd309292010-07-06 01:34:17 +00003169 // For a @synchronized (or a @try with no catches), just branch
3170 // through the cleanup to the rethrow block.
3171 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3172 // Tell the cleanup not to re-pop the exit.
John McCall2dd7d442010-08-04 05:59:32 +00003173 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003174 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00003175
3176 // Otherwise, we have to match against the caught exceptions.
3177 } else {
John McCall2dd7d442010-08-04 05:59:32 +00003178 // Retrieve the exception object. We may emit multiple blocks but
3179 // nothing can cross this so the value is already in SSA form.
3180 llvm::CallInst *Caught =
3181 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3182 ExceptionData, "caught");
3183 Caught->setDoesNotThrow();
3184
John McCallbd309292010-07-06 01:34:17 +00003185 // Push the exception to rethrow onto the EH value stack for the
3186 // benefit of any @throws in the handlers.
3187 CGF.ObjCEHValueStack.push_back(Caught);
3188
Douglas Gregor96c79492010-04-23 22:50:49 +00003189 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003190
John McCall2dd7d442010-08-04 05:59:32 +00003191 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallbd309292010-07-06 01:34:17 +00003192
John McCall2dd7d442010-08-04 05:59:32 +00003193 llvm::BasicBlock *CatchBlock = 0;
3194 llvm::BasicBlock *CatchHandler = 0;
3195 if (HasFinally) {
John McCall9916e3f2010-10-04 23:42:51 +00003196 // Save the currently-propagating exception before
3197 // objc_exception_try_enter clears the exception slot.
3198 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3199 "propagating_exception");
3200 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3201
John McCall2dd7d442010-08-04 05:59:32 +00003202 // Enter a new exception try block (in case a @catch block
3203 // throws an exception).
3204 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3205 ->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003206
John McCall2dd7d442010-08-04 05:59:32 +00003207 llvm::CallInst *SetJmpResult =
3208 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3209 "setjmp.result");
3210 SetJmpResult->setDoesNotThrow();
Bill Wendlingbd26cf92011-12-19 23:53:28 +00003211 SetJmpResult->setCanReturnTwice();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003212
John McCall2dd7d442010-08-04 05:59:32 +00003213 llvm::Value *Threw =
3214 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3215
3216 CatchBlock = CGF.createBasicBlock("catch");
3217 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3218 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3219
3220 CGF.EmitBlock(CatchBlock);
3221 }
3222
3223 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003224
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003225 // Handle catch list. As a special case we check if everything is
3226 // matched and avoid generating code for falling off the end if
3227 // so.
3228 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00003229 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3230 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003231
Douglas Gregor46a572b2010-04-26 16:46:50 +00003232 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00003233 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00003234
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003235 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003236 if (!CatchParam) {
3237 AllMatched = true;
3238 } else {
John McCall9dd450b2009-09-21 23:43:11 +00003239 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003240
John McCallbd309292010-07-06 01:34:17 +00003241 // catch(id e) always matches under this ABI, since only
3242 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00003243 // FIXME: For the time being we also match id<X>; this should
3244 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00003245 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003246 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003247 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003248
John McCallbd309292010-07-06 01:34:17 +00003249 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003250 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00003251 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3252
Anders Carlsson9396a892008-09-11 09:15:33 +00003253 if (CatchParam) {
John McCall1c9c3fd2010-10-15 04:57:14 +00003254 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003255 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00003256
3257 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00003258 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00003259 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003260
Anders Carlsson9396a892008-09-11 09:15:33 +00003261 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003262
3263 // The scope of the catch variable ends right here.
3264 CatchVarCleanups.ForceCleanup();
3265
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003266 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003267 break;
3268 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003269
Steve Naroff7cae42b2009-07-10 23:34:53 +00003270 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00003271 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00003272
3273 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00003274 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3275 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003276
3277 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00003278 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003279
John McCallbd309292010-07-06 01:34:17 +00003280 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00003281 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3282 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00003283 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003284
John McCallbd309292010-07-06 01:34:17 +00003285 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3286 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003287
3288 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00003289 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003290
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003291 // Emit the @catch block.
3292 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00003293
3294 // Collect any cleanups for the catch variable. The scope lasts until
3295 // the end of the catch body.
John McCall2dd7d442010-08-04 05:59:32 +00003296 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallbd309292010-07-06 01:34:17 +00003297
John McCall1c9c3fd2010-10-15 04:57:14 +00003298 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003299 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003300
John McCallbd309292010-07-06 01:34:17 +00003301 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003302 llvm::Value *Tmp =
3303 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003304 CGF.ConvertType(CatchParam->getType()));
Steve Naroff371b8fb2009-03-03 19:52:17 +00003305 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003306
Anders Carlsson9396a892008-09-11 09:15:33 +00003307 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00003308
3309 // We're done with the catch variable.
3310 CatchVarCleanups.ForceCleanup();
3311
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003312 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003313
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003314 CGF.EmitBlock(NextCatchBlock);
3315 }
3316
John McCallbd309292010-07-06 01:34:17 +00003317 CGF.ObjCEHValueStack.pop_back();
3318
John McCall2dd7d442010-08-04 05:59:32 +00003319 // If nothing wanted anything to do with the caught exception,
3320 // kill the extract call.
3321 if (Caught->use_empty())
3322 Caught->eraseFromParent();
3323
3324 if (!AllMatched)
3325 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3326
3327 if (HasFinally) {
3328 // Emit the exception handler for the @catch blocks.
3329 CGF.EmitBlock(CatchHandler);
3330
3331 // In theory we might now need a write hazard, but actually it's
3332 // unnecessary because there's no local-accessing code between
3333 // the try's write hazard and here.
3334 //Hazards.emitWriteHazard();
3335
John McCall9916e3f2010-10-04 23:42:51 +00003336 // Extract the new exception and save it to the
3337 // propagating-exception slot.
3338 assert(PropagatingExnVar);
3339 llvm::CallInst *NewCaught =
3340 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3341 ExceptionData, "caught");
3342 NewCaught->setDoesNotThrow();
3343 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3344
John McCall2dd7d442010-08-04 05:59:32 +00003345 // Don't pop the catch handler; the throw already did.
3346 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003347 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00003348 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003349 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003350
John McCall42227ed2010-07-31 23:20:56 +00003351 // Insert read hazards as required in the new blocks.
John McCall2dd7d442010-08-04 05:59:32 +00003352 Hazards.emitHazardsInNewBlocks();
John McCall42227ed2010-07-31 23:20:56 +00003353
John McCallbd309292010-07-06 01:34:17 +00003354 // Pop the cleanup.
John McCall2dd7d442010-08-04 05:59:32 +00003355 CGF.Builder.restoreIP(TryFallthroughIP);
3356 if (CGF.HaveInsertPoint())
3357 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallbd309292010-07-06 01:34:17 +00003358 CGF.PopCleanupBlock();
John McCall2dd7d442010-08-04 05:59:32 +00003359 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00003360
John McCallbd309292010-07-06 01:34:17 +00003361 // Emit the rethrow block.
John McCall42227ed2010-07-31 23:20:56 +00003362 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallad5d61e2010-07-23 21:56:41 +00003363 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00003364 if (CGF.HaveInsertPoint()) {
John McCall9916e3f2010-10-04 23:42:51 +00003365 // If we have a propagating-exception variable, check it.
3366 llvm::Value *PropagatingExn;
3367 if (PropagatingExnVar) {
3368 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall2dd7d442010-08-04 05:59:32 +00003369
John McCall9916e3f2010-10-04 23:42:51 +00003370 // Otherwise, just look in the buffer for the exception to throw.
3371 } else {
3372 llvm::CallInst *Caught =
3373 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3374 ExceptionData);
3375 Caught->setDoesNotThrow();
3376 PropagatingExn = Caught;
3377 }
3378
3379 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallbd309292010-07-06 01:34:17 +00003380 ->setDoesNotThrow();
3381 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00003382 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003383
John McCall42227ed2010-07-31 23:20:56 +00003384 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003385}
3386
3387void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00003388 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00003389 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003390
Anders Carlssone005aa12008-09-09 16:16:55 +00003391 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00003392 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003393 ExceptionAsObject =
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003394 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlssone005aa12008-09-09 16:16:55 +00003395 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003396 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00003397 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00003398 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00003399 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003400
John McCallbd309292010-07-06 01:34:17 +00003401 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3402 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00003403 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00003404
3405 // Clear the insertion point to indicate we are in unreachable code.
3406 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003407}
3408
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003409/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003410/// object: objc_read_weak (id *src)
3411///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003412llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003413 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003414 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003415 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3416 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3417 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00003418 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003419 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00003420 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003421 return read_weak;
3422}
3423
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003424/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3425/// objc_assign_weak (id src, id *dst)
3426///
3427void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003428 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003429 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003430 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003431 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003432 assert(Size <= 8 && "does not support size > 8");
3433 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003434 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003435 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3436 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003437 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3438 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003439 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003440 src, dst, "weakassign");
3441 return;
3442}
3443
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003444/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3445/// objc_assign_global (id src, id *dst)
3446///
3447void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00003448 llvm::Value *src, llvm::Value *dst,
3449 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003450 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003451 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003452 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003453 assert(Size <= 8 && "does not support size > 8");
3454 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003455 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003456 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3457 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003458 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3459 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00003460 if (!threadlocal)
3461 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3462 src, dst, "globalassign");
3463 else
3464 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3465 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003466 return;
3467}
3468
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003469/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003470/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003471///
3472void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003473 llvm::Value *src, llvm::Value *dst,
3474 llvm::Value *ivarOffset) {
3475 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2192fe52011-07-18 04:24:23 +00003476 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003477 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003478 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003479 assert(Size <= 8 && "does not support size > 8");
3480 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003481 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003482 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3483 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003484 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3485 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003486 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3487 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003488 return;
3489}
3490
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003491/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3492/// objc_assign_strongCast (id src, id *dst)
3493///
3494void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003495 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00003496 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003497 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003498 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003499 assert(Size <= 8 && "does not support size > 8");
3500 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003501 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003502 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3503 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003504 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3505 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003506 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003507 src, dst, "weakassign");
3508 return;
3509}
3510
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003511void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003512 llvm::Value *DestPtr,
3513 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003514 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003515 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3516 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003517 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003518 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003519 return;
3520}
3521
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003522/// EmitObjCValueForIvar - Code Gen for ivar reference.
3523///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003524LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3525 QualType ObjectTy,
3526 llvm::Value *BaseValue,
3527 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003528 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003529 const ObjCInterfaceDecl *ID =
3530 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003531 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3532 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003533}
3534
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003535llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003536 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003537 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003538 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003539 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003540 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3541 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003542}
3543
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003544/* *** Private Interface *** */
3545
3546/// EmitImageInfo - Emit the image info marker used to encode some module
3547/// level information.
3548///
3549/// See: <rdr://4810609&4810587&4810587>
3550/// struct IMAGE_INFO {
3551/// unsigned version;
3552/// unsigned flags;
3553/// };
3554enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00003555 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003556 eImageInfo_GarbageCollected = (1 << 1),
3557 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003558 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3559
Daniel Dunbar5e639272010-04-25 20:39:01 +00003560 // A flag indicating that the module has no instances of a @synthesize of a
3561 // superclass variable. <rdar://problem/6803242>
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003562 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003563};
3564
Daniel Dunbar5e639272010-04-25 20:39:01 +00003565void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003566 unsigned version = 0; // Version is unused?
Bill Wendlingb6f795e2012-02-16 01:13:30 +00003567 const char *Section = (ObjCABI == 1) ?
3568 "__OBJC, __image_info,regular" :
3569 "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003570
Bill Wendlingb6f795e2012-02-16 01:13:30 +00003571 // Generate module-level named metadata to convey this information to the
3572 // linker and code-gen.
3573 llvm::Module &Mod = CGM.getModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003574
Bill Wendlingb6f795e2012-02-16 01:13:30 +00003575 // Add the ObjC ABI version to the module flags.
3576 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Version", ObjCABI);
3577 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Version",
3578 version);
3579 Mod.addModuleFlag(llvm::Module::Error, "Objective-C Image Info Section",
3580 llvm::MDString::get(VMContext,Section));
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003581
Bill Wendlingb6f795e2012-02-16 01:13:30 +00003582 if (CGM.getLangOptions().getGC() == LangOptions::NonGC) {
3583 // Non-GC overrides those files which specify GC.
3584 Mod.addModuleFlag(llvm::Module::Override,
3585 "Objective-C Garbage Collection", (uint32_t)0);
3586 } else {
3587 // Add the ObjC garbage collection value.
3588 Mod.addModuleFlag(llvm::Module::Error,
3589 "Objective-C Garbage Collection",
3590 eImageInfo_GarbageCollected);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003591
Bill Wendlingb6f795e2012-02-16 01:13:30 +00003592 if (CGM.getLangOptions().getGC() == LangOptions::GCOnly) {
3593 // Add the ObjC GC Only value.
3594 Mod.addModuleFlag(llvm::Module::Error, "Objective-C GC Only",
3595 eImageInfo_GCOnly);
3596
3597 // Require that GC be specified and set to eImageInfo_GarbageCollected.
3598 llvm::Value *Ops[2] = {
3599 llvm::MDString::get(VMContext, "Objective-C Garbage Collection"),
3600 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
3601 eImageInfo_GarbageCollected)
3602 };
3603 Mod.addModuleFlag(llvm::Module::Require, "Objective-C GC Only",
3604 llvm::MDNode::get(VMContext, Ops));
3605 }
3606 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003607}
3608
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003609// struct objc_module {
3610// unsigned long version;
3611// unsigned long size;
3612// const char *name;
3613// Symtab symtab;
3614// };
3615
3616// FIXME: Get from somewhere
3617static const int ModuleVersion = 7;
3618
3619void CGObjCMac::EmitModuleInfo() {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003620 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003621
Benjamin Kramer22d24c22011-10-15 12:20:02 +00003622 llvm::Constant *Values[] = {
3623 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
3624 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
3625 // This used to be the filename, now it is unused. <rdr://4327263>
3626 GetClassName(&CGM.getContext().Idents.get("")),
3627 EmitModuleSymbols()
3628 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003629 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003630 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003631 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003632 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003633}
3634
3635llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003636 unsigned NumClasses = DefinedClasses.size();
3637 unsigned NumCategories = DefinedCategories.size();
3638
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003639 // Return null if no symbols were defined.
3640 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003641 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003642
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003643 llvm::Constant *Values[5];
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003644 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003645 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003646 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3647 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003648
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003649 // The runtime expects exactly the list of defined classes followed
3650 // by the list of defined categories, in a single array.
Chris Lattner3def9ae2012-02-06 22:16:34 +00003651 SmallVector<llvm::Constant*, 8> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003652 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003653 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003654 ObjCTypes.Int8PtrTy);
3655 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003656 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003657 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003658 ObjCTypes.Int8PtrTy);
3659
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003660 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003661 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner3def9ae2012-02-06 22:16:34 +00003662 Symbols.size()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003663 Symbols);
3664
Chris Lattnere64d7ba2011-06-20 04:01:35 +00003665 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003666
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003667 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003668 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3669 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003670 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003671 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003672}
3673
John McCall31168b02011-06-15 23:02:42 +00003674llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3675 IdentifierInfo *II) {
3676 LazySymbols.insert(II);
3677
3678 llvm::GlobalVariable *&Entry = ClassReferences[II];
3679
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003680 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003681 llvm::Constant *Casted =
John McCall31168b02011-06-15 23:02:42 +00003682 llvm::ConstantExpr::getBitCast(GetClassName(II),
3683 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003684 Entry =
John McCall31168b02011-06-15 23:02:42 +00003685 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3686 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3687 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003688 }
John McCall31168b02011-06-15 23:02:42 +00003689
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003690 return Builder.CreateLoad(Entry);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003691}
3692
John McCall31168b02011-06-15 23:02:42 +00003693llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3694 const ObjCInterfaceDecl *ID) {
3695 return EmitClassRefFromId(Builder, ID->getIdentifier());
3696}
3697
3698llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3699 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3700 return EmitClassRefFromId(Builder, II);
3701}
3702
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003703llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3704 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003705 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003706
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003707 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003708 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003709 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003710 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003711 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003712 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3713 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003714 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003715 }
3716
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003717 if (lvalue)
3718 return Entry;
Benjamin Kramer76399eb2011-09-27 21:06:10 +00003719 return Builder.CreateLoad(Entry);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003720}
3721
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003722llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003723 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003724
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003725 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003726 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner9c818332012-02-05 02:30:40 +00003727 llvm::ConstantDataArray::getString(VMContext,
3728 Ident->getNameStart()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00003729 ((ObjCABI == 2) ?
3730 "__TEXT,__objc_classname,cstring_literals" :
3731 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003732 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003733
Owen Anderson170229f2009-07-14 23:10:40 +00003734 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003735}
3736
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003737llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3738 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3739 I = MethodDefinitions.find(MD);
3740 if (I != MethodDefinitions.end())
3741 return I->second;
3742
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00003743 return NULL;
3744}
3745
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003746/// GetIvarLayoutName - Returns a unique constant for the given
3747/// ivar layout bitmap.
3748llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003749 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003750 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003751}
3752
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003753void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003754 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003755 bool ForStrongLayout,
3756 bool &HasUnion) {
3757 const RecordDecl *RD = RT->getDecl();
3758 // FIXME - Use iterator.
Jordy Rosea91768e2011-07-22 02:08:32 +00003759 SmallVector<const FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Chris Lattner2192fe52011-07-18 04:24:23 +00003760 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003761 const llvm::StructLayout *RecLayout =
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003762 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003763
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003764 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3765 ForStrongLayout, HasUnion);
3766}
3767
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003768void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003769 const llvm::StructLayout *Layout,
3770 const RecordDecl *RD,
Bill Wendlingf1a3fca2012-02-22 09:30:11 +00003771 ArrayRef<const FieldDecl*> RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003772 unsigned int BytePos, bool ForStrongLayout,
3773 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003774 bool IsUnion = (RD && RD->isUnion());
3775 uint64_t MaxUnionIvarSize = 0;
3776 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosea91768e2011-07-22 02:08:32 +00003777 const FieldDecl *MaxField = 0;
3778 const FieldDecl *MaxSkippedField = 0;
3779 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003780 uint64_t MaxFieldOffset = 0;
3781 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003782 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCall31168b02011-06-15 23:02:42 +00003783 uint64_t FirstFieldDelta = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003784
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003785 if (RecFields.empty())
3786 return;
Douglas Gregore8bbc122011-09-02 00:18:52 +00003787 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
3788 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
John McCall31168b02011-06-15 23:02:42 +00003789 if (!RD && CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003790 const FieldDecl *FirstField = RecFields[0];
John McCall31168b02011-06-15 23:02:42 +00003791 FirstFieldDelta =
3792 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3793 }
3794
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003795 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003796 const FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003797 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003798 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00003799 // Note that 'i' here is actually the field index inside RD of Field,
3800 // although this dependency is hidden.
3801 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCall31168b02011-06-15 23:02:42 +00003802 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003803 } else
John McCall31168b02011-06-15 23:02:42 +00003804 FieldOffset =
3805 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003806
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003807 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003808 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003809 LastFieldBitfieldOrUnnamed = Field;
3810 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003811 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003812 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003813
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003814 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003815 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003816 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003817 if (FQT->isUnionType())
3818 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003819
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003820 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003821 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003822 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003823 continue;
3824 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003825
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003826 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003827 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003828 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003829 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003830 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003831 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003832 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3833 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003834 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003835 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003836 FQT = CArray->getElementType();
3837 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003838
3839 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003840 "layout for array of unions not supported");
Fariborz Jahanian9a7d57d2011-01-03 19:23:18 +00003841 if (FQT->isRecordType() && ElCount) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003842 int OldIndex = IvarsInfo.size() - 1;
3843 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003844
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003845 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003846 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003847 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003848
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003849 // Replicate layout information for each array element. Note that
3850 // one element is already done.
3851 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003852 for (int FirstIndex = IvarsInfo.size() - 1,
3853 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003854 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003855 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3856 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3857 IvarsInfo[i].ivar_size));
3858 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3859 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3860 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003861 }
3862 continue;
3863 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003864 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003865 // At this point, we are done with Record/Union and array there of.
3866 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00003867 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003868
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003869 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00003870 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3871 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003872 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003873 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003874 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003875 MaxUnionIvarSize = UnionIvarSize;
3876 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003877 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003878 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003879 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003880 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003881 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003882 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003883 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00003884 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3885 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003886 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00003887 // FIXME: Why the asymmetry? We divide by word size in bits on other
3888 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003889 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003890 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003891 MaxSkippedUnionIvarSize = UnionIvarSize;
3892 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003893 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003894 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003895 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003896 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003897 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003898 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003899 }
3900 }
3901 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003902
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003903 if (LastFieldBitfieldOrUnnamed) {
3904 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3905 // Last field was a bitfield. Must update skip info.
Richard Smithcaf33902011-10-10 18:28:20 +00003906 uint64_t BitFieldSize
3907 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis2fdb5b52010-09-06 12:00:10 +00003908 GC_IVAR skivar;
3909 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3910 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3911 + ((BitFieldSize % ByteSizeInBits) != 0);
3912 SkipIvars.push_back(skivar);
3913 } else {
3914 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3915 // Last field was unnamed. Must update skip info.
3916 unsigned FieldSize
3917 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3918 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3919 FieldSize / ByteSizeInBits));
3920 }
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003921 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003922
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003923 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003924 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003925 MaxUnionIvarSize));
3926 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00003927 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003928 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003929}
3930
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00003931/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3932/// the computations and returning the layout bitmap (for ivar or blocks) in
3933/// the given argument BitMap string container. Routine reads
3934/// two containers, IvarsInfo and SkipIvars which are assumed to be
3935/// filled already by the caller.
Chris Lattner9c818332012-02-05 02:30:40 +00003936llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string &BitMap) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003937 unsigned int WordsToScan, WordsToSkip;
Chris Lattnerece04092012-02-07 00:39:47 +00003938 llvm::Type *PtrTy = CGM.Int8PtrTy;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003939
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003940 // Build the string of skip/scan nibbles
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003941 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003942 unsigned int WordSize =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003943 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003944 if (IvarsInfo[0].ivar_bytepos == 0) {
3945 WordsToSkip = 0;
3946 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003947 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003948 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3949 WordsToScan = IvarsInfo[0].ivar_size;
3950 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003951 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003952 unsigned int TailPrevGCObjC =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003953 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003954 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003955 // consecutive 'scanned' object pointers.
3956 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003957 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003958 // Skip over 'gc'able object pointer which lay over each other.
3959 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3960 continue;
3961 // Must skip over 1 or more words. We save current skip/scan values
3962 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003963 SKIP_SCAN SkScan;
3964 SkScan.skip = WordsToSkip;
3965 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003966 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003967
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003968 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003969 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3970 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003971 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003972 WordsToSkip = 0;
3973 WordsToScan = IvarsInfo[i].ivar_size;
3974 }
3975 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003976 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003977 SKIP_SCAN SkScan;
3978 SkScan.skip = WordsToSkip;
3979 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003980 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003981 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003982
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003983 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003984 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003985 int LastByteSkipped =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003986 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003987 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003988 int LastByteScanned =
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00003989 IvarsInfo[LastIndex].ivar_bytepos +
3990 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003991 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00003992 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003993 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003994 SKIP_SCAN SkScan;
3995 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3996 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003997 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003998 }
3999 }
4000 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
4001 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00004002 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004003 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004004 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
4005 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
4006 // 0xM0 followed by 0x0N detected.
4007 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
4008 for (int j = i+1; j < SkipScan; j++)
4009 SkipScanIvars[j] = SkipScanIvars[j+1];
4010 --SkipScan;
4011 }
4012 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004013
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004014 // Generate the string.
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004015 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004016 unsigned char byte;
4017 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
4018 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
4019 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
4020 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004021
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004022 // first skip big.
4023 for (unsigned int ix = 0; ix < skip_big; ix++)
4024 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004025
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004026 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004027 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004028 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004029 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004030 byte |= 0xf;
4031 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004032 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004033 byte |= scan_small;
4034 scan_small = 0;
4035 }
4036 BitMap += byte;
4037 }
4038 // next scan big
4039 for (unsigned int ix = 0; ix < scan_big; ix++)
4040 BitMap += (unsigned char)(0x0f);
4041 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00004042 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00004043 byte = scan_small;
4044 BitMap += byte;
4045 }
4046 }
4047 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00004048 unsigned char zero = 0;
4049 BitMap += zero;
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004050
4051 llvm::GlobalVariable * Entry =
4052 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Chris Lattner9c818332012-02-05 02:30:40 +00004053 llvm::ConstantDataArray::getString(VMContext, BitMap,false),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004054 ((ObjCABI == 2) ?
4055 "__TEXT,__objc_classname,cstring_literals" :
4056 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004057 1, true);
4058 return getConstantGEP(VMContext, Entry, 0, 0);
4059}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004060
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004061/// BuildIvarLayout - Builds ivar layout bitmap for the class
4062/// implementation for the __strong or __weak case.
4063/// The layout map displays which words in ivar list must be skipped
4064/// and which must be scanned by GC (see below). String is built of bytes.
4065/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
4066/// of words to skip and right nibble is count of words to scan. So, each
4067/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
4068/// represented by a 0x00 byte which also ends the string.
4069/// 1. when ForStrongLayout is true, following ivars are scanned:
4070/// - id, Class
4071/// - object *
4072/// - __strong anything
4073///
4074/// 2. When ForStrongLayout is false, following ivars are scanned:
4075/// - __weak anything
4076///
4077llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
4078 const ObjCImplementationDecl *OMD,
4079 bool ForStrongLayout) {
4080 bool hasUnion = false;
4081
Chris Lattnerece04092012-02-07 00:39:47 +00004082 llvm::Type *PtrTy = CGM.Int8PtrTy;
Douglas Gregor79a91412011-09-13 17:21:33 +00004083 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCall31168b02011-06-15 23:02:42 +00004084 !CGM.getLangOptions().ObjCAutoRefCount)
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004085 return llvm::Constant::getNullValue(PtrTy);
4086
Jordy Rosea91768e2011-07-22 02:08:32 +00004087 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
4088 SmallVector<const FieldDecl*, 32> RecFields;
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00004089 if (CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosea91768e2011-07-22 02:08:32 +00004090 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00004091 IVD; IVD = IVD->getNextIvar())
4092 RecFields.push_back(cast<FieldDecl>(IVD));
4093 }
4094 else {
Jordy Rosea91768e2011-07-22 02:08:32 +00004095 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCall31168b02011-06-15 23:02:42 +00004096 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004097
Jordy Rosea91768e2011-07-22 02:08:32 +00004098 // FIXME: This is not ideal; we shouldn't have to do this copy.
4099 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00004100 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004101
4102 if (RecFields.empty())
4103 return llvm::Constant::getNullValue(PtrTy);
4104
4105 SkipIvars.clear();
4106 IvarsInfo.clear();
4107
4108 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
4109 if (IvarsInfo.empty())
4110 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004111 // Sort on byte position in case we encounterred a union nested in
4112 // the ivar list.
4113 if (hasUnion && !IvarsInfo.empty())
4114 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4115 if (hasUnion && !SkipIvars.empty())
4116 std::sort(SkipIvars.begin(), SkipIvars.end());
4117
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004118 std::string BitMap;
Fariborz Jahanian1f78a9a2010-08-05 00:19:48 +00004119 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004120
4121 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004122 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00004123 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar56df9772010-08-17 22:39:59 +00004124 OMD->getClassInterface()->getName().data());
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00004125 const unsigned char *s = (unsigned char*)BitMap.c_str();
Bill Wendling53136852012-02-07 09:06:01 +00004126 for (unsigned i = 0, e = BitMap.size(); i < e; i++)
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00004127 if (!(s[i] & 0xf0))
4128 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4129 else
4130 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4131 printf("\n");
4132 }
Fariborz Jahanian9659f6b2010-08-04 23:55:24 +00004133 return C;
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00004134}
4135
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004136llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004137 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4138
Chris Lattner3def9ae2012-02-06 22:16:34 +00004139 // FIXME: Avoid std::string in "Sel.getAsString()"
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004140 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004141 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Chris Lattner9c818332012-02-05 02:30:40 +00004142 llvm::ConstantDataArray::getString(VMContext, Sel.getAsString()),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004143 ((ObjCABI == 2) ?
4144 "__TEXT,__objc_methname,cstring_literals" :
4145 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004146 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004147
Owen Anderson170229f2009-07-14 23:10:40 +00004148 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004149}
4150
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004151// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004152llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004153 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4154}
4155
Daniel Dunbarf5c18462009-04-20 06:54:31 +00004156llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004157 std::string TypeStr;
4158 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4159
4160 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00004161
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004162 if (!Entry)
4163 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner9c818332012-02-05 02:30:40 +00004164 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004165 ((ObjCABI == 2) ?
4166 "__TEXT,__objc_methtype,cstring_literals" :
4167 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004168 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004169
Owen Anderson170229f2009-07-14 23:10:40 +00004170 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00004171}
4172
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004173llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4174 bool Extended) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004175 std::string TypeStr;
Bill Wendlinge22bef72012-02-09 22:45:21 +00004176 if (CGM.getContext().getObjCEncodingForMethodDecl(D, TypeStr, Extended))
Douglas Gregora9d84932011-05-27 01:19:52 +00004177 return 0;
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004178
4179 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4180
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004181 if (!Entry)
4182 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Chris Lattner9c818332012-02-05 02:30:40 +00004183 llvm::ConstantDataArray::getString(VMContext, TypeStr),
Daniel Dunbar7c9295a2011-03-25 20:09:09 +00004184 ((ObjCABI == 2) ?
4185 "__TEXT,__objc_methtype,cstring_literals" :
4186 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004187 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00004188
Owen Anderson170229f2009-07-14 23:10:40 +00004189 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004190}
4191
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004192// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004193llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004194 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004195
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004196 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004197 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Chris Lattner9c818332012-02-05 02:30:40 +00004198 llvm::ConstantDataArray::getString(VMContext,
4199 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004200 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00004201 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004202
Owen Anderson170229f2009-07-14 23:10:40 +00004203 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004204}
4205
4206// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00004207// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00004208llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004209CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4210 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00004211 std::string TypeStr;
4212 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00004213 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4214}
4215
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004216void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00004217 const ObjCContainerDecl *CD,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004218 SmallVectorImpl<char> &Name) {
Daniel Dunbard2386812009-10-19 01:21:19 +00004219 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00004220 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00004221 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4222 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004223 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00004224 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer2f569922012-02-07 11:57:45 +00004225 OS << '(' << *CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00004226 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00004227}
4228
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004229void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004230 EmitModuleInfo();
4231
Daniel Dunbarc475d422008-10-29 22:36:39 +00004232 // Emit the dummy bodies for any protocols which were referenced but
4233 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004234 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00004235 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4236 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00004237 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004238
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004239 llvm::Constant *Values[5];
Owen Anderson0b75f232009-07-31 20:28:54 +00004240 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004241 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00004242 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004243 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00004244 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004245 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004246 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00004247 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00004248 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00004249 }
4250
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004251 // Add assembler directives to add lazy undefined symbol references
4252 // for classes which are referenced but not defined. This is
4253 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00004254 //
4255 // FIXME: It would be nice if we had an LLVM construct for this.
4256 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00004257 SmallString<256> Asm;
Daniel Dunbard027a922009-09-07 00:20:42 +00004258 Asm += CGM.getModule().getModuleInlineAsm();
4259 if (!Asm.empty() && Asm.back() != '\n')
4260 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004261
Daniel Dunbard027a922009-09-07 00:20:42 +00004262 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00004263 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4264 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00004265 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4266 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00004267 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004268 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00004269 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004270 }
4271
Bill Wendling53136852012-02-07 09:06:01 +00004272 for (size_t i = 0, e = DefinedCategoryNames.size(); i < e; ++i) {
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00004273 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4274 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4275 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00004276
Daniel Dunbard027a922009-09-07 00:20:42 +00004277 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00004278 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004279}
4280
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004281CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004282 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00004283 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004284 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004285 ObjCABI = 2;
4286}
4287
Daniel Dunbar3ad53482008-08-11 21:35:06 +00004288/* *** */
4289
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004290ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Douglas Gregora95f9aa2012-01-17 23:38:32 +00004291 : VMContext(cgm.getLLVMContext()), CGM(cgm), ExternalProtocolPtrTy(0)
4292{
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004293 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4294 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004295
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004296 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004297 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004298 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00004299 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Chris Lattnerece04092012-02-07 00:39:47 +00004300 Int8PtrTy = CGM.Int8PtrTy;
4301 Int8PtrPtrTy = CGM.Int8PtrPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004302
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004303 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004304 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004305 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004306
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004307 // I'm not sure I like this. The implicit coordination is a bit
4308 // gross. We should solve this in a reasonable fashion because this
4309 // is a pretty common task (match some runtime data structure with
4310 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004311
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004312 // FIXME: This is leaked.
4313 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004314
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004315 // struct _objc_super {
4316 // id self;
4317 // Class cls;
4318 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00004319 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004320 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004321 SourceLocation(), SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004322 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004323 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004324 Ctx.getObjCIdType(), 0, 0, false, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004325 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004326 Ctx.getObjCClassType(), 0, 0, false, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004327 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004328
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004329 SuperCTy = Ctx.getTagDeclType(RD);
4330 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004331
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004332 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004333 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4334
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004335 // struct _prop_t {
4336 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004337 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004338 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004339 PropertyTy = llvm::StructType::create("struct._prop_t",
4340 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004341
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004342 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004343 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004344 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004345 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004346 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004347 PropertyListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004348 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4349 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004350 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004351 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004352
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004353 // struct _objc_method {
4354 // SEL _cmd;
4355 // char *method_type;
4356 // char *_imp;
4357 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004358 MethodTy = llvm::StructType::create("struct._objc_method",
4359 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
4360 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004361
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004362 // struct _objc_cache *
Chris Lattner5ec04a52011-08-12 17:43:31 +00004363 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004364 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00004365
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004366}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00004367
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004368ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004369 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004370 // struct _objc_method_description {
4371 // SEL name;
4372 // char *types;
4373 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004374 MethodDescriptionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004375 llvm::StructType::create("struct._objc_method_description",
4376 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004377
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004378 // struct _objc_method_description_list {
4379 // int count;
4380 // struct _objc_method_description[1];
4381 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004382 MethodDescriptionListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004383 llvm::StructType::create("struct._objc_method_description_list",
4384 IntTy,
4385 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004386
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004387 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004388 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004389 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004390
Daniel Dunbarb036db82008-08-13 03:21:16 +00004391 // Protocol description structures
4392
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004393 // struct _objc_protocol_extension {
4394 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4395 // struct _objc_method_description_list *optional_instance_methods;
4396 // struct _objc_method_description_list *optional_class_methods;
4397 // struct _objc_property_list *instance_properties;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004398 // const char ** extendedMethodTypes;
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004399 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004400 ProtocolExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004401 llvm::StructType::create("struct._objc_protocol_extension",
4402 IntTy, MethodDescriptionListPtrTy,
4403 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004404 Int8PtrPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004405
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004406 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004407 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004408
Daniel Dunbarc475d422008-10-29 22:36:39 +00004409 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00004410
Chris Lattnera5f58b02011-07-09 17:41:47 +00004411 ProtocolTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004412 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbarb036db82008-08-13 03:21:16 +00004413
Chris Lattnera5f58b02011-07-09 17:41:47 +00004414 ProtocolListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004415 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattnera5f58b02011-07-09 17:41:47 +00004416 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004417 LongTy,
Chris Lattnera5f58b02011-07-09 17:41:47 +00004418 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00004419 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004420
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004421 // struct _objc_protocol {
4422 // struct _objc_protocol_extension *isa;
4423 // char *protocol_name;
4424 // struct _objc_protocol **_objc_protocol_list;
4425 // struct _objc_method_description_list *instance_methods;
4426 // struct _objc_method_description_list *class_methods;
4427 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004428 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
4429 llvm::PointerType::getUnqual(ProtocolListTy),
4430 MethodDescriptionListPtrTy,
4431 MethodDescriptionListPtrTy,
4432 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004433
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004434 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004435 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004436
Owen Anderson9793f0e2009-07-29 22:16:19 +00004437 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004438
4439 // Class description structures
4440
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004441 // struct _objc_ivar {
4442 // char *ivar_name;
4443 // char *ivar_type;
4444 // int ivar_offset;
4445 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004446 IvarTy = llvm::StructType::create("struct._objc_ivar",
4447 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004448
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004449 // struct _objc_ivar_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004450 IvarListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004451 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004452 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004453
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004454 // struct _objc_method_list *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004455 MethodListTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004456 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson9793f0e2009-07-29 22:16:19 +00004457 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004458
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004459 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004460 ClassExtensionTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004461 llvm::StructType::create("struct._objc_class_extension",
4462 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004463 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004464
Chris Lattner5ec04a52011-08-12 17:43:31 +00004465 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004466
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004467 // struct _objc_class {
4468 // Class isa;
4469 // Class super_class;
4470 // char *name;
4471 // long version;
4472 // long info;
4473 // long instance_size;
4474 // struct _objc_ivar_list *ivars;
4475 // struct _objc_method_list *methods;
4476 // struct _objc_cache *cache;
4477 // struct _objc_protocol_list *protocols;
4478 // char *ivar_layout;
4479 // struct _objc_class_ext *ext;
4480 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004481 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
4482 llvm::PointerType::getUnqual(ClassTy),
4483 Int8PtrTy,
4484 LongTy,
4485 LongTy,
4486 LongTy,
4487 IvarListPtrTy,
4488 MethodListPtrTy,
4489 CachePtrTy,
4490 ProtocolListPtrTy,
4491 Int8PtrTy,
4492 ClassExtensionPtrTy,
4493 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004494
Owen Anderson9793f0e2009-07-29 22:16:19 +00004495 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004496
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004497 // struct _objc_category {
4498 // char *category_name;
4499 // char *class_name;
4500 // struct _objc_method_list *instance_method;
4501 // struct _objc_method_list *class_method;
4502 // uint32_t size; // sizeof(struct _objc_category)
4503 // struct _objc_property_list *instance_properties;// category's @property
4504 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004505 CategoryTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004506 llvm::StructType::create("struct._objc_category",
4507 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4508 MethodListPtrTy, ProtocolListPtrTy,
4509 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004510
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004511 // Global metadata structures
4512
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004513 // struct _objc_symtab {
4514 // long sel_ref_cnt;
4515 // SEL *refs;
4516 // short cls_def_cnt;
4517 // short cat_def_cnt;
4518 // char *defs[cls_def_cnt + cat_def_cnt];
4519 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004520 SymtabTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004521 llvm::StructType::create("struct._objc_symtab",
4522 LongTy, SelectorPtrTy, ShortTy, ShortTy,
4523 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004524 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004525
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00004526 // struct _objc_module {
4527 // long version;
4528 // long size; // sizeof(struct _objc_module)
4529 // char *name;
4530 // struct _objc_symtab* symtab;
4531 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004532 ModuleTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004533 llvm::StructType::create("struct._objc_module",
4534 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004535
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004536
Mike Stump18bb9282009-05-16 07:57:57 +00004537 // FIXME: This is the size of the setjmp buffer and should be target
4538 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00004539 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004540
Anders Carlsson9ff22482008-09-09 10:10:21 +00004541 // Exceptions
Chris Lattnerece04092012-02-07 00:39:47 +00004542 llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004543
4544 ExceptionDataTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004545 llvm::StructType::create("struct._objc_exception_data",
Chris Lattnerece04092012-02-07 00:39:47 +00004546 llvm::ArrayType::get(CGM.Int32Ty,SetJmpBufferSize),
4547 StackPtrTy, NULL);
Anders Carlsson9ff22482008-09-09 10:10:21 +00004548
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004549}
4550
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004551ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004552 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004553 // struct _method_list_t {
4554 // uint32_t entsize; // sizeof(struct _objc_method)
4555 // uint32_t method_count;
4556 // struct _objc_method method_list[method_count];
4557 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004558 MethodListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004559 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
4560 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004561 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004562 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004563
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004564 // struct _protocol_t {
4565 // id isa; // NULL
4566 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004567 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004568 // const struct method_list_t * const instance_methods;
4569 // const struct method_list_t * const class_methods;
4570 // const struct method_list_t *optionalInstanceMethods;
4571 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004572 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004573 // const uint32_t size; // sizeof(struct _protocol_t)
4574 // const uint32_t flags; // = 0
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004575 // const char ** extendedMethodTypes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004576 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004577
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004578 // Holder for struct _protocol_list_t *
Chris Lattnera5f58b02011-07-09 17:41:47 +00004579 ProtocolListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004580 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004581
Chris Lattnera5f58b02011-07-09 17:41:47 +00004582 ProtocolnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004583 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
4584 llvm::PointerType::getUnqual(ProtocolListnfABITy),
4585 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4586 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilson5f4e3a72011-11-30 01:57:58 +00004587 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
4588 NULL);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004589
4590 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004591 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004592
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004593 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004594 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004595 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004596 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004597 ProtocolListnfABITy->setBody(LongTy,
4598 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
4599 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004600
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004601 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004602 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004603
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004604 // struct _ivar_t {
4605 // unsigned long int *offset; // pointer to ivar offset location
4606 // char *name;
4607 // char *type;
4608 // uint32_t alignment;
4609 // uint32_t size;
4610 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004611 IvarnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004612 llvm::StructType::create("struct._ivar_t",
4613 llvm::PointerType::getUnqual(LongTy),
4614 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004615
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004616 // struct _ivar_list_t {
4617 // uint32 entsize; // sizeof(struct _ivar_t)
4618 // uint32 count;
4619 // struct _iver_t list[count];
4620 // }
Chris Lattnera5f58b02011-07-09 17:41:47 +00004621 IvarListnfABITy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004622 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
4623 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004624
Owen Anderson9793f0e2009-07-29 22:16:19 +00004625 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004626
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004627 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004628 // uint32_t const flags;
4629 // uint32_t const instanceStart;
4630 // uint32_t const instanceSize;
4631 // uint32_t const reserved; // only when building for 64bit targets
4632 // const uint8_t * const ivarLayout;
4633 // const char *const name;
4634 // const struct _method_list_t * const baseMethods;
4635 // const struct _objc_protocol_list *const baseProtocols;
4636 // const struct _ivar_list_t *const ivars;
4637 // const uint8_t * const weakIvarLayout;
4638 // const struct _prop_list_t * const properties;
4639 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004640
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004641 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattner5ec04a52011-08-12 17:43:31 +00004642 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
4643 IntTy, IntTy, IntTy, Int8PtrTy,
4644 Int8PtrTy, MethodListnfABIPtrTy,
4645 ProtocolListnfABIPtrTy,
4646 IvarListnfABIPtrTy,
4647 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004648
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004649 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattnera5f58b02011-07-09 17:41:47 +00004650 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall9dc0db22011-05-15 01:53:33 +00004651 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4652 ->getPointerTo();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004653
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004654 // struct _class_t {
4655 // struct _class_t *isa;
4656 // struct _class_t * const superclass;
4657 // void *cache;
4658 // IMP *vtable;
4659 // struct class_ro_t *ro;
4660 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004661
Chris Lattner5ec04a52011-08-12 17:43:31 +00004662 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattnera5f58b02011-07-09 17:41:47 +00004663 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
4664 llvm::PointerType::getUnqual(ClassnfABITy),
4665 CachePtrTy,
4666 llvm::PointerType::getUnqual(ImpnfABITy),
4667 llvm::PointerType::getUnqual(ClassRonfABITy),
4668 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004669
Fariborz Jahanian71394042009-01-23 23:53:38 +00004670 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004671 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004672
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004673 // struct _category_t {
4674 // const char * const name;
4675 // struct _class_t *const cls;
4676 // const struct _method_list_t * const instance_methods;
4677 // const struct _method_list_t * const class_methods;
4678 // const struct _protocol_list_t * const protocols;
4679 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004680 // }
Chris Lattner5ec04a52011-08-12 17:43:31 +00004681 CategorynfABITy = llvm::StructType::create("struct._category_t",
4682 Int8PtrTy, ClassnfABIPtrTy,
4683 MethodListnfABIPtrTy,
4684 MethodListnfABIPtrTy,
4685 ProtocolListnfABIPtrTy,
4686 PropertyListPtrTy,
4687 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004688
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004689 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004690 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4691 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004692
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004693 // MessageRefTy - LLVM for:
4694 // struct _message_ref_t {
4695 // IMP messenger;
4696 // SEL name;
4697 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004698
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004699 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004700 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004701 Ctx.getTranslationUnitDecl(),
Abramo Bagnara29c2d462011-03-09 14:09:51 +00004702 SourceLocation(), SourceLocation(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004703 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004704 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004705 Ctx.VoidPtrTy, 0, 0, false, false));
Abramo Bagnaradff19302011-03-08 08:55:46 +00004706 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith938f40b2011-06-11 17:19:42 +00004707 Ctx.getObjCSelType(), 0, 0, false, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004708 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004709
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004710 MessageRefCTy = Ctx.getTagDeclType(RD);
4711 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4712 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004713
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004714 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004715 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004716
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004717 // SuperMessageRefTy - LLVM for:
4718 // struct _super_message_ref_t {
4719 // SUPER_IMP messenger;
4720 // SEL name;
4721 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004722 SuperMessageRefTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004723 llvm::StructType::create("struct._super_message_ref_t",
4724 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004725
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004726 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004727 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian0a3cfcc2011-06-22 20:21:51 +00004728
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004729
4730 // struct objc_typeinfo {
4731 // const void** vtable; // objc_ehtype_vtable + 2
4732 // const char* name; // c++ typeinfo string
4733 // Class cls;
4734 // };
Chris Lattnera5f58b02011-07-09 17:41:47 +00004735 EHTypeTy =
Chris Lattner5ec04a52011-08-12 17:43:31 +00004736 llvm::StructType::create("struct._objc_typeinfo",
4737 llvm::PointerType::getUnqual(Int8PtrTy),
4738 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004739 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004740}
4741
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004742llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004743 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004744
Fariborz Jahanian71394042009-01-23 23:53:38 +00004745 return NULL;
4746}
4747
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00004748void CGObjCNonFragileABIMac::
4749AddModuleClassList(ArrayRef<llvm::GlobalValue*> Container,
4750 const char *SymbolName,
4751 const char *SectionName) {
Daniel Dunbar19573e72009-05-15 21:48:48 +00004752 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004753
Daniel Dunbar19573e72009-05-15 21:48:48 +00004754 if (!NumClasses)
4755 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004756
Chris Lattner3def9ae2012-02-06 22:16:34 +00004757 SmallVector<llvm::Constant*, 8> Symbols(NumClasses);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004758 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004759 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004760 ObjCTypes.Int8PtrTy);
Chris Lattner3def9ae2012-02-06 22:16:34 +00004761 llvm::Constant *Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004762 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Chris Lattner3def9ae2012-02-06 22:16:34 +00004763 Symbols.size()),
Daniel Dunbar19573e72009-05-15 21:48:48 +00004764 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004765
Daniel Dunbar19573e72009-05-15 21:48:48 +00004766 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004767 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004768 llvm::GlobalValue::InternalLinkage,
4769 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004770 SymbolName);
Daniel Dunbar710cb202010-04-25 20:39:32 +00004771 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004772 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004773 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004774}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004775
Fariborz Jahanian71394042009-01-23 23:53:38 +00004776void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4777 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004778
Daniel Dunbar19573e72009-05-15 21:48:48 +00004779 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004780 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004781 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004782 "\01L_OBJC_LABEL_CLASS_$",
4783 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004784
Bill Wendling53136852012-02-07 09:06:01 +00004785 for (unsigned i = 0, e = DefinedClasses.size(); i < e; i++) {
Fariborz Jahanian67260552009-11-17 21:37:35 +00004786 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4787 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4788 continue;
4789 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004790 }
4791
Bill Wendling53136852012-02-07 09:06:01 +00004792 for (unsigned i = 0, e = DefinedMetaClasses.size(); i < e; i++) {
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004793 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4794 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4795 continue;
4796 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4797 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00004798
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004799 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004800 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4801 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004802
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004803 // Build list of all implemented category addresses in array
4804 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004805 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004806 "\01L_OBJC_LABEL_CATEGORY_$",
4807 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004808 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004809 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4810 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004811
Daniel Dunbar5e639272010-04-25 20:39:01 +00004812 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004813}
4814
John McCall9e8bb002011-05-14 03:10:52 +00004815/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4816/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004817/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004818/// message dispatch call for all the rest.
John McCall9e8bb002011-05-14 03:10:52 +00004819bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4820 // At various points we've experimented with using vtable-based
4821 // dispatch for all methods.
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004822 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004823 case CodeGenOptions::Legacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004824 return false;
John McCall9e8bb002011-05-14 03:10:52 +00004825 case CodeGenOptions::NonLegacy:
4826 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004827 case CodeGenOptions::Mixed:
4828 break;
4829 }
4830
4831 // If so, see whether this selector is in the white-list of things which must
4832 // use the new dispatch convention. We lazily build a dense set for this.
John McCall9e8bb002011-05-14 03:10:52 +00004833 if (VTableDispatchMethods.empty()) {
4834 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4835 VTableDispatchMethods.insert(GetNullarySelector("class"));
4836 VTableDispatchMethods.insert(GetNullarySelector("self"));
4837 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4838 VTableDispatchMethods.insert(GetNullarySelector("length"));
4839 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004840
John McCall9e8bb002011-05-14 03:10:52 +00004841 // These are vtable-based if GC is disabled.
4842 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregor79a91412011-09-13 17:21:33 +00004843 if (CGM.getLangOptions().getGC() != LangOptions::GCOnly) {
John McCall9e8bb002011-05-14 03:10:52 +00004844 VTableDispatchMethods.insert(GetNullarySelector("retain"));
4845 VTableDispatchMethods.insert(GetNullarySelector("release"));
4846 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4847 }
4848
4849 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4850 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4851 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4852 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4853 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4854 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4855 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4856
4857 // These are vtable-based if GC is enabled.
4858 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregor79a91412011-09-13 17:21:33 +00004859 if (CGM.getLangOptions().getGC() != LangOptions::NonGC) {
John McCall9e8bb002011-05-14 03:10:52 +00004860 VTableDispatchMethods.insert(GetNullarySelector("hash"));
4861 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4862
4863 // "countByEnumeratingWithState:objects:count"
4864 IdentifierInfo *KeyIdents[] = {
4865 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4866 &CGM.getContext().Idents.get("objects"),
4867 &CGM.getContext().Idents.get("count")
4868 };
4869 VTableDispatchMethods.insert(
4870 CGM.getContext().Selectors.getSelector(3, KeyIdents));
4871 }
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004872 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004873
John McCall9e8bb002011-05-14 03:10:52 +00004874 return VTableDispatchMethods.count(Sel);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004875}
4876
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004877// Metadata flags
4878enum MetaDataDlags {
4879 CLS = 0x0,
4880 CLS_META = 0x1,
4881 CLS_ROOT = 0x2,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004882 OBJC2_CLS_HIDDEN = 0x10,
John McCall31168b02011-06-15 23:02:42 +00004883 CLS_EXCEPTION = 0x20,
4884
4885 /// (Obsolete) ARC-specific: this class has a .release_ivars method
4886 CLS_HAS_IVAR_RELEASER = 0x40,
4887 /// class was compiled with -fobjc-arr
4888 CLS_COMPILED_BY_ARC = 0x80 // (1<<7)
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004889};
4890/// BuildClassRoTInitializer - generate meta-data for:
4891/// struct _class_ro_t {
4892/// uint32_t const flags;
4893/// uint32_t const instanceStart;
4894/// uint32_t const instanceSize;
4895/// uint32_t const reserved; // only when building for 64bit targets
4896/// const uint8_t * const ivarLayout;
4897/// const char *const name;
4898/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004899/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004900/// const struct _ivar_list_t *const ivars;
4901/// const uint8_t * const weakIvarLayout;
4902/// const struct _prop_list_t * const properties;
4903/// }
4904///
4905llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004906 unsigned flags,
4907 unsigned InstanceStart,
4908 unsigned InstanceSize,
4909 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004910 std::string ClassName = ID->getNameAsString();
Benjamin Kramer22d24c22011-10-15 12:20:02 +00004911 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCall31168b02011-06-15 23:02:42 +00004912
4913 if (CGM.getLangOptions().ObjCAutoRefCount)
4914 flags |= CLS_COMPILED_BY_ARC;
4915
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004916 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4917 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4918 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004919 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004920 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4921 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004922 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004923 // const struct _method_list_t * const baseMethods;
4924 std::vector<llvm::Constant*> Methods;
4925 std::string MethodListName("\01l_OBJC_$_");
4926 if (flags & CLS_META) {
4927 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004928 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004929 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004930 // Class methods should always be defined.
4931 Methods.push_back(GetMethodConstant(*i));
4932 }
4933 } else {
4934 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004935 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004936 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004937 // Instance methods should always be defined.
4938 Methods.push_back(GetMethodConstant(*i));
4939 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004940 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004941 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004942 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004943
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004944 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4945 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004946
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004947 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4948 if (llvm::Constant *C = GetMethodConstant(MD))
4949 Methods.push_back(C);
4950 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4951 if (llvm::Constant *C = GetMethodConstant(MD))
4952 Methods.push_back(C);
4953 }
4954 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004955 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004956 Values[ 5] = EmitMethodList(MethodListName,
4957 "__DATA, __objc_const", Methods);
4958
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004959 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4960 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004961 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004962 + OID->getName(),
Ted Kremenek0ef508d2010-09-01 01:21:15 +00004963 OID->all_referenced_protocol_begin(),
4964 OID->all_referenced_protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004965
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004966 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004967 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004968 else
4969 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004970 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4971 : BuildIvarLayout(ID, false);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004972 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004973 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004974 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004975 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4976 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004977 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004978 Values);
4979 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004980 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4981 llvm::GlobalValue::InternalLinkage,
4982 Init,
4983 (flags & CLS_META) ?
4984 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4985 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004986 CLASS_RO_GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004987 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004988 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004989 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004990
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004991}
4992
4993/// BuildClassMetaData - This routine defines that to-level meta-data
4994/// for the given ClassName for:
4995/// struct _class_t {
4996/// struct _class_t *isa;
4997/// struct _class_t * const superclass;
4998/// void *cache;
4999/// IMP *vtable;
5000/// struct class_ro_t *ro;
5001/// }
5002///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005003llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005004 std::string &ClassName,
5005 llvm::Constant *IsAGV,
5006 llvm::Constant *SuperClassGV,
5007 llvm::Constant *ClassRoGV,
5008 bool HiddenVisibility) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005009 llvm::Constant *Values[] = {
5010 IsAGV,
5011 SuperClassGV,
5012 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
5013 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
5014 ClassRoGV // &CLASS_RO_GV
5015 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005016 if (!Values[1])
5017 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005018 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005019 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005020 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
5021 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00005022 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005023 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005024 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00005025 if (HiddenVisibility)
5026 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005027 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005028}
5029
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005030bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00005031CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005032 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005033}
5034
Daniel Dunbar961202372009-05-03 12:57:56 +00005035void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00005036 uint32_t &InstanceStart,
5037 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005038 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00005039 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005040
Daniel Dunbar9b042e02009-05-04 23:23:09 +00005041 // InstanceSize is really instance end.
Ken Dyckd5090c12011-02-11 02:20:09 +00005042 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar9b042e02009-05-04 23:23:09 +00005043
5044 // If there are no fields, the start is the same as the end.
5045 if (!RL.getFieldCount())
5046 InstanceStart = InstanceSize;
5047 else
Ken Dyckc5ca8762011-04-14 00:43:09 +00005048 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbar554fd792009-04-19 23:41:48 +00005049}
5050
Fariborz Jahanian71394042009-01-23 23:53:38 +00005051void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
5052 std::string ClassName = ID->getNameAsString();
5053 if (!ObjCEmptyCacheVar) {
5054 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005055 CGM.getModule(),
5056 ObjCTypes.CacheTy,
5057 false,
5058 llvm::GlobalValue::ExternalLinkage,
5059 0,
5060 "_objc_empty_cache");
5061
Fariborz Jahanian71394042009-01-23 23:53:38 +00005062 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005063 CGM.getModule(),
5064 ObjCTypes.ImpnfABITy,
5065 false,
5066 llvm::GlobalValue::ExternalLinkage,
5067 0,
5068 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00005069 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005070 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005071 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00005072 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005073 uint32_t InstanceStart =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005074 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005075 uint32_t InstanceSize = InstanceStart;
5076 uint32_t flags = CLS_META;
Daniel Dunbar15894b72009-04-07 05:48:37 +00005077 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
5078 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005079
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005080 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005081
5082 bool classIsHidden =
John McCall457a04e2010-10-22 21:05:15 +00005083 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005084 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005085 flags |= OBJC2_CLS_HIDDEN;
John McCall31168b02011-06-15 23:02:42 +00005086 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00005087 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005088 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005089 // class is root
5090 flags |= CLS_ROOT;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005091 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005092 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005093 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005094 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005095 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5096 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5097 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005098 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005099 if (Root->isWeakImported())
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005100 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005101 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005102 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00005103 ObjCMetaClassName +
5104 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005105 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005106 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00005107 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00005108 }
5109 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5110 InstanceStart,
5111 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005112 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005113 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005114 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5115 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00005116 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00005117
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005118 // Metadata for the class
5119 flags = CLS;
Fariborz Jahanian82208252009-01-31 00:59:10 +00005120 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005121 flags |= OBJC2_CLS_HIDDEN;
John McCall31168b02011-06-15 23:02:42 +00005122 if (ID->hasCXXStructors())
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00005123 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005124
Douglas Gregor78bd61f2009-06-18 16:11:24 +00005125 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005126 flags |= CLS_EXCEPTION;
5127
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005128 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005129 flags |= CLS_ROOT;
5130 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00005131 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005132 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00005133 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005134 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005135 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005136 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahanian67260552009-11-17 21:37:35 +00005137 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005138 }
Daniel Dunbar961202372009-05-03 12:57:56 +00005139 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005140 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005141 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005142 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00005143 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005144
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00005145 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005146 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00005147 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5148 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00005149 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005150
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005151 // Determine if this class is also "non-lazy".
5152 if (ImplementationIsNonLazy(ID))
5153 DefinedNonLazyClasses.push_back(ClassMD);
5154
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005155 // Force the definition of the EHType if necessary.
5156 if (flags & CLS_EXCEPTION)
5157 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005158 // Make sure method definition entries are all clear for next implementation.
5159 MethodDefinitions.clear();
Fariborz Jahanian71394042009-01-23 23:53:38 +00005160}
5161
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005162/// GenerateProtocolRef - This routine is called to generate code for
5163/// a protocol reference expression; as in:
5164/// @code
5165/// @protocol(Proto1);
5166/// @endcode
5167/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5168/// which will hold address of the protocol meta-data.
5169///
5170llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005171 const ObjCProtocolDecl *PD) {
5172
Fariborz Jahanian464423d2009-04-10 18:47:34 +00005173 // This routine is called for @protocol only. So, we must build definition
5174 // of protocol's meta-data (not a reference to it!)
5175 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005176 llvm::Constant *Init =
5177 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Douglas Gregor020de322012-01-17 18:36:30 +00005178 ObjCTypes.getExternalProtocolPtrTy());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005179
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005180 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar56df9772010-08-17 22:39:59 +00005181 ProtocolName += PD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005182
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005183 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5184 if (PTGV)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005185 return Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005186 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005187 CGM.getModule(),
5188 Init->getType(), false,
5189 llvm::GlobalValue::WeakAnyLinkage,
5190 Init,
5191 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005192 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5193 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005194 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005195 return Builder.CreateLoad(PTGV);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005196}
5197
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005198/// GenerateCategory - Build metadata for a category implementation.
5199/// struct _category_t {
5200/// const char * const name;
5201/// struct _class_t *const cls;
5202/// const struct _method_list_t * const instance_methods;
5203/// const struct _method_list_t * const class_methods;
5204/// const struct _protocol_list_t * const protocols;
5205/// const struct _prop_list_t * const properties;
5206/// }
5207///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005208void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005209 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005210 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005211 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5212 "_$_" + OCD->getNameAsString());
5213 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00005214 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005215
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005216 llvm::Constant *Values[6];
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005217 Values[0] = GetClassName(OCD->getIdentifier());
5218 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005219 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005220 if (Interface->isWeakImported())
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00005221 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5222
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005223 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005224 std::vector<llvm::Constant*> Methods;
5225 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005226 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005227 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005228
5229 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005230 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005231 // Instance methods should always be defined.
5232 Methods.push_back(GetMethodConstant(*i));
5233 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005234
5235 Values[2] = EmitMethodList(MethodListName,
5236 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005237 Methods);
5238
5239 MethodListName = Prefix;
5240 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5241 OCD->getNameAsString();
5242 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005243 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005244 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005245 // Class methods should always be defined.
5246 Methods.push_back(GetMethodConstant(*i));
5247 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005248
5249 Values[3] = EmitMethodList(MethodListName,
5250 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00005251 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005252 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00005253 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005254 if (Category) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00005255 SmallString<256> ExtName;
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005256 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5257 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005258 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005259 + Interface->getName() + "_$_"
5260 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005261 Category->protocol_begin(),
5262 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005263 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5264 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00005265 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00005266 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5267 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00005268 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005269
5270 llvm::Constant *Init =
5271 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005272 Values);
5273 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005274 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005275 false,
5276 llvm::GlobalValue::InternalLinkage,
5277 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005278 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005279 GCATV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005280 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005281 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005282 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005283 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00005284
5285 // Determine if this category is also "non-lazy".
5286 if (ImplementationIsNonLazy(OCD))
5287 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianc0577942011-04-22 22:02:28 +00005288 // method definition entries must be clear for next implementation.
5289 MethodDefinitions.clear();
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00005290}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005291
5292/// GetMethodConstant - Return a struct objc_method constant for the
5293/// given method if it has been defined. The result is null if the
5294/// method has not been defined. The return value has type MethodPtrTy.
5295llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005296 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis13257c52010-08-09 10:54:20 +00005297 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005298 if (!Fn)
5299 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005300
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005301 llvm::Constant *Method[] = {
Owen Andersonade90fd2009-07-29 18:54:39 +00005302 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005303 ObjCTypes.SelectorPtrTy),
5304 GetMethodVarType(MD),
5305 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5306 };
Owen Anderson0e0189d2009-07-27 22:29:56 +00005307 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005308}
5309
5310/// EmitMethodList - Build meta-data for method declarations
5311/// struct _method_list_t {
5312/// uint32_t entsize; // sizeof(struct _objc_method)
5313/// uint32_t method_count;
5314/// struct _objc_method method_list[method_count];
5315/// }
5316///
Bill Wendlingcb5b5ff2012-02-07 09:25:09 +00005317llvm::Constant *
5318CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
5319 const char *Section,
5320 ArrayRef<llvm::Constant*> Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005321 // Return null for empty list.
5322 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005323 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005324
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005325 llvm::Constant *Values[3];
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005326 // sizeof(struct _objc_method)
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005327 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005328 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005329 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005330 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005331 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005332 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005333 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005334 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005335
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005336 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005337 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005338 llvm::GlobalValue::InternalLinkage, Init, Name);
5339 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005340 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005341 CGM.AddUsedGlobal(GV);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005342 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00005343}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005344
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005345/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5346/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005347llvm::GlobalVariable *
5348CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5349 const ObjCIvarDecl *Ivar) {
5350 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00005351 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00005352 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005353 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005354 CGM.getModule().getGlobalVariable(Name);
5355 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005356 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005357 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005358 false,
5359 llvm::GlobalValue::ExternalLinkage,
5360 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005361 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00005362 return IvarOffsetGV;
5363}
5364
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00005365llvm::Constant *
5366CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5367 const ObjCIvarDecl *Ivar,
5368 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005369 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005370 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005371 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005372 IvarOffsetGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005373 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005374
Mike Stump18bb9282009-05-16 07:57:57 +00005375 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5376 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00005377 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5378 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall457a04e2010-10-22 21:05:15 +00005379 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00005380 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005381 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00005382 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendlingf7d45982011-05-04 21:37:25 +00005383 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005384 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005385}
5386
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005387/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005388/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005389/// IvarListnfABIPtrTy.
5390/// struct _ivar_t {
5391/// unsigned long int *offset; // pointer to ivar offset location
5392/// char *name;
5393/// char *type;
5394/// uint32_t alignment;
5395/// uint32_t size;
5396/// }
5397/// struct _ivar_list_t {
5398/// uint32 entsize; // sizeof(struct _ivar_t)
5399/// uint32 count;
5400/// struct _iver_t list[count];
5401/// }
5402///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005403
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005404llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005405 const ObjCImplementationDecl *ID) {
5406
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005407 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005408
Jordy Rosea91768e2011-07-22 02:08:32 +00005409 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005410 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005411
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005412 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005413
Jordy Rosea91768e2011-07-22 02:08:32 +00005414 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianb26d5782011-06-28 18:05:25 +00005415 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005416 // Ignore unnamed bit-fields.
5417 if (!IVD->getDeclName())
5418 continue;
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005419 llvm::Constant *Ivar[5];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005420 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005421 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005422 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5423 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2192fe52011-07-18 04:24:23 +00005424 llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005425 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005426 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005427 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005428 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005429 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005430 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00005431 // NOTE. Size of a bitfield does not match gcc's, because of the
5432 // way bitfields are treated special in each. But I am told that
5433 // 'size' for bitfield ivars is ignored by the runtime so it does
5434 // not matter. If it matters, there is enough info to get the
5435 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005436 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005437 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005438 }
5439 // Return null for empty list.
5440 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005441 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005442
5443 llvm::Constant *Values[3];
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005444 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005445 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5446 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005447 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005448 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005449 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005450 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005451 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5452 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005453 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005454 llvm::GlobalValue::InternalLinkage,
5455 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005456 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005457 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005458 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005459 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005460
Chris Lattnerf56501c2009-07-17 23:57:13 +00005461 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005462 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005463}
5464
5465llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005466 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005467 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005468
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005469 if (!Entry) {
5470 // We use the initializer as a marker of whether this is a forward
5471 // reference or not. At module finalization we add the empty
5472 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005473 Entry =
5474 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5475 llvm::GlobalValue::ExternalLinkage,
5476 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005477 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005478 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005479 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005480
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005481 return Entry;
5482}
5483
5484/// GetOrEmitProtocol - Generate the protocol meta-data:
5485/// @code
5486/// struct _protocol_t {
5487/// id isa; // NULL
5488/// const char * const protocol_name;
5489/// const struct _protocol_list_t * protocol_list; // super protocols
5490/// const struct method_list_t * const instance_methods;
5491/// const struct method_list_t * const class_methods;
5492/// const struct method_list_t *optionalInstanceMethods;
5493/// const struct method_list_t *optionalClassMethods;
5494/// const struct _prop_list_t * properties;
5495/// const uint32_t size; // sizeof(struct _protocol_t)
5496/// const uint32_t flags; // = 0
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005497/// const char ** extendedMethodTypes;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005498/// }
5499/// @endcode
5500///
5501
5502llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005503 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005504 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005505
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005506 // Early exit if a defining object has already been generated.
5507 if (Entry && Entry->hasInitializer())
5508 return Entry;
5509
Douglas Gregora715bff2012-01-01 19:51:50 +00005510 // Use the protocol definition, if there is one.
5511 if (const ObjCProtocolDecl *Def = PD->getDefinition())
5512 PD = Def;
5513
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005514 // Construct method lists.
5515 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5516 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005517 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005518 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005519 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005520 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005521 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005522 if (!C)
5523 return GetOrEmitProtocolRef(PD);
5524
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005525 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5526 OptInstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005527 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005528 } else {
5529 InstanceMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005530 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005531 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005532 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005533
5534 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005535 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005536 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005537 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005538 if (!C)
5539 return GetOrEmitProtocolRef(PD);
5540
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005541 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5542 OptClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005543 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005544 } else {
5545 ClassMethods.push_back(C);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005546 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005547 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005548 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005549
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005550 MethodTypesExt.insert(MethodTypesExt.end(),
5551 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
5552
5553 llvm::Constant *Values[11];
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005554 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005555 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005556 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005557 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5558 PD->protocol_begin(),
5559 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005560
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005561 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005562 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005563 "__DATA, __objc_const",
5564 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005565 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005566 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005567 "__DATA, __objc_const",
5568 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005569 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005570 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005571 "__DATA, __objc_const",
5572 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005573 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005574 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005575 "__DATA, __objc_const",
5576 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005577 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005578 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005579 uint32_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005580 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005581 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005582 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilson5f4e3a72011-11-30 01:57:58 +00005583 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
5584 + PD->getName(),
5585 MethodTypesExt, ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005586 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005587 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005588
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005589 if (Entry) {
5590 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005591 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005592 Entry->setInitializer(Init);
5593 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005594 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005595 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5596 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5597 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005598 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005599 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005600 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005601 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005602 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005603 CGM.AddUsedGlobal(Entry);
5604
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005605 // Use this protocol meta-data to build protocol list table in section
5606 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005607 llvm::GlobalVariable *PTGV =
5608 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5609 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5610 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005611 PTGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005612 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005613 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005614 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005615 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005616 return Entry;
5617}
5618
5619/// EmitProtocolList - Generate protocol list meta-data:
5620/// @code
5621/// struct _protocol_list_t {
5622/// long protocol_count; // Note, this is 32/64 bit
5623/// struct _protocol_t[protocol_count];
5624/// }
5625/// @endcode
5626///
5627llvm::Constant *
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005628CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005629 ObjCProtocolDecl::protocol_iterator begin,
5630 ObjCProtocolDecl::protocol_iterator end) {
Bill Wendlinga515b582012-02-09 22:16:49 +00005631 llvm::SmallVector<llvm::Constant*, 16> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005632
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005633 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005634 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005635 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005636
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005637 // FIXME: We shouldn't need to do this lookup here, should we?
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +00005638 SmallString<256> TmpName;
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005639 Name.toVector(TmpName);
5640 llvm::GlobalVariable *GV =
5641 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005642 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005643 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005644
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005645 for (; begin != end; ++begin)
5646 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5647
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005648 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005649 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005650 ObjCTypes.ProtocolnfABIPtrTy));
5651
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005652 llvm::Constant *Values[2];
Owen Anderson170229f2009-07-14 23:10:40 +00005653 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005654 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005655 Values[1] =
Bill Wendlinga515b582012-02-09 22:16:49 +00005656 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
5657 ProtocolRefs.size()),
5658 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005659
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005660 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005661 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005662 llvm::GlobalValue::InternalLinkage,
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005663 Init, Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005664 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005665 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005666 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005667 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005668 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005669 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005670}
5671
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005672/// GetMethodDescriptionConstant - This routine build following meta-data:
5673/// struct _objc_method {
5674/// SEL _cmd;
5675/// char *method_type;
5676/// char *_imp;
5677/// }
5678
5679llvm::Constant *
5680CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer22d24c22011-10-15 12:20:02 +00005681 llvm::Constant *Desc[3];
Owen Anderson170229f2009-07-14 23:10:40 +00005682 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005683 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5684 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005685 Desc[1] = GetMethodVarType(MD);
Douglas Gregora9d84932011-05-27 01:19:52 +00005686 if (!Desc[1])
5687 return 0;
5688
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005689 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005690 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005691 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005692}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005693
5694/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5695/// This code gen. amounts to generating code for:
5696/// @code
5697/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5698/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005699///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005700LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00005701 CodeGen::CodeGenFunction &CGF,
5702 QualType ObjectTy,
5703 llvm::Value *BaseValue,
5704 const ObjCIvarDecl *Ivar,
5705 unsigned CVRQualifiers) {
5706 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Fariborz Jahaniancaabf1b2012-02-20 22:42:22 +00005707 llvm::Value *Offset = EmitIvarOffset(CGF, ID, Ivar);
5708 if (llvm::LoadInst *LI = dyn_cast<llvm::LoadInst>(Offset))
5709 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
5710 llvm::MDNode::get(VMContext,
5711 ArrayRef<llvm::Value*>()));
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00005712 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
Fariborz Jahaniancaabf1b2012-02-20 22:42:22 +00005713 Offset);
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005714}
5715
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005716llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005717 CodeGen::CodeGenFunction &CGF,
5718 const ObjCInterfaceDecl *Interface,
5719 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005720 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005721}
5722
John McCall234eac82011-05-13 23:16:18 +00005723static void appendSelectorForMessageRefTable(std::string &buffer,
5724 Selector selector) {
5725 if (selector.isUnarySelector()) {
5726 buffer += selector.getNameForSlot(0);
5727 return;
5728 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005729
John McCall234eac82011-05-13 23:16:18 +00005730 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5731 buffer += selector.getNameForSlot(i);
5732 buffer += '_';
5733 }
5734}
5735
John McCall9e8bb002011-05-14 03:10:52 +00005736/// Emit a "v-table" message send. We emit a weak hidden-visibility
5737/// struct, initially containing the selector pointer and a pointer to
5738/// a "fixup" variant of the appropriate objc_msgSend. To call, we
5739/// load and call the function pointer, passing the address of the
5740/// struct as the second parameter. The runtime determines whether
5741/// the selector is currently emitted using vtable dispatch; if so, it
5742/// substitutes a stub function which simply tail-calls through the
5743/// appropriate vtable slot, and if not, it substitues a stub function
5744/// which tail-calls objc_msgSend. Both stubs adjust the selector
5745/// argument to correctly point to the selector.
5746RValue
5747CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5748 ReturnValueSlot returnSlot,
5749 QualType resultType,
5750 Selector selector,
5751 llvm::Value *arg0,
5752 QualType arg0Type,
5753 bool isSuper,
5754 const CallArgList &formalArgs,
5755 const ObjCMethodDecl *method) {
John McCall234eac82011-05-13 23:16:18 +00005756 // Compute the actual arguments.
5757 CallArgList args;
5758
John McCall9e8bb002011-05-14 03:10:52 +00005759 // First argument: the receiver / super-call structure.
John McCall234eac82011-05-13 23:16:18 +00005760 if (!isSuper)
John McCall9e8bb002011-05-14 03:10:52 +00005761 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5762 args.add(RValue::get(arg0), arg0Type);
John McCall234eac82011-05-13 23:16:18 +00005763
John McCall9e8bb002011-05-14 03:10:52 +00005764 // Second argument: a pointer to the message ref structure. Leave
5765 // the actual argument value blank for now.
John McCall234eac82011-05-13 23:16:18 +00005766 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5767
5768 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5769
John McCalla729c622012-02-17 03:33:10 +00005770 MessageSendInfo MSI = getMessageSendInfo(method, resultType, args);
John McCall234eac82011-05-13 23:16:18 +00005771
John McCall5880fb82011-05-14 21:12:11 +00005772 NullReturnState nullReturn;
5773
John McCall9e8bb002011-05-14 03:10:52 +00005774 // Find the function to call and the mangled name for the message
5775 // ref structure. Using a different mangled name wouldn't actually
5776 // be a problem; it would just be a waste.
5777 //
5778 // The runtime currently never uses vtable dispatch for anything
5779 // except normal, non-super message-sends.
5780 // FIXME: don't use this for that.
John McCall234eac82011-05-13 23:16:18 +00005781 llvm::Constant *fn = 0;
5782 std::string messageRefName("\01l_");
John McCalla729c622012-02-17 03:33:10 +00005783 if (CGM.ReturnTypeUsesSRet(MSI.CallInfo)) {
John McCall234eac82011-05-13 23:16:18 +00005784 if (isSuper) {
5785 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5786 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005787 } else {
John McCall5880fb82011-05-14 21:12:11 +00005788 nullReturn.init(CGF, arg0);
John McCall234eac82011-05-13 23:16:18 +00005789 fn = ObjCTypes.getMessageSendStretFixupFn();
5790 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005791 }
John McCall234eac82011-05-13 23:16:18 +00005792 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5793 fn = ObjCTypes.getMessageSendFpretFixupFn();
5794 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005795 } else {
John McCall234eac82011-05-13 23:16:18 +00005796 if (isSuper) {
5797 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5798 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005799 } else {
John McCall234eac82011-05-13 23:16:18 +00005800 fn = ObjCTypes.getMessageSendFixupFn();
5801 messageRefName += "objc_msgSend_fixup";
Chris Lattner396639d2010-08-18 16:09:06 +00005802 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005803 }
John McCall234eac82011-05-13 23:16:18 +00005804 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5805 messageRefName += '_';
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005806
John McCall234eac82011-05-13 23:16:18 +00005807 // Append the selector name, except use underscores anywhere we
5808 // would have used colons.
5809 appendSelectorForMessageRefTable(messageRefName, selector);
5810
5811 llvm::GlobalVariable *messageRef
5812 = CGM.getModule().getGlobalVariable(messageRefName);
5813 if (!messageRef) {
John McCall9e8bb002011-05-14 03:10:52 +00005814 // Build the message ref structure.
John McCall234eac82011-05-13 23:16:18 +00005815 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnere64d7ba2011-06-20 04:01:35 +00005816 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCall234eac82011-05-13 23:16:18 +00005817 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5818 init->getType(),
5819 /*constant*/ false,
5820 llvm::GlobalValue::WeakAnyLinkage,
5821 init,
5822 messageRefName);
5823 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5824 messageRef->setAlignment(16);
5825 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5826 }
Fariborz Jahanianc93fa982012-01-30 23:39:30 +00005827
5828 bool requiresnullCheck = false;
5829 if (CGM.getLangOptions().ObjCAutoRefCount && method)
5830 for (ObjCMethodDecl::param_const_iterator i = method->param_begin(),
5831 e = method->param_end(); i != e; ++i) {
5832 const ParmVarDecl *ParamDecl = (*i);
5833 if (ParamDecl->hasAttr<NSConsumedAttr>()) {
5834 if (!nullReturn.NullBB)
5835 nullReturn.init(CGF, arg0);
5836 requiresnullCheck = true;
5837 break;
5838 }
5839 }
5840
John McCall234eac82011-05-13 23:16:18 +00005841 llvm::Value *mref =
5842 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5843
John McCall9e8bb002011-05-14 03:10:52 +00005844 // Update the message ref argument.
John McCall234eac82011-05-13 23:16:18 +00005845 args[1].RV = RValue::get(mref);
5846
5847 // Load the function to call from the message ref table.
5848 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5849 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5850
John McCalla729c622012-02-17 03:33:10 +00005851 callee = CGF.Builder.CreateBitCast(callee, MSI.MessengerType);
John McCall234eac82011-05-13 23:16:18 +00005852
John McCalla729c622012-02-17 03:33:10 +00005853 RValue result = CGF.EmitCall(MSI.CallInfo, callee, returnSlot, args);
Fariborz Jahanianc93fa982012-01-30 23:39:30 +00005854 return nullReturn.complete(CGF, result, resultType, formalArgs,
5855 requiresnullCheck ? method : 0);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005856}
5857
5858/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005859CodeGen::RValue
5860CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005861 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005862 QualType ResultType,
5863 Selector Sel,
5864 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005865 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00005866 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005867 const ObjCMethodDecl *Method) {
John McCall9e8bb002011-05-14 03:10:52 +00005868 return isVTableDispatchedSelector(Sel)
5869 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005870 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00005871 false, CallArgs, Method)
5872 : EmitMessageSend(CGF, Return, ResultType,
5873 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005874 Receiver, CGF.getContext().getObjCIdType(),
John McCall9e8bb002011-05-14 03:10:52 +00005875 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005876}
5877
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005878llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005879CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005880 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5881
Daniel Dunbara6468342009-03-02 05:18:14 +00005882 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005883 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005884 false, llvm::GlobalValue::ExternalLinkage,
5885 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005886 }
5887
5888 return GV;
5889}
5890
John McCall31168b02011-06-15 23:02:42 +00005891llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
5892 IdentifierInfo *II) {
5893 llvm::GlobalVariable *&Entry = ClassReferences[II];
5894
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005895 if (!Entry) {
John McCall31168b02011-06-15 23:02:42 +00005896 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005897 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005898 Entry =
John McCall31168b02011-06-15 23:02:42 +00005899 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5900 false, llvm::GlobalValue::InternalLinkage,
5901 ClassGV,
5902 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005903 Entry->setAlignment(
John McCall31168b02011-06-15 23:02:42 +00005904 CGM.getTargetData().getABITypeAlignment(
5905 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005906 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005907 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005908 }
John McCall31168b02011-06-15 23:02:42 +00005909
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005910 return Builder.CreateLoad(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005911}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005912
John McCall31168b02011-06-15 23:02:42 +00005913llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5914 const ObjCInterfaceDecl *ID) {
5915 return EmitClassRefFromId(Builder, ID->getIdentifier());
5916}
5917
5918llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
5919 CGBuilderTy &Builder) {
5920 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
5921 return EmitClassRefFromId(Builder, II);
5922}
5923
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005924llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005925CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005926 const ObjCInterfaceDecl *ID) {
5927 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005928
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005929 if (!Entry) {
5930 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5931 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005932 Entry =
5933 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005934 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005935 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005936 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005937 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005938 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005939 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005940 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005941 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005942 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005943
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005944 return Builder.CreateLoad(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005945}
5946
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005947/// EmitMetaClassRef - Return a Value * of the address of _class_t
5948/// meta-data
5949///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005950llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5951 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005952 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5953 if (Entry)
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005954 return Builder.CreateLoad(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005955
Daniel Dunbar15894b72009-04-07 05:48:37 +00005956 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005957 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005958 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005959 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005960 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005961 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005962 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005963 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005964 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005965 ObjCTypes.ClassnfABIPtrTy));
5966
Daniel Dunbare60aa052009-04-15 19:03:14 +00005967 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005968 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005969
Benjamin Kramer76399eb2011-09-27 21:06:10 +00005970 return Builder.CreateLoad(Entry);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005971}
5972
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005973/// GetClass - Return a reference to the class for the given interface
5974/// decl.
5975llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5976 const ObjCInterfaceDecl *ID) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00005977 if (ID->isWeakImported()) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00005978 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5979 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5980 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5981 }
5982
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005983 return EmitClassRef(Builder, ID);
5984}
5985
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005986/// Generates a message send where the super is the receiver. This is
5987/// a message send to self with special delivery semantics indicating
5988/// which class's method should be called.
5989CodeGen::RValue
5990CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005991 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005992 QualType ResultType,
5993 Selector Sel,
5994 const ObjCInterfaceDecl *Class,
5995 bool isCategoryImpl,
5996 llvm::Value *Receiver,
5997 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005998 const CodeGen::CallArgList &CallArgs,
5999 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006000 // ...
6001 // Create and init a super structure; this is a (receiver, class)
6002 // pair we will pass to objc_msgSendSuper.
6003 llvm::Value *ObjCSuper =
John McCall66475842011-03-04 08:00:29 +00006004 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006005
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006006 llvm::Value *ReceiverAsObject =
6007 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
6008 CGF.Builder.CreateStore(ReceiverAsObject,
6009 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006010
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006011 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00006012 llvm::Value *Target;
6013 if (IsClassMessage) {
6014 if (isCategoryImpl) {
6015 // Message sent to "super' in a class method defined in
6016 // a category implementation.
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006017 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00006018 Target = CGF.Builder.CreateStructGEP(Target, 0);
6019 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00006020 } else
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00006021 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stump658fe022009-07-30 22:28:39 +00006022 } else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00006023 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006024
Mike Stump18bb9282009-05-16 07:57:57 +00006025 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
6026 // ObjCTypes types.
Chris Lattner2192fe52011-07-18 04:24:23 +00006027 llvm::Type *ClassTy =
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006028 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
6029 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
6030 CGF.Builder.CreateStore(Target,
6031 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006032
John McCall9e8bb002011-05-14 03:10:52 +00006033 return (isVTableDispatchedSelector(Sel))
6034 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006035 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00006036 true, CallArgs, Method)
6037 : EmitMessageSend(CGF, Return, ResultType,
6038 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006039 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall9e8bb002011-05-14 03:10:52 +00006040 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00006041}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006042
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006043llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00006044 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006045 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006046
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006047 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006048 llvm::Constant *Casted =
6049 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
6050 ObjCTypes.SelectorPtrTy);
6051 Entry =
6052 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
6053 llvm::GlobalValue::InternalLinkage,
6054 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00006055 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00006056 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006057 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006058
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00006059 if (lval)
6060 return Entry;
Pete Cooper9d605512011-11-10 21:45:06 +00006061 llvm::LoadInst* LI = Builder.CreateLoad(Entry);
6062
6063 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
6064 llvm::MDNode::get(VMContext,
6065 ArrayRef<llvm::Value*>()));
6066 return LI;
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006067}
Fariborz Jahanian06292952009-02-16 22:52:32 +00006068/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00006069/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00006070///
6071void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006072 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00006073 llvm::Value *dst,
6074 llvm::Value *ivarOffset) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006075 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006076 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00006077 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006078 assert(Size <= 8 && "does not support size > 8");
6079 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6080 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006081 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6082 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006083 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6084 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00006085 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
6086 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00006087 return;
6088}
6089
6090/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
6091/// objc_assign_strongCast (id src, id *dst)
6092///
6093void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006094 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006095 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006096 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006097 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00006098 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006099 assert(Size <= 8 && "does not support size > 8");
6100 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006101 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006102 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6103 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006104 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6105 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00006106 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006107 src, dst, "weakassign");
6108 return;
6109}
6110
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006111void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006112 CodeGen::CodeGenFunction &CGF,
6113 llvm::Value *DestPtr,
6114 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00006115 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006116 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6117 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006118 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00006119 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00006120 return;
6121}
6122
Fariborz Jahanian06292952009-02-16 22:52:32 +00006123/// EmitObjCWeakRead - Code gen for loading value of a __weak
6124/// object: objc_read_weak (id *src)
6125///
6126llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006127 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006128 llvm::Value *AddrWeakObj) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006129 llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006130 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6131 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00006132 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006133 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00006134 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00006135 return read_weak;
6136}
6137
6138/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6139/// objc_assign_weak (id src, id *dst)
6140///
6141void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00006142 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006143 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006144 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00006145 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006146 assert(Size <= 8 && "does not support size > 8");
6147 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6148 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006149 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6150 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006151 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6152 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00006153 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00006154 src, dst, "weakassign");
6155 return;
6156}
6157
6158/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6159/// objc_assign_global (id src, id *dst)
6160///
6161void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00006162 llvm::Value *src, llvm::Value *dst,
6163 bool threadlocal) {
Chris Lattner2192fe52011-07-18 04:24:23 +00006164 llvm::Type * SrcTy = src->getType();
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006165 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00006166 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00006167 assert(Size <= 8 && "does not support size > 8");
6168 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6169 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00006170 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6171 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00006172 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6173 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00006174 if (!threadlocal)
6175 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6176 src, dst, "globalassign");
6177 else
6178 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6179 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00006180 return;
6181}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00006182
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006183void
John McCallbd309292010-07-06 01:34:17 +00006184CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6185 const ObjCAtSynchronizedStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006186 EmitAtSynchronizedStmt(CGF, S,
6187 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6188 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallbd309292010-07-06 01:34:17 +00006189}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006190
John McCall2ca705e2010-07-24 00:37:23 +00006191llvm::Constant *
Fariborz Jahanian831f0fc2011-06-23 19:00:08 +00006192CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall2ca705e2010-07-24 00:37:23 +00006193 // There's a particular fixed type info for 'id'.
6194 if (T->isObjCIdType() ||
6195 T->isObjCQualifiedIdType()) {
6196 llvm::Constant *IDEHType =
6197 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6198 if (!IDEHType)
6199 IDEHType =
6200 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6201 false,
6202 llvm::GlobalValue::ExternalLinkage,
6203 0, "OBJC_EHTYPE_id");
6204 return IDEHType;
6205 }
6206
6207 // All other types should be Objective-C interface pointer types.
6208 const ObjCObjectPointerType *PT =
6209 T->getAs<ObjCObjectPointerType>();
6210 assert(PT && "Invalid @catch type.");
6211 const ObjCInterfaceType *IT = PT->getInterfaceType();
6212 assert(IT && "Invalid @catch type.");
6213 return GetInterfaceEHType(IT->getDecl(), false);
6214}
6215
John McCallbd309292010-07-06 01:34:17 +00006216void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6217 const ObjCAtTryStmt &S) {
David Chisnall3e575602011-03-25 17:46:35 +00006218 EmitTryCatchStmt(CGF, S,
6219 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6220 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6221 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00006222}
6223
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006224/// EmitThrowStmt - Generate code for a throw statement.
6225void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6226 const ObjCAtThrowStmt &S) {
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006227 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall248512a2011-10-01 10:32:24 +00006228 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer76399eb2011-09-27 21:06:10 +00006229 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad5bd375a2011-07-15 08:37:34 +00006230 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall17afe452010-10-16 08:21:07 +00006231 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006232 } else {
Jay Foad5bd375a2011-07-15 08:37:34 +00006233 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall17afe452010-10-16 08:21:07 +00006234 .setDoesNotReturn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006235 }
6236
John McCall17afe452010-10-16 08:21:07 +00006237 CGF.Builder.CreateUnreachable();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00006238 CGF.Builder.ClearInsertionPoint();
6239}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006240
John McCall2ca705e2010-07-24 00:37:23 +00006241llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006242CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006243 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006244 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006245
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006246 // If we don't need a definition, return the entry if found or check
6247 // if we use an external reference.
6248 if (!ForDefinition) {
6249 if (Entry)
6250 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00006251
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006252 // If this type (or a super class) has the __objc_exception__
6253 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00006254 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006255 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00006256 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006257 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006258 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006259 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006260 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006261 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006262
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006263 // Otherwise we need to either make a new entry or fill in the
6264 // initializer.
6265 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00006266 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006267 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006268 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006269 CGM.getModule().getGlobalVariable(VTableName);
6270 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006271 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6272 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006273 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00006274 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006275
Chris Lattnerece04092012-02-07 00:39:47 +00006276 llvm::Value *VTableIdx = llvm::ConstantInt::get(CGM.Int32Ty, 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006277
Benjamin Kramer22d24c22011-10-15 12:20:02 +00006278 llvm::Constant *Values[] = {
6279 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6280 GetClassName(ID->getIdentifier()),
6281 GetClassGlobal(ClassName)
6282 };
Owen Anderson170229f2009-07-14 23:10:40 +00006283 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00006284 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006285
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006286 if (Entry) {
6287 Entry->setInitializer(Init);
6288 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00006289 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006290 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006291 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00006292 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00006293 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006294 }
6295
John McCall457a04e2010-10-22 21:05:15 +00006296 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar15894b72009-04-07 05:48:37 +00006297 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar710cb202010-04-25 20:39:32 +00006298 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6299 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00006300
6301 if (ForDefinition) {
6302 Entry->setSection("__DATA,__objc_const");
6303 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6304 } else {
6305 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6306 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006307
6308 return Entry;
6309}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006310
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006311/* *** */
6312
Daniel Dunbarb036db82008-08-13 03:21:16 +00006313CodeGen::CGObjCRuntime *
6314CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
David Chisnall067f0ed2011-03-22 21:21:24 +00006315 if (CGM.getLangOptions().ObjCNonFragileABI)
6316 return new CGObjCNonFragileABIMac(CGM);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006317 return new CGObjCMac(CGM);
6318}