blob: 2621a8491406e4c39178add515f9326a522f2f90 [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 Lattnerdce14062008-06-26 04:19:03 +000029using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000030using namespace CodeGen;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000031using llvm::dyn_cast;
32
33// The version of the runtime that this class targets. Must match the version
34// in the runtime.
35static const int RuntimeVersion = 8;
36static const int ProtocolVersion = 2;
Chris Lattner0f984262008-03-01 08:50:34 +000037
Chris Lattner0f984262008-03-01 08:50:34 +000038namespace {
Chris Lattnerdce14062008-06-26 04:19:03 +000039class CGObjCGNU : public CodeGen::CGObjCRuntime {
Chris Lattner0f984262008-03-01 08:50:34 +000040private:
Chris Lattnerdce14062008-06-26 04:19:03 +000041 CodeGen::CodeGenModule &CGM;
Chris Lattner0f984262008-03-01 08:50:34 +000042 llvm::Module &TheModule;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000043 const llvm::StructType *SelStructTy;
Chris Lattner391d77a2008-03-30 23:03:07 +000044 const llvm::Type *SelectorTy;
45 const llvm::Type *PtrToInt8Ty;
46 const llvm::Type *IMPTy;
47 const llvm::Type *IdTy;
48 const llvm::Type *IntTy;
49 const llvm::Type *PtrTy;
50 const llvm::Type *LongTy;
51 const llvm::Type *PtrToIntTy;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000052 std::vector<llvm::Constant*> Classes;
53 std::vector<llvm::Constant*> Categories;
54 std::vector<llvm::Constant*> ConstantStrings;
55 llvm::Function *LoadFunction;
56 llvm::StringMap<llvm::Constant*> ExistingProtocols;
57 typedef std::pair<std::string, std::string> TypedSelector;
58 std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors;
59 llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors;
60 // Some zeros used for GEPs in lots of places.
61 llvm::Constant *Zeros[2];
62 llvm::Constant *NULLPtr;
63private:
64 llvm::Constant *GenerateIvarList(
65 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
66 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
67 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets);
68 llvm::Constant *GenerateMethodList(const std::string &ClassName,
69 const std::string &CategoryName,
Chris Lattnera4210072008-06-26 05:08:00 +000070 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000071 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
72 bool isClassMethodList);
73 llvm::Constant *GenerateProtocolList(
74 const llvm::SmallVectorImpl<std::string> &Protocols);
75 llvm::Constant *GenerateClassStructure(
76 llvm::Constant *MetaClass,
77 llvm::Constant *SuperClass,
78 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +000079 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000080 llvm::Constant *Version,
81 llvm::Constant *InstanceSize,
82 llvm::Constant *IVars,
83 llvm::Constant *Methods,
84 llvm::Constant *Protocols);
85 llvm::Constant *GenerateProtocolMethodList(
86 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
87 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes);
88 llvm::Constant *MakeConstantString(const std::string &Str, const std::string
89 &Name="");
90 llvm::Constant *MakeGlobal(const llvm::StructType *Ty,
91 std::vector<llvm::Constant*> &V, const std::string &Name="");
92 llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty,
93 std::vector<llvm::Constant*> &V, const std::string &Name="");
Chris Lattner0f984262008-03-01 08:50:34 +000094public:
Chris Lattnerdce14062008-06-26 04:19:03 +000095 CGObjCGNU(CodeGen::CodeGenModule &cgm);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000096 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000097 virtual CodeGen::RValue
98 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000099 QualType ResultType,
100 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000101 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000102 bool IsClassMessage,
103 const CallArgList &CallArgs);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000104 virtual CodeGen::RValue
105 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000106 QualType ResultType,
107 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000108 const ObjCInterfaceDecl *Class,
109 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000110 bool IsClassMessage,
111 const CallArgList &CallArgs);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000112 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000113 const ObjCInterfaceDecl *OID);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000114 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Chris Lattner8e67b632008-06-26 04:37:12 +0000115
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000116 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
117 const ObjCContainerDecl *CD);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000118 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
119 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000120 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000121 const ObjCProtocolDecl *PD);
122 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000123 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar49f66022008-09-24 03:38:44 +0000124 virtual llvm::Function *GetPropertyGetFunction();
125 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000126 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000127
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000128 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
129 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000130 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
131 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000132 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000133 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000134 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
135 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000136 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
137 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000138 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
139 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000140 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
141 llvm::Value *src, llvm::Value *dest);
Chris Lattner0f984262008-03-01 08:50:34 +0000142};
143} // end anonymous namespace
144
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000145
146
147static std::string SymbolNameForClass(const std::string &ClassName) {
148 return ".objc_class_" + ClassName;
149}
150
151static std::string SymbolNameForMethod(const std::string &ClassName, const
152 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
153{
154 return "._objc_method_" + ClassName +"("+CategoryName+")"+
155 (isClassMethod ? "+" : "-") + MethodName;
156}
157
Chris Lattnerdce14062008-06-26 04:19:03 +0000158CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
159 : CGM(cgm), TheModule(CGM.getModule()) {
160 IntTy = CGM.getTypes().ConvertType(CGM.getContext().IntTy);
161 LongTy = CGM.getTypes().ConvertType(CGM.getContext().LongTy);
162
Sebastian Redl3a5013c2008-12-04 00:10:55 +0000163 Zeros[0] = llvm::ConstantInt::get(LongTy, 0);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000164 Zeros[1] = Zeros[0];
165 NULLPtr = llvm::ConstantPointerNull::get(
166 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
Chris Lattner391d77a2008-03-30 23:03:07 +0000167 // C string type. Used in lots of places.
168 PtrToInt8Ty =
169 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
170 // Get the selector Type.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000171 SelStructTy = llvm::StructType::get(
Chris Lattner391d77a2008-03-30 23:03:07 +0000172 PtrToInt8Ty,
173 PtrToInt8Ty,
174 NULL);
175 SelectorTy = llvm::PointerType::getUnqual(SelStructTy);
176 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
177 PtrTy = PtrToInt8Ty;
178
179 // Object type
180 llvm::PATypeHolder OpaqueObjTy = llvm::OpaqueType::get();
181 llvm::Type *OpaqueIdTy = llvm::PointerType::getUnqual(OpaqueObjTy);
182 IdTy = llvm::StructType::get(OpaqueIdTy, NULL);
183 llvm::cast<llvm::OpaqueType>(OpaqueObjTy.get())->refineAbstractTypeTo(IdTy);
184 IdTy = llvm::cast<llvm::StructType>(OpaqueObjTy.get());
185 IdTy = llvm::PointerType::getUnqual(IdTy);
186
187 // IMP type
188 std::vector<const llvm::Type*> IMPArgs;
189 IMPArgs.push_back(IdTy);
190 IMPArgs.push_back(SelectorTy);
191 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000192}
193// This has to perform the lookup every time, since posing and related
194// techniques can modify the name -> class mapping.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000195llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000196 const ObjCInterfaceDecl *OID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +0000197 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString());
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000198 ClassName = Builder.CreateStructGEP(ClassName, 0);
199
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000200 llvm::Constant *ClassLookupFn =
201 TheModule.getOrInsertFunction("objc_lookup_class", IdTy, PtrToInt8Ty,
202 NULL);
203 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner391d77a2008-03-30 23:03:07 +0000204}
205
Chris Lattner8e67b632008-06-26 04:37:12 +0000206/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000207llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Chris Lattner8e67b632008-06-26 04:37:12 +0000208 // FIXME: uniquing on the string is wasteful, unique on Sel instead!
Chris Lattner077bf5e2008-11-24 03:33:13 +0000209 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()];
Chris Lattner8e67b632008-06-26 04:37:12 +0000210 if (US == 0)
211 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
212 llvm::GlobalValue::InternalLinkage,
213 ".objc_untyped_selector_alias",
214 NULL, &TheModule);
215
216 return Builder.CreateLoad(US);
217
218}
219
Chris Lattner5e7dcc62008-06-26 04:44:19 +0000220llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
221 const std::string &Name) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000222 llvm::Constant * ConstStr = llvm::ConstantArray::get(Str);
223 ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true,
224 llvm::GlobalValue::InternalLinkage,
225 ConstStr, Name, &TheModule);
226 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
227}
228llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
229 std::vector<llvm::Constant*> &V, const std::string &Name) {
230 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
231 return new llvm::GlobalVariable(Ty, false,
232 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
233}
234llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
235 std::vector<llvm::Constant*> &V, const std::string &Name) {
236 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
237 return new llvm::GlobalVariable(Ty, false,
238 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
239}
240
241/// Generate an NSConstantString object.
242//TODO: In case there are any crazy people still using the GNU runtime without
243//an OpenStep implementation, this should let them select their own class for
244//constant strings.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000245llvm::Constant *CGObjCGNU::GenerateConstantString(const std::string &Str) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000246 std::vector<llvm::Constant*> Ivars;
247 Ivars.push_back(NULLPtr);
Chris Lattner13fd7e52008-06-21 21:44:18 +0000248 Ivars.push_back(MakeConstantString(Str));
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000249 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000250 llvm::Constant *ObjCStr = MakeGlobal(
251 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
252 Ivars, ".objc_str");
253 ConstantStrings.push_back(
254 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
255 return ObjCStr;
256}
257
258///Generates a message send where the super is the receiver. This is a message
259///send to self with special delivery semantics indicating which class's method
260///should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000261CodeGen::RValue
262CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000263 QualType ResultType,
264 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000265 const ObjCInterfaceDecl *Class,
266 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000267 bool IsClassMessage,
268 const CallArgList &CallArgs) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000269 const ObjCInterfaceDecl *SuperClass = Class->getSuperClass();
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000270 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000271 // TODO: This should be cached, not looked up every time.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000272 llvm::Value *ReceiverClass = GetClass(CGF.Builder, SuperClass);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000273 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Chris Lattner0f984262008-03-01 08:50:34 +0000274 std::vector<const llvm::Type*> impArgTypes;
275 impArgTypes.push_back(Receiver->getType());
Chris Lattner391d77a2008-03-30 23:03:07 +0000276 impArgTypes.push_back(SelectorTy);
Chris Lattner0f984262008-03-01 08:50:34 +0000277
278 // Avoid an explicit cast on the IMP by getting a version that has the right
279 // return type.
280 llvm::FunctionType *impType = llvm::FunctionType::get(ReturnTy, impArgTypes,
281 true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000282 // Construct the structure used to look up the IMP
283 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
284 IdTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000285 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Eli Friedman1e692ac2008-06-13 23:01:12 +0000286 // FIXME: volatility
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000287 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
288 CGF.Builder.CreateStore(ReceiverClass, CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000289
290 // Get the IMP
291 llvm::Constant *lookupFunction =
292 TheModule.getOrInsertFunction("objc_msg_lookup_super",
293 llvm::PointerType::getUnqual(impType),
294 llvm::PointerType::getUnqual(ObjCSuperTy),
295 SelectorTy, NULL);
296 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000297 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000298 lookupArgs+2);
299
300 // Call the method
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000301 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000302 ActualArgs.push_back(std::make_pair(RValue::get(Receiver),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000303 CGF.getContext().getObjCIdType()));
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000304 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000305 CGF.getContext().getObjCSelType()));
306 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000307 return CGF.EmitCall(imp, ResultType, ActualArgs);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000308}
309
310/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000311CodeGen::RValue
312CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000313 QualType ResultType,
314 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000315 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000316 bool IsClassMessage,
317 const CallArgList &CallArgs) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000318 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
319 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000320
321 // Look up the method implementation.
322 std::vector<const llvm::Type*> impArgTypes;
323 const llvm::Type *RetTy;
324 //TODO: Revisit this when LLVM supports aggregate return types.
325 if (ReturnTy->isSingleValueType() && ReturnTy != llvm::Type::VoidTy) {
326 RetTy = ReturnTy;
327 } else {
328 // For struct returns allocate the space in the caller and pass it up to
329 // the sender.
330 RetTy = llvm::Type::VoidTy;
331 impArgTypes.push_back(llvm::PointerType::getUnqual(ReturnTy));
332 }
333 impArgTypes.push_back(Receiver->getType());
334 impArgTypes.push_back(SelectorTy);
335
336 // Avoid an explicit cast on the IMP by getting a version that has the right
337 // return type.
338 llvm::FunctionType *impType = llvm::FunctionType::get(RetTy, impArgTypes,
339 true);
Chris Lattner0f984262008-03-01 08:50:34 +0000340
341 llvm::Constant *lookupFunction =
342 TheModule.getOrInsertFunction("objc_msg_lookup",
Chris Lattner391d77a2008-03-30 23:03:07 +0000343 llvm::PointerType::getUnqual(impType),
344 Receiver->getType(), SelectorTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000345 llvm::Value *imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000346
347 // Call the method.
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000348 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000349 ActualArgs.push_back(std::make_pair(RValue::get(Receiver),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000350 CGF.getContext().getObjCIdType()));
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000351 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000352 CGF.getContext().getObjCSelType()));
353 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000354 return CGF.EmitCall(imp, ResultType, ActualArgs);
Chris Lattner0f984262008-03-01 08:50:34 +0000355}
356
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000357/// Generates a MethodList. Used in construction of a objc_class and
358/// objc_category structures.
359llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattnera4210072008-06-26 05:08:00 +0000360 const std::string &CategoryName,
361 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000362 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
363 bool isClassMethodList) {
364 // Get the method structure type.
365 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
366 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
367 PtrToInt8Ty, // Method types
368 llvm::PointerType::getUnqual(IMPTy), //Method pointer
369 NULL);
370 std::vector<llvm::Constant*> Methods;
371 std::vector<llvm::Constant*> Elements;
372 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
373 Elements.clear();
Chris Lattner077bf5e2008-11-24 03:33:13 +0000374 llvm::Constant *C =
375 CGM.GetAddrOfConstantCString(MethodSels[i].getAsString());
Chris Lattnera4210072008-06-26 05:08:00 +0000376 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000377 Elements.push_back(
378 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
379 llvm::Constant *Method =
380 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000381 MethodSels[i].getAsString(),
Chris Lattner550b8db2008-06-26 04:05:20 +0000382 isClassMethodList));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000383 Method = llvm::ConstantExpr::getBitCast(Method,
384 llvm::PointerType::getUnqual(IMPTy));
385 Elements.push_back(Method);
386 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
387 }
388
389 // Array of method structures
390 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Chris Lattnera4210072008-06-26 05:08:00 +0000391 MethodSels.size());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000392 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerfba67632008-06-26 04:52:29 +0000393 Methods);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000394
395 // Structure containing list pointer, array and array count
396 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
397 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
398 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
399 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
400 IntTy,
401 ObjCMethodArrayTy,
402 NULL);
403 // Refine next pointer type to concrete type
404 llvm::cast<llvm::OpaqueType>(
405 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
406 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
407
408 Methods.clear();
409 Methods.push_back(llvm::ConstantPointerNull::get(
410 llvm::PointerType::getUnqual(ObjCMethodListTy)));
411 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
412 MethodTypes.size()));
413 Methods.push_back(MethodArray);
414
415 // Create an instance of the structure
416 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
417}
418
419/// Generates an IvarList. Used in construction of a objc_class.
420llvm::Constant *CGObjCGNU::GenerateIvarList(
421 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
422 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
423 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
424 // Get the method structure type.
425 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
426 PtrToInt8Ty,
427 PtrToInt8Ty,
428 IntTy,
429 NULL);
430 std::vector<llvm::Constant*> Ivars;
431 std::vector<llvm::Constant*> Elements;
432 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
433 Elements.clear();
434 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
435 Zeros, 2));
436 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
437 Zeros, 2));
438 Elements.push_back(IvarOffsets[i]);
439 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
440 }
441
442 // Array of method structures
443 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
444 IvarNames.size());
445
446
447 Elements.clear();
448 Elements.push_back(llvm::ConstantInt::get(
449 llvm::cast<llvm::IntegerType>(IntTy), (int)IvarNames.size()));
450 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
451 // Structure containing array and array count
452 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
453 ObjCIvarArrayTy,
454 NULL);
455
456 // Create an instance of the structure
457 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
458}
459
460/// Generate a class structure
461llvm::Constant *CGObjCGNU::GenerateClassStructure(
462 llvm::Constant *MetaClass,
463 llvm::Constant *SuperClass,
464 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000465 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000466 llvm::Constant *Version,
467 llvm::Constant *InstanceSize,
468 llvm::Constant *IVars,
469 llvm::Constant *Methods,
470 llvm::Constant *Protocols) {
471 // Set up the class structure
472 // Note: Several of these are char*s when they should be ids. This is
473 // because the runtime performs this translation on load.
474 llvm::StructType *ClassTy = llvm::StructType::get(
475 PtrToInt8Ty, // class_pointer
476 PtrToInt8Ty, // super_class
477 PtrToInt8Ty, // name
478 LongTy, // version
479 LongTy, // info
480 LongTy, // instance_size
481 IVars->getType(), // ivars
482 Methods->getType(), // methods
483 // These are all filled in by the runtime, so we pretend
484 PtrTy, // dtable
485 PtrTy, // subclass_list
486 PtrTy, // sibling_class
487 PtrTy, // protocols
488 PtrTy, // gc_object_type
489 NULL);
490 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
491 llvm::Constant *NullP =
492 llvm::ConstantPointerNull::get(llvm::cast<llvm::PointerType>(PtrTy));
493 // Fill in the structure
494 std::vector<llvm::Constant*> Elements;
495 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
496 Elements.push_back(SuperClass);
Chris Lattnerd002cc62008-06-26 04:47:04 +0000497 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000498 Elements.push_back(Zero);
499 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
500 Elements.push_back(InstanceSize);
501 Elements.push_back(IVars);
502 Elements.push_back(Methods);
503 Elements.push_back(NullP);
504 Elements.push_back(NullP);
505 Elements.push_back(NullP);
506 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
507 Elements.push_back(NullP);
508 // Create an instance of the structure
Chris Lattner1565e032008-07-21 06:31:05 +0000509 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000510}
511
512llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
513 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
514 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
515 // Get the method structure type.
516 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
517 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
518 PtrToInt8Ty,
519 NULL);
520 std::vector<llvm::Constant*> Methods;
521 std::vector<llvm::Constant*> Elements;
522 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
523 Elements.clear();
524 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
525 Zeros, 2));
526 Elements.push_back(
527 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
528 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
529 }
530 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
531 MethodNames.size());
532 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
533 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
534 IntTy, ObjCMethodArrayTy, NULL);
535 Methods.clear();
536 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
537 Methods.push_back(Array);
538 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
539}
540// Create the protocol list structure used in classes, categories and so on
541llvm::Constant *CGObjCGNU::GenerateProtocolList(
542 const llvm::SmallVectorImpl<std::string> &Protocols) {
543 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
544 Protocols.size());
545 llvm::StructType *ProtocolListTy = llvm::StructType::get(
546 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
547 LongTy,//FIXME: Should be size_t
548 ProtocolArrayTy,
549 NULL);
550 std::vector<llvm::Constant*> Elements;
551 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
552 iter != endIter ; iter++) {
553 llvm::Constant *Ptr =
554 llvm::ConstantExpr::getBitCast(ExistingProtocols[*iter], PtrToInt8Ty);
555 Elements.push_back(Ptr);
556 }
557 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
558 Elements);
559 Elements.clear();
560 Elements.push_back(NULLPtr);
561 Elements.push_back(llvm::ConstantInt::get(
562 llvm::cast<llvm::IntegerType>(LongTy), Protocols.size()));
563 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 {
766 SuperClass = llvm::ConstantPointerNull::get(
767 llvm::cast<llvm::PointerType>(PtrToInt8Ty));
768 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000769 // Empty vector used to construct empty method lists
770 llvm::SmallVector<llvm::Constant*, 1> empty;
771 // Generate the method and instance variable lists
772 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000773 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000774 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000775 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000776 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
777 IvarOffsets);
778 //Generate metaclass for class methods
779 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner1565e032008-07-21 06:31:05 +0000780 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000781 empty, empty, empty), ClassMethodList, NULLPtr);
782 // Generate the class structure
Chris Lattner8ec03f52008-11-24 03:54:41 +0000783 llvm::Constant *ClassStruct =
784 GenerateClassStructure(MetaClassStruct, SuperClass, 0x1L,
785 ClassName.c_str(), 0,
Sebastian Redl3a5013c2008-12-04 00:10:55 +0000786 llvm::ConstantInt::get(LongTy, instanceSize), IvarList,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000787 MethodList, GenerateProtocolList(Protocols));
788 // Add class structure to list to be added to the symtab later
789 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
790 Classes.push_back(ClassStruct);
791}
792
793llvm::Function *CGObjCGNU::ModuleInitFunction() {
794 // Only emit an ObjC load function if no Objective-C stuff has been called
795 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
796 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov5f58b912008-06-01 15:14:46 +0000797 UntypedSelectors.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000798 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +0000799
800 // Name the ObjC types to make the IR a bit easier to read
801 TheModule.addTypeName(".objc_selector", SelectorTy);
802 TheModule.addTypeName(".objc_id", IdTy);
803 TheModule.addTypeName(".objc_imp", IMPTy);
804
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000805 std::vector<llvm::Constant*> Elements;
806 // Generate statics list:
807 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
808 ConstantStrings.size() + 1);
809 ConstantStrings.push_back(NULLPtr);
810 Elements.push_back(MakeConstantString("NSConstantString",
811 ".objc_static_class_name"));
812 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings));
813 llvm::StructType *StaticsListTy =
814 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
Chris Lattner630404b2008-06-26 04:10:42 +0000815 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000816 llvm::Constant *Statics =
817 MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Chris Lattner4e0b2642008-06-24 17:01:28 +0000818 llvm::ArrayType *StaticsListArrayTy =
Chris Lattner630404b2008-06-26 04:10:42 +0000819 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner4e0b2642008-06-24 17:01:28 +0000820 Elements.clear();
821 Elements.push_back(Statics);
Chris Lattner630404b2008-06-26 04:10:42 +0000822 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner4e0b2642008-06-24 17:01:28 +0000823 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000824 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
825 // Array of classes, categories, and constant objects
826 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
827 Classes.size() + Categories.size() + 2);
Chris Lattner630404b2008-06-26 04:10:42 +0000828 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelectorTy,
829 llvm::Type::Int16Ty,
830 llvm::Type::Int16Ty,
831 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000832
833 Elements.clear();
834 // Pointer to an array of selectors used in this module.
835 std::vector<llvm::Constant*> Selectors;
836 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
837 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
838 iter != iterEnd ; ++iter) {
Chris Lattner630404b2008-06-26 04:10:42 +0000839 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
840 Elements.push_back(MakeConstantString(iter->first.second,
841 ".objc_sel_types"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000842 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
843 Elements.clear();
844 }
845 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
846 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner630404b2008-06-26 04:10:42 +0000847 iter != iterEnd; ++iter) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000848 Elements.push_back(
Chris Lattner630404b2008-06-26 04:10:42 +0000849 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000850 Elements.push_back(NULLPtr);
851 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
852 Elements.clear();
853 }
854 Elements.push_back(NULLPtr);
855 Elements.push_back(NULLPtr);
856 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
857 Elements.clear();
858 // Number of static selectors
859 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
860 llvm::Constant *SelectorList = MakeGlobal(
861 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
862 ".objc_selector_list");
863 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList, SelectorTy));
864
865 // Now that all of the static selectors exist, create pointers to them.
866 int index = 0;
867 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
868 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
869 iter != iterEnd; ++iter) {
870 llvm::Constant *Idxs[] = {Zeros[0],
871 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
872 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
873 llvm::GlobalValue::InternalLinkage,
874 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
875 ".objc_sel_ptr", &TheModule);
876 (*iter).second->setAliasee(SelPtr);
877 }
878 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
879 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
880 iter != iterEnd; iter++) {
881 llvm::Constant *Idxs[] = {Zeros[0],
882 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
883 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
884 llvm::GlobalValue::InternalLinkage,
885 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
886 ".objc_sel_ptr", &TheModule);
887 (*iter).second->setAliasee(SelPtr);
888 }
889 // Number of classes defined.
890 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
891 Classes.size()));
892 // Number of categories defined
893 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
894 Categories.size()));
895 // Create an array of classes, then categories, then static object instances
896 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
897 // NULL-terminated list of static object instances (mainly constant strings)
898 Classes.push_back(Statics);
899 Classes.push_back(NULLPtr);
900 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
901 Elements.push_back(ClassList);
902 // Construct the symbol table
903 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
904
905 // The symbol table is contained in a module which has some version-checking
906 // constants
907 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
908 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
909 Elements.clear();
910 // Runtime version used for compatibility checking.
911 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
912 //FIXME: Should be sizeof(ModuleTy)
913 Elements.push_back(llvm::ConstantInt::get(LongTy, 16));
914 //FIXME: Should be the path to the file where this module was declared
915 Elements.push_back(NULLPtr);
916 Elements.push_back(SymTab);
917 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
918
919 // Create the load function calling the runtime entry point with the module
920 // structure
921 std::vector<const llvm::Type*> VoidArgs;
922 llvm::Function * LoadFunction = llvm::Function::Create(
923 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
924 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
925 &TheModule);
926 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000927 CGBuilderTy Builder;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000928 Builder.SetInsertPoint(EntryBB);
929 llvm::Value *Register = TheModule.getOrInsertFunction("__objc_exec_class",
930 llvm::Type::VoidTy, llvm::PointerType::getUnqual(ModuleTy), NULL);
931 Builder.CreateCall(Register, Module);
932 Builder.CreateRetVoid();
933 return LoadFunction;
934}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000935
Fariborz Jahanian679a5022009-01-10 21:06:09 +0000936llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
937 const ObjCContainerDecl *CD) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000938 const ObjCCategoryImplDecl *OCD =
Steve Naroff3e0a5402009-01-08 19:41:02 +0000939 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
Chris Lattner077bf5e2008-11-24 03:33:13 +0000940 std::string CategoryName = OCD ? OCD->getNameAsString() : "";
941 std::string ClassName = OMD->getClassInterface()->getNameAsString();
942 std::string MethodName = OMD->getSelector().getAsString();
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000943 bool isClassMethod = !OMD->isInstanceMethod();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000944
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000945 const llvm::FunctionType *MethodTy =
946 CGM.getTypes().GetFunctionType(CGFunctionInfo(OMD, CGM.getContext()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000947 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
948 MethodName, isClassMethod);
949
Gabor Greif984d0b42008-04-06 20:42:52 +0000950 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000951 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000952 FunctionName,
Chris Lattner391d77a2008-03-30 23:03:07 +0000953 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +0000954 return Method;
955}
956
Daniel Dunbar49f66022008-09-24 03:38:44 +0000957llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
958 return 0;
959}
960
961llvm::Function *CGObjCGNU::GetPropertySetFunction() {
962 return 0;
963}
964
965llvm::Function *CGObjCGNU::EnumerationMutationFunction() {
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000966 return 0;
967}
968
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000969void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
970 const Stmt &S) {
971 CGF.ErrorUnsupported(&S, "@try/@synchronized statement");
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000972}
973
974void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +0000975 const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000976 CGF.ErrorUnsupported(&S, "@throw statement");
977}
978
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000979llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000980 llvm::Value *AddrWeakObj)
981{
982 return 0;
983}
984
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000985void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
986 llvm::Value *src, llvm::Value *dst)
987{
988 return;
989}
990
Fariborz Jahanian58626502008-11-19 00:59:10 +0000991void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
992 llvm::Value *src, llvm::Value *dst)
993{
994 return;
995}
996
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000997void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
998 llvm::Value *src, llvm::Value *dst)
999{
1000 return;
1001}
1002
Fariborz Jahanian58626502008-11-19 00:59:10 +00001003void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1004 llvm::Value *src, llvm::Value *dst)
1005{
1006 return;
1007}
1008
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001009CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
Chris Lattnerdce14062008-06-26 04:19:03 +00001010 return new CGObjCGNU(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +00001011}