blob: 63d45e9bd7b2b9a33c60aea48bc6eb3ecde20b49 [file] [log] [blame]
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001//===------- CGObjCGNU.cpp - Emit LLVM Code from ASTs for a Module --------===//
Chris Lattner0f984262008-03-01 08:50:34 +00002//
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//
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000010// This provides Objective-C code generation targetting the GNU runtime. The
11// class in this file generates structures used by the GNU Objective-C runtime
12// library. These structures are defined in objc/objc.h and objc/objc-api.h in
13// the GNU runtime distribution.
Chris Lattner0f984262008-03-01 08:50:34 +000014//
15//===----------------------------------------------------------------------===//
16
17#include "CGObjCRuntime.h"
Chris Lattnerdce14062008-06-26 04:19:03 +000018#include "CodeGenModule.h"
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000019#include "CodeGenFunction.h"
Chris Lattner5dc08672009-05-08 00:11:50 +000020
Chris Lattnerdce14062008-06-26 04:19:03 +000021#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000022#include "clang/AST/Decl.h"
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000025#include "clang/AST/StmtObjC.h"
Chris Lattner5dc08672009-05-08 00:11:50 +000026
27#include "llvm/Intrinsics.h"
Chris Lattner0f984262008-03-01 08:50:34 +000028#include "llvm/Module.h"
Chris Lattner0f984262008-03-01 08:50:34 +000029#include "llvm/ADT/SmallVector.h"
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000030#include "llvm/ADT/StringMap.h"
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000031#include "llvm/Support/Compiler.h"
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000032#include "llvm/Target/TargetData.h"
Chris Lattner5dc08672009-05-08 00:11:50 +000033
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000034#include <map>
Chris Lattnere160c9b2009-01-27 05:06:01 +000035
36
Chris Lattnerdce14062008-06-26 04:19:03 +000037using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000038using namespace CodeGen;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000039using llvm::dyn_cast;
40
41// The version of the runtime that this class targets. Must match the version
42// in the runtime.
43static const int RuntimeVersion = 8;
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +000044static const int NonFragileRuntimeVersion = 9;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000045static const int ProtocolVersion = 2;
Chris Lattner0f984262008-03-01 08:50:34 +000046
Chris Lattner0f984262008-03-01 08:50:34 +000047namespace {
Chris Lattnerdce14062008-06-26 04:19:03 +000048class CGObjCGNU : public CodeGen::CGObjCRuntime {
Chris Lattner0f984262008-03-01 08:50:34 +000049private:
Chris Lattnerdce14062008-06-26 04:19:03 +000050 CodeGen::CodeGenModule &CGM;
Chris Lattner0f984262008-03-01 08:50:34 +000051 llvm::Module &TheModule;
Chris Lattnere160c9b2009-01-27 05:06:01 +000052 const llvm::PointerType *SelectorTy;
53 const llvm::PointerType *PtrToInt8Ty;
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +000054 const llvm::FunctionType *IMPTy;
Chris Lattnere160c9b2009-01-27 05:06:01 +000055 const llvm::PointerType *IdTy;
David Chisnall0f436562009-08-17 16:35:33 +000056 QualType ASTIdTy;
Chris Lattnere160c9b2009-01-27 05:06:01 +000057 const llvm::IntegerType *IntTy;
58 const llvm::PointerType *PtrTy;
59 const llvm::IntegerType *LongTy;
60 const llvm::PointerType *PtrToIntTy;
Daniel Dunbar5efccb12009-05-04 15:31:17 +000061 llvm::GlobalAlias *ClassPtrAlias;
62 llvm::GlobalAlias *MetaClassPtrAlias;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000063 std::vector<llvm::Constant*> Classes;
64 std::vector<llvm::Constant*> Categories;
65 std::vector<llvm::Constant*> ConstantStrings;
66 llvm::Function *LoadFunction;
67 llvm::StringMap<llvm::Constant*> ExistingProtocols;
68 typedef std::pair<std::string, std::string> TypedSelector;
69 std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors;
70 llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors;
71 // Some zeros used for GEPs in lots of places.
72 llvm::Constant *Zeros[2];
73 llvm::Constant *NULLPtr;
Owen Andersona1cf15f2009-07-14 23:10:40 +000074 llvm::LLVMContext &VMContext;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000075private:
76 llvm::Constant *GenerateIvarList(
77 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
78 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
79 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets);
80 llvm::Constant *GenerateMethodList(const std::string &ClassName,
81 const std::string &CategoryName,
Chris Lattnera4210072008-06-26 05:08:00 +000082 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000083 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
84 bool isClassMethodList);
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +000085 llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000086 llvm::Constant *GenerateProtocolList(
87 const llvm::SmallVectorImpl<std::string> &Protocols);
88 llvm::Constant *GenerateClassStructure(
89 llvm::Constant *MetaClass,
90 llvm::Constant *SuperClass,
91 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +000092 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000093 llvm::Constant *Version,
94 llvm::Constant *InstanceSize,
95 llvm::Constant *IVars,
96 llvm::Constant *Methods,
97 llvm::Constant *Protocols);
98 llvm::Constant *GenerateProtocolMethodList(
99 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
100 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes);
101 llvm::Constant *MakeConstantString(const std::string &Str, const std::string
102 &Name="");
103 llvm::Constant *MakeGlobal(const llvm::StructType *Ty,
104 std::vector<llvm::Constant*> &V, const std::string &Name="");
105 llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty,
106 std::vector<llvm::Constant*> &V, const std::string &Name="");
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +0000107 llvm::GlobalVariable *ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
108 const ObjCIvarDecl *Ivar);
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000109 void EmitClassRef(const std::string &className);
Chris Lattner0f984262008-03-01 08:50:34 +0000110public:
Chris Lattnerdce14062008-06-26 04:19:03 +0000111 CGObjCGNU(CodeGen::CodeGenModule &cgm);
Steve Naroff33fdb732009-03-31 16:53:37 +0000112 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000113 virtual CodeGen::RValue
114 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000115 QualType ResultType,
116 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000117 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000118 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000119 const CallArgList &CallArgs,
120 const ObjCMethodDecl *Method);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000121 virtual CodeGen::RValue
122 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000123 QualType ResultType,
124 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000125 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000126 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000127 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000128 bool IsClassMessage,
129 const CallArgList &CallArgs);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000130 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000131 const ObjCInterfaceDecl *OID);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000132 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000133 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
134 *Method);
Chris Lattner8e67b632008-06-26 04:37:12 +0000135
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000136 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
137 const ObjCContainerDecl *CD);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000138 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
139 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000140 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000141 const ObjCProtocolDecl *PD);
142 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000143 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar49f66022008-09-24 03:38:44 +0000144 virtual llvm::Function *GetPropertyGetFunction();
145 virtual llvm::Function *GetPropertySetFunction();
Daniel Dunbar309a4362009-07-24 07:40:24 +0000146 virtual llvm::Constant *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000147
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000148 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
149 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000150 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
151 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000152 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000153 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000154 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
155 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000156 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
157 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000158 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
159 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000160 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
161 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000162 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
163 llvm::Value *DestPtr,
164 llvm::Value *SrcPtr,
Fariborz Jahanian08c32132009-08-31 19:33:16 +0000165 QualType Ty);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000166 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
167 QualType ObjectTy,
168 llvm::Value *BaseValue,
169 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000170 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000171 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +0000172 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +0000173 const ObjCIvarDecl *Ivar);
Chris Lattner0f984262008-03-01 08:50:34 +0000174};
175} // end anonymous namespace
176
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000177
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000178/// Emits a reference to a dummy variable which is emitted with each class.
179/// This ensures that a linker error will be generated when trying to link
180/// together modules where a referenced class is not defined.
Mike Stumpbb1c8602009-07-31 21:31:32 +0000181void CGObjCGNU::EmitClassRef(const std::string &className) {
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000182 std::string symbolRef = "__objc_class_ref_" + className;
183 // Don't emit two copies of the same symbol
Mike Stumpbb1c8602009-07-31 21:31:32 +0000184 if (TheModule.getGlobalVariable(symbolRef))
185 return;
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000186 std::string symbolName = "__objc_class_name_" + className;
187 llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(symbolName);
188 if (!ClassSymbol) {
Owen Anderson1c431b32009-07-08 19:05:04 +0000189 ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false,
190 llvm::GlobalValue::ExternalLinkage, 0, symbolName);
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000191 }
Owen Anderson1c431b32009-07-08 19:05:04 +0000192 new llvm::GlobalVariable(TheModule, ClassSymbol->getType(), true,
Chris Lattnerf35271b2009-08-05 05:25:18 +0000193 llvm::GlobalValue::WeakAnyLinkage, ClassSymbol, symbolRef);
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000194}
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000195
196static std::string SymbolNameForClass(const std::string &ClassName) {
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000197 return "_OBJC_CLASS_" + ClassName;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000198}
199
200static std::string SymbolNameForMethod(const std::string &ClassName, const
201 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
202{
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000203 return "_OBJC_METHOD_" + ClassName + "("+CategoryName+")"+
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000204 (isClassMethod ? "+" : "-") + MethodName;
205}
206
Chris Lattnerdce14062008-06-26 04:19:03 +0000207CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000208 : CGM(cgm), TheModule(CGM.getModule()), ClassPtrAlias(0),
Owen Andersona1cf15f2009-07-14 23:10:40 +0000209 MetaClassPtrAlias(0), VMContext(cgm.getLLVMContext()) {
Chris Lattnere160c9b2009-01-27 05:06:01 +0000210 IntTy = cast<llvm::IntegerType>(
211 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
212 LongTy = cast<llvm::IntegerType>(
213 CGM.getTypes().ConvertType(CGM.getContext().LongTy));
Chris Lattnerdce14062008-06-26 04:19:03 +0000214
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000215 Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000216 Zeros[1] = Zeros[0];
Owen Anderson03e20502009-07-30 23:11:26 +0000217 NULLPtr = llvm::ConstantPointerNull::get(
Owen Anderson0032b272009-08-13 21:57:51 +0000218 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext)));
Chris Lattner391d77a2008-03-30 23:03:07 +0000219 // C string type. Used in lots of places.
220 PtrToInt8Ty =
Owen Anderson0032b272009-08-13 21:57:51 +0000221 llvm::PointerType::getUnqual(llvm::Type::getInt8Ty(VMContext));
Chris Lattner391d77a2008-03-30 23:03:07 +0000222 // Get the selector Type.
Chris Lattnere160c9b2009-01-27 05:06:01 +0000223 SelectorTy = cast<llvm::PointerType>(
224 CGM.getTypes().ConvertType(CGM.getContext().getObjCSelType()));
225
Owen Anderson96e0fc72009-07-29 22:16:19 +0000226 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
Chris Lattner391d77a2008-03-30 23:03:07 +0000227 PtrTy = PtrToInt8Ty;
228
229 // Object type
David Chisnall0f436562009-08-17 16:35:33 +0000230 ASTIdTy = CGM.getContext().getObjCIdType();
231 IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
Chris Lattner391d77a2008-03-30 23:03:07 +0000232
233 // IMP type
234 std::vector<const llvm::Type*> IMPArgs;
235 IMPArgs.push_back(IdTy);
236 IMPArgs.push_back(SelectorTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000237 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000238}
Mike Stumpbb1c8602009-07-31 21:31:32 +0000239
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000240// This has to perform the lookup every time, since posing and related
241// techniques can modify the name -> class mapping.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000242llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000243 const ObjCInterfaceDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000244 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString());
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000245 EmitClassRef(OID->getNameAsString());
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000246 ClassName = Builder.CreateStructGEP(ClassName, 0);
247
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000248 std::vector<const llvm::Type*> Params(1, PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000249 llvm::Constant *ClassLookupFn =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000250 CGM.CreateRuntimeFunction(llvm::FunctionType::get(IdTy,
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000251 Params,
252 true),
253 "objc_lookup_class");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000254 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner391d77a2008-03-30 23:03:07 +0000255}
256
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000257llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Chris Lattner077bf5e2008-11-24 03:33:13 +0000258 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()];
Chris Lattner8e67b632008-06-26 04:37:12 +0000259 if (US == 0)
Owen Anderson96e0fc72009-07-29 22:16:19 +0000260 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
David Chisnall0f436562009-08-17 16:35:33 +0000261 llvm::GlobalValue::PrivateLinkage,
262 ".objc_untyped_selector_alias"+Sel.getAsString(),
Chris Lattner8e67b632008-06-26 04:37:12 +0000263 NULL, &TheModule);
264
265 return Builder.CreateLoad(US);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000266}
267
268llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
269 *Method) {
270
271 std::string SelName = Method->getSelector().getAsString();
272 std::string SelTypes;
273 CGM.getContext().getObjCEncodingForMethodDecl(Method, SelTypes);
274 // Typed selectors
275 TypedSelector Selector = TypedSelector(SelName,
276 SelTypes);
277
278 // If it's already cached, return it.
Mike Stumpbb1c8602009-07-31 21:31:32 +0000279 if (TypedSelectors[Selector]) {
280 return Builder.CreateLoad(TypedSelectors[Selector]);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000281 }
282
283 // If it isn't, cache it.
284 llvm::GlobalAlias *Sel = new llvm::GlobalAlias(
Owen Anderson96e0fc72009-07-29 22:16:19 +0000285 llvm::PointerType::getUnqual(SelectorTy),
David Chisnall0f436562009-08-17 16:35:33 +0000286 llvm::GlobalValue::PrivateLinkage, ".objc_selector_alias" + SelName,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000287 NULL, &TheModule);
288 TypedSelectors[Selector] = Sel;
289
290 return Builder.CreateLoad(Sel);
Chris Lattner8e67b632008-06-26 04:37:12 +0000291}
292
Chris Lattner5e7dcc62008-06-26 04:44:19 +0000293llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
294 const std::string &Name) {
David Chisnall8a5a9aa2009-08-31 16:41:57 +0000295 llvm::Constant *ConstStr = CGM.GetAddrOfConstantCString(Str, Name.c_str());
Owen Anderson3c4972d2009-07-29 18:54:39 +0000296 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000297}
Mike Stumpbb1c8602009-07-31 21:31:32 +0000298
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000299llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
300 std::vector<llvm::Constant*> &V, const std::string &Name) {
Owen Anderson08e25242009-07-27 22:29:56 +0000301 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
Owen Anderson1c431b32009-07-08 19:05:04 +0000302 return new llvm::GlobalVariable(TheModule, Ty, false,
303 llvm::GlobalValue::InternalLinkage, C, Name);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000304}
Mike Stumpbb1c8602009-07-31 21:31:32 +0000305
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000306llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
307 std::vector<llvm::Constant*> &V, const std::string &Name) {
Owen Anderson7db6d832009-07-28 18:33:04 +0000308 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
Owen Anderson1c431b32009-07-08 19:05:04 +0000309 return new llvm::GlobalVariable(TheModule, Ty, false,
Mike Stumpbb1c8602009-07-31 21:31:32 +0000310 llvm::GlobalValue::InternalLinkage, C, Name);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000311}
312
313/// Generate an NSConstantString object.
314//TODO: In case there are any crazy people still using the GNU runtime without
315//an OpenStep implementation, this should let them select their own class for
316//constant strings.
Steve Naroff33fdb732009-03-31 16:53:37 +0000317llvm::Constant *CGObjCGNU::GenerateConstantString(const ObjCStringLiteral *SL) {
318 std::string Str(SL->getString()->getStrData(),
319 SL->getString()->getByteLength());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000320 std::vector<llvm::Constant*> Ivars;
321 Ivars.push_back(NULLPtr);
Chris Lattner13fd7e52008-06-21 21:44:18 +0000322 Ivars.push_back(MakeConstantString(Str));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000323 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000324 llvm::Constant *ObjCStr = MakeGlobal(
Owen Anderson47a434f2009-08-05 23:18:46 +0000325 llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000326 Ivars, ".objc_str");
327 ConstantStrings.push_back(
Owen Anderson3c4972d2009-07-29 18:54:39 +0000328 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000329 return ObjCStr;
330}
331
332///Generates a message send where the super is the receiver. This is a message
333///send to self with special delivery semantics indicating which class's method
334///should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000335CodeGen::RValue
336CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000337 QualType ResultType,
338 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000339 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +0000340 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000341 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000342 bool IsClassMessage,
343 const CallArgList &CallArgs) {
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000344 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
345
346 CallArgList ActualArgs;
347
348 ActualArgs.push_back(
David Chisnall0f436562009-08-17 16:35:33 +0000349 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
350 ASTIdTy));
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000351 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
352 CGF.getContext().getObjCSelType()));
353 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
354
355 CodeGenTypes &Types = CGM.getTypes();
356 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
357 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
358
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000359 llvm::Value *ReceiverClass = 0;
Chris Lattner48e6e7e2009-05-08 15:39:58 +0000360 if (isCategoryImpl) {
361 llvm::Constant *classLookupFunction = 0;
362 std::vector<const llvm::Type*> Params;
363 Params.push_back(PtrTy);
364 if (IsClassMessage) {
Owen Anderson96e0fc72009-07-29 22:16:19 +0000365 classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Chris Lattner48e6e7e2009-05-08 15:39:58 +0000366 IdTy, Params, true), "objc_get_meta_class");
367 } else {
Owen Anderson96e0fc72009-07-29 22:16:19 +0000368 classLookupFunction = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Chris Lattner48e6e7e2009-05-08 15:39:58 +0000369 IdTy, Params, true), "objc_get_class");
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000370 }
Chris Lattner48e6e7e2009-05-08 15:39:58 +0000371 ReceiverClass = CGF.Builder.CreateCall(classLookupFunction,
372 MakeConstantString(Class->getNameAsString()));
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000373 } else {
Chris Lattner48e6e7e2009-05-08 15:39:58 +0000374 // Set up global aliases for the metaclass or class pointer if they do not
375 // already exist. These will are forward-references which will be set to
Mike Stumpbb1c8602009-07-31 21:31:32 +0000376 // pointers to the class and metaclass structure created for the runtime
377 // load function. To send a message to super, we look up the value of the
Chris Lattner48e6e7e2009-05-08 15:39:58 +0000378 // super_class pointer from either the class or metaclass structure.
379 if (IsClassMessage) {
380 if (!MetaClassPtrAlias) {
381 MetaClassPtrAlias = new llvm::GlobalAlias(IdTy,
382 llvm::GlobalValue::InternalLinkage, ".objc_metaclass_ref" +
383 Class->getNameAsString(), NULL, &TheModule);
384 }
385 ReceiverClass = MetaClassPtrAlias;
386 } else {
387 if (!ClassPtrAlias) {
388 ClassPtrAlias = new llvm::GlobalAlias(IdTy,
389 llvm::GlobalValue::InternalLinkage, ".objc_class_ref" +
390 Class->getNameAsString(), NULL, &TheModule);
391 }
392 ReceiverClass = ClassPtrAlias;
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000393 }
Chris Lattner71238f62009-04-25 23:19:45 +0000394 }
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000395 // Cast the pointer to a simplified version of the class structure
396 ReceiverClass = CGF.Builder.CreateBitCast(ReceiverClass,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000397 llvm::PointerType::getUnqual(
Owen Anderson47a434f2009-08-05 23:18:46 +0000398 llvm::StructType::get(VMContext, IdTy, IdTy, NULL)));
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000399 // Get the superclass pointer
400 ReceiverClass = CGF.Builder.CreateStructGEP(ReceiverClass, 1);
401 // Load the superclass pointer
402 ReceiverClass = CGF.Builder.CreateLoad(ReceiverClass);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000403 // Construct the structure used to look up the IMP
Owen Anderson47a434f2009-08-05 23:18:46 +0000404 llvm::StructType *ObjCSuperTy = llvm::StructType::get(VMContext,
405 Receiver->getType(), IdTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000406 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000407
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000408 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar5efccb12009-05-04 15:31:17 +0000409 CGF.Builder.CreateStore(ReceiverClass,
410 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000411
412 // Get the IMP
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000413 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000414 Params.push_back(llvm::PointerType::getUnqual(ObjCSuperTy));
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000415 Params.push_back(SelectorTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000416 llvm::Constant *lookupFunction =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000417 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
418 llvm::PointerType::getUnqual(impType), Params, true),
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000419 "objc_msg_lookup_super");
420
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000421 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000422 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000423 lookupArgs+2);
424
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000425 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000426}
427
428/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000429CodeGen::RValue
430CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000431 QualType ResultType,
432 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000433 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000434 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000435 const CallArgList &CallArgs,
436 const ObjCMethodDecl *Method) {
David Chisnall0f436562009-08-17 16:35:33 +0000437 IdTy = cast<llvm::PointerType>(CGM.getTypes().ConvertType(ASTIdTy));
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +0000438 llvm::Value *cmd;
439 if (Method)
440 cmd = GetSelector(CGF.Builder, Method);
441 else
442 cmd = GetSelector(CGF.Builder, Sel);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000443 CallArgList ActualArgs;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000444
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000445 ActualArgs.push_back(
David Chisnall0f436562009-08-17 16:35:33 +0000446 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)), ASTIdTy));
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000447 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
448 CGF.getContext().getObjCSelType()));
449 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
450
451 CodeGenTypes &Types = CGM.getTypes();
452 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
453 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
454
Fariborz Jahanian34e65772009-05-22 20:17:16 +0000455 llvm::Value *imp;
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000456 std::vector<const llvm::Type*> Params;
457 Params.push_back(Receiver->getType());
458 Params.push_back(SelectorTy);
Fariborz Jahanian34e65772009-05-22 20:17:16 +0000459 // For sender-aware dispatch, we pass the sender as the third argument to a
460 // lookup function. When sending messages from C code, the sender is nil.
461 // objc_msg_lookup_sender(id receiver, SEL selector, id sender);
462 if (CGM.getContext().getLangOptions().ObjCSenderDispatch) {
463 llvm::Value *self;
Fariborz Jahanian26c82942009-03-30 18:02:14 +0000464
Fariborz Jahanian34e65772009-05-22 20:17:16 +0000465 if (isa<ObjCMethodDecl>(CGF.CurFuncDecl)) {
466 self = CGF.LoadObjCSelf();
467 } else {
Owen Anderson03e20502009-07-30 23:11:26 +0000468 self = llvm::ConstantPointerNull::get(IdTy);
Fariborz Jahanian34e65772009-05-22 20:17:16 +0000469 }
470 Params.push_back(self->getType());
471 llvm::Constant *lookupFunction =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000472 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
473 llvm::PointerType::getUnqual(impType), Params, true),
Fariborz Jahanian34e65772009-05-22 20:17:16 +0000474 "objc_msg_lookup_sender");
475
476 imp = CGF.Builder.CreateCall3(lookupFunction, Receiver, cmd, self);
477 } else {
478 llvm::Constant *lookupFunction =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000479 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
480 llvm::PointerType::getUnqual(impType), Params, true),
Fariborz Jahanian34e65772009-05-22 20:17:16 +0000481 "objc_msg_lookup");
482
483 imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
484 }
Chris Lattner3eae03e2008-05-06 00:56:42 +0000485
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000486 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Chris Lattner0f984262008-03-01 08:50:34 +0000487}
488
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000489/// Generates a MethodList. Used in construction of a objc_class and
490/// objc_category structures.
491llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattnera4210072008-06-26 05:08:00 +0000492 const std::string &CategoryName,
493 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000494 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
495 bool isClassMethodList) {
David Chisnall0f436562009-08-17 16:35:33 +0000496 if (MethodSels.empty())
497 return NULLPtr;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000498 // Get the method structure type.
Owen Anderson47a434f2009-08-05 23:18:46 +0000499 llvm::StructType *ObjCMethodTy = llvm::StructType::get(VMContext,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000500 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
501 PtrToInt8Ty, // Method types
Owen Anderson96e0fc72009-07-29 22:16:19 +0000502 llvm::PointerType::getUnqual(IMPTy), //Method pointer
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000503 NULL);
504 std::vector<llvm::Constant*> Methods;
505 std::vector<llvm::Constant*> Elements;
506 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
507 Elements.clear();
Fariborz Jahanian1e64a952009-05-17 16:49:27 +0000508 if (llvm::Constant *Method =
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000509 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000510 MethodSels[i].getAsString(),
Fariborz Jahanian1e64a952009-05-17 16:49:27 +0000511 isClassMethodList))) {
David Chisnall8a5a9aa2009-08-31 16:41:57 +0000512 llvm::Constant *C = MakeConstantString(MethodSels[i].getAsString());
513 Elements.push_back(C);
514 Elements.push_back(MethodTypes[i]);
Owen Anderson3c4972d2009-07-29 18:54:39 +0000515 Method = llvm::ConstantExpr::getBitCast(Method,
Owen Anderson96e0fc72009-07-29 22:16:19 +0000516 llvm::PointerType::getUnqual(IMPTy));
Fariborz Jahanian1e64a952009-05-17 16:49:27 +0000517 Elements.push_back(Method);
Owen Anderson08e25242009-07-27 22:29:56 +0000518 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
Fariborz Jahanian1e64a952009-05-17 16:49:27 +0000519 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000520 }
521
522 // Array of method structures
Owen Anderson96e0fc72009-07-29 22:16:19 +0000523 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Fariborz Jahanian1e64a952009-05-17 16:49:27 +0000524 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +0000525 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerfba67632008-06-26 04:52:29 +0000526 Methods);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000527
528 // Structure containing list pointer, array and array count
529 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
Owen Anderson8c8f69e2009-08-13 23:27:53 +0000530 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get(VMContext);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000531 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
Owen Anderson47a434f2009-08-05 23:18:46 +0000532 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(VMContext,
533 NextPtrTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000534 IntTy,
535 ObjCMethodArrayTy,
536 NULL);
537 // Refine next pointer type to concrete type
538 llvm::cast<llvm::OpaqueType>(
539 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
540 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
541
542 Methods.clear();
Owen Anderson03e20502009-07-30 23:11:26 +0000543 Methods.push_back(llvm::ConstantPointerNull::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +0000544 llvm::PointerType::getUnqual(ObjCMethodListTy)));
Owen Anderson0032b272009-08-13 21:57:51 +0000545 Methods.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000546 MethodTypes.size()));
547 Methods.push_back(MethodArray);
548
549 // Create an instance of the structure
550 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
551}
552
553/// Generates an IvarList. Used in construction of a objc_class.
554llvm::Constant *CGObjCGNU::GenerateIvarList(
555 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
556 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
557 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
558 // Get the method structure type.
Owen Anderson47a434f2009-08-05 23:18:46 +0000559 llvm::StructType *ObjCIvarTy = llvm::StructType::get(VMContext,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000560 PtrToInt8Ty,
561 PtrToInt8Ty,
562 IntTy,
563 NULL);
564 std::vector<llvm::Constant*> Ivars;
565 std::vector<llvm::Constant*> Elements;
566 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
567 Elements.clear();
David Chisnall8a5a9aa2009-08-31 16:41:57 +0000568 Elements.push_back(IvarNames[i]);
569 Elements.push_back(IvarTypes[i]);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000570 Elements.push_back(IvarOffsets[i]);
Owen Anderson08e25242009-07-27 22:29:56 +0000571 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000572 }
573
574 // Array of method structures
Owen Anderson96e0fc72009-07-29 22:16:19 +0000575 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000576 IvarNames.size());
577
578
579 Elements.clear();
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000580 Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
Owen Anderson7db6d832009-07-28 18:33:04 +0000581 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000582 // Structure containing array and array count
Owen Anderson47a434f2009-08-05 23:18:46 +0000583 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(VMContext, IntTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000584 ObjCIvarArrayTy,
585 NULL);
586
587 // Create an instance of the structure
588 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
589}
590
591/// Generate a class structure
592llvm::Constant *CGObjCGNU::GenerateClassStructure(
593 llvm::Constant *MetaClass,
594 llvm::Constant *SuperClass,
595 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000596 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000597 llvm::Constant *Version,
598 llvm::Constant *InstanceSize,
599 llvm::Constant *IVars,
600 llvm::Constant *Methods,
601 llvm::Constant *Protocols) {
602 // Set up the class structure
603 // Note: Several of these are char*s when they should be ids. This is
604 // because the runtime performs this translation on load.
Owen Anderson47a434f2009-08-05 23:18:46 +0000605 llvm::StructType *ClassTy = llvm::StructType::get(VMContext,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000606 PtrToInt8Ty, // class_pointer
607 PtrToInt8Ty, // super_class
608 PtrToInt8Ty, // name
609 LongTy, // version
610 LongTy, // info
611 LongTy, // instance_size
612 IVars->getType(), // ivars
613 Methods->getType(), // methods
614 // These are all filled in by the runtime, so we pretend
615 PtrTy, // dtable
616 PtrTy, // subclass_list
617 PtrTy, // sibling_class
618 PtrTy, // protocols
619 PtrTy, // gc_object_type
620 NULL);
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000621 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000622 llvm::Constant *NullP =
Owen Anderson03e20502009-07-30 23:11:26 +0000623 llvm::ConstantPointerNull::get(PtrTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000624 // Fill in the structure
625 std::vector<llvm::Constant*> Elements;
Owen Anderson3c4972d2009-07-29 18:54:39 +0000626 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000627 Elements.push_back(SuperClass);
Chris Lattnerd002cc62008-06-26 04:47:04 +0000628 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000629 Elements.push_back(Zero);
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000630 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000631 Elements.push_back(InstanceSize);
632 Elements.push_back(IVars);
633 Elements.push_back(Methods);
634 Elements.push_back(NullP);
635 Elements.push_back(NullP);
636 Elements.push_back(NullP);
Owen Anderson3c4972d2009-07-29 18:54:39 +0000637 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000638 Elements.push_back(NullP);
639 // Create an instance of the structure
Chris Lattner1565e032008-07-21 06:31:05 +0000640 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000641}
642
643llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
644 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
645 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
646 // Get the method structure type.
Owen Anderson47a434f2009-08-05 23:18:46 +0000647 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(VMContext,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000648 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
649 PtrToInt8Ty,
650 NULL);
651 std::vector<llvm::Constant*> Methods;
652 std::vector<llvm::Constant*> Elements;
653 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
654 Elements.clear();
David Chisnall8a5a9aa2009-08-31 16:41:57 +0000655 Elements.push_back(MethodNames[i]);
656 Elements.push_back(MethodTypes[i]);
Owen Anderson08e25242009-07-27 22:29:56 +0000657 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000658 }
Owen Anderson96e0fc72009-07-29 22:16:19 +0000659 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000660 MethodNames.size());
Owen Anderson7db6d832009-07-28 18:33:04 +0000661 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy,
Mike Stumpbb1c8602009-07-31 21:31:32 +0000662 Methods);
Owen Anderson47a434f2009-08-05 23:18:46 +0000663 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(VMContext,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000664 IntTy, ObjCMethodArrayTy, NULL);
665 Methods.clear();
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000666 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000667 Methods.push_back(Array);
668 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
669}
Mike Stumpbb1c8602009-07-31 21:31:32 +0000670
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000671// Create the protocol list structure used in classes, categories and so on
672llvm::Constant *CGObjCGNU::GenerateProtocolList(
673 const llvm::SmallVectorImpl<std::string> &Protocols) {
Owen Anderson96e0fc72009-07-29 22:16:19 +0000674 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000675 Protocols.size());
Owen Anderson47a434f2009-08-05 23:18:46 +0000676 llvm::StructType *ProtocolListTy = llvm::StructType::get(VMContext,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000677 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
678 LongTy,//FIXME: Should be size_t
679 ProtocolArrayTy,
680 NULL);
681 std::vector<llvm::Constant*> Elements;
682 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
683 iter != endIter ; iter++) {
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000684 llvm::Constant *protocol = ExistingProtocols[*iter];
685 if (!protocol)
686 protocol = GenerateEmptyProtocol(*iter);
Owen Anderson3c4972d2009-07-29 18:54:39 +0000687 llvm::Constant *Ptr = llvm::ConstantExpr::getBitCast(protocol,
Owen Andersona1cf15f2009-07-14 23:10:40 +0000688 PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000689 Elements.push_back(Ptr);
690 }
Owen Anderson7db6d832009-07-28 18:33:04 +0000691 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000692 Elements);
693 Elements.clear();
694 Elements.push_back(NULLPtr);
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000695 Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000696 Elements.push_back(ProtocolArray);
697 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
698}
699
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000700llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000701 const ObjCProtocolDecl *PD) {
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000702 llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()];
703 const llvm::Type *T =
704 CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +0000705 return Builder.CreateBitCast(protocol, llvm::PointerType::getUnqual(T));
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000706}
707
708llvm::Constant *CGObjCGNU::GenerateEmptyProtocol(
709 const std::string &ProtocolName) {
710 llvm::SmallVector<std::string, 0> EmptyStringVector;
711 llvm::SmallVector<llvm::Constant*, 0> EmptyConstantVector;
712
713 llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector);
714 llvm::Constant *InstanceMethodList =
715 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
716 llvm::Constant *ClassMethodList =
717 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
718 // Protocols are objects containing lists of the methods implemented and
719 // protocols adopted.
Owen Anderson47a434f2009-08-05 23:18:46 +0000720 llvm::StructType *ProtocolTy = llvm::StructType::get(VMContext, IdTy,
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000721 PtrToInt8Ty,
722 ProtocolList->getType(),
723 InstanceMethodList->getType(),
724 ClassMethodList->getType(),
725 NULL);
726 std::vector<llvm::Constant*> Elements;
727 // The isa pointer must be set to a magic number so the runtime knows it's
728 // the correct layout.
Owen Anderson3c4972d2009-07-29 18:54:39 +0000729 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
Owen Anderson0032b272009-08-13 21:57:51 +0000730 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), ProtocolVersion), IdTy));
Fariborz Jahanianf8c4f542009-03-31 18:27:22 +0000731 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
732 Elements.push_back(ProtocolList);
733 Elements.push_back(InstanceMethodList);
734 Elements.push_back(ClassMethodList);
735 return MakeGlobal(ProtocolTy, Elements, ".objc_protocol");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000736}
737
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000738void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
739 ASTContext &Context = CGM.getContext();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000740 std::string ProtocolName = PD->getNameAsString();
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000741 llvm::SmallVector<std::string, 16> Protocols;
742 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
743 E = PD->protocol_end(); PI != E; ++PI)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000744 Protocols.push_back((*PI)->getNameAsString());
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000745 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
746 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000747 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
748 E = PD->instmeth_end(); iter != E; iter++) {
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000749 std::string TypeStr;
750 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
751 InstanceMethodNames.push_back(
David Chisnall8a5a9aa2009-08-31 16:41:57 +0000752 MakeConstantString((*iter)->getSelector().getAsString()));
753 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000754 }
755 // Collect information about class methods:
756 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
757 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000758 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000759 iter = PD->classmeth_begin(), endIter = PD->classmeth_end();
760 iter != endIter ; iter++) {
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000761 std::string TypeStr;
762 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
763 ClassMethodNames.push_back(
David Chisnall8a5a9aa2009-08-31 16:41:57 +0000764 MakeConstantString((*iter)->getSelector().getAsString()));
765 ClassMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000766 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000767
768 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
769 llvm::Constant *InstanceMethodList =
770 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
771 llvm::Constant *ClassMethodList =
772 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
773 // Protocols are objects containing lists of the methods implemented and
774 // protocols adopted.
Owen Anderson47a434f2009-08-05 23:18:46 +0000775 llvm::StructType *ProtocolTy = llvm::StructType::get(VMContext, IdTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000776 PtrToInt8Ty,
777 ProtocolList->getType(),
778 InstanceMethodList->getType(),
779 ClassMethodList->getType(),
780 NULL);
781 std::vector<llvm::Constant*> Elements;
782 // The isa pointer must be set to a magic number so the runtime knows it's
783 // the correct layout.
Owen Anderson3c4972d2009-07-29 18:54:39 +0000784 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
Owen Anderson0032b272009-08-13 21:57:51 +0000785 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), ProtocolVersion), IdTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000786 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
787 Elements.push_back(ProtocolList);
788 Elements.push_back(InstanceMethodList);
789 Elements.push_back(ClassMethodList);
790 ExistingProtocols[ProtocolName] =
Owen Anderson3c4972d2009-07-29 18:54:39 +0000791 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000792 ".objc_protocol"), IdTy);
793}
794
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000795void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Chris Lattner8ec03f52008-11-24 03:54:41 +0000796 std::string ClassName = OCD->getClassInterface()->getNameAsString();
797 std::string CategoryName = OCD->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000798 // Collect information about instance methods
799 llvm::SmallVector<Selector, 16> InstanceMethodSels;
800 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Douglas Gregor653f1b12009-04-23 01:02:12 +0000801 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000802 iter = OCD->instmeth_begin(), endIter = OCD->instmeth_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +0000803 iter != endIter ; iter++) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000804 InstanceMethodSels.push_back((*iter)->getSelector());
805 std::string TypeStr;
806 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +0000807 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000808 }
809
810 // Collect information about class methods
811 llvm::SmallVector<Selector, 16> ClassMethodSels;
812 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregor653f1b12009-04-23 01:02:12 +0000813 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000814 iter = OCD->classmeth_begin(), endIter = OCD->classmeth_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +0000815 iter != endIter ; iter++) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000816 ClassMethodSels.push_back((*iter)->getSelector());
817 std::string TypeStr;
818 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +0000819 ClassMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000820 }
821
822 // Collect the names of referenced protocols
823 llvm::SmallVector<std::string, 16> Protocols;
824 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
825 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
826 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
827 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000828 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000829
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000830 std::vector<llvm::Constant*> Elements;
831 Elements.push_back(MakeConstantString(CategoryName));
832 Elements.push_back(MakeConstantString(ClassName));
833 // Instance method list
Owen Anderson3c4972d2009-07-29 18:54:39 +0000834 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000835 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000836 false), PtrTy));
837 // Class method list
Owen Anderson3c4972d2009-07-29 18:54:39 +0000838 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000839 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000840 PtrTy));
841 // Protocol list
Owen Anderson3c4972d2009-07-29 18:54:39 +0000842 Elements.push_back(llvm::ConstantExpr::getBitCast(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000843 GenerateProtocolList(Protocols), PtrTy));
Owen Anderson3c4972d2009-07-29 18:54:39 +0000844 Categories.push_back(llvm::ConstantExpr::getBitCast(
Owen Anderson47a434f2009-08-05 23:18:46 +0000845 MakeGlobal(llvm::StructType::get(VMContext, PtrToInt8Ty, PtrToInt8Ty,
846 PtrTy, PtrTy, PtrTy, NULL), Elements), PtrTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000847}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000848
849void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
850 ASTContext &Context = CGM.getContext();
851
852 // Get the superclass name.
853 const ObjCInterfaceDecl * SuperClassDecl =
854 OID->getClassInterface()->getSuperClass();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000855 std::string SuperClassName;
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000856 if (SuperClassDecl) {
Chris Lattner8ec03f52008-11-24 03:54:41 +0000857 SuperClassName = SuperClassDecl->getNameAsString();
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000858 EmitClassRef(SuperClassName);
859 }
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000860
861 // Get the class name
Chris Lattner09dc6662009-04-01 02:00:48 +0000862 ObjCInterfaceDecl *ClassDecl =
863 const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
Chris Lattner8ec03f52008-11-24 03:54:41 +0000864 std::string ClassName = ClassDecl->getNameAsString();
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000865 // Emit the symbol that is used to generate linker errors if this class is
866 // referenced in other modules but not declared.
Fariborz Jahanianc51db232009-07-03 15:10:14 +0000867 std::string classSymbolName = "__objc_class_name_" + ClassName;
868 if (llvm::GlobalVariable *symbol =
869 TheModule.getGlobalVariable(classSymbolName)) {
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000870 symbol->setInitializer(llvm::ConstantInt::get(LongTy, 0));
Fariborz Jahanianc51db232009-07-03 15:10:14 +0000871 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +0000872 new llvm::GlobalVariable(TheModule, LongTy, false,
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000873 llvm::GlobalValue::ExternalLinkage, llvm::ConstantInt::get(LongTy, 0),
Owen Anderson1c431b32009-07-08 19:05:04 +0000874 classSymbolName);
Fariborz Jahanianc51db232009-07-03 15:10:14 +0000875 }
Chris Lattner2a8e4e12009-06-15 01:09:11 +0000876
Daniel Dunbar2bebbf02009-05-03 10:46:44 +0000877 // Get the size of instances.
878 int instanceSize = Context.getASTObjCImplementationLayout(OID).getSize() / 8;
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000879
880 // Collect information about instance variables.
881 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
882 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
883 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +0000884
885 int superInstanceSize = !SuperClassDecl ? 0 :
886 Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize() / 8;
887 // For non-fragile ivars, set the instance size to 0 - {the size of just this
888 // class}. The runtime will then set this to the correct value on load.
889 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
890 instanceSize = 0 - (instanceSize - superInstanceSize);
891 }
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000892 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
893 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
894 // Store the name
David Chisnall8a5a9aa2009-08-31 16:41:57 +0000895 IvarNames.push_back(MakeConstantString((*iter)->getNameAsString()));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000896 // Get the type encoding for this ivar
897 std::string TypeStr;
Daniel Dunbar0d504c12008-10-17 20:21:44 +0000898 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +0000899 IvarTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000900 // Get the offset
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +0000901 uint64_t Offset;
902 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
David Chisnall8a5a9aa2009-08-31 16:41:57 +0000903 Offset = ComputeIvarBaseOffset(CGM, ClassDecl, *iter) -
904 superInstanceSize;
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +0000905 } else {
906 Offset = ComputeIvarBaseOffset(CGM, ClassDecl, *iter);
907 }
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000908 IvarOffsets.push_back(
Owen Anderson0032b272009-08-13 21:57:51 +0000909 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), Offset));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000910 }
911
912 // Collect information about instance methods
913 llvm::SmallVector<Selector, 16> InstanceMethodSels;
914 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Douglas Gregor653f1b12009-04-23 01:02:12 +0000915 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000916 iter = OID->instmeth_begin(), endIter = OID->instmeth_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +0000917 iter != endIter ; iter++) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000918 InstanceMethodSels.push_back((*iter)->getSelector());
919 std::string TypeStr;
920 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +0000921 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000922 }
Fariborz Jahanian1e64a952009-05-17 16:49:27 +0000923 for (ObjCImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000924 iter = OID->propimpl_begin(), endIter = OID->propimpl_end();
Fariborz Jahanian1e64a952009-05-17 16:49:27 +0000925 iter != endIter ; iter++) {
926 ObjCPropertyDecl *property = (*iter)->getPropertyDecl();
927 if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
928 InstanceMethodSels.push_back(getter->getSelector());
929 std::string TypeStr;
930 Context.getObjCEncodingForMethodDecl(getter,TypeStr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +0000931 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
Fariborz Jahanian1e64a952009-05-17 16:49:27 +0000932 }
933 if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
934 InstanceMethodSels.push_back(setter->getSelector());
935 std::string TypeStr;
936 Context.getObjCEncodingForMethodDecl(setter,TypeStr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +0000937 InstanceMethodTypes.push_back(MakeConstantString(TypeStr));
Fariborz Jahanian1e64a952009-05-17 16:49:27 +0000938 }
939 }
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000940
941 // Collect information about class methods
942 llvm::SmallVector<Selector, 16> ClassMethodSels;
943 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregor653f1b12009-04-23 01:02:12 +0000944 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000945 iter = OID->classmeth_begin(), endIter = OID->classmeth_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +0000946 iter != endIter ; iter++) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000947 ClassMethodSels.push_back((*iter)->getSelector());
948 std::string TypeStr;
949 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +0000950 ClassMethodTypes.push_back(MakeConstantString(TypeStr));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000951 }
952 // Collect the names of referenced protocols
953 llvm::SmallVector<std::string, 16> Protocols;
954 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
955 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
956 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000957 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000958
959
960
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000961 // Get the superclass pointer.
962 llvm::Constant *SuperClass;
Chris Lattner8ec03f52008-11-24 03:54:41 +0000963 if (!SuperClassName.empty()) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000964 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
965 } else {
Owen Anderson03e20502009-07-30 23:11:26 +0000966 SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000967 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000968 // Empty vector used to construct empty method lists
969 llvm::SmallVector<llvm::Constant*, 1> empty;
970 // Generate the method and instance variable lists
971 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000972 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000973 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000974 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000975 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
976 IvarOffsets);
David Chisnall8a5a9aa2009-08-31 16:41:57 +0000977 // Irrespective of whether we are compiling for a fragile or non-fragile ABI,
978 // we emit a symbol containing the offset for each ivar in the class. This
979 // allows code compiled for the non-Fragile ABI to inherit from code compiled
980 // for the legacy ABI, without causing problems. The converse is also
981 // possible, but causes all ivar accesses to be fragile.
982 int i = 0;
983 // Offset pointer for getting at the correct field in the ivar list when
984 // setting up the alias. These are: The base address for the global, the
985 // ivar array (second field), the ivar in this list (set for each ivar), and
986 // the offset (third field in ivar structure)
987 const llvm::Type *IndexTy = llvm::Type::getInt32Ty(VMContext);
988 llvm::Constant *offsetPointerIndexes[] = {Zeros[0],
989 llvm::ConstantInt::get(IndexTy, 1), 0,
990 llvm::ConstantInt::get(IndexTy, 2) };
991
992 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
993 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
994 const std::string Name = "__objc_ivar_offset_" + ClassName + '.'
995 +(*iter)->getNameAsString();
996 offsetPointerIndexes[2] = llvm::ConstantInt::get(IndexTy, i++);
997 // Get the correct ivar field
998 llvm::Constant *offsetValue = llvm::ConstantExpr::getGetElementPtr(
999 IvarList, offsetPointerIndexes, 4);
1000 // Get the existing alias, if one exists.
1001 llvm::GlobalVariable *offset = TheModule.getNamedGlobal(Name);
1002 if (offset) {
1003 offset->setInitializer(offsetValue);
1004 // If this is the real definition, change its linkage type so that
1005 // different modules will use this one, rather than their private
1006 // copy.
1007 offset->setLinkage(llvm::GlobalValue::ExternalLinkage);
1008 } else {
1009 // Add a new alias if there isn't one already.
1010 offset = new llvm::GlobalVariable(TheModule, offsetValue->getType(),
1011 false, llvm::GlobalValue::ExternalLinkage, offsetValue, Name);
1012 }
1013 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001014 //Generate metaclass for class methods
1015 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner1565e032008-07-21 06:31:05 +00001016 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001017 empty, empty, empty), ClassMethodList, NULLPtr);
Daniel Dunbar5efccb12009-05-04 15:31:17 +00001018
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001019 // Generate the class structure
Chris Lattner8ec03f52008-11-24 03:54:41 +00001020 llvm::Constant *ClassStruct =
1021 GenerateClassStructure(MetaClassStruct, SuperClass, 0x1L,
1022 ClassName.c_str(), 0,
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001023 llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001024 MethodList, GenerateProtocolList(Protocols));
Daniel Dunbar5efccb12009-05-04 15:31:17 +00001025
1026 // Resolve the class aliases, if they exist.
1027 if (ClassPtrAlias) {
1028 ClassPtrAlias->setAliasee(
Owen Anderson3c4972d2009-07-29 18:54:39 +00001029 llvm::ConstantExpr::getBitCast(ClassStruct, IdTy));
Daniel Dunbar5efccb12009-05-04 15:31:17 +00001030 ClassPtrAlias = 0;
1031 }
1032 if (MetaClassPtrAlias) {
1033 MetaClassPtrAlias->setAliasee(
Owen Anderson3c4972d2009-07-29 18:54:39 +00001034 llvm::ConstantExpr::getBitCast(MetaClassStruct, IdTy));
Daniel Dunbar5efccb12009-05-04 15:31:17 +00001035 MetaClassPtrAlias = 0;
1036 }
1037
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001038 // Add class structure to list to be added to the symtab later
Owen Anderson3c4972d2009-07-29 18:54:39 +00001039 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001040 Classes.push_back(ClassStruct);
1041}
1042
Fariborz Jahanianc38e9af2009-06-23 21:47:46 +00001043
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001044llvm::Function *CGObjCGNU::ModuleInitFunction() {
1045 // Only emit an ObjC load function if no Objective-C stuff has been called
1046 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
1047 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov5f58b912008-06-01 15:14:46 +00001048 UntypedSelectors.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001049 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +00001050
Chris Lattnere160c9b2009-01-27 05:06:01 +00001051 const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
1052 SelectorTy->getElementType());
1053 const llvm::Type *SelStructPtrTy = SelectorTy;
1054 bool isSelOpaque = false;
1055 if (SelStructTy == 0) {
Owen Anderson47a434f2009-08-05 23:18:46 +00001056 SelStructTy = llvm::StructType::get(VMContext, PtrToInt8Ty,
1057 PtrToInt8Ty, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00001058 SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
Chris Lattnere160c9b2009-01-27 05:06:01 +00001059 isSelOpaque = true;
1060 }
1061
Eli Friedman1b8956e2008-06-01 16:00:02 +00001062 // Name the ObjC types to make the IR a bit easier to read
Chris Lattnere160c9b2009-01-27 05:06:01 +00001063 TheModule.addTypeName(".objc_selector", SelStructPtrTy);
Eli Friedman1b8956e2008-06-01 16:00:02 +00001064 TheModule.addTypeName(".objc_id", IdTy);
1065 TheModule.addTypeName(".objc_imp", IMPTy);
1066
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001067 std::vector<llvm::Constant*> Elements;
Chris Lattner71238f62009-04-25 23:19:45 +00001068 llvm::Constant *Statics = NULLPtr;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001069 // Generate statics list:
Chris Lattner71238f62009-04-25 23:19:45 +00001070 if (ConstantStrings.size()) {
Owen Anderson96e0fc72009-07-29 22:16:19 +00001071 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
Chris Lattner71238f62009-04-25 23:19:45 +00001072 ConstantStrings.size() + 1);
1073 ConstantStrings.push_back(NULLPtr);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001074
1075 const char *StringClass = CGM.getLangOptions().ObjCConstantStringClass;
1076 if (!StringClass) StringClass = "NXConstantString";
1077 Elements.push_back(MakeConstantString(StringClass,
1078 ".objc_static_class_name"));
Owen Anderson7db6d832009-07-28 18:33:04 +00001079 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy,
Chris Lattner71238f62009-04-25 23:19:45 +00001080 ConstantStrings));
1081 llvm::StructType *StaticsListTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00001082 llvm::StructType::get(VMContext, PtrToInt8Ty, StaticsArrayTy, NULL);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001083 llvm::Type *StaticsListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00001084 llvm::PointerType::getUnqual(StaticsListTy);
Chris Lattner71238f62009-04-25 23:19:45 +00001085 Statics = MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Owen Andersona1cf15f2009-07-14 23:10:40 +00001086 llvm::ArrayType *StaticsListArrayTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00001087 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner71238f62009-04-25 23:19:45 +00001088 Elements.clear();
1089 Elements.push_back(Statics);
Owen Andersonc9c88b42009-07-31 20:28:54 +00001090 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner71238f62009-04-25 23:19:45 +00001091 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Owen Anderson3c4972d2009-07-29 18:54:39 +00001092 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
Chris Lattner71238f62009-04-25 23:19:45 +00001093 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001094 // Array of classes, categories, and constant objects
Owen Anderson96e0fc72009-07-29 22:16:19 +00001095 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001096 Classes.size() + Categories.size() + 2);
Owen Anderson47a434f2009-08-05 23:18:46 +00001097 llvm::StructType *SymTabTy = llvm::StructType::get(VMContext,
1098 LongTy, SelStructPtrTy,
Owen Anderson0032b272009-08-13 21:57:51 +00001099 llvm::Type::getInt16Ty(VMContext),
1100 llvm::Type::getInt16Ty(VMContext),
Chris Lattner630404b2008-06-26 04:10:42 +00001101 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001102
1103 Elements.clear();
1104 // Pointer to an array of selectors used in this module.
1105 std::vector<llvm::Constant*> Selectors;
1106 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
1107 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
1108 iter != iterEnd ; ++iter) {
Chris Lattner630404b2008-06-26 04:10:42 +00001109 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
1110 Elements.push_back(MakeConstantString(iter->first.second,
1111 ".objc_sel_types"));
Owen Anderson08e25242009-07-27 22:29:56 +00001112 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001113 Elements.clear();
1114 }
1115 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
1116 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner630404b2008-06-26 04:10:42 +00001117 iter != iterEnd; ++iter) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001118 Elements.push_back(
Chris Lattner630404b2008-06-26 04:10:42 +00001119 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001120 Elements.push_back(NULLPtr);
Owen Anderson08e25242009-07-27 22:29:56 +00001121 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001122 Elements.clear();
1123 }
1124 Elements.push_back(NULLPtr);
1125 Elements.push_back(NULLPtr);
Owen Anderson08e25242009-07-27 22:29:56 +00001126 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001127 Elements.clear();
1128 // Number of static selectors
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001129 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001130 llvm::Constant *SelectorList = MakeGlobal(
Owen Anderson96e0fc72009-07-29 22:16:19 +00001131 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001132 ".objc_selector_list");
Owen Anderson3c4972d2009-07-29 18:54:39 +00001133 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
Chris Lattnere160c9b2009-01-27 05:06:01 +00001134 SelStructPtrTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001135
1136 // Now that all of the static selectors exist, create pointers to them.
1137 int index = 0;
1138 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
1139 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
1140 iter != iterEnd; ++iter) {
1141 llvm::Constant *Idxs[] = {Zeros[0],
Owen Anderson0032b272009-08-13 21:57:51 +00001142 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), index++), Zeros[0]};
Owen Anderson1c431b32009-07-08 19:05:04 +00001143 llvm::Constant *SelPtr = new llvm::GlobalVariable(TheModule, SelStructPtrTy,
1144 true, llvm::GlobalValue::InternalLinkage,
Owen Anderson3c4972d2009-07-29 18:54:39 +00001145 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
Owen Anderson1c431b32009-07-08 19:05:04 +00001146 ".objc_sel_ptr");
Chris Lattnere160c9b2009-01-27 05:06:01 +00001147 // If selectors are defined as an opaque type, cast the pointer to this
1148 // type.
1149 if (isSelOpaque) {
Owen Anderson3c4972d2009-07-29 18:54:39 +00001150 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001151 llvm::PointerType::getUnqual(SelectorTy));
Chris Lattnere160c9b2009-01-27 05:06:01 +00001152 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001153 (*iter).second->setAliasee(SelPtr);
1154 }
1155 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
1156 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
1157 iter != iterEnd; iter++) {
1158 llvm::Constant *Idxs[] = {Zeros[0],
Owen Anderson0032b272009-08-13 21:57:51 +00001159 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), index++), Zeros[0]};
Mike Stumpbb1c8602009-07-31 21:31:32 +00001160 llvm::Constant *SelPtr = new llvm::GlobalVariable
1161 (TheModule, SelStructPtrTy,
1162 true, llvm::GlobalValue::InternalLinkage,
1163 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
1164 ".objc_sel_ptr");
Chris Lattnere160c9b2009-01-27 05:06:01 +00001165 // If selectors are defined as an opaque type, cast the pointer to this
1166 // type.
1167 if (isSelOpaque) {
Owen Anderson3c4972d2009-07-29 18:54:39 +00001168 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001169 llvm::PointerType::getUnqual(SelectorTy));
Chris Lattnere160c9b2009-01-27 05:06:01 +00001170 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001171 (*iter).second->setAliasee(SelPtr);
1172 }
1173 // Number of classes defined.
Owen Anderson0032b272009-08-13 21:57:51 +00001174 Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001175 Classes.size()));
1176 // Number of categories defined
Owen Anderson0032b272009-08-13 21:57:51 +00001177 Elements.push_back(llvm::ConstantInt::get(llvm::Type::getInt16Ty(VMContext),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001178 Categories.size()));
1179 // Create an array of classes, then categories, then static object instances
1180 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
1181 // NULL-terminated list of static object instances (mainly constant strings)
1182 Classes.push_back(Statics);
1183 Classes.push_back(NULLPtr);
Owen Anderson7db6d832009-07-28 18:33:04 +00001184 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001185 Elements.push_back(ClassList);
1186 // Construct the symbol table
1187 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
1188
1189 // The symbol table is contained in a module which has some version-checking
1190 // constants
Owen Anderson47a434f2009-08-05 23:18:46 +00001191 llvm::StructType * ModuleTy = llvm::StructType::get(VMContext, LongTy, LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001192 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001193 Elements.clear();
1194 // Runtime version used for compatibility checking.
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001195 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001196 Elements.push_back(llvm::ConstantInt::get(LongTy,
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001197 NonFragileRuntimeVersion));
1198 } else {
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001199 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001200 }
Fariborz Jahanian91a0b512009-04-01 19:49:42 +00001201 // sizeof(ModuleTy)
1202 llvm::TargetData td = llvm::TargetData::TargetData(&TheModule);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001203 Elements.push_back(llvm::ConstantInt::get(LongTy,
Owen Andersona1cf15f2009-07-14 23:10:40 +00001204 td.getTypeSizeInBits(ModuleTy)/8));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001205 //FIXME: Should be the path to the file where this module was declared
1206 Elements.push_back(NULLPtr);
1207 Elements.push_back(SymTab);
1208 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
1209
1210 // Create the load function calling the runtime entry point with the module
1211 // structure
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001212 llvm::Function * LoadFunction = llvm::Function::Create(
Owen Anderson0032b272009-08-13 21:57:51 +00001213 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), false),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001214 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
1215 &TheModule);
Owen Anderson0032b272009-08-13 21:57:51 +00001216 llvm::BasicBlock *EntryBB =
1217 llvm::BasicBlock::Create(VMContext, "entry", LoadFunction);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001218 CGBuilderTy Builder(VMContext);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001219 Builder.SetInsertPoint(EntryBB);
Fariborz Jahanian26c82942009-03-30 18:02:14 +00001220
1221 std::vector<const llvm::Type*> Params(1,
Owen Anderson96e0fc72009-07-29 22:16:19 +00001222 llvm::PointerType::getUnqual(ModuleTy));
1223 llvm::Value *Register = CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Owen Anderson0032b272009-08-13 21:57:51 +00001224 llvm::Type::getVoidTy(VMContext), Params, true), "__objc_exec_class");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001225 Builder.CreateCall(Register, Module);
1226 Builder.CreateRetVoid();
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001227
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001228 return LoadFunction;
1229}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001230
Fariborz Jahanian679a5022009-01-10 21:06:09 +00001231llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
1232 const ObjCContainerDecl *CD) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001233 const ObjCCategoryImplDecl *OCD =
Steve Naroff3e0a5402009-01-08 19:41:02 +00001234 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
Chris Lattner077bf5e2008-11-24 03:33:13 +00001235 std::string CategoryName = OCD ? OCD->getNameAsString() : "";
1236 std::string ClassName = OMD->getClassInterface()->getNameAsString();
1237 std::string MethodName = OMD->getSelector().getAsString();
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001238 bool isClassMethod = !OMD->isInstanceMethod();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001239
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001240 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00001241 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001242 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001243 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
1244 MethodName, isClassMethod);
1245
Gabor Greif984d0b42008-04-06 20:42:52 +00001246 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattner391d77a2008-03-30 23:03:07 +00001247 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001248 FunctionName,
Chris Lattner391d77a2008-03-30 23:03:07 +00001249 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +00001250 return Method;
1251}
1252
Daniel Dunbar49f66022008-09-24 03:38:44 +00001253llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
Fariborz Jahaniancb9dad02009-05-19 00:28:43 +00001254 std::vector<const llvm::Type*> Params;
1255 const llvm::Type *BoolTy =
1256 CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
1257 Params.push_back(IdTy);
1258 Params.push_back(SelectorTy);
1259 // FIXME: Using LongTy for ptrdiff_t is probably broken on Win64
1260 Params.push_back(LongTy);
1261 Params.push_back(BoolTy);
1262 // void objc_getProperty (id, SEL, ptrdiff_t, bool)
1263 const llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00001264 llvm::FunctionType::get(IdTy, Params, false);
Fariborz Jahaniancb9dad02009-05-19 00:28:43 +00001265 return cast<llvm::Function>(CGM.CreateRuntimeFunction(FTy,
1266 "objc_getProperty"));
Daniel Dunbar49f66022008-09-24 03:38:44 +00001267}
1268
1269llvm::Function *CGObjCGNU::GetPropertySetFunction() {
Fariborz Jahaniancb9dad02009-05-19 00:28:43 +00001270 std::vector<const llvm::Type*> Params;
1271 const llvm::Type *BoolTy =
1272 CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
1273 Params.push_back(IdTy);
1274 Params.push_back(SelectorTy);
1275 // FIXME: Using LongTy for ptrdiff_t is probably broken on Win64
1276 Params.push_back(LongTy);
1277 Params.push_back(IdTy);
1278 Params.push_back(BoolTy);
1279 Params.push_back(BoolTy);
1280 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
1281 const llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +00001282 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Params, false);
Fariborz Jahaniancb9dad02009-05-19 00:28:43 +00001283 return cast<llvm::Function>(CGM.CreateRuntimeFunction(FTy,
1284 "objc_setProperty"));
Daniel Dunbar49f66022008-09-24 03:38:44 +00001285}
1286
Daniel Dunbar309a4362009-07-24 07:40:24 +00001287llvm::Constant *CGObjCGNU::EnumerationMutationFunction() {
1288 CodeGen::CodeGenTypes &Types = CGM.getTypes();
1289 ASTContext &Ctx = CGM.getContext();
1290 // void objc_enumerationMutation (id)
1291 llvm::SmallVector<QualType,16> Params;
David Chisnall0f436562009-08-17 16:35:33 +00001292 Params.push_back(ASTIdTy);
Daniel Dunbar309a4362009-07-24 07:40:24 +00001293 const llvm::FunctionType *FTy =
1294 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
1295 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
Anders Carlsson2abd89c2008-08-31 04:05:03 +00001296}
1297
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001298void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1299 const Stmt &S) {
Chris Lattner5dc08672009-05-08 00:11:50 +00001300 // Pointer to the personality function
1301 llvm::Constant *Personality =
Owen Anderson0032b272009-08-13 21:57:51 +00001302 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattnerb59761b2009-07-01 04:13:52 +00001303 true),
Chris Lattner5dc08672009-05-08 00:11:50 +00001304 "__gnu_objc_personality_v0");
Owen Anderson3c4972d2009-07-29 18:54:39 +00001305 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrTy);
Chris Lattner5dc08672009-05-08 00:11:50 +00001306 std::vector<const llvm::Type*> Params;
1307 Params.push_back(PtrTy);
1308 llvm::Value *RethrowFn =
Owen Anderson0032b272009-08-13 21:57:51 +00001309 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Chris Lattner5dc08672009-05-08 00:11:50 +00001310 Params, false), "_Unwind_Resume_or_Rethrow");
1311
1312 bool isTry = isa<ObjCAtTryStmt>(S);
1313 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1314 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
1315 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Chris Lattnerd6a99072009-05-11 18:16:28 +00001316 llvm::BasicBlock *CatchInCatch = CGF.createBasicBlock("catch.rethrow");
Chris Lattner5dc08672009-05-08 00:11:50 +00001317 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1318 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1319 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
1320
1321 // GNU runtime does not currently support @synchronized()
1322 if (!isTry) {
Chris Lattnerd6a99072009-05-11 18:16:28 +00001323 std::vector<const llvm::Type*> Args(1, IdTy);
1324 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +00001325 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerd6a99072009-05-11 18:16:28 +00001326 llvm::Value *SyncEnter = CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
1327 llvm::Value *SyncArg =
1328 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1329 SyncArg = CGF.Builder.CreateBitCast(SyncArg, IdTy);
1330 CGF.Builder.CreateCall(SyncEnter, SyncArg);
Chris Lattner5dc08672009-05-08 00:11:50 +00001331 }
1332
Chris Lattnerd6a99072009-05-11 18:16:28 +00001333
Chris Lattner5dc08672009-05-08 00:11:50 +00001334 // Push an EH context entry, used for handling rethrows and jumps
1335 // through finally.
1336 CGF.PushCleanupBlock(FinallyBlock);
1337
1338 // Emit the statements in the @try {} block
1339 CGF.setInvokeDest(TryHandler);
1340
1341 CGF.EmitBlock(TryBlock);
1342 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1343 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
1344
1345 // Jump to @finally if there is no exception
1346 CGF.EmitBranchThroughCleanup(FinallyEnd);
1347
1348 // Emit the handlers
1349 CGF.EmitBlock(TryHandler);
1350
Chris Lattnerbb422ad2009-05-08 17:36:08 +00001351 // Get the correct versions of the exception handling intrinsics
1352 llvm::TargetData td = llvm::TargetData::TargetData(&TheModule);
1353 int PointerWidth = td.getTypeSizeInBits(PtrTy);
1354 assert((PointerWidth == 32 || PointerWidth == 64) &&
1355 "Can't yet handle exceptions if pointers are not 32 or 64 bits");
Chris Lattner5dc08672009-05-08 00:11:50 +00001356 llvm::Value *llvm_eh_exception =
1357 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
Chris Lattnerbb422ad2009-05-08 17:36:08 +00001358 llvm::Value *llvm_eh_selector = PointerWidth == 32 ?
1359 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i32) :
1360 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
1361 llvm::Value *llvm_eh_typeid_for = PointerWidth == 32 ?
1362 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i32) :
1363 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
Chris Lattner5dc08672009-05-08 00:11:50 +00001364
1365 // Exception object
1366 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
1367 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
1368
1369 llvm::SmallVector<llvm::Value*, 8> ESelArgs;
1370 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
1371
1372 ESelArgs.push_back(Exc);
1373 ESelArgs.push_back(Personality);
1374
1375 bool HasCatchAll = false;
1376 // Only @try blocks are allowed @catch blocks, but both can have @finally
1377 if (isTry) {
1378 if (const ObjCAtCatchStmt* CatchStmt =
1379 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
Chris Lattnerd6a99072009-05-11 18:16:28 +00001380 CGF.setInvokeDest(CatchInCatch);
Chris Lattner5dc08672009-05-08 00:11:50 +00001381
1382 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
1383 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Mike Stumpbb1c8602009-07-31 21:31:32 +00001384 Handlers.push_back(std::make_pair(CatchDecl,
1385 CatchStmt->getCatchBody()));
Chris Lattner5dc08672009-05-08 00:11:50 +00001386
1387 // @catch() and @catch(id) both catch any ObjC exception
Steve Naroff14108da2009-07-10 23:34:53 +00001388 if (!CatchDecl || CatchDecl->getType()->isObjCIdType()
Chris Lattner5dc08672009-05-08 00:11:50 +00001389 || CatchDecl->getType()->isObjCQualifiedIdType()) {
1390 // Use i8* null here to signal this is a catch all, not a cleanup.
1391 ESelArgs.push_back(NULLPtr);
1392 HasCatchAll = true;
1393 // No further catches after this one will ever by reached
1394 break;
1395 }
1396
1397 // All other types should be Objective-C interface pointer types.
Steve Naroff14108da2009-07-10 23:34:53 +00001398 const ObjCObjectPointerType *OPT =
1399 CatchDecl->getType()->getAsObjCObjectPointerType();
1400 assert(OPT && "Invalid @catch type.");
Chris Lattner5dc08672009-05-08 00:11:50 +00001401 const ObjCInterfaceType *IT =
Steve Naroff14108da2009-07-10 23:34:53 +00001402 OPT->getPointeeType()->getAsObjCInterfaceType();
Chris Lattner5dc08672009-05-08 00:11:50 +00001403 assert(IT && "Invalid @catch type.");
Chris Lattnerbb422ad2009-05-08 17:36:08 +00001404 llvm::Value *EHType =
1405 MakeConstantString(IT->getDecl()->getNameAsString());
Chris Lattner5dc08672009-05-08 00:11:50 +00001406 ESelArgs.push_back(EHType);
1407 }
1408 }
1409 }
1410
1411 // We use a cleanup unless there was already a catch all.
1412 if (!HasCatchAll) {
Owen Anderson0032b272009-08-13 21:57:51 +00001413 ESelArgs.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0));
Chris Lattner5dc08672009-05-08 00:11:50 +00001414 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
1415 }
1416
1417 // Find which handler was matched.
Chris Lattnerbb422ad2009-05-08 17:36:08 +00001418 llvm::Value *ESelector = CGF.Builder.CreateCall(llvm_eh_selector,
Chris Lattner5dc08672009-05-08 00:11:50 +00001419 ESelArgs.begin(), ESelArgs.end(), "selector");
1420
1421 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
1422 const ParmVarDecl *CatchParam = Handlers[i].first;
1423 const Stmt *CatchBody = Handlers[i].second;
1424
1425 llvm::BasicBlock *Next = 0;
1426
1427 // The last handler always matches.
1428 if (i + 1 != e) {
1429 assert(CatchParam && "Only last handler can be a catch all.");
1430
1431 // Test whether this block matches the type for the selector and branch
1432 // to Match if it does, or to the next BB if it doesn't.
1433 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
1434 Next = CGF.createBasicBlock("catch.next");
Chris Lattnerbb422ad2009-05-08 17:36:08 +00001435 llvm::Value *Id = CGF.Builder.CreateCall(llvm_eh_typeid_for,
Chris Lattner5dc08672009-05-08 00:11:50 +00001436 CGF.Builder.CreateBitCast(ESelArgs[i+2], PtrTy));
1437 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(ESelector, Id), Match,
1438 Next);
1439
1440 CGF.EmitBlock(Match);
1441 }
1442
1443 if (CatchBody) {
Chris Lattner5dc08672009-05-08 00:11:50 +00001444 llvm::Value *ExcObject = CGF.Builder.CreateBitCast(Exc,
1445 CGF.ConvertType(CatchParam->getType()));
1446
1447 // Bind the catch parameter if it exists.
1448 if (CatchParam) {
1449 // CatchParam is a ParmVarDecl because of the grammar
1450 // construction used to handle this, but for codegen purposes
1451 // we treat this as a local decl.
1452 CGF.EmitLocalBlockVarDecl(*CatchParam);
1453 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
1454 }
1455
1456 CGF.ObjCEHValueStack.push_back(ExcObject);
1457 CGF.EmitStmt(CatchBody);
1458 CGF.ObjCEHValueStack.pop_back();
1459
1460 CGF.EmitBranchThroughCleanup(FinallyEnd);
1461
1462 if (Next)
1463 CGF.EmitBlock(Next);
1464 } else {
1465 assert(!Next && "catchup should be last handler.");
1466
1467 CGF.Builder.CreateStore(Exc, RethrowPtr);
1468 CGF.EmitBranchThroughCleanup(FinallyRethrow);
1469 }
1470 }
Chris Lattnerd6a99072009-05-11 18:16:28 +00001471 // The @finally block is a secondary landing pad for any exceptions thrown in
1472 // @catch() blocks
1473 CGF.EmitBlock(CatchInCatch);
1474 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
1475 ESelArgs.clear();
1476 ESelArgs.push_back(Exc);
1477 ESelArgs.push_back(Personality);
Owen Anderson0032b272009-08-13 21:57:51 +00001478 ESelArgs.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 0));
Chris Lattnerd6a99072009-05-11 18:16:28 +00001479 CGF.Builder.CreateCall(llvm_eh_selector, ESelArgs.begin(), ESelArgs.end(),
1480 "selector");
1481 CGF.Builder.CreateCall(llvm_eh_typeid_for,
1482 CGF.Builder.CreateIntToPtr(ESelArgs[2], PtrTy));
1483 CGF.Builder.CreateStore(Exc, RethrowPtr);
1484 CGF.EmitBranchThroughCleanup(FinallyRethrow);
1485
Chris Lattner5dc08672009-05-08 00:11:50 +00001486 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
1487
1488 CGF.setInvokeDest(PrevLandingPad);
1489
1490 CGF.EmitBlock(FinallyBlock);
1491
Chris Lattnerd6a99072009-05-11 18:16:28 +00001492
Chris Lattner5dc08672009-05-08 00:11:50 +00001493 if (isTry) {
1494 if (const ObjCAtFinallyStmt* FinallyStmt =
1495 cast<ObjCAtTryStmt>(S).getFinallyStmt())
1496 CGF.EmitStmt(FinallyStmt->getFinallyBody());
1497 } else {
Chris Lattnerd6a99072009-05-11 18:16:28 +00001498 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
Chris Lattner5dc08672009-05-08 00:11:50 +00001499 // @synchronized.
Chris Lattnerd6a99072009-05-11 18:16:28 +00001500 std::vector<const llvm::Type*> Args(1, IdTy);
1501 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +00001502 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerd6a99072009-05-11 18:16:28 +00001503 llvm::Value *SyncExit = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
1504 llvm::Value *SyncArg =
1505 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1506 SyncArg = CGF.Builder.CreateBitCast(SyncArg, IdTy);
1507 CGF.Builder.CreateCall(SyncExit, SyncArg);
Chris Lattner5dc08672009-05-08 00:11:50 +00001508 }
1509
1510 if (Info.SwitchBlock)
1511 CGF.EmitBlock(Info.SwitchBlock);
1512 if (Info.EndBlock)
1513 CGF.EmitBlock(Info.EndBlock);
1514
1515 // Branch around the rethrow code.
1516 CGF.EmitBranch(FinallyEnd);
1517
1518 CGF.EmitBlock(FinallyRethrow);
1519 CGF.Builder.CreateCall(RethrowFn, CGF.Builder.CreateLoad(RethrowPtr));
1520 CGF.Builder.CreateUnreachable();
1521
1522 CGF.EmitBlock(FinallyEnd);
1523
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001524}
1525
1526void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +00001527 const ObjCAtThrowStmt &S) {
Chris Lattner5dc08672009-05-08 00:11:50 +00001528 llvm::Value *ExceptionAsObject;
1529
1530 std::vector<const llvm::Type*> Args(1, IdTy);
1531 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +00001532 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner5dc08672009-05-08 00:11:50 +00001533 llvm::Value *ThrowFn =
1534 CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
1535
1536 if (const Expr *ThrowExpr = S.getThrowExpr()) {
1537 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Fariborz Jahanian1e64a952009-05-17 16:49:27 +00001538 ExceptionAsObject = Exception;
Chris Lattner5dc08672009-05-08 00:11:50 +00001539 } else {
1540 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
1541 "Unexpected rethrow outside @catch block.");
1542 ExceptionAsObject = CGF.ObjCEHValueStack.back();
1543 }
Fariborz Jahanian1e64a952009-05-17 16:49:27 +00001544 ExceptionAsObject =
1545 CGF.Builder.CreateBitCast(ExceptionAsObject, IdTy, "tmp");
Chris Lattner5dc08672009-05-08 00:11:50 +00001546
1547 // Note: This may have to be an invoke, if we want to support constructs like:
1548 // @try {
1549 // @throw(obj);
1550 // }
1551 // @catch(id) ...
1552 //
1553 // This is effectively turning @throw into an incredibly-expensive goto, but
1554 // it may happen as a result of inlining followed by missed optimizations, or
1555 // as a result of stupidity.
1556 llvm::BasicBlock *UnwindBB = CGF.getInvokeDest();
1557 if (!UnwindBB) {
1558 CGF.Builder.CreateCall(ThrowFn, ExceptionAsObject);
1559 CGF.Builder.CreateUnreachable();
1560 } else {
1561 CGF.Builder.CreateInvoke(ThrowFn, UnwindBB, UnwindBB, &ExceptionAsObject,
1562 &ExceptionAsObject+1);
1563 }
1564 // Clear the insertion point to indicate we are in unreachable code.
1565 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001566}
1567
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001568llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001569 llvm::Value *AddrWeakObj)
1570{
1571 return 0;
1572}
1573
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001574void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1575 llvm::Value *src, llvm::Value *dst)
1576{
1577 return;
1578}
1579
Fariborz Jahanian58626502008-11-19 00:59:10 +00001580void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1581 llvm::Value *src, llvm::Value *dst)
1582{
1583 return;
1584}
1585
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001586void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1587 llvm::Value *src, llvm::Value *dst)
1588{
1589 return;
1590}
1591
Fariborz Jahanian58626502008-11-19 00:59:10 +00001592void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1593 llvm::Value *src, llvm::Value *dst)
1594{
1595 return;
1596}
1597
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001598void CGObjCGNU::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1599 llvm::Value *DestPtr,
1600 llvm::Value *SrcPtr,
Fariborz Jahanian08c32132009-08-31 19:33:16 +00001601 QualType Ty) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001602 return;
1603}
1604
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001605llvm::GlobalVariable *CGObjCGNU::ObjCIvarOffsetVariable(
1606 const ObjCInterfaceDecl *ID,
1607 const ObjCIvarDecl *Ivar) {
1608 const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString()
1609 + '.' + Ivar->getNameAsString();
1610 // Emit the variable and initialize it with what we think the correct value
1611 // is. This allows code compiled with non-fragile ivars to work correctly
1612 // when linked against code which isn't (most of the time).
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001613 llvm::GlobalVariable *IvarOffsetPointer = TheModule.getNamedGlobal(Name);
1614 if (!IvarOffsetPointer) {
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001615 uint64_t Offset = ComputeIvarBaseOffset(CGM, ID, Ivar);
1616 llvm::ConstantInt *OffsetGuess =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001617 llvm::ConstantInt::get(LongTy, Offset, "ivar");
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001618 // Don't emit the guess in non-PIC code because the linker will not be able
1619 // to replace it with the real version for a library. In non-PIC code you
1620 // must compile with the fragile ABI if you want to use ivars from a
1621 // GCC-compiled class.
1622 if (CGM.getLangOptions().PICLevel) {
1623 llvm::GlobalVariable *IvarOffsetGV = new llvm::GlobalVariable(TheModule,
1624 llvm::Type::getInt32Ty(VMContext), false,
1625 llvm::GlobalValue::PrivateLinkage, OffsetGuess, Name+".guess");
1626 IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
1627 IvarOffsetGV->getType(), false, llvm::GlobalValue::LinkOnceAnyLinkage,
1628 IvarOffsetGV, Name);
1629 } else {
1630 IvarOffsetPointer = new llvm::GlobalVariable(TheModule,
1631 llvm::PointerType::getUnqual(llvm::Type::getInt32Ty(VMContext)),
1632 false, llvm::GlobalValue::ExternalLinkage, 0, Name);
1633 }
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001634 }
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001635 return IvarOffsetPointer;
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001636}
1637
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001638LValue CGObjCGNU::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1639 QualType ObjectTy,
1640 llvm::Value *BaseValue,
1641 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001642 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00001643 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00001644 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
1645 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001646}
Mike Stumpbb1c8602009-07-31 21:31:32 +00001647
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001648static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context,
1649 const ObjCInterfaceDecl *OID,
1650 const ObjCIvarDecl *OIVD) {
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001651 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00001652 Context.ShallowCollectObjCIvars(OID, Ivars);
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001653 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
1654 if (OIVD == Ivars[k])
1655 return OID;
1656 }
1657
1658 // Otherwise check in the super class.
1659 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
1660 return FindIvarInterface(Context, Super, OIVD);
1661
1662 return 0;
1663}
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001664
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001665llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001666 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001667 const ObjCIvarDecl *Ivar) {
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001668 if (CGM.getLangOptions().ObjCNonFragileABI) {
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001669 Interface = FindIvarInterface(CGM.getContext(), Interface, Ivar);
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001670 return CGF.Builder.CreateLoad(CGF.Builder.CreateLoad(
1671 ObjCIvarOffsetVariable(Interface, Ivar), false, "ivar"));
Fariborz Jahanian9cd96ff2009-05-20 18:41:51 +00001672 }
Daniel Dunbar97776872009-04-22 07:32:20 +00001673 uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001674 return llvm::ConstantInt::get(LongTy, Offset, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001675}
1676
Mike Stumpbb1c8602009-07-31 21:31:32 +00001677CodeGen::CGObjCRuntime *
1678CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM) {
Chris Lattnerdce14062008-06-26 04:19:03 +00001679 return new CGObjCGNU(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +00001680}