blob: 10ceee9c6b206def2d3963e5420707f3637e0848 [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 Lattnerdce14062008-06-26 04:19:03 +000020#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000021#include "clang/AST/Decl.h"
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000022#include "clang/AST/DeclObjC.h"
Chris Lattner0f984262008-03-01 08:50:34 +000023#include "llvm/Module.h"
Chris Lattner0f984262008-03-01 08:50:34 +000024#include "llvm/ADT/SmallVector.h"
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000025#include "llvm/ADT/StringMap.h"
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000026#include "llvm/Support/Compiler.h"
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000027#include "llvm/Target/TargetData.h"
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000028#include <map>
Chris Lattnere160c9b2009-01-27 05:06:01 +000029
30
Chris Lattnerdce14062008-06-26 04:19:03 +000031using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000032using namespace CodeGen;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000033using llvm::dyn_cast;
34
35// The version of the runtime that this class targets. Must match the version
36// in the runtime.
37static const int RuntimeVersion = 8;
38static const int ProtocolVersion = 2;
Chris Lattner0f984262008-03-01 08:50:34 +000039
Chris Lattner0f984262008-03-01 08:50:34 +000040namespace {
Chris Lattnerdce14062008-06-26 04:19:03 +000041class CGObjCGNU : public CodeGen::CGObjCRuntime {
Chris Lattner0f984262008-03-01 08:50:34 +000042private:
Chris Lattnerdce14062008-06-26 04:19:03 +000043 CodeGen::CodeGenModule &CGM;
Chris Lattner0f984262008-03-01 08:50:34 +000044 llvm::Module &TheModule;
Chris Lattnere160c9b2009-01-27 05:06:01 +000045 const llvm::PointerType *SelectorTy;
46 const llvm::PointerType *PtrToInt8Ty;
Chris Lattner391d77a2008-03-30 23:03:07 +000047 const llvm::Type *IMPTy;
Chris Lattnere160c9b2009-01-27 05:06:01 +000048 const llvm::PointerType *IdTy;
49 const llvm::IntegerType *IntTy;
50 const llvm::PointerType *PtrTy;
51 const llvm::IntegerType *LongTy;
52 const llvm::PointerType *PtrToIntTy;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000053 std::vector<llvm::Constant*> Classes;
54 std::vector<llvm::Constant*> Categories;
55 std::vector<llvm::Constant*> ConstantStrings;
56 llvm::Function *LoadFunction;
57 llvm::StringMap<llvm::Constant*> ExistingProtocols;
58 typedef std::pair<std::string, std::string> TypedSelector;
59 std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors;
60 llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors;
61 // Some zeros used for GEPs in lots of places.
62 llvm::Constant *Zeros[2];
63 llvm::Constant *NULLPtr;
64private:
65 llvm::Constant *GenerateIvarList(
66 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
67 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
68 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets);
69 llvm::Constant *GenerateMethodList(const std::string &ClassName,
70 const std::string &CategoryName,
Chris Lattnera4210072008-06-26 05:08:00 +000071 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000072 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
73 bool isClassMethodList);
74 llvm::Constant *GenerateProtocolList(
75 const llvm::SmallVectorImpl<std::string> &Protocols);
76 llvm::Constant *GenerateClassStructure(
77 llvm::Constant *MetaClass,
78 llvm::Constant *SuperClass,
79 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +000080 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000081 llvm::Constant *Version,
82 llvm::Constant *InstanceSize,
83 llvm::Constant *IVars,
84 llvm::Constant *Methods,
85 llvm::Constant *Protocols);
86 llvm::Constant *GenerateProtocolMethodList(
87 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
88 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes);
89 llvm::Constant *MakeConstantString(const std::string &Str, const std::string
90 &Name="");
91 llvm::Constant *MakeGlobal(const llvm::StructType *Ty,
92 std::vector<llvm::Constant*> &V, const std::string &Name="");
93 llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty,
94 std::vector<llvm::Constant*> &V, const std::string &Name="");
Chris Lattner0f984262008-03-01 08:50:34 +000095public:
Chris Lattnerdce14062008-06-26 04:19:03 +000096 CGObjCGNU(CodeGen::CodeGenModule &cgm);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000097 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000098 virtual CodeGen::RValue
99 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000100 QualType ResultType,
101 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000102 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000103 bool IsClassMessage,
104 const CallArgList &CallArgs);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000105 virtual CodeGen::RValue
106 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000107 QualType ResultType,
108 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000109 const ObjCInterfaceDecl *Class,
110 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000111 bool IsClassMessage,
112 const CallArgList &CallArgs);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000113 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000114 const ObjCInterfaceDecl *OID);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000115 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Chris Lattner8e67b632008-06-26 04:37:12 +0000116
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000117 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
118 const ObjCContainerDecl *CD);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000119 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
120 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000121 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000122 const ObjCProtocolDecl *PD);
123 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000124 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar49f66022008-09-24 03:38:44 +0000125 virtual llvm::Function *GetPropertyGetFunction();
126 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000127 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000128
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000129 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
130 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000131 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
132 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000133 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000134 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000135 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
136 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000137 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
138 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000139 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
140 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000141 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
142 llvm::Value *src, llvm::Value *dest);
Chris Lattner0f984262008-03-01 08:50:34 +0000143};
144} // end anonymous namespace
145
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000146
147
148static std::string SymbolNameForClass(const std::string &ClassName) {
149 return ".objc_class_" + ClassName;
150}
151
152static std::string SymbolNameForMethod(const std::string &ClassName, const
153 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
154{
155 return "._objc_method_" + ClassName +"("+CategoryName+")"+
156 (isClassMethod ? "+" : "-") + MethodName;
157}
158
Chris Lattnerdce14062008-06-26 04:19:03 +0000159CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
160 : CGM(cgm), TheModule(CGM.getModule()) {
Chris Lattnere160c9b2009-01-27 05:06:01 +0000161 IntTy = cast<llvm::IntegerType>(
162 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
163 LongTy = cast<llvm::IntegerType>(
164 CGM.getTypes().ConvertType(CGM.getContext().LongTy));
Chris Lattnerdce14062008-06-26 04:19:03 +0000165
Sebastian Redl3a5013c2008-12-04 00:10:55 +0000166 Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000167 Zeros[1] = Zeros[0];
168 NULLPtr = llvm::ConstantPointerNull::get(
169 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
Chris Lattner391d77a2008-03-30 23:03:07 +0000170 // C string type. Used in lots of places.
171 PtrToInt8Ty =
172 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
173 // Get the selector Type.
Chris Lattnere160c9b2009-01-27 05:06:01 +0000174 SelectorTy = cast<llvm::PointerType>(
175 CGM.getTypes().ConvertType(CGM.getContext().getObjCSelType()));
176
Chris Lattner391d77a2008-03-30 23:03:07 +0000177 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
178 PtrTy = PtrToInt8Ty;
179
180 // Object type
181 llvm::PATypeHolder OpaqueObjTy = llvm::OpaqueType::get();
182 llvm::Type *OpaqueIdTy = llvm::PointerType::getUnqual(OpaqueObjTy);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000183 llvm::Type *ObjectTy = llvm::StructType::get(OpaqueIdTy, NULL);
184 llvm::cast<llvm::OpaqueType>(OpaqueObjTy.get())->refineAbstractTypeTo(
185 ObjectTy);
186 ObjectTy = llvm::cast<llvm::StructType>(OpaqueObjTy.get());
187 IdTy = llvm::PointerType::getUnqual(ObjectTy);
Chris Lattner391d77a2008-03-30 23:03:07 +0000188
189 // IMP type
190 std::vector<const llvm::Type*> IMPArgs;
191 IMPArgs.push_back(IdTy);
192 IMPArgs.push_back(SelectorTy);
193 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000194}
195// This has to perform the lookup every time, since posing and related
196// techniques can modify the name -> class mapping.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000197llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000198 const ObjCInterfaceDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000199 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString());
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000200 ClassName = Builder.CreateStructGEP(ClassName, 0);
201
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000202 llvm::Constant *ClassLookupFn =
203 TheModule.getOrInsertFunction("objc_lookup_class", IdTy, PtrToInt8Ty,
204 NULL);
205 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner391d77a2008-03-30 23:03:07 +0000206}
207
Chris Lattner8e67b632008-06-26 04:37:12 +0000208/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000209llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Chris Lattner8e67b632008-06-26 04:37:12 +0000210 // FIXME: uniquing on the string is wasteful, unique on Sel instead!
Chris Lattner077bf5e2008-11-24 03:33:13 +0000211 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()];
Chris Lattner8e67b632008-06-26 04:37:12 +0000212 if (US == 0)
213 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
214 llvm::GlobalValue::InternalLinkage,
215 ".objc_untyped_selector_alias",
216 NULL, &TheModule);
217
218 return Builder.CreateLoad(US);
219
220}
221
Chris Lattner5e7dcc62008-06-26 04:44:19 +0000222llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
223 const std::string &Name) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000224 llvm::Constant * ConstStr = llvm::ConstantArray::get(Str);
225 ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true,
226 llvm::GlobalValue::InternalLinkage,
227 ConstStr, Name, &TheModule);
228 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
229}
230llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
231 std::vector<llvm::Constant*> &V, const std::string &Name) {
232 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
233 return new llvm::GlobalVariable(Ty, false,
234 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
235}
236llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
237 std::vector<llvm::Constant*> &V, const std::string &Name) {
238 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
239 return new llvm::GlobalVariable(Ty, false,
240 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
241}
242
243/// Generate an NSConstantString object.
244//TODO: In case there are any crazy people still using the GNU runtime without
245//an OpenStep implementation, this should let them select their own class for
246//constant strings.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000247llvm::Constant *CGObjCGNU::GenerateConstantString(const std::string &Str) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000248 std::vector<llvm::Constant*> Ivars;
249 Ivars.push_back(NULLPtr);
Chris Lattner13fd7e52008-06-21 21:44:18 +0000250 Ivars.push_back(MakeConstantString(Str));
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000251 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000252 llvm::Constant *ObjCStr = MakeGlobal(
253 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
254 Ivars, ".objc_str");
255 ConstantStrings.push_back(
256 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
257 return ObjCStr;
258}
259
260///Generates a message send where the super is the receiver. This is a message
261///send to self with special delivery semantics indicating which class's method
262///should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000263CodeGen::RValue
264CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000265 QualType ResultType,
266 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000267 const ObjCInterfaceDecl *Class,
268 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000269 bool IsClassMessage,
270 const CallArgList &CallArgs) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000271 const ObjCInterfaceDecl *SuperClass = Class->getSuperClass();
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000272 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000273 // TODO: This should be cached, not looked up every time.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000274 llvm::Value *ReceiverClass = GetClass(CGF.Builder, SuperClass);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000275 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Chris Lattner0f984262008-03-01 08:50:34 +0000276 std::vector<const llvm::Type*> impArgTypes;
277 impArgTypes.push_back(Receiver->getType());
Chris Lattner391d77a2008-03-30 23:03:07 +0000278 impArgTypes.push_back(SelectorTy);
Chris Lattner0f984262008-03-01 08:50:34 +0000279
280 // Avoid an explicit cast on the IMP by getting a version that has the right
281 // return type.
282 llvm::FunctionType *impType = llvm::FunctionType::get(ReturnTy, impArgTypes,
283 true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000284 // Construct the structure used to look up the IMP
285 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
286 IdTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000287 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Eli Friedman1e692ac2008-06-13 23:01:12 +0000288 // FIXME: volatility
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000289 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
290 CGF.Builder.CreateStore(ReceiverClass, CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000291
292 // Get the IMP
293 llvm::Constant *lookupFunction =
294 TheModule.getOrInsertFunction("objc_msg_lookup_super",
295 llvm::PointerType::getUnqual(impType),
296 llvm::PointerType::getUnqual(ObjCSuperTy),
297 SelectorTy, NULL);
298 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000299 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000300 lookupArgs+2);
301
302 // Call the method
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000303 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000304 ActualArgs.push_back(std::make_pair(RValue::get(Receiver),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000305 CGF.getContext().getObjCIdType()));
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000306 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000307 CGF.getContext().getObjCSelType()));
308 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000309 return CGF.EmitCall(imp, ResultType, ActualArgs);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000310}
311
312/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000313CodeGen::RValue
314CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000315 QualType ResultType,
316 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000317 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000318 bool IsClassMessage,
319 const CallArgList &CallArgs) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000320 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
321 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000322
323 // Look up the method implementation.
324 std::vector<const llvm::Type*> impArgTypes;
325 const llvm::Type *RetTy;
326 //TODO: Revisit this when LLVM supports aggregate return types.
327 if (ReturnTy->isSingleValueType() && ReturnTy != llvm::Type::VoidTy) {
328 RetTy = ReturnTy;
329 } else {
330 // For struct returns allocate the space in the caller and pass it up to
331 // the sender.
332 RetTy = llvm::Type::VoidTy;
333 impArgTypes.push_back(llvm::PointerType::getUnqual(ReturnTy));
334 }
335 impArgTypes.push_back(Receiver->getType());
336 impArgTypes.push_back(SelectorTy);
337
338 // Avoid an explicit cast on the IMP by getting a version that has the right
339 // return type.
340 llvm::FunctionType *impType = llvm::FunctionType::get(RetTy, impArgTypes,
341 true);
Chris Lattner0f984262008-03-01 08:50:34 +0000342
343 llvm::Constant *lookupFunction =
344 TheModule.getOrInsertFunction("objc_msg_lookup",
Chris Lattner391d77a2008-03-30 23:03:07 +0000345 llvm::PointerType::getUnqual(impType),
346 Receiver->getType(), SelectorTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000347 llvm::Value *imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000348
349 // Call the method.
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000350 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000351 ActualArgs.push_back(std::make_pair(RValue::get(Receiver),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000352 CGF.getContext().getObjCIdType()));
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000353 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000354 CGF.getContext().getObjCSelType()));
355 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000356 return CGF.EmitCall(imp, ResultType, ActualArgs);
Chris Lattner0f984262008-03-01 08:50:34 +0000357}
358
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000359/// Generates a MethodList. Used in construction of a objc_class and
360/// objc_category structures.
361llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattnera4210072008-06-26 05:08:00 +0000362 const std::string &CategoryName,
363 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000364 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
365 bool isClassMethodList) {
366 // Get the method structure type.
367 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
368 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
369 PtrToInt8Ty, // Method types
370 llvm::PointerType::getUnqual(IMPTy), //Method pointer
371 NULL);
372 std::vector<llvm::Constant*> Methods;
373 std::vector<llvm::Constant*> Elements;
374 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
375 Elements.clear();
Chris Lattner077bf5e2008-11-24 03:33:13 +0000376 llvm::Constant *C =
377 CGM.GetAddrOfConstantCString(MethodSels[i].getAsString());
Chris Lattnera4210072008-06-26 05:08:00 +0000378 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000379 Elements.push_back(
380 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
381 llvm::Constant *Method =
382 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000383 MethodSels[i].getAsString(),
Chris Lattner550b8db2008-06-26 04:05:20 +0000384 isClassMethodList));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000385 Method = llvm::ConstantExpr::getBitCast(Method,
386 llvm::PointerType::getUnqual(IMPTy));
387 Elements.push_back(Method);
388 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
389 }
390
391 // Array of method structures
392 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Chris Lattnera4210072008-06-26 05:08:00 +0000393 MethodSels.size());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000394 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerfba67632008-06-26 04:52:29 +0000395 Methods);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000396
397 // Structure containing list pointer, array and array count
398 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
399 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
400 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
401 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
402 IntTy,
403 ObjCMethodArrayTy,
404 NULL);
405 // Refine next pointer type to concrete type
406 llvm::cast<llvm::OpaqueType>(
407 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
408 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
409
410 Methods.clear();
411 Methods.push_back(llvm::ConstantPointerNull::get(
412 llvm::PointerType::getUnqual(ObjCMethodListTy)));
413 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
414 MethodTypes.size()));
415 Methods.push_back(MethodArray);
416
417 // Create an instance of the structure
418 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
419}
420
421/// Generates an IvarList. Used in construction of a objc_class.
422llvm::Constant *CGObjCGNU::GenerateIvarList(
423 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
424 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
425 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
426 // Get the method structure type.
427 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
428 PtrToInt8Ty,
429 PtrToInt8Ty,
430 IntTy,
431 NULL);
432 std::vector<llvm::Constant*> Ivars;
433 std::vector<llvm::Constant*> Elements;
434 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
435 Elements.clear();
436 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
437 Zeros, 2));
438 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
439 Zeros, 2));
440 Elements.push_back(IvarOffsets[i]);
441 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
442 }
443
444 // Array of method structures
445 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
446 IvarNames.size());
447
448
449 Elements.clear();
Chris Lattnere160c9b2009-01-27 05:06:01 +0000450 Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000451 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
452 // Structure containing array and array count
453 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
454 ObjCIvarArrayTy,
455 NULL);
456
457 // Create an instance of the structure
458 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
459}
460
461/// Generate a class structure
462llvm::Constant *CGObjCGNU::GenerateClassStructure(
463 llvm::Constant *MetaClass,
464 llvm::Constant *SuperClass,
465 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000466 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000467 llvm::Constant *Version,
468 llvm::Constant *InstanceSize,
469 llvm::Constant *IVars,
470 llvm::Constant *Methods,
471 llvm::Constant *Protocols) {
472 // Set up the class structure
473 // Note: Several of these are char*s when they should be ids. This is
474 // because the runtime performs this translation on load.
475 llvm::StructType *ClassTy = llvm::StructType::get(
476 PtrToInt8Ty, // class_pointer
477 PtrToInt8Ty, // super_class
478 PtrToInt8Ty, // name
479 LongTy, // version
480 LongTy, // info
481 LongTy, // instance_size
482 IVars->getType(), // ivars
483 Methods->getType(), // methods
484 // These are all filled in by the runtime, so we pretend
485 PtrTy, // dtable
486 PtrTy, // subclass_list
487 PtrTy, // sibling_class
488 PtrTy, // protocols
489 PtrTy, // gc_object_type
490 NULL);
491 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
492 llvm::Constant *NullP =
Chris Lattnere160c9b2009-01-27 05:06:01 +0000493 llvm::ConstantPointerNull::get(PtrTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000494 // Fill in the structure
495 std::vector<llvm::Constant*> Elements;
496 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
497 Elements.push_back(SuperClass);
Chris Lattnerd002cc62008-06-26 04:47:04 +0000498 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000499 Elements.push_back(Zero);
500 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
501 Elements.push_back(InstanceSize);
502 Elements.push_back(IVars);
503 Elements.push_back(Methods);
504 Elements.push_back(NullP);
505 Elements.push_back(NullP);
506 Elements.push_back(NullP);
507 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
508 Elements.push_back(NullP);
509 // Create an instance of the structure
Chris Lattner1565e032008-07-21 06:31:05 +0000510 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000511}
512
513llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
514 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
515 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
516 // Get the method structure type.
517 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
518 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
519 PtrToInt8Ty,
520 NULL);
521 std::vector<llvm::Constant*> Methods;
522 std::vector<llvm::Constant*> Elements;
523 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
524 Elements.clear();
525 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
526 Zeros, 2));
527 Elements.push_back(
528 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
529 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
530 }
531 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
532 MethodNames.size());
533 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
534 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
535 IntTy, ObjCMethodArrayTy, NULL);
536 Methods.clear();
537 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
538 Methods.push_back(Array);
539 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
540}
541// Create the protocol list structure used in classes, categories and so on
542llvm::Constant *CGObjCGNU::GenerateProtocolList(
543 const llvm::SmallVectorImpl<std::string> &Protocols) {
544 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
545 Protocols.size());
546 llvm::StructType *ProtocolListTy = llvm::StructType::get(
547 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
548 LongTy,//FIXME: Should be size_t
549 ProtocolArrayTy,
550 NULL);
551 std::vector<llvm::Constant*> Elements;
552 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
553 iter != endIter ; iter++) {
554 llvm::Constant *Ptr =
555 llvm::ConstantExpr::getBitCast(ExistingProtocols[*iter], PtrToInt8Ty);
556 Elements.push_back(Ptr);
557 }
558 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
559 Elements);
560 Elements.clear();
561 Elements.push_back(NULLPtr);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000562 Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000563 Elements.push_back(ProtocolArray);
564 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
565}
566
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000567llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000568 const ObjCProtocolDecl *PD) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000569 return ExistingProtocols[PD->getNameAsString()];
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000570}
571
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000572void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
573 ASTContext &Context = CGM.getContext();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000574 std::string ProtocolName = PD->getNameAsString();
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000575 llvm::SmallVector<std::string, 16> Protocols;
576 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
577 E = PD->protocol_end(); PI != E; ++PI)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000578 Protocols.push_back((*PI)->getNameAsString());
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000579 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
580 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
581 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
582 E = PD->instmeth_end(); iter != E; iter++) {
583 std::string TypeStr;
584 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
585 InstanceMethodNames.push_back(
Chris Lattner077bf5e2008-11-24 03:33:13 +0000586 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar61432932008-08-13 23:20:05 +0000587 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000588 }
589 // Collect information about class methods:
590 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
591 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
592 for (ObjCProtocolDecl::classmeth_iterator iter = PD->classmeth_begin(),
593 endIter = PD->classmeth_end() ; iter != endIter ; iter++) {
594 std::string TypeStr;
595 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
596 ClassMethodNames.push_back(
Chris Lattner077bf5e2008-11-24 03:33:13 +0000597 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar61432932008-08-13 23:20:05 +0000598 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000599 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000600
601 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
602 llvm::Constant *InstanceMethodList =
603 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
604 llvm::Constant *ClassMethodList =
605 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
606 // Protocols are objects containing lists of the methods implemented and
607 // protocols adopted.
608 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
609 PtrToInt8Ty,
610 ProtocolList->getType(),
611 InstanceMethodList->getType(),
612 ClassMethodList->getType(),
613 NULL);
614 std::vector<llvm::Constant*> Elements;
615 // The isa pointer must be set to a magic number so the runtime knows it's
616 // the correct layout.
617 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
618 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
619 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
620 Elements.push_back(ProtocolList);
621 Elements.push_back(InstanceMethodList);
622 Elements.push_back(ClassMethodList);
623 ExistingProtocols[ProtocolName] =
624 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
625 ".objc_protocol"), IdTy);
626}
627
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000628void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Chris Lattner8ec03f52008-11-24 03:54:41 +0000629 std::string ClassName = OCD->getClassInterface()->getNameAsString();
630 std::string CategoryName = OCD->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000631 // Collect information about instance methods
632 llvm::SmallVector<Selector, 16> InstanceMethodSels;
633 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Steve Naroff0701bbb2009-01-08 17:28:14 +0000634 for (ObjCCategoryImplDecl::instmeth_iterator iter = OCD->instmeth_begin(),
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000635 endIter = OCD->instmeth_end() ; iter != endIter ; iter++) {
636 InstanceMethodSels.push_back((*iter)->getSelector());
637 std::string TypeStr;
638 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
639 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
640 }
641
642 // Collect information about class methods
643 llvm::SmallVector<Selector, 16> ClassMethodSels;
644 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Steve Naroff0701bbb2009-01-08 17:28:14 +0000645 for (ObjCCategoryImplDecl::classmeth_iterator iter = OCD->classmeth_begin(),
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000646 endIter = OCD->classmeth_end() ; iter != endIter ; iter++) {
647 ClassMethodSels.push_back((*iter)->getSelector());
648 std::string TypeStr;
649 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
650 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
651 }
652
653 // Collect the names of referenced protocols
654 llvm::SmallVector<std::string, 16> Protocols;
655 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
656 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
657 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
658 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000659 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000660
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000661 std::vector<llvm::Constant*> Elements;
662 Elements.push_back(MakeConstantString(CategoryName));
663 Elements.push_back(MakeConstantString(ClassName));
664 // Instance method list
665 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000666 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000667 false), PtrTy));
668 // Class method list
669 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000670 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000671 PtrTy));
672 // Protocol list
673 Elements.push_back(llvm::ConstantExpr::getBitCast(
674 GenerateProtocolList(Protocols), PtrTy));
675 Categories.push_back(llvm::ConstantExpr::getBitCast(
676 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
677 PtrTy, PtrTy, NULL), Elements), PtrTy));
678}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000679
680void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
681 ASTContext &Context = CGM.getContext();
682
683 // Get the superclass name.
684 const ObjCInterfaceDecl * SuperClassDecl =
685 OID->getClassInterface()->getSuperClass();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000686 std::string SuperClassName;
687 if (SuperClassDecl)
688 SuperClassName = SuperClassDecl->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000689
690 // Get the class name
691 ObjCInterfaceDecl * ClassDecl = (ObjCInterfaceDecl*)OID->getClassInterface();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000692 std::string ClassName = ClassDecl->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000693
694 // Get the size of instances. For runtimes that support late-bound instances
695 // this should probably be something different (size just of instance
696 // varaibles in this class, not superclasses?).
697 int instanceSize = 0;
698 const llvm::Type *ObjTy = 0;
699 if (!LateBoundIVars()) {
700 ObjTy = CGM.getTypes().ConvertType(Context.getObjCInterfaceType(ClassDecl));
Daniel Dunbar491c7b72009-01-12 21:08:18 +0000701 instanceSize = CGM.getTargetData().getTypePaddedSize(ObjTy);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000702 } else {
703 // This is required by newer ObjC runtimes.
704 assert(0 && "Late-bound instance variables not yet supported");
705 }
706
707 // Collect information about instance variables.
708 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
709 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
710 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
711 const llvm::StructLayout *Layout =
712 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(ObjTy));
713 ObjTy = llvm::PointerType::getUnqual(ObjTy);
714 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
715 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
716 // Store the name
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000717 IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter)
718 ->getNameAsString()));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000719 // Get the type encoding for this ivar
720 std::string TypeStr;
Daniel Dunbar0d504c12008-10-17 20:21:44 +0000721 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000722 IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
723 // Get the offset
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000724 FieldDecl *Field = ClassDecl->lookupFieldDeclForIvar(Context, (*iter));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000725 int offset =
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000726 (int)Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000727 IvarOffsets.push_back(
728 llvm::ConstantInt::get(llvm::Type::Int32Ty, offset));
729 }
730
731 // Collect information about instance methods
732 llvm::SmallVector<Selector, 16> InstanceMethodSels;
733 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
734 for (ObjCImplementationDecl::instmeth_iterator iter = OID->instmeth_begin(),
735 endIter = OID->instmeth_end() ; iter != endIter ; iter++) {
736 InstanceMethodSels.push_back((*iter)->getSelector());
737 std::string TypeStr;
738 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
739 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
740 }
741
742 // Collect information about class methods
743 llvm::SmallVector<Selector, 16> ClassMethodSels;
744 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
745 for (ObjCImplementationDecl::classmeth_iterator iter = OID->classmeth_begin(),
746 endIter = OID->classmeth_end() ; iter != endIter ; iter++) {
747 ClassMethodSels.push_back((*iter)->getSelector());
748 std::string TypeStr;
749 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
750 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
751 }
752 // Collect the names of referenced protocols
753 llvm::SmallVector<std::string, 16> Protocols;
754 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
755 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
756 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000757 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000758
759
760
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000761 // Get the superclass pointer.
762 llvm::Constant *SuperClass;
Chris Lattner8ec03f52008-11-24 03:54:41 +0000763 if (!SuperClassName.empty()) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000764 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
765 } else {
Chris Lattnere160c9b2009-01-27 05:06:01 +0000766 SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000767 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000768 // Empty vector used to construct empty method lists
769 llvm::SmallVector<llvm::Constant*, 1> empty;
770 // Generate the method and instance variable lists
771 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000772 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000773 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000774 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000775 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
776 IvarOffsets);
777 //Generate metaclass for class methods
778 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner1565e032008-07-21 06:31:05 +0000779 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000780 empty, empty, empty), ClassMethodList, NULLPtr);
781 // Generate the class structure
Chris Lattner8ec03f52008-11-24 03:54:41 +0000782 llvm::Constant *ClassStruct =
783 GenerateClassStructure(MetaClassStruct, SuperClass, 0x1L,
784 ClassName.c_str(), 0,
Sebastian Redl3a5013c2008-12-04 00:10:55 +0000785 llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000786 MethodList, GenerateProtocolList(Protocols));
787 // Add class structure to list to be added to the symtab later
788 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
789 Classes.push_back(ClassStruct);
790}
791
792llvm::Function *CGObjCGNU::ModuleInitFunction() {
793 // Only emit an ObjC load function if no Objective-C stuff has been called
794 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
795 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov5f58b912008-06-01 15:14:46 +0000796 UntypedSelectors.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000797 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +0000798
Chris Lattnere160c9b2009-01-27 05:06:01 +0000799 const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
800 SelectorTy->getElementType());
801 const llvm::Type *SelStructPtrTy = SelectorTy;
802 bool isSelOpaque = false;
803 if (SelStructTy == 0) {
804 SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, NULL);
805 SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
806 isSelOpaque = true;
807 }
808
Eli Friedman1b8956e2008-06-01 16:00:02 +0000809 // Name the ObjC types to make the IR a bit easier to read
Chris Lattnere160c9b2009-01-27 05:06:01 +0000810 TheModule.addTypeName(".objc_selector", SelStructPtrTy);
Eli Friedman1b8956e2008-06-01 16:00:02 +0000811 TheModule.addTypeName(".objc_id", IdTy);
812 TheModule.addTypeName(".objc_imp", IMPTy);
813
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000814 std::vector<llvm::Constant*> Elements;
815 // Generate statics list:
816 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
817 ConstantStrings.size() + 1);
818 ConstantStrings.push_back(NULLPtr);
819 Elements.push_back(MakeConstantString("NSConstantString",
820 ".objc_static_class_name"));
821 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings));
822 llvm::StructType *StaticsListTy =
823 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
Chris Lattner630404b2008-06-26 04:10:42 +0000824 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000825 llvm::Constant *Statics =
826 MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Chris Lattner4e0b2642008-06-24 17:01:28 +0000827 llvm::ArrayType *StaticsListArrayTy =
Chris Lattner630404b2008-06-26 04:10:42 +0000828 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner4e0b2642008-06-24 17:01:28 +0000829 Elements.clear();
830 Elements.push_back(Statics);
Chris Lattner630404b2008-06-26 04:10:42 +0000831 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner4e0b2642008-06-24 17:01:28 +0000832 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000833 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
834 // Array of classes, categories, and constant objects
835 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
836 Classes.size() + Categories.size() + 2);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000837 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy,
Chris Lattner630404b2008-06-26 04:10:42 +0000838 llvm::Type::Int16Ty,
839 llvm::Type::Int16Ty,
840 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000841
842 Elements.clear();
843 // Pointer to an array of selectors used in this module.
844 std::vector<llvm::Constant*> Selectors;
845 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
846 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
847 iter != iterEnd ; ++iter) {
Chris Lattner630404b2008-06-26 04:10:42 +0000848 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
849 Elements.push_back(MakeConstantString(iter->first.second,
850 ".objc_sel_types"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000851 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
852 Elements.clear();
853 }
854 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
855 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner630404b2008-06-26 04:10:42 +0000856 iter != iterEnd; ++iter) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000857 Elements.push_back(
Chris Lattner630404b2008-06-26 04:10:42 +0000858 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000859 Elements.push_back(NULLPtr);
860 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
861 Elements.clear();
862 }
863 Elements.push_back(NULLPtr);
864 Elements.push_back(NULLPtr);
865 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
866 Elements.clear();
867 // Number of static selectors
868 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
869 llvm::Constant *SelectorList = MakeGlobal(
870 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
871 ".objc_selector_list");
Chris Lattnere160c9b2009-01-27 05:06:01 +0000872 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
873 SelStructPtrTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000874
875 // Now that all of the static selectors exist, create pointers to them.
876 int index = 0;
877 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
878 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
879 iter != iterEnd; ++iter) {
880 llvm::Constant *Idxs[] = {Zeros[0],
881 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
Chris Lattnere160c9b2009-01-27 05:06:01 +0000882 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy,
883 true, llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000884 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
885 ".objc_sel_ptr", &TheModule);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000886 // If selectors are defined as an opaque type, cast the pointer to this
887 // type.
888 if (isSelOpaque) {
889 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
890 llvm::PointerType::getUnqual(SelectorTy));
891 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000892 (*iter).second->setAliasee(SelPtr);
893 }
894 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
895 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
896 iter != iterEnd; iter++) {
897 llvm::Constant *Idxs[] = {Zeros[0],
898 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
Chris Lattnere160c9b2009-01-27 05:06:01 +0000899 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy, true,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000900 llvm::GlobalValue::InternalLinkage,
901 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
902 ".objc_sel_ptr", &TheModule);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000903 // If selectors are defined as an opaque type, cast the pointer to this
904 // type.
905 if (isSelOpaque) {
906 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
907 llvm::PointerType::getUnqual(SelectorTy));
908 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000909 (*iter).second->setAliasee(SelPtr);
910 }
911 // Number of classes defined.
912 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
913 Classes.size()));
914 // Number of categories defined
915 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
916 Categories.size()));
917 // Create an array of classes, then categories, then static object instances
918 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
919 // NULL-terminated list of static object instances (mainly constant strings)
920 Classes.push_back(Statics);
921 Classes.push_back(NULLPtr);
922 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
923 Elements.push_back(ClassList);
924 // Construct the symbol table
925 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
926
927 // The symbol table is contained in a module which has some version-checking
928 // constants
929 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
930 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
931 Elements.clear();
932 // Runtime version used for compatibility checking.
933 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
934 //FIXME: Should be sizeof(ModuleTy)
935 Elements.push_back(llvm::ConstantInt::get(LongTy, 16));
936 //FIXME: Should be the path to the file where this module was declared
937 Elements.push_back(NULLPtr);
938 Elements.push_back(SymTab);
939 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
940
941 // Create the load function calling the runtime entry point with the module
942 // structure
943 std::vector<const llvm::Type*> VoidArgs;
944 llvm::Function * LoadFunction = llvm::Function::Create(
945 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
946 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
947 &TheModule);
948 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000949 CGBuilderTy Builder;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000950 Builder.SetInsertPoint(EntryBB);
951 llvm::Value *Register = TheModule.getOrInsertFunction("__objc_exec_class",
952 llvm::Type::VoidTy, llvm::PointerType::getUnqual(ModuleTy), NULL);
953 Builder.CreateCall(Register, Module);
954 Builder.CreateRetVoid();
955 return LoadFunction;
956}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000957
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000958llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
959 const ObjCContainerDecl *CD) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000960 const ObjCCategoryImplDecl *OCD =
Steve Naroff3e0a5402009-01-08 19:41:02 +0000961 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
Chris Lattner077bf5e2008-11-24 03:33:13 +0000962 std::string CategoryName = OCD ? OCD->getNameAsString() : "";
963 std::string ClassName = OMD->getClassInterface()->getNameAsString();
964 std::string MethodName = OMD->getSelector().getAsString();
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000965 bool isClassMethod = !OMD->isInstanceMethod();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000966
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000967 const llvm::FunctionType *MethodTy =
968 CGM.getTypes().GetFunctionType(CGFunctionInfo(OMD, CGM.getContext()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000969 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
970 MethodName, isClassMethod);
971
Gabor Greif984d0b42008-04-06 20:42:52 +0000972 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000973 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000974 FunctionName,
Chris Lattner391d77a2008-03-30 23:03:07 +0000975 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +0000976 return Method;
977}
978
Daniel Dunbar49f66022008-09-24 03:38:44 +0000979llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
980 return 0;
981}
982
983llvm::Function *CGObjCGNU::GetPropertySetFunction() {
984 return 0;
985}
986
987llvm::Function *CGObjCGNU::EnumerationMutationFunction() {
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000988 return 0;
989}
990
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000991void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
992 const Stmt &S) {
993 CGF.ErrorUnsupported(&S, "@try/@synchronized statement");
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000994}
995
996void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +0000997 const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000998 CGF.ErrorUnsupported(&S, "@throw statement");
999}
1000
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001001llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001002 llvm::Value *AddrWeakObj)
1003{
1004 return 0;
1005}
1006
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001007void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1008 llvm::Value *src, llvm::Value *dst)
1009{
1010 return;
1011}
1012
Fariborz Jahanian58626502008-11-19 00:59:10 +00001013void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1014 llvm::Value *src, llvm::Value *dst)
1015{
1016 return;
1017}
1018
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001019void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1020 llvm::Value *src, llvm::Value *dst)
1021{
1022 return;
1023}
1024
Fariborz Jahanian58626502008-11-19 00:59:10 +00001025void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1026 llvm::Value *src, llvm::Value *dst)
1027{
1028 return;
1029}
1030
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001031CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
Chris Lattnerdce14062008-06-26 04:19:03 +00001032 return new CGObjCGNU(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +00001033}