blob: 2c030e9f5ab905aaf229478004dd1a448530572c [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();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000124 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000125
126 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
127 const ObjCAtTryStmt &S);
128 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
129 const ObjCAtThrowStmt &S);
Chris Lattner0f984262008-03-01 08:50:34 +0000130};
131} // end anonymous namespace
132
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000133
134
135static std::string SymbolNameForClass(const std::string &ClassName) {
136 return ".objc_class_" + ClassName;
137}
138
139static std::string SymbolNameForMethod(const std::string &ClassName, const
140 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
141{
142 return "._objc_method_" + ClassName +"("+CategoryName+")"+
143 (isClassMethod ? "+" : "-") + MethodName;
144}
145
Chris Lattnerdce14062008-06-26 04:19:03 +0000146CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
147 : CGM(cgm), TheModule(CGM.getModule()) {
148 IntTy = CGM.getTypes().ConvertType(CGM.getContext().IntTy);
149 LongTy = CGM.getTypes().ConvertType(CGM.getContext().LongTy);
150
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000151 Zeros[0] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
152 Zeros[1] = Zeros[0];
153 NULLPtr = llvm::ConstantPointerNull::get(
154 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
Chris Lattner391d77a2008-03-30 23:03:07 +0000155 // C string type. Used in lots of places.
156 PtrToInt8Ty =
157 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
158 // Get the selector Type.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000159 SelStructTy = llvm::StructType::get(
Chris Lattner391d77a2008-03-30 23:03:07 +0000160 PtrToInt8Ty,
161 PtrToInt8Ty,
162 NULL);
163 SelectorTy = llvm::PointerType::getUnqual(SelStructTy);
164 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
165 PtrTy = PtrToInt8Ty;
166
167 // Object type
168 llvm::PATypeHolder OpaqueObjTy = llvm::OpaqueType::get();
169 llvm::Type *OpaqueIdTy = llvm::PointerType::getUnqual(OpaqueObjTy);
170 IdTy = llvm::StructType::get(OpaqueIdTy, NULL);
171 llvm::cast<llvm::OpaqueType>(OpaqueObjTy.get())->refineAbstractTypeTo(IdTy);
172 IdTy = llvm::cast<llvm::StructType>(OpaqueObjTy.get());
173 IdTy = llvm::PointerType::getUnqual(IdTy);
174
175 // IMP type
176 std::vector<const llvm::Type*> IMPArgs;
177 IMPArgs.push_back(IdTy);
178 IMPArgs.push_back(SelectorTy);
179 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000180}
181// This has to perform the lookup every time, since posing and related
182// techniques can modify the name -> class mapping.
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000183llvm::Value *CGObjCGNU::GetClass(llvm::IRBuilder<> &Builder,
184 const ObjCInterfaceDecl *OID) {
185 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getName());
186 ClassName = Builder.CreateStructGEP(ClassName, 0);
187
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000188 llvm::Constant *ClassLookupFn =
189 TheModule.getOrInsertFunction("objc_lookup_class", IdTy, PtrToInt8Ty,
190 NULL);
191 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner391d77a2008-03-30 23:03:07 +0000192}
193
Chris Lattner8e67b632008-06-26 04:37:12 +0000194/// GetSelector - Return the pointer to the unique'd string for this selector.
Chris Lattner85e35682008-08-08 19:57:58 +0000195llvm::Value *CGObjCGNU::GetSelector(llvm::IRBuilder<> &Builder, Selector Sel) {
Chris Lattner8e67b632008-06-26 04:37:12 +0000196 // FIXME: uniquing on the string is wasteful, unique on Sel instead!
197 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getName()];
198 if (US == 0)
199 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
200 llvm::GlobalValue::InternalLinkage,
201 ".objc_untyped_selector_alias",
202 NULL, &TheModule);
203
204 return Builder.CreateLoad(US);
205
206}
207
Chris Lattner5e7dcc62008-06-26 04:44:19 +0000208llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
209 const std::string &Name) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000210 llvm::Constant * ConstStr = llvm::ConstantArray::get(Str);
211 ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true,
212 llvm::GlobalValue::InternalLinkage,
213 ConstStr, Name, &TheModule);
214 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
215}
216llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
217 std::vector<llvm::Constant*> &V, const std::string &Name) {
218 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
219 return new llvm::GlobalVariable(Ty, false,
220 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
221}
222llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
223 std::vector<llvm::Constant*> &V, const std::string &Name) {
224 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
225 return new llvm::GlobalVariable(Ty, false,
226 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
227}
228
229/// Generate an NSConstantString object.
230//TODO: In case there are any crazy people still using the GNU runtime without
231//an OpenStep implementation, this should let them select their own class for
232//constant strings.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000233llvm::Constant *CGObjCGNU::GenerateConstantString(const std::string &Str) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000234 std::vector<llvm::Constant*> Ivars;
235 Ivars.push_back(NULLPtr);
Chris Lattner13fd7e52008-06-21 21:44:18 +0000236 Ivars.push_back(MakeConstantString(Str));
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000237 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000238 llvm::Constant *ObjCStr = MakeGlobal(
239 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
240 Ivars, ".objc_str");
241 ConstantStrings.push_back(
242 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
243 return ObjCStr;
244}
245
246///Generates a message send where the super is the receiver. This is a message
247///send to self with special delivery semantics indicating which class's method
248///should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000249CodeGen::RValue
250CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000251 QualType ResultType,
252 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000253 const ObjCInterfaceDecl *Class,
254 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000255 bool IsClassMessage,
256 const CallArgList &CallArgs) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000257 const ObjCInterfaceDecl *SuperClass = Class->getSuperClass();
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000258 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000259 // TODO: This should be cached, not looked up every time.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000260 llvm::Value *ReceiverClass = GetClass(CGF.Builder, SuperClass);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000261 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Chris Lattner0f984262008-03-01 08:50:34 +0000262 std::vector<const llvm::Type*> impArgTypes;
263 impArgTypes.push_back(Receiver->getType());
Chris Lattner391d77a2008-03-30 23:03:07 +0000264 impArgTypes.push_back(SelectorTy);
Chris Lattner0f984262008-03-01 08:50:34 +0000265
266 // Avoid an explicit cast on the IMP by getting a version that has the right
267 // return type.
268 llvm::FunctionType *impType = llvm::FunctionType::get(ReturnTy, impArgTypes,
269 true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000270 // Construct the structure used to look up the IMP
271 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
272 IdTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000273 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Eli Friedman1e692ac2008-06-13 23:01:12 +0000274 // FIXME: volatility
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000275 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
276 CGF.Builder.CreateStore(ReceiverClass, CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000277
278 // Get the IMP
279 llvm::Constant *lookupFunction =
280 TheModule.getOrInsertFunction("objc_msg_lookup_super",
281 llvm::PointerType::getUnqual(impType),
282 llvm::PointerType::getUnqual(ObjCSuperTy),
283 SelectorTy, NULL);
284 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000285 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000286 lookupArgs+2);
287
288 // Call the method
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000289 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000290 ActualArgs.push_back(std::make_pair(RValue::get(Receiver),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000291 CGF.getContext().getObjCIdType()));
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000292 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000293 CGF.getContext().getObjCSelType()));
294 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000295 return CGF.EmitCall(imp, ResultType, ActualArgs);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000296}
297
298/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000299CodeGen::RValue
300CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000301 QualType ResultType,
302 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000303 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000304 bool IsClassMessage,
305 const CallArgList &CallArgs) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000306 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
307 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000308
309 // Look up the method implementation.
310 std::vector<const llvm::Type*> impArgTypes;
311 const llvm::Type *RetTy;
312 //TODO: Revisit this when LLVM supports aggregate return types.
313 if (ReturnTy->isSingleValueType() && ReturnTy != llvm::Type::VoidTy) {
314 RetTy = ReturnTy;
315 } else {
316 // For struct returns allocate the space in the caller and pass it up to
317 // the sender.
318 RetTy = llvm::Type::VoidTy;
319 impArgTypes.push_back(llvm::PointerType::getUnqual(ReturnTy));
320 }
321 impArgTypes.push_back(Receiver->getType());
322 impArgTypes.push_back(SelectorTy);
323
324 // Avoid an explicit cast on the IMP by getting a version that has the right
325 // return type.
326 llvm::FunctionType *impType = llvm::FunctionType::get(RetTy, impArgTypes,
327 true);
Chris Lattner0f984262008-03-01 08:50:34 +0000328
329 llvm::Constant *lookupFunction =
330 TheModule.getOrInsertFunction("objc_msg_lookup",
Chris Lattner391d77a2008-03-30 23:03:07 +0000331 llvm::PointerType::getUnqual(impType),
332 Receiver->getType(), SelectorTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000333 llvm::Value *imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000334
335 // Call the method.
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000336 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000337 ActualArgs.push_back(std::make_pair(RValue::get(Receiver),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000338 CGF.getContext().getObjCIdType()));
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000339 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000340 CGF.getContext().getObjCSelType()));
341 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000342 return CGF.EmitCall(imp, ResultType, ActualArgs);
Chris Lattner0f984262008-03-01 08:50:34 +0000343}
344
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000345/// Generates a MethodList. Used in construction of a objc_class and
346/// objc_category structures.
347llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattnera4210072008-06-26 05:08:00 +0000348 const std::string &CategoryName,
349 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000350 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
351 bool isClassMethodList) {
352 // Get the method structure type.
353 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
354 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
355 PtrToInt8Ty, // Method types
356 llvm::PointerType::getUnqual(IMPTy), //Method pointer
357 NULL);
358 std::vector<llvm::Constant*> Methods;
359 std::vector<llvm::Constant*> Elements;
360 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
361 Elements.clear();
Daniel Dunbar61432932008-08-13 23:20:05 +0000362 llvm::Constant *C = CGM.GetAddrOfConstantCString(MethodSels[i].getName());
Chris Lattnera4210072008-06-26 05:08:00 +0000363 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000364 Elements.push_back(
365 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
366 llvm::Constant *Method =
367 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattnera4210072008-06-26 05:08:00 +0000368 MethodSels[i].getName(),
Chris Lattner550b8db2008-06-26 04:05:20 +0000369 isClassMethodList));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000370 Method = llvm::ConstantExpr::getBitCast(Method,
371 llvm::PointerType::getUnqual(IMPTy));
372 Elements.push_back(Method);
373 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
374 }
375
376 // Array of method structures
377 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Chris Lattnera4210072008-06-26 05:08:00 +0000378 MethodSels.size());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000379 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerfba67632008-06-26 04:52:29 +0000380 Methods);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000381
382 // Structure containing list pointer, array and array count
383 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
384 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
385 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
386 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
387 IntTy,
388 ObjCMethodArrayTy,
389 NULL);
390 // Refine next pointer type to concrete type
391 llvm::cast<llvm::OpaqueType>(
392 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
393 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
394
395 Methods.clear();
396 Methods.push_back(llvm::ConstantPointerNull::get(
397 llvm::PointerType::getUnqual(ObjCMethodListTy)));
398 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
399 MethodTypes.size()));
400 Methods.push_back(MethodArray);
401
402 // Create an instance of the structure
403 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
404}
405
406/// Generates an IvarList. Used in construction of a objc_class.
407llvm::Constant *CGObjCGNU::GenerateIvarList(
408 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
409 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
410 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
411 // Get the method structure type.
412 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
413 PtrToInt8Ty,
414 PtrToInt8Ty,
415 IntTy,
416 NULL);
417 std::vector<llvm::Constant*> Ivars;
418 std::vector<llvm::Constant*> Elements;
419 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
420 Elements.clear();
421 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
422 Zeros, 2));
423 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
424 Zeros, 2));
425 Elements.push_back(IvarOffsets[i]);
426 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
427 }
428
429 // Array of method structures
430 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
431 IvarNames.size());
432
433
434 Elements.clear();
435 Elements.push_back(llvm::ConstantInt::get(
436 llvm::cast<llvm::IntegerType>(IntTy), (int)IvarNames.size()));
437 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
438 // Structure containing array and array count
439 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
440 ObjCIvarArrayTy,
441 NULL);
442
443 // Create an instance of the structure
444 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
445}
446
447/// Generate a class structure
448llvm::Constant *CGObjCGNU::GenerateClassStructure(
449 llvm::Constant *MetaClass,
450 llvm::Constant *SuperClass,
451 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000452 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000453 llvm::Constant *Version,
454 llvm::Constant *InstanceSize,
455 llvm::Constant *IVars,
456 llvm::Constant *Methods,
457 llvm::Constant *Protocols) {
458 // Set up the class structure
459 // Note: Several of these are char*s when they should be ids. This is
460 // because the runtime performs this translation on load.
461 llvm::StructType *ClassTy = llvm::StructType::get(
462 PtrToInt8Ty, // class_pointer
463 PtrToInt8Ty, // super_class
464 PtrToInt8Ty, // name
465 LongTy, // version
466 LongTy, // info
467 LongTy, // instance_size
468 IVars->getType(), // ivars
469 Methods->getType(), // methods
470 // These are all filled in by the runtime, so we pretend
471 PtrTy, // dtable
472 PtrTy, // subclass_list
473 PtrTy, // sibling_class
474 PtrTy, // protocols
475 PtrTy, // gc_object_type
476 NULL);
477 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
478 llvm::Constant *NullP =
479 llvm::ConstantPointerNull::get(llvm::cast<llvm::PointerType>(PtrTy));
480 // Fill in the structure
481 std::vector<llvm::Constant*> Elements;
482 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
483 Elements.push_back(SuperClass);
Chris Lattnerd002cc62008-06-26 04:47:04 +0000484 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000485 Elements.push_back(Zero);
486 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
487 Elements.push_back(InstanceSize);
488 Elements.push_back(IVars);
489 Elements.push_back(Methods);
490 Elements.push_back(NullP);
491 Elements.push_back(NullP);
492 Elements.push_back(NullP);
493 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
494 Elements.push_back(NullP);
495 // Create an instance of the structure
Chris Lattner1565e032008-07-21 06:31:05 +0000496 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000497}
498
499llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
500 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
501 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
502 // Get the method structure type.
503 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
504 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
505 PtrToInt8Ty,
506 NULL);
507 std::vector<llvm::Constant*> Methods;
508 std::vector<llvm::Constant*> Elements;
509 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
510 Elements.clear();
511 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
512 Zeros, 2));
513 Elements.push_back(
514 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
515 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
516 }
517 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
518 MethodNames.size());
519 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
520 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
521 IntTy, ObjCMethodArrayTy, NULL);
522 Methods.clear();
523 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
524 Methods.push_back(Array);
525 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
526}
527// Create the protocol list structure used in classes, categories and so on
528llvm::Constant *CGObjCGNU::GenerateProtocolList(
529 const llvm::SmallVectorImpl<std::string> &Protocols) {
530 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
531 Protocols.size());
532 llvm::StructType *ProtocolListTy = llvm::StructType::get(
533 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
534 LongTy,//FIXME: Should be size_t
535 ProtocolArrayTy,
536 NULL);
537 std::vector<llvm::Constant*> Elements;
538 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
539 iter != endIter ; iter++) {
540 llvm::Constant *Ptr =
541 llvm::ConstantExpr::getBitCast(ExistingProtocols[*iter], PtrToInt8Ty);
542 Elements.push_back(Ptr);
543 }
544 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
545 Elements);
546 Elements.clear();
547 Elements.push_back(NULLPtr);
548 Elements.push_back(llvm::ConstantInt::get(
549 llvm::cast<llvm::IntegerType>(LongTy), Protocols.size()));
550 Elements.push_back(ProtocolArray);
551 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
552}
553
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000554llvm::Value *CGObjCGNU::GenerateProtocolRef(llvm::IRBuilder<> &Builder,
555 const ObjCProtocolDecl *PD) {
556 return ExistingProtocols[PD->getName()];
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000557}
558
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000559void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
560 ASTContext &Context = CGM.getContext();
561 const char *ProtocolName = PD->getName();
562 llvm::SmallVector<std::string, 16> Protocols;
563 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
564 E = PD->protocol_end(); PI != E; ++PI)
565 Protocols.push_back((*PI)->getName());
566 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
567 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
568 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
569 E = PD->instmeth_end(); iter != E; iter++) {
570 std::string TypeStr;
571 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
572 InstanceMethodNames.push_back(
Daniel Dunbar61432932008-08-13 23:20:05 +0000573 CGM.GetAddrOfConstantCString((*iter)->getSelector().getName()));
574 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000575 }
576 // Collect information about class methods:
577 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
578 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
579 for (ObjCProtocolDecl::classmeth_iterator iter = PD->classmeth_begin(),
580 endIter = PD->classmeth_end() ; iter != endIter ; iter++) {
581 std::string TypeStr;
582 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
583 ClassMethodNames.push_back(
Daniel Dunbar61432932008-08-13 23:20:05 +0000584 CGM.GetAddrOfConstantCString((*iter)->getSelector().getName()));
585 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000586 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000587
588 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
589 llvm::Constant *InstanceMethodList =
590 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
591 llvm::Constant *ClassMethodList =
592 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
593 // Protocols are objects containing lists of the methods implemented and
594 // protocols adopted.
595 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
596 PtrToInt8Ty,
597 ProtocolList->getType(),
598 InstanceMethodList->getType(),
599 ClassMethodList->getType(),
600 NULL);
601 std::vector<llvm::Constant*> Elements;
602 // The isa pointer must be set to a magic number so the runtime knows it's
603 // the correct layout.
604 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
605 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
606 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
607 Elements.push_back(ProtocolList);
608 Elements.push_back(InstanceMethodList);
609 Elements.push_back(ClassMethodList);
610 ExistingProtocols[ProtocolName] =
611 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
612 ".objc_protocol"), IdTy);
613}
614
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000615void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
616 const char *ClassName = OCD->getClassInterface()->getName();
617 const char *CategoryName = OCD->getName();
618 // Collect information about instance methods
619 llvm::SmallVector<Selector, 16> InstanceMethodSels;
620 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
621 for (ObjCCategoryDecl::instmeth_iterator iter = OCD->instmeth_begin(),
622 endIter = OCD->instmeth_end() ; iter != endIter ; iter++) {
623 InstanceMethodSels.push_back((*iter)->getSelector());
624 std::string TypeStr;
625 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
626 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
627 }
628
629 // Collect information about class methods
630 llvm::SmallVector<Selector, 16> ClassMethodSels;
631 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
632 for (ObjCCategoryDecl::classmeth_iterator iter = OCD->classmeth_begin(),
633 endIter = OCD->classmeth_end() ; iter != endIter ; iter++) {
634 ClassMethodSels.push_back((*iter)->getSelector());
635 std::string TypeStr;
636 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
637 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
638 }
639
640 // Collect the names of referenced protocols
641 llvm::SmallVector<std::string, 16> Protocols;
642 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
643 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
644 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
645 E = Protos.end(); I != E; ++I)
646 Protocols.push_back((*I)->getName());
647
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000648 std::vector<llvm::Constant*> Elements;
649 Elements.push_back(MakeConstantString(CategoryName));
650 Elements.push_back(MakeConstantString(ClassName));
651 // Instance method list
652 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000653 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000654 false), PtrTy));
655 // Class method list
656 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000657 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000658 PtrTy));
659 // Protocol list
660 Elements.push_back(llvm::ConstantExpr::getBitCast(
661 GenerateProtocolList(Protocols), PtrTy));
662 Categories.push_back(llvm::ConstantExpr::getBitCast(
663 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
664 PtrTy, PtrTy, NULL), Elements), PtrTy));
665}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000666
667void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
668 ASTContext &Context = CGM.getContext();
669
670 // Get the superclass name.
671 const ObjCInterfaceDecl * SuperClassDecl =
672 OID->getClassInterface()->getSuperClass();
673 const char * SuperClassName = NULL;
674 if (SuperClassDecl) {
675 SuperClassName = SuperClassDecl->getName();
676 }
677
678 // Get the class name
679 ObjCInterfaceDecl * ClassDecl = (ObjCInterfaceDecl*)OID->getClassInterface();
680 const char * ClassName = ClassDecl->getName();
681
682 // Get the size of instances. For runtimes that support late-bound instances
683 // this should probably be something different (size just of instance
684 // varaibles in this class, not superclasses?).
685 int instanceSize = 0;
686 const llvm::Type *ObjTy = 0;
687 if (!LateBoundIVars()) {
688 ObjTy = CGM.getTypes().ConvertType(Context.getObjCInterfaceType(ClassDecl));
689 instanceSize = CGM.getTargetData().getABITypeSize(ObjTy);
690 } else {
691 // This is required by newer ObjC runtimes.
692 assert(0 && "Late-bound instance variables not yet supported");
693 }
694
695 // Collect information about instance variables.
696 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
697 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
698 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
699 const llvm::StructLayout *Layout =
700 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(ObjTy));
701 ObjTy = llvm::PointerType::getUnqual(ObjTy);
702 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
703 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
704 // Store the name
705 IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter)->getName()));
706 // Get the type encoding for this ivar
707 std::string TypeStr;
708 llvm::SmallVector<const RecordType *, 8> EncodingRecordTypes;
709 Context.getObjCEncodingForType((*iter)->getType(), TypeStr,
710 EncodingRecordTypes);
711 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
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000943llvm::Function *CGObjCGNU::EnumerationMutationFunction()
944{
945 assert(0 && "No enumeration mutation function in the GNU runtime!");
946
947 return 0;
948}
949
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000950void CGObjCGNU::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
951 const ObjCAtTryStmt &S)
952{
953 CGF.ErrorUnsupported(&S, "@try statement");
954}
955
956void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
957 const ObjCAtThrowStmt &S)
958{
959 CGF.ErrorUnsupported(&S, "@throw statement");
960}
961
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000962CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
Chris Lattnerdce14062008-06-26 04:19:03 +0000963 return new CGObjCGNU(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +0000964}