blob: d57710c5e5199bac3967e7c419dcad0b1322b205 [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
127 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
128 const ObjCAtTryStmt &S);
129 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
130 const ObjCAtThrowStmt &S);
Chris Lattner10cac6f2008-11-15 21:26:17 +0000131 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
132 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000133 virtual llvm::Value * EmitObjCWeakCall(CodeGen::CodeGenFunction &CGF,
134 llvm::Value *AddrWeakObj);
Chris Lattner0f984262008-03-01 08:50:34 +0000135};
136} // end anonymous namespace
137
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000138
139
140static std::string SymbolNameForClass(const std::string &ClassName) {
141 return ".objc_class_" + ClassName;
142}
143
144static std::string SymbolNameForMethod(const std::string &ClassName, const
145 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
146{
147 return "._objc_method_" + ClassName +"("+CategoryName+")"+
148 (isClassMethod ? "+" : "-") + MethodName;
149}
150
Chris Lattnerdce14062008-06-26 04:19:03 +0000151CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
152 : CGM(cgm), TheModule(CGM.getModule()) {
153 IntTy = CGM.getTypes().ConvertType(CGM.getContext().IntTy);
154 LongTy = CGM.getTypes().ConvertType(CGM.getContext().LongTy);
155
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000156 Zeros[0] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
157 Zeros[1] = Zeros[0];
158 NULLPtr = llvm::ConstantPointerNull::get(
159 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
Chris Lattner391d77a2008-03-30 23:03:07 +0000160 // C string type. Used in lots of places.
161 PtrToInt8Ty =
162 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
163 // Get the selector Type.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000164 SelStructTy = llvm::StructType::get(
Chris Lattner391d77a2008-03-30 23:03:07 +0000165 PtrToInt8Ty,
166 PtrToInt8Ty,
167 NULL);
168 SelectorTy = llvm::PointerType::getUnqual(SelStructTy);
169 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
170 PtrTy = PtrToInt8Ty;
171
172 // Object type
173 llvm::PATypeHolder OpaqueObjTy = llvm::OpaqueType::get();
174 llvm::Type *OpaqueIdTy = llvm::PointerType::getUnqual(OpaqueObjTy);
175 IdTy = llvm::StructType::get(OpaqueIdTy, NULL);
176 llvm::cast<llvm::OpaqueType>(OpaqueObjTy.get())->refineAbstractTypeTo(IdTy);
177 IdTy = llvm::cast<llvm::StructType>(OpaqueObjTy.get());
178 IdTy = llvm::PointerType::getUnqual(IdTy);
179
180 // IMP type
181 std::vector<const llvm::Type*> IMPArgs;
182 IMPArgs.push_back(IdTy);
183 IMPArgs.push_back(SelectorTy);
184 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000185}
186// This has to perform the lookup every time, since posing and related
187// techniques can modify the name -> class mapping.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000188llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000189 const ObjCInterfaceDecl *OID) {
190 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getName());
191 ClassName = Builder.CreateStructGEP(ClassName, 0);
192
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000193 llvm::Constant *ClassLookupFn =
194 TheModule.getOrInsertFunction("objc_lookup_class", IdTy, PtrToInt8Ty,
195 NULL);
196 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner391d77a2008-03-30 23:03:07 +0000197}
198
Chris Lattner8e67b632008-06-26 04:37:12 +0000199/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000200llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Chris Lattner8e67b632008-06-26 04:37:12 +0000201 // FIXME: uniquing on the string is wasteful, unique on Sel instead!
202 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getName()];
203 if (US == 0)
204 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
205 llvm::GlobalValue::InternalLinkage,
206 ".objc_untyped_selector_alias",
207 NULL, &TheModule);
208
209 return Builder.CreateLoad(US);
210
211}
212
Chris Lattner5e7dcc62008-06-26 04:44:19 +0000213llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
214 const std::string &Name) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000215 llvm::Constant * ConstStr = llvm::ConstantArray::get(Str);
216 ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true,
217 llvm::GlobalValue::InternalLinkage,
218 ConstStr, Name, &TheModule);
219 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
220}
221llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
222 std::vector<llvm::Constant*> &V, const std::string &Name) {
223 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
224 return new llvm::GlobalVariable(Ty, false,
225 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
226}
227llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
228 std::vector<llvm::Constant*> &V, const std::string &Name) {
229 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
230 return new llvm::GlobalVariable(Ty, false,
231 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
232}
233
234/// Generate an NSConstantString object.
235//TODO: In case there are any crazy people still using the GNU runtime without
236//an OpenStep implementation, this should let them select their own class for
237//constant strings.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000238llvm::Constant *CGObjCGNU::GenerateConstantString(const std::string &Str) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000239 std::vector<llvm::Constant*> Ivars;
240 Ivars.push_back(NULLPtr);
Chris Lattner13fd7e52008-06-21 21:44:18 +0000241 Ivars.push_back(MakeConstantString(Str));
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000242 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000243 llvm::Constant *ObjCStr = MakeGlobal(
244 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
245 Ivars, ".objc_str");
246 ConstantStrings.push_back(
247 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
248 return ObjCStr;
249}
250
251///Generates a message send where the super is the receiver. This is a message
252///send to self with special delivery semantics indicating which class's method
253///should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000254CodeGen::RValue
255CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000256 QualType ResultType,
257 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000258 const ObjCInterfaceDecl *Class,
259 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000260 bool IsClassMessage,
261 const CallArgList &CallArgs) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000262 const ObjCInterfaceDecl *SuperClass = Class->getSuperClass();
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000263 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000264 // TODO: This should be cached, not looked up every time.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000265 llvm::Value *ReceiverClass = GetClass(CGF.Builder, SuperClass);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000266 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Chris Lattner0f984262008-03-01 08:50:34 +0000267 std::vector<const llvm::Type*> impArgTypes;
268 impArgTypes.push_back(Receiver->getType());
Chris Lattner391d77a2008-03-30 23:03:07 +0000269 impArgTypes.push_back(SelectorTy);
Chris Lattner0f984262008-03-01 08:50:34 +0000270
271 // Avoid an explicit cast on the IMP by getting a version that has the right
272 // return type.
273 llvm::FunctionType *impType = llvm::FunctionType::get(ReturnTy, impArgTypes,
274 true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000275 // Construct the structure used to look up the IMP
276 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
277 IdTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000278 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Eli Friedman1e692ac2008-06-13 23:01:12 +0000279 // FIXME: volatility
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000280 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
281 CGF.Builder.CreateStore(ReceiverClass, CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000282
283 // Get the IMP
284 llvm::Constant *lookupFunction =
285 TheModule.getOrInsertFunction("objc_msg_lookup_super",
286 llvm::PointerType::getUnqual(impType),
287 llvm::PointerType::getUnqual(ObjCSuperTy),
288 SelectorTy, NULL);
289 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000290 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000291 lookupArgs+2);
292
293 // Call the method
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000294 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000295 ActualArgs.push_back(std::make_pair(RValue::get(Receiver),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000296 CGF.getContext().getObjCIdType()));
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000297 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000298 CGF.getContext().getObjCSelType()));
299 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000300 return CGF.EmitCall(imp, ResultType, ActualArgs);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000301}
302
303/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000304CodeGen::RValue
305CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000306 QualType ResultType,
307 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000308 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000309 bool IsClassMessage,
310 const CallArgList &CallArgs) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000311 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
312 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000313
314 // Look up the method implementation.
315 std::vector<const llvm::Type*> impArgTypes;
316 const llvm::Type *RetTy;
317 //TODO: Revisit this when LLVM supports aggregate return types.
318 if (ReturnTy->isSingleValueType() && ReturnTy != llvm::Type::VoidTy) {
319 RetTy = ReturnTy;
320 } else {
321 // For struct returns allocate the space in the caller and pass it up to
322 // the sender.
323 RetTy = llvm::Type::VoidTy;
324 impArgTypes.push_back(llvm::PointerType::getUnqual(ReturnTy));
325 }
326 impArgTypes.push_back(Receiver->getType());
327 impArgTypes.push_back(SelectorTy);
328
329 // Avoid an explicit cast on the IMP by getting a version that has the right
330 // return type.
331 llvm::FunctionType *impType = llvm::FunctionType::get(RetTy, impArgTypes,
332 true);
Chris Lattner0f984262008-03-01 08:50:34 +0000333
334 llvm::Constant *lookupFunction =
335 TheModule.getOrInsertFunction("objc_msg_lookup",
Chris Lattner391d77a2008-03-30 23:03:07 +0000336 llvm::PointerType::getUnqual(impType),
337 Receiver->getType(), SelectorTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000338 llvm::Value *imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000339
340 // Call the method.
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000341 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000342 ActualArgs.push_back(std::make_pair(RValue::get(Receiver),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000343 CGF.getContext().getObjCIdType()));
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000344 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000345 CGF.getContext().getObjCSelType()));
346 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000347 return CGF.EmitCall(imp, ResultType, ActualArgs);
Chris Lattner0f984262008-03-01 08:50:34 +0000348}
349
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000350/// Generates a MethodList. Used in construction of a objc_class and
351/// objc_category structures.
352llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattnera4210072008-06-26 05:08:00 +0000353 const std::string &CategoryName,
354 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000355 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
356 bool isClassMethodList) {
357 // Get the method structure type.
358 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
359 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
360 PtrToInt8Ty, // Method types
361 llvm::PointerType::getUnqual(IMPTy), //Method pointer
362 NULL);
363 std::vector<llvm::Constant*> Methods;
364 std::vector<llvm::Constant*> Elements;
365 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
366 Elements.clear();
Daniel Dunbar61432932008-08-13 23:20:05 +0000367 llvm::Constant *C = CGM.GetAddrOfConstantCString(MethodSels[i].getName());
Chris Lattnera4210072008-06-26 05:08:00 +0000368 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000369 Elements.push_back(
370 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
371 llvm::Constant *Method =
372 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattnera4210072008-06-26 05:08:00 +0000373 MethodSels[i].getName(),
Chris Lattner550b8db2008-06-26 04:05:20 +0000374 isClassMethodList));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000375 Method = llvm::ConstantExpr::getBitCast(Method,
376 llvm::PointerType::getUnqual(IMPTy));
377 Elements.push_back(Method);
378 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
379 }
380
381 // Array of method structures
382 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Chris Lattnera4210072008-06-26 05:08:00 +0000383 MethodSels.size());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000384 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerfba67632008-06-26 04:52:29 +0000385 Methods);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000386
387 // Structure containing list pointer, array and array count
388 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
389 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
390 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
391 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
392 IntTy,
393 ObjCMethodArrayTy,
394 NULL);
395 // Refine next pointer type to concrete type
396 llvm::cast<llvm::OpaqueType>(
397 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
398 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
399
400 Methods.clear();
401 Methods.push_back(llvm::ConstantPointerNull::get(
402 llvm::PointerType::getUnqual(ObjCMethodListTy)));
403 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
404 MethodTypes.size()));
405 Methods.push_back(MethodArray);
406
407 // Create an instance of the structure
408 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
409}
410
411/// Generates an IvarList. Used in construction of a objc_class.
412llvm::Constant *CGObjCGNU::GenerateIvarList(
413 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
414 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
415 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
416 // Get the method structure type.
417 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
418 PtrToInt8Ty,
419 PtrToInt8Ty,
420 IntTy,
421 NULL);
422 std::vector<llvm::Constant*> Ivars;
423 std::vector<llvm::Constant*> Elements;
424 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
425 Elements.clear();
426 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
427 Zeros, 2));
428 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
429 Zeros, 2));
430 Elements.push_back(IvarOffsets[i]);
431 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
432 }
433
434 // Array of method structures
435 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
436 IvarNames.size());
437
438
439 Elements.clear();
440 Elements.push_back(llvm::ConstantInt::get(
441 llvm::cast<llvm::IntegerType>(IntTy), (int)IvarNames.size()));
442 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
443 // Structure containing array and array count
444 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
445 ObjCIvarArrayTy,
446 NULL);
447
448 // Create an instance of the structure
449 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
450}
451
452/// Generate a class structure
453llvm::Constant *CGObjCGNU::GenerateClassStructure(
454 llvm::Constant *MetaClass,
455 llvm::Constant *SuperClass,
456 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000457 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000458 llvm::Constant *Version,
459 llvm::Constant *InstanceSize,
460 llvm::Constant *IVars,
461 llvm::Constant *Methods,
462 llvm::Constant *Protocols) {
463 // Set up the class structure
464 // Note: Several of these are char*s when they should be ids. This is
465 // because the runtime performs this translation on load.
466 llvm::StructType *ClassTy = llvm::StructType::get(
467 PtrToInt8Ty, // class_pointer
468 PtrToInt8Ty, // super_class
469 PtrToInt8Ty, // name
470 LongTy, // version
471 LongTy, // info
472 LongTy, // instance_size
473 IVars->getType(), // ivars
474 Methods->getType(), // methods
475 // These are all filled in by the runtime, so we pretend
476 PtrTy, // dtable
477 PtrTy, // subclass_list
478 PtrTy, // sibling_class
479 PtrTy, // protocols
480 PtrTy, // gc_object_type
481 NULL);
482 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
483 llvm::Constant *NullP =
484 llvm::ConstantPointerNull::get(llvm::cast<llvm::PointerType>(PtrTy));
485 // Fill in the structure
486 std::vector<llvm::Constant*> Elements;
487 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
488 Elements.push_back(SuperClass);
Chris Lattnerd002cc62008-06-26 04:47:04 +0000489 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000490 Elements.push_back(Zero);
491 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
492 Elements.push_back(InstanceSize);
493 Elements.push_back(IVars);
494 Elements.push_back(Methods);
495 Elements.push_back(NullP);
496 Elements.push_back(NullP);
497 Elements.push_back(NullP);
498 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
499 Elements.push_back(NullP);
500 // Create an instance of the structure
Chris Lattner1565e032008-07-21 06:31:05 +0000501 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000502}
503
504llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
505 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
506 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
507 // Get the method structure type.
508 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
509 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
510 PtrToInt8Ty,
511 NULL);
512 std::vector<llvm::Constant*> Methods;
513 std::vector<llvm::Constant*> Elements;
514 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
515 Elements.clear();
516 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
517 Zeros, 2));
518 Elements.push_back(
519 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
520 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
521 }
522 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
523 MethodNames.size());
524 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
525 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
526 IntTy, ObjCMethodArrayTy, NULL);
527 Methods.clear();
528 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
529 Methods.push_back(Array);
530 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
531}
532// Create the protocol list structure used in classes, categories and so on
533llvm::Constant *CGObjCGNU::GenerateProtocolList(
534 const llvm::SmallVectorImpl<std::string> &Protocols) {
535 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
536 Protocols.size());
537 llvm::StructType *ProtocolListTy = llvm::StructType::get(
538 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
539 LongTy,//FIXME: Should be size_t
540 ProtocolArrayTy,
541 NULL);
542 std::vector<llvm::Constant*> Elements;
543 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
544 iter != endIter ; iter++) {
545 llvm::Constant *Ptr =
546 llvm::ConstantExpr::getBitCast(ExistingProtocols[*iter], PtrToInt8Ty);
547 Elements.push_back(Ptr);
548 }
549 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
550 Elements);
551 Elements.clear();
552 Elements.push_back(NULLPtr);
553 Elements.push_back(llvm::ConstantInt::get(
554 llvm::cast<llvm::IntegerType>(LongTy), Protocols.size()));
555 Elements.push_back(ProtocolArray);
556 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
557}
558
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000559llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000560 const ObjCProtocolDecl *PD) {
561 return ExistingProtocols[PD->getName()];
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000562}
563
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000564void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
565 ASTContext &Context = CGM.getContext();
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000566 const char *ProtocolName = PD->getIdentifierName();
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000567 llvm::SmallVector<std::string, 16> Protocols;
568 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
569 E = PD->protocol_end(); PI != E; ++PI)
570 Protocols.push_back((*PI)->getName());
571 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
572 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
573 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
574 E = PD->instmeth_end(); iter != E; iter++) {
575 std::string TypeStr;
576 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
577 InstanceMethodNames.push_back(
Daniel Dunbar61432932008-08-13 23:20:05 +0000578 CGM.GetAddrOfConstantCString((*iter)->getSelector().getName()));
579 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000580 }
581 // Collect information about class methods:
582 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
583 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
584 for (ObjCProtocolDecl::classmeth_iterator iter = PD->classmeth_begin(),
585 endIter = PD->classmeth_end() ; iter != endIter ; iter++) {
586 std::string TypeStr;
587 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
588 ClassMethodNames.push_back(
Daniel Dunbar61432932008-08-13 23:20:05 +0000589 CGM.GetAddrOfConstantCString((*iter)->getSelector().getName()));
590 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000591 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000592
593 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
594 llvm::Constant *InstanceMethodList =
595 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
596 llvm::Constant *ClassMethodList =
597 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
598 // Protocols are objects containing lists of the methods implemented and
599 // protocols adopted.
600 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
601 PtrToInt8Ty,
602 ProtocolList->getType(),
603 InstanceMethodList->getType(),
604 ClassMethodList->getType(),
605 NULL);
606 std::vector<llvm::Constant*> Elements;
607 // The isa pointer must be set to a magic number so the runtime knows it's
608 // the correct layout.
609 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
610 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
611 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
612 Elements.push_back(ProtocolList);
613 Elements.push_back(InstanceMethodList);
614 Elements.push_back(ClassMethodList);
615 ExistingProtocols[ProtocolName] =
616 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
617 ".objc_protocol"), IdTy);
618}
619
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000620void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000621 const char *ClassName = OCD->getClassInterface()->getIdentifierName();
622 const char *CategoryName = OCD->getIdentifierName();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000623 // Collect information about instance methods
624 llvm::SmallVector<Selector, 16> InstanceMethodSels;
625 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
626 for (ObjCCategoryDecl::instmeth_iterator iter = OCD->instmeth_begin(),
627 endIter = OCD->instmeth_end() ; iter != endIter ; iter++) {
628 InstanceMethodSels.push_back((*iter)->getSelector());
629 std::string TypeStr;
630 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
631 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
632 }
633
634 // Collect information about class methods
635 llvm::SmallVector<Selector, 16> ClassMethodSels;
636 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
637 for (ObjCCategoryDecl::classmeth_iterator iter = OCD->classmeth_begin(),
638 endIter = OCD->classmeth_end() ; iter != endIter ; iter++) {
639 ClassMethodSels.push_back((*iter)->getSelector());
640 std::string TypeStr;
641 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
642 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
643 }
644
645 // Collect the names of referenced protocols
646 llvm::SmallVector<std::string, 16> Protocols;
647 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
648 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
649 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
650 E = Protos.end(); I != E; ++I)
651 Protocols.push_back((*I)->getName());
652
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000653 std::vector<llvm::Constant*> Elements;
654 Elements.push_back(MakeConstantString(CategoryName));
655 Elements.push_back(MakeConstantString(ClassName));
656 // Instance method list
657 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000658 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000659 false), PtrTy));
660 // Class method list
661 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000662 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000663 PtrTy));
664 // Protocol list
665 Elements.push_back(llvm::ConstantExpr::getBitCast(
666 GenerateProtocolList(Protocols), PtrTy));
667 Categories.push_back(llvm::ConstantExpr::getBitCast(
668 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
669 PtrTy, PtrTy, NULL), Elements), PtrTy));
670}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000671
672void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
673 ASTContext &Context = CGM.getContext();
674
675 // Get the superclass name.
676 const ObjCInterfaceDecl * SuperClassDecl =
677 OID->getClassInterface()->getSuperClass();
678 const char * SuperClassName = NULL;
679 if (SuperClassDecl) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000680 SuperClassName = SuperClassDecl->getIdentifierName();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000681 }
682
683 // Get the class name
684 ObjCInterfaceDecl * ClassDecl = (ObjCInterfaceDecl*)OID->getClassInterface();
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000685 const char * ClassName = ClassDecl->getIdentifierName();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000686
687 // Get the size of instances. For runtimes that support late-bound instances
688 // this should probably be something different (size just of instance
689 // varaibles in this class, not superclasses?).
690 int instanceSize = 0;
691 const llvm::Type *ObjTy = 0;
692 if (!LateBoundIVars()) {
693 ObjTy = CGM.getTypes().ConvertType(Context.getObjCInterfaceType(ClassDecl));
694 instanceSize = CGM.getTargetData().getABITypeSize(ObjTy);
695 } else {
696 // This is required by newer ObjC runtimes.
697 assert(0 && "Late-bound instance variables not yet supported");
698 }
699
700 // Collect information about instance variables.
701 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
702 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
703 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
704 const llvm::StructLayout *Layout =
705 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(ObjTy));
706 ObjTy = llvm::PointerType::getUnqual(ObjTy);
707 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
708 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
709 // Store the name
710 IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter)->getName()));
711 // Get the type encoding for this ivar
712 std::string TypeStr;
Daniel Dunbar0d504c12008-10-17 20:21:44 +0000713 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000714 IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
715 // Get the offset
716 int offset =
717 (int)Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(*iter));
718 IvarOffsets.push_back(
719 llvm::ConstantInt::get(llvm::Type::Int32Ty, offset));
720 }
721
722 // Collect information about instance methods
723 llvm::SmallVector<Selector, 16> InstanceMethodSels;
724 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
725 for (ObjCImplementationDecl::instmeth_iterator iter = OID->instmeth_begin(),
726 endIter = OID->instmeth_end() ; iter != endIter ; iter++) {
727 InstanceMethodSels.push_back((*iter)->getSelector());
728 std::string TypeStr;
729 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
730 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
731 }
732
733 // Collect information about class methods
734 llvm::SmallVector<Selector, 16> ClassMethodSels;
735 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
736 for (ObjCImplementationDecl::classmeth_iterator iter = OID->classmeth_begin(),
737 endIter = OID->classmeth_end() ; iter != endIter ; iter++) {
738 ClassMethodSels.push_back((*iter)->getSelector());
739 std::string TypeStr;
740 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
741 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
742 }
743 // Collect the names of referenced protocols
744 llvm::SmallVector<std::string, 16> Protocols;
745 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
746 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
747 E = Protos.end(); I != E; ++I)
748 Protocols.push_back((*I)->getName());
749
750
751
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000752 // Get the superclass pointer.
753 llvm::Constant *SuperClass;
754 if (SuperClassName) {
755 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
756 } else {
757 SuperClass = llvm::ConstantPointerNull::get(
758 llvm::cast<llvm::PointerType>(PtrToInt8Ty));
759 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000760 // Empty vector used to construct empty method lists
761 llvm::SmallVector<llvm::Constant*, 1> empty;
762 // Generate the method and instance variable lists
763 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000764 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000765 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000766 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000767 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
768 IvarOffsets);
769 //Generate metaclass for class methods
770 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner1565e032008-07-21 06:31:05 +0000771 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000772 empty, empty, empty), ClassMethodList, NULLPtr);
773 // Generate the class structure
774 llvm::Constant *ClassStruct = GenerateClassStructure(MetaClassStruct,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000775 SuperClass, 0x1L, ClassName, 0,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000776 llvm::ConstantInt::get(llvm::Type::Int32Ty, instanceSize), IvarList,
777 MethodList, GenerateProtocolList(Protocols));
778 // Add class structure to list to be added to the symtab later
779 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
780 Classes.push_back(ClassStruct);
781}
782
783llvm::Function *CGObjCGNU::ModuleInitFunction() {
784 // Only emit an ObjC load function if no Objective-C stuff has been called
785 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
786 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov5f58b912008-06-01 15:14:46 +0000787 UntypedSelectors.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000788 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +0000789
790 // Name the ObjC types to make the IR a bit easier to read
791 TheModule.addTypeName(".objc_selector", SelectorTy);
792 TheModule.addTypeName(".objc_id", IdTy);
793 TheModule.addTypeName(".objc_imp", IMPTy);
794
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000795 std::vector<llvm::Constant*> Elements;
796 // Generate statics list:
797 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
798 ConstantStrings.size() + 1);
799 ConstantStrings.push_back(NULLPtr);
800 Elements.push_back(MakeConstantString("NSConstantString",
801 ".objc_static_class_name"));
802 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings));
803 llvm::StructType *StaticsListTy =
804 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
Chris Lattner630404b2008-06-26 04:10:42 +0000805 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000806 llvm::Constant *Statics =
807 MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Chris Lattner4e0b2642008-06-24 17:01:28 +0000808 llvm::ArrayType *StaticsListArrayTy =
Chris Lattner630404b2008-06-26 04:10:42 +0000809 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner4e0b2642008-06-24 17:01:28 +0000810 Elements.clear();
811 Elements.push_back(Statics);
Chris Lattner630404b2008-06-26 04:10:42 +0000812 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner4e0b2642008-06-24 17:01:28 +0000813 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000814 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
815 // Array of classes, categories, and constant objects
816 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
817 Classes.size() + Categories.size() + 2);
Chris Lattner630404b2008-06-26 04:10:42 +0000818 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelectorTy,
819 llvm::Type::Int16Ty,
820 llvm::Type::Int16Ty,
821 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000822
823 Elements.clear();
824 // Pointer to an array of selectors used in this module.
825 std::vector<llvm::Constant*> Selectors;
826 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
827 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
828 iter != iterEnd ; ++iter) {
Chris Lattner630404b2008-06-26 04:10:42 +0000829 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
830 Elements.push_back(MakeConstantString(iter->first.second,
831 ".objc_sel_types"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000832 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
833 Elements.clear();
834 }
835 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
836 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner630404b2008-06-26 04:10:42 +0000837 iter != iterEnd; ++iter) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000838 Elements.push_back(
Chris Lattner630404b2008-06-26 04:10:42 +0000839 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000840 Elements.push_back(NULLPtr);
841 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
842 Elements.clear();
843 }
844 Elements.push_back(NULLPtr);
845 Elements.push_back(NULLPtr);
846 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
847 Elements.clear();
848 // Number of static selectors
849 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
850 llvm::Constant *SelectorList = MakeGlobal(
851 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
852 ".objc_selector_list");
853 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList, SelectorTy));
854
855 // Now that all of the static selectors exist, create pointers to them.
856 int index = 0;
857 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
858 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
859 iter != iterEnd; ++iter) {
860 llvm::Constant *Idxs[] = {Zeros[0],
861 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
862 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
863 llvm::GlobalValue::InternalLinkage,
864 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
865 ".objc_sel_ptr", &TheModule);
866 (*iter).second->setAliasee(SelPtr);
867 }
868 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
869 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
870 iter != iterEnd; iter++) {
871 llvm::Constant *Idxs[] = {Zeros[0],
872 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
873 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
874 llvm::GlobalValue::InternalLinkage,
875 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
876 ".objc_sel_ptr", &TheModule);
877 (*iter).second->setAliasee(SelPtr);
878 }
879 // Number of classes defined.
880 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
881 Classes.size()));
882 // Number of categories defined
883 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
884 Categories.size()));
885 // Create an array of classes, then categories, then static object instances
886 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
887 // NULL-terminated list of static object instances (mainly constant strings)
888 Classes.push_back(Statics);
889 Classes.push_back(NULLPtr);
890 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
891 Elements.push_back(ClassList);
892 // Construct the symbol table
893 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
894
895 // The symbol table is contained in a module which has some version-checking
896 // constants
897 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
898 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
899 Elements.clear();
900 // Runtime version used for compatibility checking.
901 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
902 //FIXME: Should be sizeof(ModuleTy)
903 Elements.push_back(llvm::ConstantInt::get(LongTy, 16));
904 //FIXME: Should be the path to the file where this module was declared
905 Elements.push_back(NULLPtr);
906 Elements.push_back(SymTab);
907 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
908
909 // Create the load function calling the runtime entry point with the module
910 // structure
911 std::vector<const llvm::Type*> VoidArgs;
912 llvm::Function * LoadFunction = llvm::Function::Create(
913 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
914 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
915 &TheModule);
916 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000917 CGBuilderTy Builder;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000918 Builder.SetInsertPoint(EntryBB);
919 llvm::Value *Register = TheModule.getOrInsertFunction("__objc_exec_class",
920 llvm::Type::VoidTy, llvm::PointerType::getUnqual(ModuleTy), NULL);
921 Builder.CreateCall(Register, Module);
922 Builder.CreateRetVoid();
923 return LoadFunction;
924}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000925
926llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000927 const ObjCCategoryImplDecl *OCD =
928 dyn_cast<ObjCCategoryImplDecl>(OMD->getMethodContext());
929 const std::string &CategoryName = OCD ? OCD->getName() : "";
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000930 const std::string &ClassName = OMD->getClassInterface()->getName();
931 const std::string &MethodName = OMD->getSelector().getName();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000932 bool isClassMethod = !OMD->isInstance();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000933
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000934 const llvm::FunctionType *MethodTy =
935 CGM.getTypes().GetFunctionType(CGFunctionInfo(OMD, CGM.getContext()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000936 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
937 MethodName, isClassMethod);
938
Gabor Greif984d0b42008-04-06 20:42:52 +0000939 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000940 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000941 FunctionName,
Chris Lattner391d77a2008-03-30 23:03:07 +0000942 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +0000943 return Method;
944}
945
Daniel Dunbar49f66022008-09-24 03:38:44 +0000946llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
947 return 0;
948}
949
950llvm::Function *CGObjCGNU::GetPropertySetFunction() {
951 return 0;
952}
953
954llvm::Function *CGObjCGNU::EnumerationMutationFunction() {
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000955 return 0;
956}
957
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000958void CGObjCGNU::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +0000959 const ObjCAtTryStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000960 CGF.ErrorUnsupported(&S, "@try statement");
961}
962
963void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +0000964 const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000965 CGF.ErrorUnsupported(&S, "@throw statement");
966}
967
Chris Lattner10cac6f2008-11-15 21:26:17 +0000968void CGObjCGNU::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
969 const ObjCAtSynchronizedStmt &S) {
970 CGF.ErrorUnsupported(&S, "@synchronized statement");
971}
972
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000973llvm::Value * CGObjCGNU::EmitObjCWeakCall(CodeGen::CodeGenFunction &CGF,
974 llvm::Value *AddrWeakObj)
975{
976 return 0;
977}
978
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000979CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
Chris Lattnerdce14062008-06-26 04:19:03 +0000980 return new CGObjCGNU(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +0000981}