blob: 2a43cedcfc5885a46f4bbe7c36eeddb595b37b5c [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"
27#include "llvm/Support/IRBuilder.h"
28#include "llvm/Target/TargetData.h"
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000029#include <map>
Chris Lattnerdce14062008-06-26 04:19:03 +000030using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000031using namespace CodeGen;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000032using llvm::dyn_cast;
33
34// The version of the runtime that this class targets. Must match the version
35// in the runtime.
36static const int RuntimeVersion = 8;
37static const int ProtocolVersion = 2;
Chris Lattner0f984262008-03-01 08:50:34 +000038
Chris Lattner0f984262008-03-01 08:50:34 +000039namespace {
Chris Lattnerdce14062008-06-26 04:19:03 +000040class CGObjCGNU : public CodeGen::CGObjCRuntime {
Chris Lattner0f984262008-03-01 08:50:34 +000041private:
Chris Lattnerdce14062008-06-26 04:19:03 +000042 CodeGen::CodeGenModule &CGM;
Chris Lattner0f984262008-03-01 08:50:34 +000043 llvm::Module &TheModule;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000044 const llvm::StructType *SelStructTy;
Chris Lattner391d77a2008-03-30 23:03:07 +000045 const llvm::Type *SelectorTy;
46 const llvm::Type *PtrToInt8Ty;
47 const llvm::Type *IMPTy;
48 const llvm::Type *IdTy;
49 const llvm::Type *IntTy;
50 const llvm::Type *PtrTy;
51 const llvm::Type *LongTy;
52 const llvm::Type *PtrToIntTy;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000053 std::vector<llvm::Constant*> Classes;
54 std::vector<llvm::Constant*> Categories;
55 std::vector<llvm::Constant*> ConstantStrings;
56 llvm::Function *LoadFunction;
57 llvm::StringMap<llvm::Constant*> ExistingProtocols;
58 typedef std::pair<std::string, std::string> TypedSelector;
59 std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors;
60 llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors;
61 // Some zeros used for GEPs in lots of places.
62 llvm::Constant *Zeros[2];
63 llvm::Constant *NULLPtr;
64private:
65 llvm::Constant *GenerateIvarList(
66 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
67 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
68 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets);
69 llvm::Constant *GenerateMethodList(const std::string &ClassName,
70 const std::string &CategoryName,
Chris Lattnera4210072008-06-26 05:08:00 +000071 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000072 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
73 bool isClassMethodList);
74 llvm::Constant *GenerateProtocolList(
75 const llvm::SmallVectorImpl<std::string> &Protocols);
76 llvm::Constant *GenerateClassStructure(
77 llvm::Constant *MetaClass,
78 llvm::Constant *SuperClass,
79 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +000080 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000081 llvm::Constant *Version,
82 llvm::Constant *InstanceSize,
83 llvm::Constant *IVars,
84 llvm::Constant *Methods,
85 llvm::Constant *Protocols);
86 llvm::Constant *GenerateProtocolMethodList(
87 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
88 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes);
89 llvm::Constant *MakeConstantString(const std::string &Str, const std::string
90 &Name="");
91 llvm::Constant *MakeGlobal(const llvm::StructType *Ty,
92 std::vector<llvm::Constant*> &V, const std::string &Name="");
93 llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty,
94 std::vector<llvm::Constant*> &V, const std::string &Name="");
Chris Lattner0f984262008-03-01 08:50:34 +000095public:
Chris Lattnerdce14062008-06-26 04:19:03 +000096 CGObjCGNU(CodeGen::CodeGenModule &cgm);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000097 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000098 virtual CodeGen::RValue
99 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000100 QualType ResultType,
101 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000102 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000103 bool IsClassMessage,
104 const CallArgList &CallArgs);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000105 virtual CodeGen::RValue
106 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000107 QualType ResultType,
108 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000109 const ObjCInterfaceDecl *Class,
110 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000111 bool IsClassMessage,
112 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000113 virtual llvm::Value *GetClass(llvm::IRBuilder<> &Builder,
114 const ObjCInterfaceDecl *OID);
Chris Lattner85e35682008-08-08 19:57:58 +0000115 virtual llvm::Value *GetSelector(llvm::IRBuilder<> &Builder, Selector Sel);
Chris Lattner8e67b632008-06-26 04:37:12 +0000116
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000117 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD);
118 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
119 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Chris Lattner85e35682008-08-08 19:57:58 +0000120 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<> &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000121 const ObjCProtocolDecl *PD);
122 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000123 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar49f66022008-09-24 03:38:44 +0000124 virtual llvm::Function *GetPropertyGetFunction();
125 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000126 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000127
128 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
129 const ObjCAtTryStmt &S);
130 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
131 const ObjCAtThrowStmt &S);
Chris Lattner0f984262008-03-01 08:50:34 +0000132};
133} // end anonymous namespace
134
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000135
136
137static std::string SymbolNameForClass(const std::string &ClassName) {
138 return ".objc_class_" + ClassName;
139}
140
141static std::string SymbolNameForMethod(const std::string &ClassName, const
142 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
143{
144 return "._objc_method_" + ClassName +"("+CategoryName+")"+
145 (isClassMethod ? "+" : "-") + MethodName;
146}
147
Chris Lattnerdce14062008-06-26 04:19:03 +0000148CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
149 : CGM(cgm), TheModule(CGM.getModule()) {
150 IntTy = CGM.getTypes().ConvertType(CGM.getContext().IntTy);
151 LongTy = CGM.getTypes().ConvertType(CGM.getContext().LongTy);
152
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000153 Zeros[0] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
154 Zeros[1] = Zeros[0];
155 NULLPtr = llvm::ConstantPointerNull::get(
156 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
Chris Lattner391d77a2008-03-30 23:03:07 +0000157 // C string type. Used in lots of places.
158 PtrToInt8Ty =
159 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
160 // Get the selector Type.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000161 SelStructTy = llvm::StructType::get(
Chris Lattner391d77a2008-03-30 23:03:07 +0000162 PtrToInt8Ty,
163 PtrToInt8Ty,
164 NULL);
165 SelectorTy = llvm::PointerType::getUnqual(SelStructTy);
166 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
167 PtrTy = PtrToInt8Ty;
168
169 // Object type
170 llvm::PATypeHolder OpaqueObjTy = llvm::OpaqueType::get();
171 llvm::Type *OpaqueIdTy = llvm::PointerType::getUnqual(OpaqueObjTy);
172 IdTy = llvm::StructType::get(OpaqueIdTy, NULL);
173 llvm::cast<llvm::OpaqueType>(OpaqueObjTy.get())->refineAbstractTypeTo(IdTy);
174 IdTy = llvm::cast<llvm::StructType>(OpaqueObjTy.get());
175 IdTy = llvm::PointerType::getUnqual(IdTy);
176
177 // IMP type
178 std::vector<const llvm::Type*> IMPArgs;
179 IMPArgs.push_back(IdTy);
180 IMPArgs.push_back(SelectorTy);
181 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000182}
183// This has to perform the lookup every time, since posing and related
184// techniques can modify the name -> class mapping.
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000185llvm::Value *CGObjCGNU::GetClass(llvm::IRBuilder<> &Builder,
186 const ObjCInterfaceDecl *OID) {
187 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getName());
188 ClassName = Builder.CreateStructGEP(ClassName, 0);
189
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000190 llvm::Constant *ClassLookupFn =
191 TheModule.getOrInsertFunction("objc_lookup_class", IdTy, PtrToInt8Ty,
192 NULL);
193 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner391d77a2008-03-30 23:03:07 +0000194}
195
Chris Lattner8e67b632008-06-26 04:37:12 +0000196/// GetSelector - Return the pointer to the unique'd string for this selector.
Chris Lattner85e35682008-08-08 19:57:58 +0000197llvm::Value *CGObjCGNU::GetSelector(llvm::IRBuilder<> &Builder, Selector Sel) {
Chris Lattner8e67b632008-06-26 04:37:12 +0000198 // FIXME: uniquing on the string is wasteful, unique on Sel instead!
199 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getName()];
200 if (US == 0)
201 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
202 llvm::GlobalValue::InternalLinkage,
203 ".objc_untyped_selector_alias",
204 NULL, &TheModule);
205
206 return Builder.CreateLoad(US);
207
208}
209
Chris Lattner5e7dcc62008-06-26 04:44:19 +0000210llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
211 const std::string &Name) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000212 llvm::Constant * ConstStr = llvm::ConstantArray::get(Str);
213 ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true,
214 llvm::GlobalValue::InternalLinkage,
215 ConstStr, Name, &TheModule);
216 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
217}
218llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
219 std::vector<llvm::Constant*> &V, const std::string &Name) {
220 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
221 return new llvm::GlobalVariable(Ty, false,
222 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
223}
224llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
225 std::vector<llvm::Constant*> &V, const std::string &Name) {
226 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
227 return new llvm::GlobalVariable(Ty, false,
228 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
229}
230
231/// Generate an NSConstantString object.
232//TODO: In case there are any crazy people still using the GNU runtime without
233//an OpenStep implementation, this should let them select their own class for
234//constant strings.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000235llvm::Constant *CGObjCGNU::GenerateConstantString(const std::string &Str) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000236 std::vector<llvm::Constant*> Ivars;
237 Ivars.push_back(NULLPtr);
Chris Lattner13fd7e52008-06-21 21:44:18 +0000238 Ivars.push_back(MakeConstantString(Str));
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000239 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000240 llvm::Constant *ObjCStr = MakeGlobal(
241 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
242 Ivars, ".objc_str");
243 ConstantStrings.push_back(
244 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
245 return ObjCStr;
246}
247
248///Generates a message send where the super is the receiver. This is a message
249///send to self with special delivery semantics indicating which class's method
250///should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000251CodeGen::RValue
252CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000253 QualType ResultType,
254 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000255 const ObjCInterfaceDecl *Class,
256 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000257 bool IsClassMessage,
258 const CallArgList &CallArgs) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000259 const ObjCInterfaceDecl *SuperClass = Class->getSuperClass();
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000260 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000261 // TODO: This should be cached, not looked up every time.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000262 llvm::Value *ReceiverClass = GetClass(CGF.Builder, SuperClass);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000263 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Chris Lattner0f984262008-03-01 08:50:34 +0000264 std::vector<const llvm::Type*> impArgTypes;
265 impArgTypes.push_back(Receiver->getType());
Chris Lattner391d77a2008-03-30 23:03:07 +0000266 impArgTypes.push_back(SelectorTy);
Chris Lattner0f984262008-03-01 08:50:34 +0000267
268 // Avoid an explicit cast on the IMP by getting a version that has the right
269 // return type.
270 llvm::FunctionType *impType = llvm::FunctionType::get(ReturnTy, impArgTypes,
271 true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000272 // Construct the structure used to look up the IMP
273 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
274 IdTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000275 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Eli Friedman1e692ac2008-06-13 23:01:12 +0000276 // FIXME: volatility
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000277 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
278 CGF.Builder.CreateStore(ReceiverClass, CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000279
280 // Get the IMP
281 llvm::Constant *lookupFunction =
282 TheModule.getOrInsertFunction("objc_msg_lookup_super",
283 llvm::PointerType::getUnqual(impType),
284 llvm::PointerType::getUnqual(ObjCSuperTy),
285 SelectorTy, NULL);
286 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000287 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000288 lookupArgs+2);
289
290 // Call the method
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000291 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000292 ActualArgs.push_back(std::make_pair(RValue::get(Receiver),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000293 CGF.getContext().getObjCIdType()));
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000294 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000295 CGF.getContext().getObjCSelType()));
296 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000297 return CGF.EmitCall(imp, ResultType, ActualArgs);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000298}
299
300/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000301CodeGen::RValue
302CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000303 QualType ResultType,
304 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000305 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000306 bool IsClassMessage,
307 const CallArgList &CallArgs) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000308 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
309 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000310
311 // Look up the method implementation.
312 std::vector<const llvm::Type*> impArgTypes;
313 const llvm::Type *RetTy;
314 //TODO: Revisit this when LLVM supports aggregate return types.
315 if (ReturnTy->isSingleValueType() && ReturnTy != llvm::Type::VoidTy) {
316 RetTy = ReturnTy;
317 } else {
318 // For struct returns allocate the space in the caller and pass it up to
319 // the sender.
320 RetTy = llvm::Type::VoidTy;
321 impArgTypes.push_back(llvm::PointerType::getUnqual(ReturnTy));
322 }
323 impArgTypes.push_back(Receiver->getType());
324 impArgTypes.push_back(SelectorTy);
325
326 // Avoid an explicit cast on the IMP by getting a version that has the right
327 // return type.
328 llvm::FunctionType *impType = llvm::FunctionType::get(RetTy, impArgTypes,
329 true);
Chris Lattner0f984262008-03-01 08:50:34 +0000330
331 llvm::Constant *lookupFunction =
332 TheModule.getOrInsertFunction("objc_msg_lookup",
Chris Lattner391d77a2008-03-30 23:03:07 +0000333 llvm::PointerType::getUnqual(impType),
334 Receiver->getType(), SelectorTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000335 llvm::Value *imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000336
337 // Call the method.
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000338 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000339 ActualArgs.push_back(std::make_pair(RValue::get(Receiver),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000340 CGF.getContext().getObjCIdType()));
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000341 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000342 CGF.getContext().getObjCSelType()));
343 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000344 return CGF.EmitCall(imp, ResultType, ActualArgs);
Chris Lattner0f984262008-03-01 08:50:34 +0000345}
346
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000347/// Generates a MethodList. Used in construction of a objc_class and
348/// objc_category structures.
349llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattnera4210072008-06-26 05:08:00 +0000350 const std::string &CategoryName,
351 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000352 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
353 bool isClassMethodList) {
354 // Get the method structure type.
355 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
356 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
357 PtrToInt8Ty, // Method types
358 llvm::PointerType::getUnqual(IMPTy), //Method pointer
359 NULL);
360 std::vector<llvm::Constant*> Methods;
361 std::vector<llvm::Constant*> Elements;
362 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
363 Elements.clear();
Daniel Dunbar61432932008-08-13 23:20:05 +0000364 llvm::Constant *C = CGM.GetAddrOfConstantCString(MethodSels[i].getName());
Chris Lattnera4210072008-06-26 05:08:00 +0000365 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000366 Elements.push_back(
367 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
368 llvm::Constant *Method =
369 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattnera4210072008-06-26 05:08:00 +0000370 MethodSels[i].getName(),
Chris Lattner550b8db2008-06-26 04:05:20 +0000371 isClassMethodList));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000372 Method = llvm::ConstantExpr::getBitCast(Method,
373 llvm::PointerType::getUnqual(IMPTy));
374 Elements.push_back(Method);
375 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
376 }
377
378 // Array of method structures
379 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Chris Lattnera4210072008-06-26 05:08:00 +0000380 MethodSels.size());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000381 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerfba67632008-06-26 04:52:29 +0000382 Methods);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000383
384 // Structure containing list pointer, array and array count
385 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
386 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
387 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
388 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
389 IntTy,
390 ObjCMethodArrayTy,
391 NULL);
392 // Refine next pointer type to concrete type
393 llvm::cast<llvm::OpaqueType>(
394 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
395 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
396
397 Methods.clear();
398 Methods.push_back(llvm::ConstantPointerNull::get(
399 llvm::PointerType::getUnqual(ObjCMethodListTy)));
400 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
401 MethodTypes.size()));
402 Methods.push_back(MethodArray);
403
404 // Create an instance of the structure
405 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
406}
407
408/// Generates an IvarList. Used in construction of a objc_class.
409llvm::Constant *CGObjCGNU::GenerateIvarList(
410 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
411 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
412 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
413 // Get the method structure type.
414 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
415 PtrToInt8Ty,
416 PtrToInt8Ty,
417 IntTy,
418 NULL);
419 std::vector<llvm::Constant*> Ivars;
420 std::vector<llvm::Constant*> Elements;
421 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
422 Elements.clear();
423 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
424 Zeros, 2));
425 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
426 Zeros, 2));
427 Elements.push_back(IvarOffsets[i]);
428 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
429 }
430
431 // Array of method structures
432 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
433 IvarNames.size());
434
435
436 Elements.clear();
437 Elements.push_back(llvm::ConstantInt::get(
438 llvm::cast<llvm::IntegerType>(IntTy), (int)IvarNames.size()));
439 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
440 // Structure containing array and array count
441 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
442 ObjCIvarArrayTy,
443 NULL);
444
445 // Create an instance of the structure
446 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
447}
448
449/// Generate a class structure
450llvm::Constant *CGObjCGNU::GenerateClassStructure(
451 llvm::Constant *MetaClass,
452 llvm::Constant *SuperClass,
453 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000454 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000455 llvm::Constant *Version,
456 llvm::Constant *InstanceSize,
457 llvm::Constant *IVars,
458 llvm::Constant *Methods,
459 llvm::Constant *Protocols) {
460 // Set up the class structure
461 // Note: Several of these are char*s when they should be ids. This is
462 // because the runtime performs this translation on load.
463 llvm::StructType *ClassTy = llvm::StructType::get(
464 PtrToInt8Ty, // class_pointer
465 PtrToInt8Ty, // super_class
466 PtrToInt8Ty, // name
467 LongTy, // version
468 LongTy, // info
469 LongTy, // instance_size
470 IVars->getType(), // ivars
471 Methods->getType(), // methods
472 // These are all filled in by the runtime, so we pretend
473 PtrTy, // dtable
474 PtrTy, // subclass_list
475 PtrTy, // sibling_class
476 PtrTy, // protocols
477 PtrTy, // gc_object_type
478 NULL);
479 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
480 llvm::Constant *NullP =
481 llvm::ConstantPointerNull::get(llvm::cast<llvm::PointerType>(PtrTy));
482 // Fill in the structure
483 std::vector<llvm::Constant*> Elements;
484 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
485 Elements.push_back(SuperClass);
Chris Lattnerd002cc62008-06-26 04:47:04 +0000486 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000487 Elements.push_back(Zero);
488 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
489 Elements.push_back(InstanceSize);
490 Elements.push_back(IVars);
491 Elements.push_back(Methods);
492 Elements.push_back(NullP);
493 Elements.push_back(NullP);
494 Elements.push_back(NullP);
495 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
496 Elements.push_back(NullP);
497 // Create an instance of the structure
Chris Lattner1565e032008-07-21 06:31:05 +0000498 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000499}
500
501llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
502 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
503 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
504 // Get the method structure type.
505 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
506 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
507 PtrToInt8Ty,
508 NULL);
509 std::vector<llvm::Constant*> Methods;
510 std::vector<llvm::Constant*> Elements;
511 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
512 Elements.clear();
513 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
514 Zeros, 2));
515 Elements.push_back(
516 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
517 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
518 }
519 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
520 MethodNames.size());
521 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
522 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
523 IntTy, ObjCMethodArrayTy, NULL);
524 Methods.clear();
525 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
526 Methods.push_back(Array);
527 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
528}
529// Create the protocol list structure used in classes, categories and so on
530llvm::Constant *CGObjCGNU::GenerateProtocolList(
531 const llvm::SmallVectorImpl<std::string> &Protocols) {
532 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
533 Protocols.size());
534 llvm::StructType *ProtocolListTy = llvm::StructType::get(
535 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
536 LongTy,//FIXME: Should be size_t
537 ProtocolArrayTy,
538 NULL);
539 std::vector<llvm::Constant*> Elements;
540 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
541 iter != endIter ; iter++) {
542 llvm::Constant *Ptr =
543 llvm::ConstantExpr::getBitCast(ExistingProtocols[*iter], PtrToInt8Ty);
544 Elements.push_back(Ptr);
545 }
546 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
547 Elements);
548 Elements.clear();
549 Elements.push_back(NULLPtr);
550 Elements.push_back(llvm::ConstantInt::get(
551 llvm::cast<llvm::IntegerType>(LongTy), Protocols.size()));
552 Elements.push_back(ProtocolArray);
553 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
554}
555
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000556llvm::Value *CGObjCGNU::GenerateProtocolRef(llvm::IRBuilder<> &Builder,
557 const ObjCProtocolDecl *PD) {
558 return ExistingProtocols[PD->getName()];
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000559}
560
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000561void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
562 ASTContext &Context = CGM.getContext();
563 const char *ProtocolName = PD->getName();
564 llvm::SmallVector<std::string, 16> Protocols;
565 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
566 E = PD->protocol_end(); PI != E; ++PI)
567 Protocols.push_back((*PI)->getName());
568 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
569 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
570 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
571 E = PD->instmeth_end(); iter != E; iter++) {
572 std::string TypeStr;
573 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
574 InstanceMethodNames.push_back(
Daniel Dunbar61432932008-08-13 23:20:05 +0000575 CGM.GetAddrOfConstantCString((*iter)->getSelector().getName()));
576 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000577 }
578 // Collect information about class methods:
579 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
580 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
581 for (ObjCProtocolDecl::classmeth_iterator iter = PD->classmeth_begin(),
582 endIter = PD->classmeth_end() ; iter != endIter ; iter++) {
583 std::string TypeStr;
584 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
585 ClassMethodNames.push_back(
Daniel Dunbar61432932008-08-13 23:20:05 +0000586 CGM.GetAddrOfConstantCString((*iter)->getSelector().getName()));
587 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000588 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000589
590 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
591 llvm::Constant *InstanceMethodList =
592 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
593 llvm::Constant *ClassMethodList =
594 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
595 // Protocols are objects containing lists of the methods implemented and
596 // protocols adopted.
597 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
598 PtrToInt8Ty,
599 ProtocolList->getType(),
600 InstanceMethodList->getType(),
601 ClassMethodList->getType(),
602 NULL);
603 std::vector<llvm::Constant*> Elements;
604 // The isa pointer must be set to a magic number so the runtime knows it's
605 // the correct layout.
606 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
607 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
608 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
609 Elements.push_back(ProtocolList);
610 Elements.push_back(InstanceMethodList);
611 Elements.push_back(ClassMethodList);
612 ExistingProtocols[ProtocolName] =
613 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
614 ".objc_protocol"), IdTy);
615}
616
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000617void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
618 const char *ClassName = OCD->getClassInterface()->getName();
619 const char *CategoryName = OCD->getName();
620 // Collect information about instance methods
621 llvm::SmallVector<Selector, 16> InstanceMethodSels;
622 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
623 for (ObjCCategoryDecl::instmeth_iterator iter = OCD->instmeth_begin(),
624 endIter = OCD->instmeth_end() ; iter != endIter ; iter++) {
625 InstanceMethodSels.push_back((*iter)->getSelector());
626 std::string TypeStr;
627 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
628 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
629 }
630
631 // Collect information about class methods
632 llvm::SmallVector<Selector, 16> ClassMethodSels;
633 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
634 for (ObjCCategoryDecl::classmeth_iterator iter = OCD->classmeth_begin(),
635 endIter = OCD->classmeth_end() ; iter != endIter ; iter++) {
636 ClassMethodSels.push_back((*iter)->getSelector());
637 std::string TypeStr;
638 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
639 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
640 }
641
642 // Collect the names of referenced protocols
643 llvm::SmallVector<std::string, 16> Protocols;
644 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
645 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
646 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
647 E = Protos.end(); I != E; ++I)
648 Protocols.push_back((*I)->getName());
649
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000650 std::vector<llvm::Constant*> Elements;
651 Elements.push_back(MakeConstantString(CategoryName));
652 Elements.push_back(MakeConstantString(ClassName));
653 // Instance method list
654 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000655 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000656 false), PtrTy));
657 // Class method list
658 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000659 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000660 PtrTy));
661 // Protocol list
662 Elements.push_back(llvm::ConstantExpr::getBitCast(
663 GenerateProtocolList(Protocols), PtrTy));
664 Categories.push_back(llvm::ConstantExpr::getBitCast(
665 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
666 PtrTy, PtrTy, NULL), Elements), PtrTy));
667}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000668
669void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
670 ASTContext &Context = CGM.getContext();
671
672 // Get the superclass name.
673 const ObjCInterfaceDecl * SuperClassDecl =
674 OID->getClassInterface()->getSuperClass();
675 const char * SuperClassName = NULL;
676 if (SuperClassDecl) {
677 SuperClassName = SuperClassDecl->getName();
678 }
679
680 // Get the class name
681 ObjCInterfaceDecl * ClassDecl = (ObjCInterfaceDecl*)OID->getClassInterface();
682 const char * ClassName = ClassDecl->getName();
683
684 // Get the size of instances. For runtimes that support late-bound instances
685 // this should probably be something different (size just of instance
686 // varaibles in this class, not superclasses?).
687 int instanceSize = 0;
688 const llvm::Type *ObjTy = 0;
689 if (!LateBoundIVars()) {
690 ObjTy = CGM.getTypes().ConvertType(Context.getObjCInterfaceType(ClassDecl));
691 instanceSize = CGM.getTargetData().getABITypeSize(ObjTy);
692 } else {
693 // This is required by newer ObjC runtimes.
694 assert(0 && "Late-bound instance variables not yet supported");
695 }
696
697 // Collect information about instance variables.
698 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
699 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
700 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
701 const llvm::StructLayout *Layout =
702 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(ObjTy));
703 ObjTy = llvm::PointerType::getUnqual(ObjTy);
704 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
705 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
706 // Store the name
707 IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter)->getName()));
708 // Get the type encoding for this ivar
709 std::string TypeStr;
Daniel Dunbar0d504c12008-10-17 20:21:44 +0000710 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000711 IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
712 // Get the offset
713 int offset =
714 (int)Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(*iter));
715 IvarOffsets.push_back(
716 llvm::ConstantInt::get(llvm::Type::Int32Ty, offset));
717 }
718
719 // Collect information about instance methods
720 llvm::SmallVector<Selector, 16> InstanceMethodSels;
721 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
722 for (ObjCImplementationDecl::instmeth_iterator iter = OID->instmeth_begin(),
723 endIter = OID->instmeth_end() ; iter != endIter ; iter++) {
724 InstanceMethodSels.push_back((*iter)->getSelector());
725 std::string TypeStr;
726 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
727 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
728 }
729
730 // Collect information about class methods
731 llvm::SmallVector<Selector, 16> ClassMethodSels;
732 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
733 for (ObjCImplementationDecl::classmeth_iterator iter = OID->classmeth_begin(),
734 endIter = OID->classmeth_end() ; iter != endIter ; iter++) {
735 ClassMethodSels.push_back((*iter)->getSelector());
736 std::string TypeStr;
737 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
738 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
739 }
740 // Collect the names of referenced protocols
741 llvm::SmallVector<std::string, 16> Protocols;
742 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
743 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
744 E = Protos.end(); I != E; ++I)
745 Protocols.push_back((*I)->getName());
746
747
748
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000749 // Get the superclass pointer.
750 llvm::Constant *SuperClass;
751 if (SuperClassName) {
752 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
753 } else {
754 SuperClass = llvm::ConstantPointerNull::get(
755 llvm::cast<llvm::PointerType>(PtrToInt8Ty));
756 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000757 // Empty vector used to construct empty method lists
758 llvm::SmallVector<llvm::Constant*, 1> empty;
759 // Generate the method and instance variable lists
760 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000761 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000762 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000763 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000764 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
765 IvarOffsets);
766 //Generate metaclass for class methods
767 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner1565e032008-07-21 06:31:05 +0000768 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000769 empty, empty, empty), ClassMethodList, NULLPtr);
770 // Generate the class structure
771 llvm::Constant *ClassStruct = GenerateClassStructure(MetaClassStruct,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000772 SuperClass, 0x1L, ClassName, 0,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000773 llvm::ConstantInt::get(llvm::Type::Int32Ty, instanceSize), IvarList,
774 MethodList, GenerateProtocolList(Protocols));
775 // Add class structure to list to be added to the symtab later
776 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
777 Classes.push_back(ClassStruct);
778}
779
780llvm::Function *CGObjCGNU::ModuleInitFunction() {
781 // Only emit an ObjC load function if no Objective-C stuff has been called
782 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
783 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov5f58b912008-06-01 15:14:46 +0000784 UntypedSelectors.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000785 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +0000786
787 // Name the ObjC types to make the IR a bit easier to read
788 TheModule.addTypeName(".objc_selector", SelectorTy);
789 TheModule.addTypeName(".objc_id", IdTy);
790 TheModule.addTypeName(".objc_imp", IMPTy);
791
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000792 std::vector<llvm::Constant*> Elements;
793 // Generate statics list:
794 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
795 ConstantStrings.size() + 1);
796 ConstantStrings.push_back(NULLPtr);
797 Elements.push_back(MakeConstantString("NSConstantString",
798 ".objc_static_class_name"));
799 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings));
800 llvm::StructType *StaticsListTy =
801 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
Chris Lattner630404b2008-06-26 04:10:42 +0000802 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000803 llvm::Constant *Statics =
804 MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Chris Lattner4e0b2642008-06-24 17:01:28 +0000805 llvm::ArrayType *StaticsListArrayTy =
Chris Lattner630404b2008-06-26 04:10:42 +0000806 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner4e0b2642008-06-24 17:01:28 +0000807 Elements.clear();
808 Elements.push_back(Statics);
Chris Lattner630404b2008-06-26 04:10:42 +0000809 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner4e0b2642008-06-24 17:01:28 +0000810 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000811 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
812 // Array of classes, categories, and constant objects
813 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
814 Classes.size() + Categories.size() + 2);
Chris Lattner630404b2008-06-26 04:10:42 +0000815 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelectorTy,
816 llvm::Type::Int16Ty,
817 llvm::Type::Int16Ty,
818 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000819
820 Elements.clear();
821 // Pointer to an array of selectors used in this module.
822 std::vector<llvm::Constant*> Selectors;
823 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
824 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
825 iter != iterEnd ; ++iter) {
Chris Lattner630404b2008-06-26 04:10:42 +0000826 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
827 Elements.push_back(MakeConstantString(iter->first.second,
828 ".objc_sel_types"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000829 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
830 Elements.clear();
831 }
832 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
833 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner630404b2008-06-26 04:10:42 +0000834 iter != iterEnd; ++iter) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000835 Elements.push_back(
Chris Lattner630404b2008-06-26 04:10:42 +0000836 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000837 Elements.push_back(NULLPtr);
838 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
839 Elements.clear();
840 }
841 Elements.push_back(NULLPtr);
842 Elements.push_back(NULLPtr);
843 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
844 Elements.clear();
845 // Number of static selectors
846 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
847 llvm::Constant *SelectorList = MakeGlobal(
848 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
849 ".objc_selector_list");
850 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList, SelectorTy));
851
852 // Now that all of the static selectors exist, create pointers to them.
853 int index = 0;
854 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
855 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
856 iter != iterEnd; ++iter) {
857 llvm::Constant *Idxs[] = {Zeros[0],
858 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
859 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
860 llvm::GlobalValue::InternalLinkage,
861 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
862 ".objc_sel_ptr", &TheModule);
863 (*iter).second->setAliasee(SelPtr);
864 }
865 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
866 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
867 iter != iterEnd; iter++) {
868 llvm::Constant *Idxs[] = {Zeros[0],
869 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
870 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
871 llvm::GlobalValue::InternalLinkage,
872 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
873 ".objc_sel_ptr", &TheModule);
874 (*iter).second->setAliasee(SelPtr);
875 }
876 // Number of classes defined.
877 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
878 Classes.size()));
879 // Number of categories defined
880 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
881 Categories.size()));
882 // Create an array of classes, then categories, then static object instances
883 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
884 // NULL-terminated list of static object instances (mainly constant strings)
885 Classes.push_back(Statics);
886 Classes.push_back(NULLPtr);
887 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
888 Elements.push_back(ClassList);
889 // Construct the symbol table
890 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
891
892 // The symbol table is contained in a module which has some version-checking
893 // constants
894 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
895 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
896 Elements.clear();
897 // Runtime version used for compatibility checking.
898 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
899 //FIXME: Should be sizeof(ModuleTy)
900 Elements.push_back(llvm::ConstantInt::get(LongTy, 16));
901 //FIXME: Should be the path to the file where this module was declared
902 Elements.push_back(NULLPtr);
903 Elements.push_back(SymTab);
904 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
905
906 // Create the load function calling the runtime entry point with the module
907 // structure
908 std::vector<const llvm::Type*> VoidArgs;
909 llvm::Function * LoadFunction = llvm::Function::Create(
910 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
911 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
912 &TheModule);
913 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
Chris Lattner85e35682008-08-08 19:57:58 +0000914 llvm::IRBuilder<> Builder;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000915 Builder.SetInsertPoint(EntryBB);
916 llvm::Value *Register = TheModule.getOrInsertFunction("__objc_exec_class",
917 llvm::Type::VoidTy, llvm::PointerType::getUnqual(ModuleTy), NULL);
918 Builder.CreateCall(Register, Module);
919 Builder.CreateRetVoid();
920 return LoadFunction;
921}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000922
923llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000924 const ObjCCategoryImplDecl *OCD =
925 dyn_cast<ObjCCategoryImplDecl>(OMD->getMethodContext());
926 const std::string &CategoryName = OCD ? OCD->getName() : "";
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000927 const std::string &ClassName = OMD->getClassInterface()->getName();
928 const std::string &MethodName = OMD->getSelector().getName();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000929 bool isClassMethod = !OMD->isInstance();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000930
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000931 const llvm::FunctionType *MethodTy =
932 CGM.getTypes().GetFunctionType(CGFunctionInfo(OMD, CGM.getContext()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000933 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
934 MethodName, isClassMethod);
935
Gabor Greif984d0b42008-04-06 20:42:52 +0000936 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000937 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000938 FunctionName,
Chris Lattner391d77a2008-03-30 23:03:07 +0000939 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +0000940 return Method;
941}
942
Daniel Dunbar49f66022008-09-24 03:38:44 +0000943llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
944 return 0;
945}
946
947llvm::Function *CGObjCGNU::GetPropertySetFunction() {
948 return 0;
949}
950
951llvm::Function *CGObjCGNU::EnumerationMutationFunction() {
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000952 return 0;
953}
954
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000955void CGObjCGNU::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +0000956 const ObjCAtTryStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000957 CGF.ErrorUnsupported(&S, "@try statement");
958}
959
960void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +0000961 const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000962 CGF.ErrorUnsupported(&S, "@throw statement");
963}
964
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000965CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
Chris Lattnerdce14062008-06-26 04:19:03 +0000966 return new CGObjCGNU(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +0000967}