blob: 4afbf3c2fb4ee943b25182696729714efcbd4c34 [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 Lattner0f984262008-03-01 08:50:34 +0000131};
132} // end anonymous namespace
133
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000134
135
136static std::string SymbolNameForClass(const std::string &ClassName) {
137 return ".objc_class_" + ClassName;
138}
139
140static std::string SymbolNameForMethod(const std::string &ClassName, const
141 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
142{
143 return "._objc_method_" + ClassName +"("+CategoryName+")"+
144 (isClassMethod ? "+" : "-") + MethodName;
145}
146
Chris Lattnerdce14062008-06-26 04:19:03 +0000147CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
148 : CGM(cgm), TheModule(CGM.getModule()) {
149 IntTy = CGM.getTypes().ConvertType(CGM.getContext().IntTy);
150 LongTy = CGM.getTypes().ConvertType(CGM.getContext().LongTy);
151
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000152 Zeros[0] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
153 Zeros[1] = Zeros[0];
154 NULLPtr = llvm::ConstantPointerNull::get(
155 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
Chris Lattner391d77a2008-03-30 23:03:07 +0000156 // C string type. Used in lots of places.
157 PtrToInt8Ty =
158 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
159 // Get the selector Type.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000160 SelStructTy = llvm::StructType::get(
Chris Lattner391d77a2008-03-30 23:03:07 +0000161 PtrToInt8Ty,
162 PtrToInt8Ty,
163 NULL);
164 SelectorTy = llvm::PointerType::getUnqual(SelStructTy);
165 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
166 PtrTy = PtrToInt8Ty;
167
168 // Object type
169 llvm::PATypeHolder OpaqueObjTy = llvm::OpaqueType::get();
170 llvm::Type *OpaqueIdTy = llvm::PointerType::getUnqual(OpaqueObjTy);
171 IdTy = llvm::StructType::get(OpaqueIdTy, NULL);
172 llvm::cast<llvm::OpaqueType>(OpaqueObjTy.get())->refineAbstractTypeTo(IdTy);
173 IdTy = llvm::cast<llvm::StructType>(OpaqueObjTy.get());
174 IdTy = llvm::PointerType::getUnqual(IdTy);
175
176 // IMP type
177 std::vector<const llvm::Type*> IMPArgs;
178 IMPArgs.push_back(IdTy);
179 IMPArgs.push_back(SelectorTy);
180 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000181}
182// This has to perform the lookup every time, since posing and related
183// techniques can modify the name -> class mapping.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000184llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000185 const ObjCInterfaceDecl *OID) {
186 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getName());
187 ClassName = Builder.CreateStructGEP(ClassName, 0);
188
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000189 llvm::Constant *ClassLookupFn =
190 TheModule.getOrInsertFunction("objc_lookup_class", IdTy, PtrToInt8Ty,
191 NULL);
192 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner391d77a2008-03-30 23:03:07 +0000193}
194
Chris Lattner8e67b632008-06-26 04:37:12 +0000195/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000196llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Chris Lattner8e67b632008-06-26 04:37:12 +0000197 // FIXME: uniquing on the string is wasteful, unique on Sel instead!
198 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getName()];
199 if (US == 0)
200 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
201 llvm::GlobalValue::InternalLinkage,
202 ".objc_untyped_selector_alias",
203 NULL, &TheModule);
204
205 return Builder.CreateLoad(US);
206
207}
208
Chris Lattner5e7dcc62008-06-26 04:44:19 +0000209llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
210 const std::string &Name) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000211 llvm::Constant * ConstStr = llvm::ConstantArray::get(Str);
212 ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true,
213 llvm::GlobalValue::InternalLinkage,
214 ConstStr, Name, &TheModule);
215 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
216}
217llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
218 std::vector<llvm::Constant*> &V, const std::string &Name) {
219 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
220 return new llvm::GlobalVariable(Ty, false,
221 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
222}
223llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
224 std::vector<llvm::Constant*> &V, const std::string &Name) {
225 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
226 return new llvm::GlobalVariable(Ty, false,
227 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
228}
229
230/// Generate an NSConstantString object.
231//TODO: In case there are any crazy people still using the GNU runtime without
232//an OpenStep implementation, this should let them select their own class for
233//constant strings.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000234llvm::Constant *CGObjCGNU::GenerateConstantString(const std::string &Str) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000235 std::vector<llvm::Constant*> Ivars;
236 Ivars.push_back(NULLPtr);
Chris Lattner13fd7e52008-06-21 21:44:18 +0000237 Ivars.push_back(MakeConstantString(Str));
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000238 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000239 llvm::Constant *ObjCStr = MakeGlobal(
240 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
241 Ivars, ".objc_str");
242 ConstantStrings.push_back(
243 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
244 return ObjCStr;
245}
246
247///Generates a message send where the super is the receiver. This is a message
248///send to self with special delivery semantics indicating which class's method
249///should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000250CodeGen::RValue
251CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000252 QualType ResultType,
253 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000254 const ObjCInterfaceDecl *Class,
255 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000256 bool IsClassMessage,
257 const CallArgList &CallArgs) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000258 const ObjCInterfaceDecl *SuperClass = Class->getSuperClass();
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000259 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000260 // TODO: This should be cached, not looked up every time.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000261 llvm::Value *ReceiverClass = GetClass(CGF.Builder, SuperClass);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000262 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Chris Lattner0f984262008-03-01 08:50:34 +0000263 std::vector<const llvm::Type*> impArgTypes;
264 impArgTypes.push_back(Receiver->getType());
Chris Lattner391d77a2008-03-30 23:03:07 +0000265 impArgTypes.push_back(SelectorTy);
Chris Lattner0f984262008-03-01 08:50:34 +0000266
267 // Avoid an explicit cast on the IMP by getting a version that has the right
268 // return type.
269 llvm::FunctionType *impType = llvm::FunctionType::get(ReturnTy, impArgTypes,
270 true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000271 // Construct the structure used to look up the IMP
272 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
273 IdTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000274 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Eli Friedman1e692ac2008-06-13 23:01:12 +0000275 // FIXME: volatility
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000276 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
277 CGF.Builder.CreateStore(ReceiverClass, CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000278
279 // Get the IMP
280 llvm::Constant *lookupFunction =
281 TheModule.getOrInsertFunction("objc_msg_lookup_super",
282 llvm::PointerType::getUnqual(impType),
283 llvm::PointerType::getUnqual(ObjCSuperTy),
284 SelectorTy, NULL);
285 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000286 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000287 lookupArgs+2);
288
289 // Call the method
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000290 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000291 ActualArgs.push_back(std::make_pair(RValue::get(Receiver),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000292 CGF.getContext().getObjCIdType()));
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000293 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000294 CGF.getContext().getObjCSelType()));
295 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000296 return CGF.EmitCall(imp, ResultType, ActualArgs);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000297}
298
299/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000300CodeGen::RValue
301CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000302 QualType ResultType,
303 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000304 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000305 bool IsClassMessage,
306 const CallArgList &CallArgs) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000307 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
308 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000309
310 // Look up the method implementation.
311 std::vector<const llvm::Type*> impArgTypes;
312 const llvm::Type *RetTy;
313 //TODO: Revisit this when LLVM supports aggregate return types.
314 if (ReturnTy->isSingleValueType() && ReturnTy != llvm::Type::VoidTy) {
315 RetTy = ReturnTy;
316 } else {
317 // For struct returns allocate the space in the caller and pass it up to
318 // the sender.
319 RetTy = llvm::Type::VoidTy;
320 impArgTypes.push_back(llvm::PointerType::getUnqual(ReturnTy));
321 }
322 impArgTypes.push_back(Receiver->getType());
323 impArgTypes.push_back(SelectorTy);
324
325 // Avoid an explicit cast on the IMP by getting a version that has the right
326 // return type.
327 llvm::FunctionType *impType = llvm::FunctionType::get(RetTy, impArgTypes,
328 true);
Chris Lattner0f984262008-03-01 08:50:34 +0000329
330 llvm::Constant *lookupFunction =
331 TheModule.getOrInsertFunction("objc_msg_lookup",
Chris Lattner391d77a2008-03-30 23:03:07 +0000332 llvm::PointerType::getUnqual(impType),
333 Receiver->getType(), SelectorTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000334 llvm::Value *imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000335
336 // Call the method.
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000337 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000338 ActualArgs.push_back(std::make_pair(RValue::get(Receiver),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000339 CGF.getContext().getObjCIdType()));
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000340 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000341 CGF.getContext().getObjCSelType()));
342 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000343 return CGF.EmitCall(imp, ResultType, ActualArgs);
Chris Lattner0f984262008-03-01 08:50:34 +0000344}
345
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000346/// Generates a MethodList. Used in construction of a objc_class and
347/// objc_category structures.
348llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattnera4210072008-06-26 05:08:00 +0000349 const std::string &CategoryName,
350 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000351 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
352 bool isClassMethodList) {
353 // Get the method structure type.
354 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
355 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
356 PtrToInt8Ty, // Method types
357 llvm::PointerType::getUnqual(IMPTy), //Method pointer
358 NULL);
359 std::vector<llvm::Constant*> Methods;
360 std::vector<llvm::Constant*> Elements;
361 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
362 Elements.clear();
Daniel Dunbar61432932008-08-13 23:20:05 +0000363 llvm::Constant *C = CGM.GetAddrOfConstantCString(MethodSels[i].getName());
Chris Lattnera4210072008-06-26 05:08:00 +0000364 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000365 Elements.push_back(
366 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
367 llvm::Constant *Method =
368 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattnera4210072008-06-26 05:08:00 +0000369 MethodSels[i].getName(),
Chris Lattner550b8db2008-06-26 04:05:20 +0000370 isClassMethodList));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000371 Method = llvm::ConstantExpr::getBitCast(Method,
372 llvm::PointerType::getUnqual(IMPTy));
373 Elements.push_back(Method);
374 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
375 }
376
377 // Array of method structures
378 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Chris Lattnera4210072008-06-26 05:08:00 +0000379 MethodSels.size());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000380 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerfba67632008-06-26 04:52:29 +0000381 Methods);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000382
383 // Structure containing list pointer, array and array count
384 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
385 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
386 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
387 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
388 IntTy,
389 ObjCMethodArrayTy,
390 NULL);
391 // Refine next pointer type to concrete type
392 llvm::cast<llvm::OpaqueType>(
393 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
394 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
395
396 Methods.clear();
397 Methods.push_back(llvm::ConstantPointerNull::get(
398 llvm::PointerType::getUnqual(ObjCMethodListTy)));
399 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
400 MethodTypes.size()));
401 Methods.push_back(MethodArray);
402
403 // Create an instance of the structure
404 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
405}
406
407/// Generates an IvarList. Used in construction of a objc_class.
408llvm::Constant *CGObjCGNU::GenerateIvarList(
409 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
410 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
411 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
412 // Get the method structure type.
413 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
414 PtrToInt8Ty,
415 PtrToInt8Ty,
416 IntTy,
417 NULL);
418 std::vector<llvm::Constant*> Ivars;
419 std::vector<llvm::Constant*> Elements;
420 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
421 Elements.clear();
422 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
423 Zeros, 2));
424 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
425 Zeros, 2));
426 Elements.push_back(IvarOffsets[i]);
427 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
428 }
429
430 // Array of method structures
431 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
432 IvarNames.size());
433
434
435 Elements.clear();
436 Elements.push_back(llvm::ConstantInt::get(
437 llvm::cast<llvm::IntegerType>(IntTy), (int)IvarNames.size()));
438 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
439 // Structure containing array and array count
440 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
441 ObjCIvarArrayTy,
442 NULL);
443
444 // Create an instance of the structure
445 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
446}
447
448/// Generate a class structure
449llvm::Constant *CGObjCGNU::GenerateClassStructure(
450 llvm::Constant *MetaClass,
451 llvm::Constant *SuperClass,
452 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000453 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000454 llvm::Constant *Version,
455 llvm::Constant *InstanceSize,
456 llvm::Constant *IVars,
457 llvm::Constant *Methods,
458 llvm::Constant *Protocols) {
459 // Set up the class structure
460 // Note: Several of these are char*s when they should be ids. This is
461 // because the runtime performs this translation on load.
462 llvm::StructType *ClassTy = llvm::StructType::get(
463 PtrToInt8Ty, // class_pointer
464 PtrToInt8Ty, // super_class
465 PtrToInt8Ty, // name
466 LongTy, // version
467 LongTy, // info
468 LongTy, // instance_size
469 IVars->getType(), // ivars
470 Methods->getType(), // methods
471 // These are all filled in by the runtime, so we pretend
472 PtrTy, // dtable
473 PtrTy, // subclass_list
474 PtrTy, // sibling_class
475 PtrTy, // protocols
476 PtrTy, // gc_object_type
477 NULL);
478 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
479 llvm::Constant *NullP =
480 llvm::ConstantPointerNull::get(llvm::cast<llvm::PointerType>(PtrTy));
481 // Fill in the structure
482 std::vector<llvm::Constant*> Elements;
483 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
484 Elements.push_back(SuperClass);
Chris Lattnerd002cc62008-06-26 04:47:04 +0000485 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000486 Elements.push_back(Zero);
487 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
488 Elements.push_back(InstanceSize);
489 Elements.push_back(IVars);
490 Elements.push_back(Methods);
491 Elements.push_back(NullP);
492 Elements.push_back(NullP);
493 Elements.push_back(NullP);
494 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
495 Elements.push_back(NullP);
496 // Create an instance of the structure
Chris Lattner1565e032008-07-21 06:31:05 +0000497 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000498}
499
500llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
501 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
502 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
503 // Get the method structure type.
504 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
505 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
506 PtrToInt8Ty,
507 NULL);
508 std::vector<llvm::Constant*> Methods;
509 std::vector<llvm::Constant*> Elements;
510 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
511 Elements.clear();
512 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
513 Zeros, 2));
514 Elements.push_back(
515 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
516 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
517 }
518 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
519 MethodNames.size());
520 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
521 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
522 IntTy, ObjCMethodArrayTy, NULL);
523 Methods.clear();
524 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
525 Methods.push_back(Array);
526 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
527}
528// Create the protocol list structure used in classes, categories and so on
529llvm::Constant *CGObjCGNU::GenerateProtocolList(
530 const llvm::SmallVectorImpl<std::string> &Protocols) {
531 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
532 Protocols.size());
533 llvm::StructType *ProtocolListTy = llvm::StructType::get(
534 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
535 LongTy,//FIXME: Should be size_t
536 ProtocolArrayTy,
537 NULL);
538 std::vector<llvm::Constant*> Elements;
539 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
540 iter != endIter ; iter++) {
541 llvm::Constant *Ptr =
542 llvm::ConstantExpr::getBitCast(ExistingProtocols[*iter], PtrToInt8Ty);
543 Elements.push_back(Ptr);
544 }
545 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
546 Elements);
547 Elements.clear();
548 Elements.push_back(NULLPtr);
549 Elements.push_back(llvm::ConstantInt::get(
550 llvm::cast<llvm::IntegerType>(LongTy), Protocols.size()));
551 Elements.push_back(ProtocolArray);
552 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
553}
554
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000555llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000556 const ObjCProtocolDecl *PD) {
557 return ExistingProtocols[PD->getName()];
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000558}
559
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000560void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
561 ASTContext &Context = CGM.getContext();
562 const char *ProtocolName = PD->getName();
563 llvm::SmallVector<std::string, 16> Protocols;
564 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
565 E = PD->protocol_end(); PI != E; ++PI)
566 Protocols.push_back((*PI)->getName());
567 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
568 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
569 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
570 E = PD->instmeth_end(); iter != E; iter++) {
571 std::string TypeStr;
572 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
573 InstanceMethodNames.push_back(
Daniel Dunbar61432932008-08-13 23:20:05 +0000574 CGM.GetAddrOfConstantCString((*iter)->getSelector().getName()));
575 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000576 }
577 // Collect information about class methods:
578 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
579 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
580 for (ObjCProtocolDecl::classmeth_iterator iter = PD->classmeth_begin(),
581 endIter = PD->classmeth_end() ; iter != endIter ; iter++) {
582 std::string TypeStr;
583 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
584 ClassMethodNames.push_back(
Daniel Dunbar61432932008-08-13 23:20:05 +0000585 CGM.GetAddrOfConstantCString((*iter)->getSelector().getName()));
586 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000587 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000588
589 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
590 llvm::Constant *InstanceMethodList =
591 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
592 llvm::Constant *ClassMethodList =
593 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
594 // Protocols are objects containing lists of the methods implemented and
595 // protocols adopted.
596 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
597 PtrToInt8Ty,
598 ProtocolList->getType(),
599 InstanceMethodList->getType(),
600 ClassMethodList->getType(),
601 NULL);
602 std::vector<llvm::Constant*> Elements;
603 // The isa pointer must be set to a magic number so the runtime knows it's
604 // the correct layout.
605 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
606 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
607 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
608 Elements.push_back(ProtocolList);
609 Elements.push_back(InstanceMethodList);
610 Elements.push_back(ClassMethodList);
611 ExistingProtocols[ProtocolName] =
612 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
613 ".objc_protocol"), IdTy);
614}
615
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000616void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
617 const char *ClassName = OCD->getClassInterface()->getName();
618 const char *CategoryName = OCD->getName();
619 // Collect information about instance methods
620 llvm::SmallVector<Selector, 16> InstanceMethodSels;
621 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
622 for (ObjCCategoryDecl::instmeth_iterator iter = OCD->instmeth_begin(),
623 endIter = OCD->instmeth_end() ; iter != endIter ; iter++) {
624 InstanceMethodSels.push_back((*iter)->getSelector());
625 std::string TypeStr;
626 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
627 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
628 }
629
630 // Collect information about class methods
631 llvm::SmallVector<Selector, 16> ClassMethodSels;
632 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
633 for (ObjCCategoryDecl::classmeth_iterator iter = OCD->classmeth_begin(),
634 endIter = OCD->classmeth_end() ; iter != endIter ; iter++) {
635 ClassMethodSels.push_back((*iter)->getSelector());
636 std::string TypeStr;
637 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
638 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
639 }
640
641 // Collect the names of referenced protocols
642 llvm::SmallVector<std::string, 16> Protocols;
643 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
644 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
645 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
646 E = Protos.end(); I != E; ++I)
647 Protocols.push_back((*I)->getName());
648
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000649 std::vector<llvm::Constant*> Elements;
650 Elements.push_back(MakeConstantString(CategoryName));
651 Elements.push_back(MakeConstantString(ClassName));
652 // Instance method list
653 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000654 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000655 false), PtrTy));
656 // Class method list
657 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000658 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000659 PtrTy));
660 // Protocol list
661 Elements.push_back(llvm::ConstantExpr::getBitCast(
662 GenerateProtocolList(Protocols), PtrTy));
663 Categories.push_back(llvm::ConstantExpr::getBitCast(
664 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
665 PtrTy, PtrTy, NULL), Elements), PtrTy));
666}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000667
668void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
669 ASTContext &Context = CGM.getContext();
670
671 // Get the superclass name.
672 const ObjCInterfaceDecl * SuperClassDecl =
673 OID->getClassInterface()->getSuperClass();
674 const char * SuperClassName = NULL;
675 if (SuperClassDecl) {
676 SuperClassName = SuperClassDecl->getName();
677 }
678
679 // Get the class name
680 ObjCInterfaceDecl * ClassDecl = (ObjCInterfaceDecl*)OID->getClassInterface();
681 const char * ClassName = ClassDecl->getName();
682
683 // Get the size of instances. For runtimes that support late-bound instances
684 // this should probably be something different (size just of instance
685 // varaibles in this class, not superclasses?).
686 int instanceSize = 0;
687 const llvm::Type *ObjTy = 0;
688 if (!LateBoundIVars()) {
689 ObjTy = CGM.getTypes().ConvertType(Context.getObjCInterfaceType(ClassDecl));
690 instanceSize = CGM.getTargetData().getABITypeSize(ObjTy);
691 } else {
692 // This is required by newer ObjC runtimes.
693 assert(0 && "Late-bound instance variables not yet supported");
694 }
695
696 // Collect information about instance variables.
697 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
698 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
699 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
700 const llvm::StructLayout *Layout =
701 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(ObjTy));
702 ObjTy = llvm::PointerType::getUnqual(ObjTy);
703 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
704 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
705 // Store the name
706 IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter)->getName()));
707 // Get the type encoding for this ivar
708 std::string TypeStr;
Daniel Dunbar0d504c12008-10-17 20:21:44 +0000709 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000710 IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
711 // Get the offset
712 int offset =
713 (int)Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(*iter));
714 IvarOffsets.push_back(
715 llvm::ConstantInt::get(llvm::Type::Int32Ty, offset));
716 }
717
718 // Collect information about instance methods
719 llvm::SmallVector<Selector, 16> InstanceMethodSels;
720 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
721 for (ObjCImplementationDecl::instmeth_iterator iter = OID->instmeth_begin(),
722 endIter = OID->instmeth_end() ; iter != endIter ; iter++) {
723 InstanceMethodSels.push_back((*iter)->getSelector());
724 std::string TypeStr;
725 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
726 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
727 }
728
729 // Collect information about class methods
730 llvm::SmallVector<Selector, 16> ClassMethodSels;
731 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
732 for (ObjCImplementationDecl::classmeth_iterator iter = OID->classmeth_begin(),
733 endIter = OID->classmeth_end() ; iter != endIter ; iter++) {
734 ClassMethodSels.push_back((*iter)->getSelector());
735 std::string TypeStr;
736 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
737 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
738 }
739 // Collect the names of referenced protocols
740 llvm::SmallVector<std::string, 16> Protocols;
741 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
742 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
743 E = Protos.end(); I != E; ++I)
744 Protocols.push_back((*I)->getName());
745
746
747
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000748 // Get the superclass pointer.
749 llvm::Constant *SuperClass;
750 if (SuperClassName) {
751 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
752 } else {
753 SuperClass = llvm::ConstantPointerNull::get(
754 llvm::cast<llvm::PointerType>(PtrToInt8Ty));
755 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000756 // Empty vector used to construct empty method lists
757 llvm::SmallVector<llvm::Constant*, 1> empty;
758 // Generate the method and instance variable lists
759 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000760 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000761 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000762 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000763 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
764 IvarOffsets);
765 //Generate metaclass for class methods
766 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner1565e032008-07-21 06:31:05 +0000767 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000768 empty, empty, empty), ClassMethodList, NULLPtr);
769 // Generate the class structure
770 llvm::Constant *ClassStruct = GenerateClassStructure(MetaClassStruct,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000771 SuperClass, 0x1L, ClassName, 0,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000772 llvm::ConstantInt::get(llvm::Type::Int32Ty, instanceSize), IvarList,
773 MethodList, GenerateProtocolList(Protocols));
774 // Add class structure to list to be added to the symtab later
775 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
776 Classes.push_back(ClassStruct);
777}
778
779llvm::Function *CGObjCGNU::ModuleInitFunction() {
780 // Only emit an ObjC load function if no Objective-C stuff has been called
781 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
782 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov5f58b912008-06-01 15:14:46 +0000783 UntypedSelectors.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000784 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +0000785
786 // Name the ObjC types to make the IR a bit easier to read
787 TheModule.addTypeName(".objc_selector", SelectorTy);
788 TheModule.addTypeName(".objc_id", IdTy);
789 TheModule.addTypeName(".objc_imp", IMPTy);
790
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000791 std::vector<llvm::Constant*> Elements;
792 // Generate statics list:
793 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
794 ConstantStrings.size() + 1);
795 ConstantStrings.push_back(NULLPtr);
796 Elements.push_back(MakeConstantString("NSConstantString",
797 ".objc_static_class_name"));
798 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings));
799 llvm::StructType *StaticsListTy =
800 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
Chris Lattner630404b2008-06-26 04:10:42 +0000801 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000802 llvm::Constant *Statics =
803 MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Chris Lattner4e0b2642008-06-24 17:01:28 +0000804 llvm::ArrayType *StaticsListArrayTy =
Chris Lattner630404b2008-06-26 04:10:42 +0000805 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner4e0b2642008-06-24 17:01:28 +0000806 Elements.clear();
807 Elements.push_back(Statics);
Chris Lattner630404b2008-06-26 04:10:42 +0000808 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner4e0b2642008-06-24 17:01:28 +0000809 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000810 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
811 // Array of classes, categories, and constant objects
812 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
813 Classes.size() + Categories.size() + 2);
Chris Lattner630404b2008-06-26 04:10:42 +0000814 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelectorTy,
815 llvm::Type::Int16Ty,
816 llvm::Type::Int16Ty,
817 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000818
819 Elements.clear();
820 // Pointer to an array of selectors used in this module.
821 std::vector<llvm::Constant*> Selectors;
822 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
823 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
824 iter != iterEnd ; ++iter) {
Chris Lattner630404b2008-06-26 04:10:42 +0000825 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
826 Elements.push_back(MakeConstantString(iter->first.second,
827 ".objc_sel_types"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000828 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
829 Elements.clear();
830 }
831 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
832 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner630404b2008-06-26 04:10:42 +0000833 iter != iterEnd; ++iter) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000834 Elements.push_back(
Chris Lattner630404b2008-06-26 04:10:42 +0000835 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000836 Elements.push_back(NULLPtr);
837 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
838 Elements.clear();
839 }
840 Elements.push_back(NULLPtr);
841 Elements.push_back(NULLPtr);
842 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
843 Elements.clear();
844 // Number of static selectors
845 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
846 llvm::Constant *SelectorList = MakeGlobal(
847 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
848 ".objc_selector_list");
849 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList, SelectorTy));
850
851 // Now that all of the static selectors exist, create pointers to them.
852 int index = 0;
853 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
854 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
855 iter != iterEnd; ++iter) {
856 llvm::Constant *Idxs[] = {Zeros[0],
857 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
858 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
859 llvm::GlobalValue::InternalLinkage,
860 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
861 ".objc_sel_ptr", &TheModule);
862 (*iter).second->setAliasee(SelPtr);
863 }
864 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
865 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
866 iter != iterEnd; iter++) {
867 llvm::Constant *Idxs[] = {Zeros[0],
868 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
869 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
870 llvm::GlobalValue::InternalLinkage,
871 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
872 ".objc_sel_ptr", &TheModule);
873 (*iter).second->setAliasee(SelPtr);
874 }
875 // Number of classes defined.
876 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
877 Classes.size()));
878 // Number of categories defined
879 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
880 Categories.size()));
881 // Create an array of classes, then categories, then static object instances
882 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
883 // NULL-terminated list of static object instances (mainly constant strings)
884 Classes.push_back(Statics);
885 Classes.push_back(NULLPtr);
886 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
887 Elements.push_back(ClassList);
888 // Construct the symbol table
889 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
890
891 // The symbol table is contained in a module which has some version-checking
892 // constants
893 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
894 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
895 Elements.clear();
896 // Runtime version used for compatibility checking.
897 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
898 //FIXME: Should be sizeof(ModuleTy)
899 Elements.push_back(llvm::ConstantInt::get(LongTy, 16));
900 //FIXME: Should be the path to the file where this module was declared
901 Elements.push_back(NULLPtr);
902 Elements.push_back(SymTab);
903 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
904
905 // Create the load function calling the runtime entry point with the module
906 // structure
907 std::vector<const llvm::Type*> VoidArgs;
908 llvm::Function * LoadFunction = llvm::Function::Create(
909 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
910 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
911 &TheModule);
912 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000913 CGBuilderTy Builder;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000914 Builder.SetInsertPoint(EntryBB);
915 llvm::Value *Register = TheModule.getOrInsertFunction("__objc_exec_class",
916 llvm::Type::VoidTy, llvm::PointerType::getUnqual(ModuleTy), NULL);
917 Builder.CreateCall(Register, Module);
918 Builder.CreateRetVoid();
919 return LoadFunction;
920}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000921
922llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000923 const ObjCCategoryImplDecl *OCD =
924 dyn_cast<ObjCCategoryImplDecl>(OMD->getMethodContext());
925 const std::string &CategoryName = OCD ? OCD->getName() : "";
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000926 const std::string &ClassName = OMD->getClassInterface()->getName();
927 const std::string &MethodName = OMD->getSelector().getName();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000928 bool isClassMethod = !OMD->isInstance();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000929
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000930 const llvm::FunctionType *MethodTy =
931 CGM.getTypes().GetFunctionType(CGFunctionInfo(OMD, CGM.getContext()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000932 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
933 MethodName, isClassMethod);
934
Gabor Greif984d0b42008-04-06 20:42:52 +0000935 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000936 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000937 FunctionName,
Chris Lattner391d77a2008-03-30 23:03:07 +0000938 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +0000939 return Method;
940}
941
Daniel Dunbar49f66022008-09-24 03:38:44 +0000942llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
943 return 0;
944}
945
946llvm::Function *CGObjCGNU::GetPropertySetFunction() {
947 return 0;
948}
949
950llvm::Function *CGObjCGNU::EnumerationMutationFunction() {
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000951 return 0;
952}
953
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000954void CGObjCGNU::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +0000955 const ObjCAtTryStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000956 CGF.ErrorUnsupported(&S, "@try statement");
957}
958
959void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +0000960 const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000961 CGF.ErrorUnsupported(&S, "@throw statement");
962}
963
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000964CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
Chris Lattnerdce14062008-06-26 04:19:03 +0000965 return new CGObjCGNU(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +0000966}