blob: 25e3915049086ca67c745f13de813994265aa250 [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
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000116 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD);
117 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
118 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000119 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000120 const ObjCProtocolDecl *PD);
121 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000122 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar49f66022008-09-24 03:38:44 +0000123 virtual llvm::Function *GetPropertyGetFunction();
124 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000125 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000126
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000127 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
128 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000129 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
130 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000131 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000132 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000133 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
134 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000135 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
136 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000137 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
138 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000139 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
140 llvm::Value *src, llvm::Value *dest);
Chris Lattner0f984262008-03-01 08:50:34 +0000141};
142} // end anonymous namespace
143
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000144
145
146static std::string SymbolNameForClass(const std::string &ClassName) {
147 return ".objc_class_" + ClassName;
148}
149
150static std::string SymbolNameForMethod(const std::string &ClassName, const
151 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
152{
153 return "._objc_method_" + ClassName +"("+CategoryName+")"+
154 (isClassMethod ? "+" : "-") + MethodName;
155}
156
Chris Lattnerdce14062008-06-26 04:19:03 +0000157CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
158 : CGM(cgm), TheModule(CGM.getModule()) {
159 IntTy = CGM.getTypes().ConvertType(CGM.getContext().IntTy);
160 LongTy = CGM.getTypes().ConvertType(CGM.getContext().LongTy);
161
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000162 Zeros[0] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
163 Zeros[1] = Zeros[0];
164 NULLPtr = llvm::ConstantPointerNull::get(
165 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
Chris Lattner391d77a2008-03-30 23:03:07 +0000166 // C string type. Used in lots of places.
167 PtrToInt8Ty =
168 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
169 // Get the selector Type.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000170 SelStructTy = llvm::StructType::get(
Chris Lattner391d77a2008-03-30 23:03:07 +0000171 PtrToInt8Ty,
172 PtrToInt8Ty,
173 NULL);
174 SelectorTy = llvm::PointerType::getUnqual(SelStructTy);
175 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
176 PtrTy = PtrToInt8Ty;
177
178 // Object type
179 llvm::PATypeHolder OpaqueObjTy = llvm::OpaqueType::get();
180 llvm::Type *OpaqueIdTy = llvm::PointerType::getUnqual(OpaqueObjTy);
181 IdTy = llvm::StructType::get(OpaqueIdTy, NULL);
182 llvm::cast<llvm::OpaqueType>(OpaqueObjTy.get())->refineAbstractTypeTo(IdTy);
183 IdTy = llvm::cast<llvm::StructType>(OpaqueObjTy.get());
184 IdTy = llvm::PointerType::getUnqual(IdTy);
185
186 // IMP type
187 std::vector<const llvm::Type*> IMPArgs;
188 IMPArgs.push_back(IdTy);
189 IMPArgs.push_back(SelectorTy);
190 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000191}
192// This has to perform the lookup every time, since posing and related
193// techniques can modify the name -> class mapping.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000194llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000195 const ObjCInterfaceDecl *OID) {
196 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getName());
197 ClassName = Builder.CreateStructGEP(ClassName, 0);
198
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000199 llvm::Constant *ClassLookupFn =
200 TheModule.getOrInsertFunction("objc_lookup_class", IdTy, PtrToInt8Ty,
201 NULL);
202 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner391d77a2008-03-30 23:03:07 +0000203}
204
Chris Lattner8e67b632008-06-26 04:37:12 +0000205/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000206llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Chris Lattner8e67b632008-06-26 04:37:12 +0000207 // FIXME: uniquing on the string is wasteful, unique on Sel instead!
208 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getName()];
209 if (US == 0)
210 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
211 llvm::GlobalValue::InternalLinkage,
212 ".objc_untyped_selector_alias",
213 NULL, &TheModule);
214
215 return Builder.CreateLoad(US);
216
217}
218
Chris Lattner5e7dcc62008-06-26 04:44:19 +0000219llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
220 const std::string &Name) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000221 llvm::Constant * ConstStr = llvm::ConstantArray::get(Str);
222 ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true,
223 llvm::GlobalValue::InternalLinkage,
224 ConstStr, Name, &TheModule);
225 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
226}
227llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
228 std::vector<llvm::Constant*> &V, const std::string &Name) {
229 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
230 return new llvm::GlobalVariable(Ty, false,
231 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
232}
233llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
234 std::vector<llvm::Constant*> &V, const std::string &Name) {
235 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
236 return new llvm::GlobalVariable(Ty, false,
237 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
238}
239
240/// Generate an NSConstantString object.
241//TODO: In case there are any crazy people still using the GNU runtime without
242//an OpenStep implementation, this should let them select their own class for
243//constant strings.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000244llvm::Constant *CGObjCGNU::GenerateConstantString(const std::string &Str) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000245 std::vector<llvm::Constant*> Ivars;
246 Ivars.push_back(NULLPtr);
Chris Lattner13fd7e52008-06-21 21:44:18 +0000247 Ivars.push_back(MakeConstantString(Str));
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000248 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000249 llvm::Constant *ObjCStr = MakeGlobal(
250 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
251 Ivars, ".objc_str");
252 ConstantStrings.push_back(
253 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
254 return ObjCStr;
255}
256
257///Generates a message send where the super is the receiver. This is a message
258///send to self with special delivery semantics indicating which class's method
259///should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000260CodeGen::RValue
261CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000262 QualType ResultType,
263 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000264 const ObjCInterfaceDecl *Class,
265 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000266 bool IsClassMessage,
267 const CallArgList &CallArgs) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000268 const ObjCInterfaceDecl *SuperClass = Class->getSuperClass();
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000269 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000270 // TODO: This should be cached, not looked up every time.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000271 llvm::Value *ReceiverClass = GetClass(CGF.Builder, SuperClass);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000272 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Chris Lattner0f984262008-03-01 08:50:34 +0000273 std::vector<const llvm::Type*> impArgTypes;
274 impArgTypes.push_back(Receiver->getType());
Chris Lattner391d77a2008-03-30 23:03:07 +0000275 impArgTypes.push_back(SelectorTy);
Chris Lattner0f984262008-03-01 08:50:34 +0000276
277 // Avoid an explicit cast on the IMP by getting a version that has the right
278 // return type.
279 llvm::FunctionType *impType = llvm::FunctionType::get(ReturnTy, impArgTypes,
280 true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000281 // Construct the structure used to look up the IMP
282 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
283 IdTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000284 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Eli Friedman1e692ac2008-06-13 23:01:12 +0000285 // FIXME: volatility
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000286 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
287 CGF.Builder.CreateStore(ReceiverClass, CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000288
289 // Get the IMP
290 llvm::Constant *lookupFunction =
291 TheModule.getOrInsertFunction("objc_msg_lookup_super",
292 llvm::PointerType::getUnqual(impType),
293 llvm::PointerType::getUnqual(ObjCSuperTy),
294 SelectorTy, NULL);
295 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000296 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000297 lookupArgs+2);
298
299 // Call the method
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000300 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000301 ActualArgs.push_back(std::make_pair(RValue::get(Receiver),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000302 CGF.getContext().getObjCIdType()));
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000303 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000304 CGF.getContext().getObjCSelType()));
305 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000306 return CGF.EmitCall(imp, ResultType, ActualArgs);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000307}
308
309/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000310CodeGen::RValue
311CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000312 QualType ResultType,
313 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000314 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000315 bool IsClassMessage,
316 const CallArgList &CallArgs) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000317 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
318 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000319
320 // Look up the method implementation.
321 std::vector<const llvm::Type*> impArgTypes;
322 const llvm::Type *RetTy;
323 //TODO: Revisit this when LLVM supports aggregate return types.
324 if (ReturnTy->isSingleValueType() && ReturnTy != llvm::Type::VoidTy) {
325 RetTy = ReturnTy;
326 } else {
327 // For struct returns allocate the space in the caller and pass it up to
328 // the sender.
329 RetTy = llvm::Type::VoidTy;
330 impArgTypes.push_back(llvm::PointerType::getUnqual(ReturnTy));
331 }
332 impArgTypes.push_back(Receiver->getType());
333 impArgTypes.push_back(SelectorTy);
334
335 // Avoid an explicit cast on the IMP by getting a version that has the right
336 // return type.
337 llvm::FunctionType *impType = llvm::FunctionType::get(RetTy, impArgTypes,
338 true);
Chris Lattner0f984262008-03-01 08:50:34 +0000339
340 llvm::Constant *lookupFunction =
341 TheModule.getOrInsertFunction("objc_msg_lookup",
Chris Lattner391d77a2008-03-30 23:03:07 +0000342 llvm::PointerType::getUnqual(impType),
343 Receiver->getType(), SelectorTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000344 llvm::Value *imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000345
346 // Call the method.
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000347 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000348 ActualArgs.push_back(std::make_pair(RValue::get(Receiver),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000349 CGF.getContext().getObjCIdType()));
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000350 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000351 CGF.getContext().getObjCSelType()));
352 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000353 return CGF.EmitCall(imp, ResultType, ActualArgs);
Chris Lattner0f984262008-03-01 08:50:34 +0000354}
355
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000356/// Generates a MethodList. Used in construction of a objc_class and
357/// objc_category structures.
358llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattnera4210072008-06-26 05:08:00 +0000359 const std::string &CategoryName,
360 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000361 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
362 bool isClassMethodList) {
363 // Get the method structure type.
364 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
365 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
366 PtrToInt8Ty, // Method types
367 llvm::PointerType::getUnqual(IMPTy), //Method pointer
368 NULL);
369 std::vector<llvm::Constant*> Methods;
370 std::vector<llvm::Constant*> Elements;
371 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
372 Elements.clear();
Daniel Dunbar61432932008-08-13 23:20:05 +0000373 llvm::Constant *C = CGM.GetAddrOfConstantCString(MethodSels[i].getName());
Chris Lattnera4210072008-06-26 05:08:00 +0000374 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000375 Elements.push_back(
376 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
377 llvm::Constant *Method =
378 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattnera4210072008-06-26 05:08:00 +0000379 MethodSels[i].getName(),
Chris Lattner550b8db2008-06-26 04:05:20 +0000380 isClassMethodList));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000381 Method = llvm::ConstantExpr::getBitCast(Method,
382 llvm::PointerType::getUnqual(IMPTy));
383 Elements.push_back(Method);
384 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
385 }
386
387 // Array of method structures
388 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Chris Lattnera4210072008-06-26 05:08:00 +0000389 MethodSels.size());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000390 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerfba67632008-06-26 04:52:29 +0000391 Methods);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000392
393 // Structure containing list pointer, array and array count
394 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
395 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
396 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
397 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
398 IntTy,
399 ObjCMethodArrayTy,
400 NULL);
401 // Refine next pointer type to concrete type
402 llvm::cast<llvm::OpaqueType>(
403 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
404 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
405
406 Methods.clear();
407 Methods.push_back(llvm::ConstantPointerNull::get(
408 llvm::PointerType::getUnqual(ObjCMethodListTy)));
409 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
410 MethodTypes.size()));
411 Methods.push_back(MethodArray);
412
413 // Create an instance of the structure
414 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
415}
416
417/// Generates an IvarList. Used in construction of a objc_class.
418llvm::Constant *CGObjCGNU::GenerateIvarList(
419 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
420 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
421 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
422 // Get the method structure type.
423 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
424 PtrToInt8Ty,
425 PtrToInt8Ty,
426 IntTy,
427 NULL);
428 std::vector<llvm::Constant*> Ivars;
429 std::vector<llvm::Constant*> Elements;
430 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
431 Elements.clear();
432 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
433 Zeros, 2));
434 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
435 Zeros, 2));
436 Elements.push_back(IvarOffsets[i]);
437 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
438 }
439
440 // Array of method structures
441 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
442 IvarNames.size());
443
444
445 Elements.clear();
446 Elements.push_back(llvm::ConstantInt::get(
447 llvm::cast<llvm::IntegerType>(IntTy), (int)IvarNames.size()));
448 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
449 // Structure containing array and array count
450 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
451 ObjCIvarArrayTy,
452 NULL);
453
454 // Create an instance of the structure
455 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
456}
457
458/// Generate a class structure
459llvm::Constant *CGObjCGNU::GenerateClassStructure(
460 llvm::Constant *MetaClass,
461 llvm::Constant *SuperClass,
462 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000463 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000464 llvm::Constant *Version,
465 llvm::Constant *InstanceSize,
466 llvm::Constant *IVars,
467 llvm::Constant *Methods,
468 llvm::Constant *Protocols) {
469 // Set up the class structure
470 // Note: Several of these are char*s when they should be ids. This is
471 // because the runtime performs this translation on load.
472 llvm::StructType *ClassTy = llvm::StructType::get(
473 PtrToInt8Ty, // class_pointer
474 PtrToInt8Ty, // super_class
475 PtrToInt8Ty, // name
476 LongTy, // version
477 LongTy, // info
478 LongTy, // instance_size
479 IVars->getType(), // ivars
480 Methods->getType(), // methods
481 // These are all filled in by the runtime, so we pretend
482 PtrTy, // dtable
483 PtrTy, // subclass_list
484 PtrTy, // sibling_class
485 PtrTy, // protocols
486 PtrTy, // gc_object_type
487 NULL);
488 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
489 llvm::Constant *NullP =
490 llvm::ConstantPointerNull::get(llvm::cast<llvm::PointerType>(PtrTy));
491 // Fill in the structure
492 std::vector<llvm::Constant*> Elements;
493 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
494 Elements.push_back(SuperClass);
Chris Lattnerd002cc62008-06-26 04:47:04 +0000495 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000496 Elements.push_back(Zero);
497 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
498 Elements.push_back(InstanceSize);
499 Elements.push_back(IVars);
500 Elements.push_back(Methods);
501 Elements.push_back(NullP);
502 Elements.push_back(NullP);
503 Elements.push_back(NullP);
504 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
505 Elements.push_back(NullP);
506 // Create an instance of the structure
Chris Lattner1565e032008-07-21 06:31:05 +0000507 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000508}
509
510llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
511 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
512 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
513 // Get the method structure type.
514 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
515 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
516 PtrToInt8Ty,
517 NULL);
518 std::vector<llvm::Constant*> Methods;
519 std::vector<llvm::Constant*> Elements;
520 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
521 Elements.clear();
522 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
523 Zeros, 2));
524 Elements.push_back(
525 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
526 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
527 }
528 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
529 MethodNames.size());
530 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
531 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
532 IntTy, ObjCMethodArrayTy, NULL);
533 Methods.clear();
534 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
535 Methods.push_back(Array);
536 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
537}
538// Create the protocol list structure used in classes, categories and so on
539llvm::Constant *CGObjCGNU::GenerateProtocolList(
540 const llvm::SmallVectorImpl<std::string> &Protocols) {
541 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
542 Protocols.size());
543 llvm::StructType *ProtocolListTy = llvm::StructType::get(
544 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
545 LongTy,//FIXME: Should be size_t
546 ProtocolArrayTy,
547 NULL);
548 std::vector<llvm::Constant*> Elements;
549 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
550 iter != endIter ; iter++) {
551 llvm::Constant *Ptr =
552 llvm::ConstantExpr::getBitCast(ExistingProtocols[*iter], PtrToInt8Ty);
553 Elements.push_back(Ptr);
554 }
555 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
556 Elements);
557 Elements.clear();
558 Elements.push_back(NULLPtr);
559 Elements.push_back(llvm::ConstantInt::get(
560 llvm::cast<llvm::IntegerType>(LongTy), Protocols.size()));
561 Elements.push_back(ProtocolArray);
562 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
563}
564
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000565llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000566 const ObjCProtocolDecl *PD) {
567 return ExistingProtocols[PD->getName()];
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000568}
569
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000570void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
571 ASTContext &Context = CGM.getContext();
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000572 const char *ProtocolName = PD->getIdentifierName();
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000573 llvm::SmallVector<std::string, 16> Protocols;
574 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
575 E = PD->protocol_end(); PI != E; ++PI)
576 Protocols.push_back((*PI)->getName());
577 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
578 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
579 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
580 E = PD->instmeth_end(); iter != E; iter++) {
581 std::string TypeStr;
582 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
583 InstanceMethodNames.push_back(
Daniel Dunbar61432932008-08-13 23:20:05 +0000584 CGM.GetAddrOfConstantCString((*iter)->getSelector().getName()));
585 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000586 }
587 // Collect information about class methods:
588 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
589 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
590 for (ObjCProtocolDecl::classmeth_iterator iter = PD->classmeth_begin(),
591 endIter = PD->classmeth_end() ; iter != endIter ; iter++) {
592 std::string TypeStr;
593 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
594 ClassMethodNames.push_back(
Daniel Dunbar61432932008-08-13 23:20:05 +0000595 CGM.GetAddrOfConstantCString((*iter)->getSelector().getName()));
596 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000597 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000598
599 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
600 llvm::Constant *InstanceMethodList =
601 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
602 llvm::Constant *ClassMethodList =
603 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
604 // Protocols are objects containing lists of the methods implemented and
605 // protocols adopted.
606 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
607 PtrToInt8Ty,
608 ProtocolList->getType(),
609 InstanceMethodList->getType(),
610 ClassMethodList->getType(),
611 NULL);
612 std::vector<llvm::Constant*> Elements;
613 // The isa pointer must be set to a magic number so the runtime knows it's
614 // the correct layout.
615 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
616 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
617 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
618 Elements.push_back(ProtocolList);
619 Elements.push_back(InstanceMethodList);
620 Elements.push_back(ClassMethodList);
621 ExistingProtocols[ProtocolName] =
622 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
623 ".objc_protocol"), IdTy);
624}
625
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000626void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000627 const char *ClassName = OCD->getClassInterface()->getIdentifierName();
628 const char *CategoryName = OCD->getIdentifierName();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000629 // Collect information about instance methods
630 llvm::SmallVector<Selector, 16> InstanceMethodSels;
631 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
632 for (ObjCCategoryDecl::instmeth_iterator iter = OCD->instmeth_begin(),
633 endIter = OCD->instmeth_end() ; iter != endIter ; iter++) {
634 InstanceMethodSels.push_back((*iter)->getSelector());
635 std::string TypeStr;
636 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
637 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
638 }
639
640 // Collect information about class methods
641 llvm::SmallVector<Selector, 16> ClassMethodSels;
642 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
643 for (ObjCCategoryDecl::classmeth_iterator iter = OCD->classmeth_begin(),
644 endIter = OCD->classmeth_end() ; iter != endIter ; iter++) {
645 ClassMethodSels.push_back((*iter)->getSelector());
646 std::string TypeStr;
647 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
648 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
649 }
650
651 // Collect the names of referenced protocols
652 llvm::SmallVector<std::string, 16> Protocols;
653 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
654 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
655 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
656 E = Protos.end(); I != E; ++I)
657 Protocols.push_back((*I)->getName());
658
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000659 std::vector<llvm::Constant*> Elements;
660 Elements.push_back(MakeConstantString(CategoryName));
661 Elements.push_back(MakeConstantString(ClassName));
662 // Instance method list
663 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000664 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000665 false), PtrTy));
666 // Class method list
667 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000668 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000669 PtrTy));
670 // Protocol list
671 Elements.push_back(llvm::ConstantExpr::getBitCast(
672 GenerateProtocolList(Protocols), PtrTy));
673 Categories.push_back(llvm::ConstantExpr::getBitCast(
674 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
675 PtrTy, PtrTy, NULL), Elements), PtrTy));
676}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000677
678void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
679 ASTContext &Context = CGM.getContext();
680
681 // Get the superclass name.
682 const ObjCInterfaceDecl * SuperClassDecl =
683 OID->getClassInterface()->getSuperClass();
684 const char * SuperClassName = NULL;
685 if (SuperClassDecl) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000686 SuperClassName = SuperClassDecl->getIdentifierName();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000687 }
688
689 // Get the class name
690 ObjCInterfaceDecl * ClassDecl = (ObjCInterfaceDecl*)OID->getClassInterface();
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000691 const char * ClassName = ClassDecl->getIdentifierName();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000692
693 // Get the size of instances. For runtimes that support late-bound instances
694 // this should probably be something different (size just of instance
695 // varaibles in this class, not superclasses?).
696 int instanceSize = 0;
697 const llvm::Type *ObjTy = 0;
698 if (!LateBoundIVars()) {
699 ObjTy = CGM.getTypes().ConvertType(Context.getObjCInterfaceType(ClassDecl));
700 instanceSize = CGM.getTargetData().getABITypeSize(ObjTy);
701 } else {
702 // This is required by newer ObjC runtimes.
703 assert(0 && "Late-bound instance variables not yet supported");
704 }
705
706 // Collect information about instance variables.
707 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
708 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
709 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
710 const llvm::StructLayout *Layout =
711 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(ObjTy));
712 ObjTy = llvm::PointerType::getUnqual(ObjTy);
713 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
714 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
715 // Store the name
716 IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter)->getName()));
717 // Get the type encoding for this ivar
718 std::string TypeStr;
Daniel Dunbar0d504c12008-10-17 20:21:44 +0000719 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000720 IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
721 // Get the offset
722 int offset =
723 (int)Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(*iter));
724 IvarOffsets.push_back(
725 llvm::ConstantInt::get(llvm::Type::Int32Ty, offset));
726 }
727
728 // Collect information about instance methods
729 llvm::SmallVector<Selector, 16> InstanceMethodSels;
730 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
731 for (ObjCImplementationDecl::instmeth_iterator iter = OID->instmeth_begin(),
732 endIter = OID->instmeth_end() ; iter != endIter ; iter++) {
733 InstanceMethodSels.push_back((*iter)->getSelector());
734 std::string TypeStr;
735 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
736 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
737 }
738
739 // Collect information about class methods
740 llvm::SmallVector<Selector, 16> ClassMethodSels;
741 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
742 for (ObjCImplementationDecl::classmeth_iterator iter = OID->classmeth_begin(),
743 endIter = OID->classmeth_end() ; iter != endIter ; iter++) {
744 ClassMethodSels.push_back((*iter)->getSelector());
745 std::string TypeStr;
746 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
747 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
748 }
749 // Collect the names of referenced protocols
750 llvm::SmallVector<std::string, 16> Protocols;
751 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
752 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
753 E = Protos.end(); I != E; ++I)
754 Protocols.push_back((*I)->getName());
755
756
757
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000758 // Get the superclass pointer.
759 llvm::Constant *SuperClass;
760 if (SuperClassName) {
761 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
762 } else {
763 SuperClass = llvm::ConstantPointerNull::get(
764 llvm::cast<llvm::PointerType>(PtrToInt8Ty));
765 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000766 // Empty vector used to construct empty method lists
767 llvm::SmallVector<llvm::Constant*, 1> empty;
768 // Generate the method and instance variable lists
769 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000770 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000771 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000772 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000773 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
774 IvarOffsets);
775 //Generate metaclass for class methods
776 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner1565e032008-07-21 06:31:05 +0000777 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000778 empty, empty, empty), ClassMethodList, NULLPtr);
779 // Generate the class structure
780 llvm::Constant *ClassStruct = GenerateClassStructure(MetaClassStruct,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000781 SuperClass, 0x1L, ClassName, 0,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000782 llvm::ConstantInt::get(llvm::Type::Int32Ty, instanceSize), IvarList,
783 MethodList, GenerateProtocolList(Protocols));
784 // Add class structure to list to be added to the symtab later
785 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
786 Classes.push_back(ClassStruct);
787}
788
789llvm::Function *CGObjCGNU::ModuleInitFunction() {
790 // Only emit an ObjC load function if no Objective-C stuff has been called
791 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
792 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov5f58b912008-06-01 15:14:46 +0000793 UntypedSelectors.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000794 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +0000795
796 // Name the ObjC types to make the IR a bit easier to read
797 TheModule.addTypeName(".objc_selector", SelectorTy);
798 TheModule.addTypeName(".objc_id", IdTy);
799 TheModule.addTypeName(".objc_imp", IMPTy);
800
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000801 std::vector<llvm::Constant*> Elements;
802 // Generate statics list:
803 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
804 ConstantStrings.size() + 1);
805 ConstantStrings.push_back(NULLPtr);
806 Elements.push_back(MakeConstantString("NSConstantString",
807 ".objc_static_class_name"));
808 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings));
809 llvm::StructType *StaticsListTy =
810 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
Chris Lattner630404b2008-06-26 04:10:42 +0000811 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000812 llvm::Constant *Statics =
813 MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Chris Lattner4e0b2642008-06-24 17:01:28 +0000814 llvm::ArrayType *StaticsListArrayTy =
Chris Lattner630404b2008-06-26 04:10:42 +0000815 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner4e0b2642008-06-24 17:01:28 +0000816 Elements.clear();
817 Elements.push_back(Statics);
Chris Lattner630404b2008-06-26 04:10:42 +0000818 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner4e0b2642008-06-24 17:01:28 +0000819 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000820 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
821 // Array of classes, categories, and constant objects
822 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
823 Classes.size() + Categories.size() + 2);
Chris Lattner630404b2008-06-26 04:10:42 +0000824 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelectorTy,
825 llvm::Type::Int16Ty,
826 llvm::Type::Int16Ty,
827 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000828
829 Elements.clear();
830 // Pointer to an array of selectors used in this module.
831 std::vector<llvm::Constant*> Selectors;
832 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
833 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
834 iter != iterEnd ; ++iter) {
Chris Lattner630404b2008-06-26 04:10:42 +0000835 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
836 Elements.push_back(MakeConstantString(iter->first.second,
837 ".objc_sel_types"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000838 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
839 Elements.clear();
840 }
841 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
842 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner630404b2008-06-26 04:10:42 +0000843 iter != iterEnd; ++iter) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000844 Elements.push_back(
Chris Lattner630404b2008-06-26 04:10:42 +0000845 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000846 Elements.push_back(NULLPtr);
847 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
848 Elements.clear();
849 }
850 Elements.push_back(NULLPtr);
851 Elements.push_back(NULLPtr);
852 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
853 Elements.clear();
854 // Number of static selectors
855 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
856 llvm::Constant *SelectorList = MakeGlobal(
857 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
858 ".objc_selector_list");
859 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList, SelectorTy));
860
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]};
868 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
869 llvm::GlobalValue::InternalLinkage,
870 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
871 ".objc_sel_ptr", &TheModule);
872 (*iter).second->setAliasee(SelPtr);
873 }
874 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
875 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
876 iter != iterEnd; iter++) {
877 llvm::Constant *Idxs[] = {Zeros[0],
878 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
879 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
880 llvm::GlobalValue::InternalLinkage,
881 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
882 ".objc_sel_ptr", &TheModule);
883 (*iter).second->setAliasee(SelPtr);
884 }
885 // Number of classes defined.
886 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
887 Classes.size()));
888 // Number of categories defined
889 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
890 Categories.size()));
891 // Create an array of classes, then categories, then static object instances
892 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
893 // NULL-terminated list of static object instances (mainly constant strings)
894 Classes.push_back(Statics);
895 Classes.push_back(NULLPtr);
896 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
897 Elements.push_back(ClassList);
898 // Construct the symbol table
899 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
900
901 // The symbol table is contained in a module which has some version-checking
902 // constants
903 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
904 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
905 Elements.clear();
906 // Runtime version used for compatibility checking.
907 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
908 //FIXME: Should be sizeof(ModuleTy)
909 Elements.push_back(llvm::ConstantInt::get(LongTy, 16));
910 //FIXME: Should be the path to the file where this module was declared
911 Elements.push_back(NULLPtr);
912 Elements.push_back(SymTab);
913 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
914
915 // Create the load function calling the runtime entry point with the module
916 // structure
917 std::vector<const llvm::Type*> VoidArgs;
918 llvm::Function * LoadFunction = llvm::Function::Create(
919 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
920 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
921 &TheModule);
922 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000923 CGBuilderTy Builder;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000924 Builder.SetInsertPoint(EntryBB);
925 llvm::Value *Register = TheModule.getOrInsertFunction("__objc_exec_class",
926 llvm::Type::VoidTy, llvm::PointerType::getUnqual(ModuleTy), NULL);
927 Builder.CreateCall(Register, Module);
928 Builder.CreateRetVoid();
929 return LoadFunction;
930}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000931
932llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000933 const ObjCCategoryImplDecl *OCD =
934 dyn_cast<ObjCCategoryImplDecl>(OMD->getMethodContext());
935 const std::string &CategoryName = OCD ? OCD->getName() : "";
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000936 const std::string &ClassName = OMD->getClassInterface()->getName();
937 const std::string &MethodName = OMD->getSelector().getName();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000938 bool isClassMethod = !OMD->isInstance();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000939
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000940 const llvm::FunctionType *MethodTy =
941 CGM.getTypes().GetFunctionType(CGFunctionInfo(OMD, CGM.getContext()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000942 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
943 MethodName, isClassMethod);
944
Gabor Greif984d0b42008-04-06 20:42:52 +0000945 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000946 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000947 FunctionName,
Chris Lattner391d77a2008-03-30 23:03:07 +0000948 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +0000949 return Method;
950}
951
Daniel Dunbar49f66022008-09-24 03:38:44 +0000952llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
953 return 0;
954}
955
956llvm::Function *CGObjCGNU::GetPropertySetFunction() {
957 return 0;
958}
959
960llvm::Function *CGObjCGNU::EnumerationMutationFunction() {
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000961 return 0;
962}
963
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000964void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
965 const Stmt &S) {
966 CGF.ErrorUnsupported(&S, "@try/@synchronized statement");
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000967}
968
969void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +0000970 const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000971 CGF.ErrorUnsupported(&S, "@throw statement");
972}
973
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000974llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000975 llvm::Value *AddrWeakObj)
976{
977 return 0;
978}
979
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000980void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
981 llvm::Value *src, llvm::Value *dst)
982{
983 return;
984}
985
Fariborz Jahanian58626502008-11-19 00:59:10 +0000986void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
987 llvm::Value *src, llvm::Value *dst)
988{
989 return;
990}
991
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000992void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
993 llvm::Value *src, llvm::Value *dst)
994{
995 return;
996}
997
Fariborz Jahanian58626502008-11-19 00:59:10 +0000998void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
999 llvm::Value *src, llvm::Value *dst)
1000{
1001 return;
1002}
1003
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001004CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
Chris Lattnerdce14062008-06-26 04:19:03 +00001005 return new CGObjCGNU(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +00001006}