blob: 8cee0f0bce4f5cea24beb1d204b5256eeb53ecb9 [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;
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +000047 const llvm::FunctionType *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);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +0000143 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
144 QualType ObjectTy,
145 llvm::Value *BaseValue,
146 const ObjCIvarDecl *Ivar,
147 const FieldDecl *Field,
148 unsigned CVRQualifiers);
Chris Lattner0f984262008-03-01 08:50:34 +0000149};
150} // end anonymous namespace
151
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000152
153
154static std::string SymbolNameForClass(const std::string &ClassName) {
155 return ".objc_class_" + ClassName;
156}
157
158static std::string SymbolNameForMethod(const std::string &ClassName, const
159 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
160{
161 return "._objc_method_" + ClassName +"("+CategoryName+")"+
162 (isClassMethod ? "+" : "-") + MethodName;
163}
164
Chris Lattnerdce14062008-06-26 04:19:03 +0000165CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
166 : CGM(cgm), TheModule(CGM.getModule()) {
Chris Lattnere160c9b2009-01-27 05:06:01 +0000167 IntTy = cast<llvm::IntegerType>(
168 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
169 LongTy = cast<llvm::IntegerType>(
170 CGM.getTypes().ConvertType(CGM.getContext().LongTy));
Chris Lattnerdce14062008-06-26 04:19:03 +0000171
Sebastian Redl3a5013c2008-12-04 00:10:55 +0000172 Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000173 Zeros[1] = Zeros[0];
174 NULLPtr = llvm::ConstantPointerNull::get(
175 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
Chris Lattner391d77a2008-03-30 23:03:07 +0000176 // C string type. Used in lots of places.
177 PtrToInt8Ty =
178 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
179 // Get the selector Type.
Chris Lattnere160c9b2009-01-27 05:06:01 +0000180 SelectorTy = cast<llvm::PointerType>(
181 CGM.getTypes().ConvertType(CGM.getContext().getObjCSelType()));
182
Chris Lattner391d77a2008-03-30 23:03:07 +0000183 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
184 PtrTy = PtrToInt8Ty;
185
186 // Object type
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000187 IdTy = cast<llvm::PointerType>(
188 CGM.getTypes().ConvertType(CGM.getContext().getObjCIdType()));
Chris Lattner391d77a2008-03-30 23:03:07 +0000189
190 // IMP type
191 std::vector<const llvm::Type*> IMPArgs;
192 IMPArgs.push_back(IdTy);
193 IMPArgs.push_back(SelectorTy);
194 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000195}
196// This has to perform the lookup every time, since posing and related
197// techniques can modify the name -> class mapping.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000198llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000199 const ObjCInterfaceDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000200 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString());
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000201 ClassName = Builder.CreateStructGEP(ClassName, 0);
202
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000203 llvm::Constant *ClassLookupFn =
204 TheModule.getOrInsertFunction("objc_lookup_class", IdTy, PtrToInt8Ty,
205 NULL);
206 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner391d77a2008-03-30 23:03:07 +0000207}
208
Chris Lattner8e67b632008-06-26 04:37:12 +0000209/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000210llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Chris Lattner8e67b632008-06-26 04:37:12 +0000211 // FIXME: uniquing on the string is wasteful, unique on Sel instead!
Chris Lattner077bf5e2008-11-24 03:33:13 +0000212 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()];
Chris Lattner8e67b632008-06-26 04:37:12 +0000213 if (US == 0)
214 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
215 llvm::GlobalValue::InternalLinkage,
216 ".objc_untyped_selector_alias",
217 NULL, &TheModule);
218
219 return Builder.CreateLoad(US);
220
221}
222
Chris Lattner5e7dcc62008-06-26 04:44:19 +0000223llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
224 const std::string &Name) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000225 llvm::Constant * ConstStr = llvm::ConstantArray::get(Str);
226 ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true,
227 llvm::GlobalValue::InternalLinkage,
228 ConstStr, Name, &TheModule);
229 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
230}
231llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
232 std::vector<llvm::Constant*> &V, const std::string &Name) {
233 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
234 return new llvm::GlobalVariable(Ty, false,
235 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
236}
237llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
238 std::vector<llvm::Constant*> &V, const std::string &Name) {
239 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
240 return new llvm::GlobalVariable(Ty, false,
241 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
242}
243
244/// Generate an NSConstantString object.
245//TODO: In case there are any crazy people still using the GNU runtime without
246//an OpenStep implementation, this should let them select their own class for
247//constant strings.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000248llvm::Constant *CGObjCGNU::GenerateConstantString(const std::string &Str) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000249 std::vector<llvm::Constant*> Ivars;
250 Ivars.push_back(NULLPtr);
Chris Lattner13fd7e52008-06-21 21:44:18 +0000251 Ivars.push_back(MakeConstantString(Str));
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000252 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000253 llvm::Constant *ObjCStr = MakeGlobal(
254 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
255 Ivars, ".objc_str");
256 ConstantStrings.push_back(
257 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
258 return ObjCStr;
259}
260
261///Generates a message send where the super is the receiver. This is a message
262///send to self with special delivery semantics indicating which class's method
263///should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000264CodeGen::RValue
265CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000266 QualType ResultType,
267 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000268 const ObjCInterfaceDecl *Class,
269 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000270 bool IsClassMessage,
271 const CallArgList &CallArgs) {
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000272 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
273
274 CallArgList ActualArgs;
275
276 ActualArgs.push_back(
277 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
278 CGF.getContext().getObjCIdType()));
279 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
280 CGF.getContext().getObjCSelType()));
281 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
282
283 CodeGenTypes &Types = CGM.getTypes();
284 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
285 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
286
287
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000288 const ObjCInterfaceDecl *SuperClass = Class->getSuperClass();
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000289 // TODO: This should be cached, not looked up every time.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000290 llvm::Value *ReceiverClass = GetClass(CGF.Builder, SuperClass);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000291
292
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000293 // Construct the structure used to look up the IMP
294 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
295 IdTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000296 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000297
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000298 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
299 CGF.Builder.CreateStore(ReceiverClass, CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000300
301 // Get the IMP
302 llvm::Constant *lookupFunction =
303 TheModule.getOrInsertFunction("objc_msg_lookup_super",
304 llvm::PointerType::getUnqual(impType),
305 llvm::PointerType::getUnqual(ObjCSuperTy),
306 SelectorTy, NULL);
307 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000308 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000309 lookupArgs+2);
310
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000311 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000312}
313
314/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000315CodeGen::RValue
316CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000317 QualType ResultType,
318 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000319 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000320 bool IsClassMessage,
321 const CallArgList &CallArgs) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000322 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000323 CallArgList ActualArgs;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000324
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000325 ActualArgs.push_back(
326 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
327 CGF.getContext().getObjCIdType()));
328 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
329 CGF.getContext().getObjCSelType()));
330 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
331
332 CodeGenTypes &Types = CGM.getTypes();
333 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
334 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
335
Chris Lattner0f984262008-03-01 08:50:34 +0000336 llvm::Constant *lookupFunction =
337 TheModule.getOrInsertFunction("objc_msg_lookup",
Chris Lattner391d77a2008-03-30 23:03:07 +0000338 llvm::PointerType::getUnqual(impType),
339 Receiver->getType(), SelectorTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000340 llvm::Value *imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000341
Fariborz Jahanianb3716ef2009-02-04 20:31:19 +0000342 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Chris Lattner0f984262008-03-01 08:50:34 +0000343}
344
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000345/// Generates a MethodList. Used in construction of a objc_class and
346/// objc_category structures.
347llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattnera4210072008-06-26 05:08:00 +0000348 const std::string &CategoryName,
349 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000350 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
351 bool isClassMethodList) {
352 // Get the method structure type.
353 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
354 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
355 PtrToInt8Ty, // Method types
356 llvm::PointerType::getUnqual(IMPTy), //Method pointer
357 NULL);
358 std::vector<llvm::Constant*> Methods;
359 std::vector<llvm::Constant*> Elements;
360 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
361 Elements.clear();
Chris Lattner077bf5e2008-11-24 03:33:13 +0000362 llvm::Constant *C =
363 CGM.GetAddrOfConstantCString(MethodSels[i].getAsString());
Chris Lattnera4210072008-06-26 05:08:00 +0000364 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000365 Elements.push_back(
366 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
367 llvm::Constant *Method =
368 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000369 MethodSels[i].getAsString(),
Chris Lattner550b8db2008-06-26 04:05:20 +0000370 isClassMethodList));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000371 Method = llvm::ConstantExpr::getBitCast(Method,
372 llvm::PointerType::getUnqual(IMPTy));
373 Elements.push_back(Method);
374 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
375 }
376
377 // Array of method structures
378 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Chris Lattnera4210072008-06-26 05:08:00 +0000379 MethodSels.size());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000380 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerfba67632008-06-26 04:52:29 +0000381 Methods);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000382
383 // Structure containing list pointer, array and array count
384 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
385 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
386 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
387 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
388 IntTy,
389 ObjCMethodArrayTy,
390 NULL);
391 // Refine next pointer type to concrete type
392 llvm::cast<llvm::OpaqueType>(
393 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
394 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
395
396 Methods.clear();
397 Methods.push_back(llvm::ConstantPointerNull::get(
398 llvm::PointerType::getUnqual(ObjCMethodListTy)));
399 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
400 MethodTypes.size()));
401 Methods.push_back(MethodArray);
402
403 // Create an instance of the structure
404 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
405}
406
407/// Generates an IvarList. Used in construction of a objc_class.
408llvm::Constant *CGObjCGNU::GenerateIvarList(
409 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
410 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
411 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
412 // Get the method structure type.
413 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
414 PtrToInt8Ty,
415 PtrToInt8Ty,
416 IntTy,
417 NULL);
418 std::vector<llvm::Constant*> Ivars;
419 std::vector<llvm::Constant*> Elements;
420 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
421 Elements.clear();
422 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
423 Zeros, 2));
424 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
425 Zeros, 2));
426 Elements.push_back(IvarOffsets[i]);
427 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
428 }
429
430 // Array of method structures
431 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
432 IvarNames.size());
433
434
435 Elements.clear();
Chris Lattnere160c9b2009-01-27 05:06:01 +0000436 Elements.push_back(llvm::ConstantInt::get(IntTy, (int)IvarNames.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000437 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
438 // Structure containing array and array count
439 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
440 ObjCIvarArrayTy,
441 NULL);
442
443 // Create an instance of the structure
444 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
445}
446
447/// Generate a class structure
448llvm::Constant *CGObjCGNU::GenerateClassStructure(
449 llvm::Constant *MetaClass,
450 llvm::Constant *SuperClass,
451 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000452 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000453 llvm::Constant *Version,
454 llvm::Constant *InstanceSize,
455 llvm::Constant *IVars,
456 llvm::Constant *Methods,
457 llvm::Constant *Protocols) {
458 // Set up the class structure
459 // Note: Several of these are char*s when they should be ids. This is
460 // because the runtime performs this translation on load.
461 llvm::StructType *ClassTy = llvm::StructType::get(
462 PtrToInt8Ty, // class_pointer
463 PtrToInt8Ty, // super_class
464 PtrToInt8Ty, // name
465 LongTy, // version
466 LongTy, // info
467 LongTy, // instance_size
468 IVars->getType(), // ivars
469 Methods->getType(), // methods
470 // These are all filled in by the runtime, so we pretend
471 PtrTy, // dtable
472 PtrTy, // subclass_list
473 PtrTy, // sibling_class
474 PtrTy, // protocols
475 PtrTy, // gc_object_type
476 NULL);
477 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
478 llvm::Constant *NullP =
Chris Lattnere160c9b2009-01-27 05:06:01 +0000479 llvm::ConstantPointerNull::get(PtrTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000480 // Fill in the structure
481 std::vector<llvm::Constant*> Elements;
482 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
483 Elements.push_back(SuperClass);
Chris Lattnerd002cc62008-06-26 04:47:04 +0000484 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000485 Elements.push_back(Zero);
486 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
487 Elements.push_back(InstanceSize);
488 Elements.push_back(IVars);
489 Elements.push_back(Methods);
490 Elements.push_back(NullP);
491 Elements.push_back(NullP);
492 Elements.push_back(NullP);
493 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
494 Elements.push_back(NullP);
495 // Create an instance of the structure
Chris Lattner1565e032008-07-21 06:31:05 +0000496 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000497}
498
499llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
500 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
501 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
502 // Get the method structure type.
503 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
504 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
505 PtrToInt8Ty,
506 NULL);
507 std::vector<llvm::Constant*> Methods;
508 std::vector<llvm::Constant*> Elements;
509 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
510 Elements.clear();
511 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
512 Zeros, 2));
513 Elements.push_back(
514 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
515 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
516 }
517 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
518 MethodNames.size());
519 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
520 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
521 IntTy, ObjCMethodArrayTy, NULL);
522 Methods.clear();
523 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
524 Methods.push_back(Array);
525 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
526}
527// Create the protocol list structure used in classes, categories and so on
528llvm::Constant *CGObjCGNU::GenerateProtocolList(
529 const llvm::SmallVectorImpl<std::string> &Protocols) {
530 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
531 Protocols.size());
532 llvm::StructType *ProtocolListTy = llvm::StructType::get(
533 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
534 LongTy,//FIXME: Should be size_t
535 ProtocolArrayTy,
536 NULL);
537 std::vector<llvm::Constant*> Elements;
538 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
539 iter != endIter ; iter++) {
540 llvm::Constant *Ptr =
541 llvm::ConstantExpr::getBitCast(ExistingProtocols[*iter], PtrToInt8Ty);
542 Elements.push_back(Ptr);
543 }
544 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
545 Elements);
546 Elements.clear();
547 Elements.push_back(NULLPtr);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000548 Elements.push_back(llvm::ConstantInt::get(LongTy, Protocols.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000549 Elements.push_back(ProtocolArray);
550 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
551}
552
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000553llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000554 const ObjCProtocolDecl *PD) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000555 return ExistingProtocols[PD->getNameAsString()];
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000556}
557
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000558void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
559 ASTContext &Context = CGM.getContext();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000560 std::string ProtocolName = PD->getNameAsString();
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000561 llvm::SmallVector<std::string, 16> Protocols;
562 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
563 E = PD->protocol_end(); PI != E; ++PI)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000564 Protocols.push_back((*PI)->getNameAsString());
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000565 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
566 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
567 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
568 E = PD->instmeth_end(); iter != E; iter++) {
569 std::string TypeStr;
570 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
571 InstanceMethodNames.push_back(
Chris Lattner077bf5e2008-11-24 03:33:13 +0000572 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar61432932008-08-13 23:20:05 +0000573 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000574 }
575 // Collect information about class methods:
576 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
577 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
578 for (ObjCProtocolDecl::classmeth_iterator iter = PD->classmeth_begin(),
579 endIter = PD->classmeth_end() ; iter != endIter ; iter++) {
580 std::string TypeStr;
581 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
582 ClassMethodNames.push_back(
Chris Lattner077bf5e2008-11-24 03:33:13 +0000583 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar61432932008-08-13 23:20:05 +0000584 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000585 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000586
587 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
588 llvm::Constant *InstanceMethodList =
589 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
590 llvm::Constant *ClassMethodList =
591 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
592 // Protocols are objects containing lists of the methods implemented and
593 // protocols adopted.
594 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
595 PtrToInt8Ty,
596 ProtocolList->getType(),
597 InstanceMethodList->getType(),
598 ClassMethodList->getType(),
599 NULL);
600 std::vector<llvm::Constant*> Elements;
601 // The isa pointer must be set to a magic number so the runtime knows it's
602 // the correct layout.
603 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
604 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
605 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
606 Elements.push_back(ProtocolList);
607 Elements.push_back(InstanceMethodList);
608 Elements.push_back(ClassMethodList);
609 ExistingProtocols[ProtocolName] =
610 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
611 ".objc_protocol"), IdTy);
612}
613
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000614void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Chris Lattner8ec03f52008-11-24 03:54:41 +0000615 std::string ClassName = OCD->getClassInterface()->getNameAsString();
616 std::string CategoryName = OCD->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000617 // Collect information about instance methods
618 llvm::SmallVector<Selector, 16> InstanceMethodSels;
619 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Steve Naroff0701bbb2009-01-08 17:28:14 +0000620 for (ObjCCategoryImplDecl::instmeth_iterator iter = OCD->instmeth_begin(),
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000621 endIter = OCD->instmeth_end() ; iter != endIter ; iter++) {
622 InstanceMethodSels.push_back((*iter)->getSelector());
623 std::string TypeStr;
624 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
625 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
626 }
627
628 // Collect information about class methods
629 llvm::SmallVector<Selector, 16> ClassMethodSels;
630 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Steve Naroff0701bbb2009-01-08 17:28:14 +0000631 for (ObjCCategoryImplDecl::classmeth_iterator iter = OCD->classmeth_begin(),
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000632 endIter = OCD->classmeth_end() ; iter != endIter ; iter++) {
633 ClassMethodSels.push_back((*iter)->getSelector());
634 std::string TypeStr;
635 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
636 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
637 }
638
639 // Collect the names of referenced protocols
640 llvm::SmallVector<std::string, 16> Protocols;
641 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
642 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
643 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
644 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000645 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000646
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000647 std::vector<llvm::Constant*> Elements;
648 Elements.push_back(MakeConstantString(CategoryName));
649 Elements.push_back(MakeConstantString(ClassName));
650 // Instance method list
651 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000652 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000653 false), PtrTy));
654 // Class method list
655 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000656 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000657 PtrTy));
658 // Protocol list
659 Elements.push_back(llvm::ConstantExpr::getBitCast(
660 GenerateProtocolList(Protocols), PtrTy));
661 Categories.push_back(llvm::ConstantExpr::getBitCast(
662 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
663 PtrTy, PtrTy, NULL), Elements), PtrTy));
664}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000665
666void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
667 ASTContext &Context = CGM.getContext();
668
669 // Get the superclass name.
670 const ObjCInterfaceDecl * SuperClassDecl =
671 OID->getClassInterface()->getSuperClass();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000672 std::string SuperClassName;
673 if (SuperClassDecl)
674 SuperClassName = SuperClassDecl->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000675
676 // Get the class name
677 ObjCInterfaceDecl * ClassDecl = (ObjCInterfaceDecl*)OID->getClassInterface();
Chris Lattner8ec03f52008-11-24 03:54:41 +0000678 std::string ClassName = ClassDecl->getNameAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000679
680 // Get the size of instances. For runtimes that support late-bound instances
681 // this should probably be something different (size just of instance
682 // varaibles in this class, not superclasses?).
683 int instanceSize = 0;
684 const llvm::Type *ObjTy = 0;
685 if (!LateBoundIVars()) {
686 ObjTy = CGM.getTypes().ConvertType(Context.getObjCInterfaceType(ClassDecl));
Daniel Dunbar491c7b72009-01-12 21:08:18 +0000687 instanceSize = CGM.getTargetData().getTypePaddedSize(ObjTy);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000688 } else {
689 // This is required by newer ObjC runtimes.
690 assert(0 && "Late-bound instance variables not yet supported");
691 }
692
693 // Collect information about instance variables.
694 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
695 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
696 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
697 const llvm::StructLayout *Layout =
698 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(ObjTy));
699 ObjTy = llvm::PointerType::getUnqual(ObjTy);
700 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
701 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
702 // Store the name
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000703 IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter)
704 ->getNameAsString()));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000705 // Get the type encoding for this ivar
706 std::string TypeStr;
Daniel Dunbar0d504c12008-10-17 20:21:44 +0000707 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000708 IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
709 // Get the offset
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000710 FieldDecl *Field = ClassDecl->lookupFieldDeclForIvar(Context, (*iter));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000711 int offset =
Fariborz Jahanianfd64bb62008-12-15 20:35:07 +0000712 (int)Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000713 IvarOffsets.push_back(
714 llvm::ConstantInt::get(llvm::Type::Int32Ty, offset));
715 }
716
717 // Collect information about instance methods
718 llvm::SmallVector<Selector, 16> InstanceMethodSels;
719 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
720 for (ObjCImplementationDecl::instmeth_iterator iter = OID->instmeth_begin(),
721 endIter = OID->instmeth_end() ; iter != endIter ; iter++) {
722 InstanceMethodSels.push_back((*iter)->getSelector());
723 std::string TypeStr;
724 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
725 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
726 }
727
728 // Collect information about class methods
729 llvm::SmallVector<Selector, 16> ClassMethodSels;
730 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
731 for (ObjCImplementationDecl::classmeth_iterator iter = OID->classmeth_begin(),
732 endIter = OID->classmeth_end() ; iter != endIter ; iter++) {
733 ClassMethodSels.push_back((*iter)->getSelector());
734 std::string TypeStr;
735 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
736 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
737 }
738 // Collect the names of referenced protocols
739 llvm::SmallVector<std::string, 16> Protocols;
740 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
741 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
742 E = Protos.end(); I != E; ++I)
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000743 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000744
745
746
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000747 // Get the superclass pointer.
748 llvm::Constant *SuperClass;
Chris Lattner8ec03f52008-11-24 03:54:41 +0000749 if (!SuperClassName.empty()) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000750 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
751 } else {
Chris Lattnere160c9b2009-01-27 05:06:01 +0000752 SuperClass = llvm::ConstantPointerNull::get(PtrToInt8Ty);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000753 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000754 // Empty vector used to construct empty method lists
755 llvm::SmallVector<llvm::Constant*, 1> empty;
756 // Generate the method and instance variable lists
757 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000758 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000759 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000760 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000761 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
762 IvarOffsets);
763 //Generate metaclass for class methods
764 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner1565e032008-07-21 06:31:05 +0000765 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000766 empty, empty, empty), ClassMethodList, NULLPtr);
767 // Generate the class structure
Chris Lattner8ec03f52008-11-24 03:54:41 +0000768 llvm::Constant *ClassStruct =
769 GenerateClassStructure(MetaClassStruct, SuperClass, 0x1L,
770 ClassName.c_str(), 0,
Sebastian Redl3a5013c2008-12-04 00:10:55 +0000771 llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000772 MethodList, GenerateProtocolList(Protocols));
773 // Add class structure to list to be added to the symtab later
774 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
775 Classes.push_back(ClassStruct);
776}
777
778llvm::Function *CGObjCGNU::ModuleInitFunction() {
779 // Only emit an ObjC load function if no Objective-C stuff has been called
780 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
781 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov5f58b912008-06-01 15:14:46 +0000782 UntypedSelectors.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000783 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +0000784
Chris Lattnere160c9b2009-01-27 05:06:01 +0000785 const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
786 SelectorTy->getElementType());
787 const llvm::Type *SelStructPtrTy = SelectorTy;
788 bool isSelOpaque = false;
789 if (SelStructTy == 0) {
790 SelStructTy = llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, NULL);
791 SelStructPtrTy = llvm::PointerType::getUnqual(SelStructTy);
792 isSelOpaque = true;
793 }
794
Eli Friedman1b8956e2008-06-01 16:00:02 +0000795 // Name the ObjC types to make the IR a bit easier to read
Chris Lattnere160c9b2009-01-27 05:06:01 +0000796 TheModule.addTypeName(".objc_selector", SelStructPtrTy);
Eli Friedman1b8956e2008-06-01 16:00:02 +0000797 TheModule.addTypeName(".objc_id", IdTy);
798 TheModule.addTypeName(".objc_imp", IMPTy);
799
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000800 std::vector<llvm::Constant*> Elements;
801 // Generate statics list:
802 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
803 ConstantStrings.size() + 1);
804 ConstantStrings.push_back(NULLPtr);
805 Elements.push_back(MakeConstantString("NSConstantString",
806 ".objc_static_class_name"));
807 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings));
808 llvm::StructType *StaticsListTy =
809 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
Chris Lattner630404b2008-06-26 04:10:42 +0000810 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000811 llvm::Constant *Statics =
812 MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Chris Lattner4e0b2642008-06-24 17:01:28 +0000813 llvm::ArrayType *StaticsListArrayTy =
Chris Lattner630404b2008-06-26 04:10:42 +0000814 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner4e0b2642008-06-24 17:01:28 +0000815 Elements.clear();
816 Elements.push_back(Statics);
Chris Lattner630404b2008-06-26 04:10:42 +0000817 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner4e0b2642008-06-24 17:01:28 +0000818 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000819 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
820 // Array of classes, categories, and constant objects
821 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
822 Classes.size() + Categories.size() + 2);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000823 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelStructPtrTy,
Chris Lattner630404b2008-06-26 04:10:42 +0000824 llvm::Type::Int16Ty,
825 llvm::Type::Int16Ty,
826 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000827
828 Elements.clear();
829 // Pointer to an array of selectors used in this module.
830 std::vector<llvm::Constant*> Selectors;
831 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
832 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
833 iter != iterEnd ; ++iter) {
Chris Lattner630404b2008-06-26 04:10:42 +0000834 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
835 Elements.push_back(MakeConstantString(iter->first.second,
836 ".objc_sel_types"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000837 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
838 Elements.clear();
839 }
840 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
841 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner630404b2008-06-26 04:10:42 +0000842 iter != iterEnd; ++iter) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000843 Elements.push_back(
Chris Lattner630404b2008-06-26 04:10:42 +0000844 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000845 Elements.push_back(NULLPtr);
846 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
847 Elements.clear();
848 }
849 Elements.push_back(NULLPtr);
850 Elements.push_back(NULLPtr);
851 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
852 Elements.clear();
853 // Number of static selectors
854 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
855 llvm::Constant *SelectorList = MakeGlobal(
856 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
857 ".objc_selector_list");
Chris Lattnere160c9b2009-01-27 05:06:01 +0000858 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList,
859 SelStructPtrTy));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000860
861 // Now that all of the static selectors exist, create pointers to them.
862 int index = 0;
863 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
864 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
865 iter != iterEnd; ++iter) {
866 llvm::Constant *Idxs[] = {Zeros[0],
867 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
Chris Lattnere160c9b2009-01-27 05:06:01 +0000868 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy,
869 true, llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000870 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
871 ".objc_sel_ptr", &TheModule);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000872 // If selectors are defined as an opaque type, cast the pointer to this
873 // type.
874 if (isSelOpaque) {
875 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
876 llvm::PointerType::getUnqual(SelectorTy));
877 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000878 (*iter).second->setAliasee(SelPtr);
879 }
880 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
881 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
882 iter != iterEnd; iter++) {
883 llvm::Constant *Idxs[] = {Zeros[0],
884 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
Chris Lattnere160c9b2009-01-27 05:06:01 +0000885 llvm::Constant *SelPtr = new llvm::GlobalVariable(SelStructPtrTy, true,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000886 llvm::GlobalValue::InternalLinkage,
887 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
888 ".objc_sel_ptr", &TheModule);
Chris Lattnere160c9b2009-01-27 05:06:01 +0000889 // If selectors are defined as an opaque type, cast the pointer to this
890 // type.
891 if (isSelOpaque) {
892 SelPtr = llvm::ConstantExpr::getBitCast(SelPtr,
893 llvm::PointerType::getUnqual(SelectorTy));
894 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000895 (*iter).second->setAliasee(SelPtr);
896 }
897 // Number of classes defined.
898 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
899 Classes.size()));
900 // Number of categories defined
901 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
902 Categories.size()));
903 // Create an array of classes, then categories, then static object instances
904 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
905 // NULL-terminated list of static object instances (mainly constant strings)
906 Classes.push_back(Statics);
907 Classes.push_back(NULLPtr);
908 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
909 Elements.push_back(ClassList);
910 // Construct the symbol table
911 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
912
913 // The symbol table is contained in a module which has some version-checking
914 // constants
915 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
916 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
917 Elements.clear();
918 // Runtime version used for compatibility checking.
919 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
920 //FIXME: Should be sizeof(ModuleTy)
921 Elements.push_back(llvm::ConstantInt::get(LongTy, 16));
922 //FIXME: Should be the path to the file where this module was declared
923 Elements.push_back(NULLPtr);
924 Elements.push_back(SymTab);
925 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
926
927 // Create the load function calling the runtime entry point with the module
928 // structure
929 std::vector<const llvm::Type*> VoidArgs;
930 llvm::Function * LoadFunction = llvm::Function::Create(
931 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
932 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
933 &TheModule);
934 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000935 CGBuilderTy Builder;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000936 Builder.SetInsertPoint(EntryBB);
937 llvm::Value *Register = TheModule.getOrInsertFunction("__objc_exec_class",
938 llvm::Type::VoidTy, llvm::PointerType::getUnqual(ModuleTy), NULL);
939 Builder.CreateCall(Register, Module);
940 Builder.CreateRetVoid();
941 return LoadFunction;
942}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000943
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000944llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
945 const ObjCContainerDecl *CD) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000946 const ObjCCategoryImplDecl *OCD =
Steve Naroff3e0a5402009-01-08 19:41:02 +0000947 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
Chris Lattner077bf5e2008-11-24 03:33:13 +0000948 std::string CategoryName = OCD ? OCD->getNameAsString() : "";
949 std::string ClassName = OMD->getClassInterface()->getNameAsString();
950 std::string MethodName = OMD->getSelector().getAsString();
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000951 bool isClassMethod = !OMD->isInstanceMethod();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000952
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000953 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000954 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +0000955 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000956 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
957 MethodName, isClassMethod);
958
Gabor Greif984d0b42008-04-06 20:42:52 +0000959 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000960 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000961 FunctionName,
Chris Lattner391d77a2008-03-30 23:03:07 +0000962 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +0000963 return Method;
964}
965
Daniel Dunbar49f66022008-09-24 03:38:44 +0000966llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
967 return 0;
968}
969
970llvm::Function *CGObjCGNU::GetPropertySetFunction() {
971 return 0;
972}
973
974llvm::Function *CGObjCGNU::EnumerationMutationFunction() {
Fariborz Jahanian660af742009-02-03 21:52:35 +0000975return
976 (llvm::Function*)TheModule.getOrInsertFunction(
977 "objc_enumerationMutation", llvm::Type::VoidTy, IdTy, NULL);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000978}
979
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000980void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
981 const Stmt &S) {
982 CGF.ErrorUnsupported(&S, "@try/@synchronized statement");
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000983}
984
985void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +0000986 const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000987 CGF.ErrorUnsupported(&S, "@throw statement");
988}
989
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000990llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000991 llvm::Value *AddrWeakObj)
992{
993 return 0;
994}
995
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000996void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
997 llvm::Value *src, llvm::Value *dst)
998{
999 return;
1000}
1001
Fariborz Jahanian58626502008-11-19 00:59:10 +00001002void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1003 llvm::Value *src, llvm::Value *dst)
1004{
1005 return;
1006}
1007
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001008void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1009 llvm::Value *src, llvm::Value *dst)
1010{
1011 return;
1012}
1013
Fariborz Jahanian58626502008-11-19 00:59:10 +00001014void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1015 llvm::Value *src, llvm::Value *dst)
1016{
1017 return;
1018}
1019
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001020LValue CGObjCGNU::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1021 QualType ObjectTy,
1022 llvm::Value *BaseValue,
1023 const ObjCIvarDecl *Ivar,
1024 const FieldDecl *Field,
1025 unsigned CVRQualifiers) {
1026 if (Ivar->isBitField())
1027 return CGF.EmitLValueForBitfield(BaseValue, const_cast<FieldDecl *>(Field),
1028 CVRQualifiers);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001029 // TODO: Add a special case for isa (index 0)
1030 unsigned Index = CGM.getTypes().getLLVMFieldNo(Field);
1031 llvm::Value *V = CGF.Builder.CreateStructGEP(BaseValue, Index, "tmp");
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001032 LValue LV = LValue::MakeAddr(V,
1033 Ivar->getType().getCVRQualifiers()|CVRQualifiers);
1034 LValue::SetObjCIvar(LV, true);
1035 return LV;
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001036}
1037
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001038CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
Chris Lattnerdce14062008-06-26 04:19:03 +00001039 return new CGObjCGNU(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +00001040}