blob: 0833b0832d74c8081d8585f6b869ca6af053cb31 [file] [log] [blame]
Anton Korobeynikov20ff3102008-06-01 14:13:53 +00001//===------- CGObjCGNU.cpp - Emit LLVM Code from ASTs for a Module --------===//
Chris Lattner0f984262008-03-01 08:50:34 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000010// This provides Objective-C code generation targetting the GNU runtime. The
11// class in this file generates structures used by the GNU Objective-C runtime
12// library. These structures are defined in objc/objc.h and objc/objc-api.h in
13// the GNU runtime distribution.
Chris Lattner0f984262008-03-01 08:50:34 +000014//
15//===----------------------------------------------------------------------===//
16
17#include "CGObjCRuntime.h"
Chris Lattnerdce14062008-06-26 04:19:03 +000018#include "CodeGenModule.h"
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000019#include "CodeGenFunction.h"
Chris Lattnerdce14062008-06-26 04:19:03 +000020#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000021#include "clang/AST/Decl.h"
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000022#include "clang/AST/DeclObjC.h"
Chris Lattner0f984262008-03-01 08:50:34 +000023#include "llvm/Module.h"
Chris Lattner0f984262008-03-01 08:50:34 +000024#include "llvm/ADT/SmallVector.h"
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000025#include "llvm/ADT/StringMap.h"
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000026#include "llvm/Support/Compiler.h"
Daniel Dunbar7ded7f42008-08-15 22:20:32 +000027#include "llvm/Target/TargetData.h"
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000028#include <map>
Chris Lattnerdce14062008-06-26 04:19:03 +000029using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000030using namespace CodeGen;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000031using llvm::dyn_cast;
32
33// The version of the runtime that this class targets. Must match the version
34// in the runtime.
35static const int RuntimeVersion = 8;
36static const int ProtocolVersion = 2;
Chris Lattner0f984262008-03-01 08:50:34 +000037
Chris Lattner0f984262008-03-01 08:50:34 +000038namespace {
Chris Lattnerdce14062008-06-26 04:19:03 +000039class CGObjCGNU : public CodeGen::CGObjCRuntime {
Chris Lattner0f984262008-03-01 08:50:34 +000040private:
Chris Lattnerdce14062008-06-26 04:19:03 +000041 CodeGen::CodeGenModule &CGM;
Chris Lattner0f984262008-03-01 08:50:34 +000042 llvm::Module &TheModule;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000043 const llvm::StructType *SelStructTy;
Chris Lattner391d77a2008-03-30 23:03:07 +000044 const llvm::Type *SelectorTy;
45 const llvm::Type *PtrToInt8Ty;
46 const llvm::Type *IMPTy;
47 const llvm::Type *IdTy;
48 const llvm::Type *IntTy;
49 const llvm::Type *PtrTy;
50 const llvm::Type *LongTy;
51 const llvm::Type *PtrToIntTy;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000052 std::vector<llvm::Constant*> Classes;
53 std::vector<llvm::Constant*> Categories;
54 std::vector<llvm::Constant*> ConstantStrings;
55 llvm::Function *LoadFunction;
56 llvm::StringMap<llvm::Constant*> ExistingProtocols;
57 typedef std::pair<std::string, std::string> TypedSelector;
58 std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors;
59 llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors;
60 // Some zeros used for GEPs in lots of places.
61 llvm::Constant *Zeros[2];
62 llvm::Constant *NULLPtr;
63private:
64 llvm::Constant *GenerateIvarList(
65 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
66 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
67 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets);
68 llvm::Constant *GenerateMethodList(const std::string &ClassName,
69 const std::string &CategoryName,
Chris Lattnera4210072008-06-26 05:08:00 +000070 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000071 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
72 bool isClassMethodList);
73 llvm::Constant *GenerateProtocolList(
74 const llvm::SmallVectorImpl<std::string> &Protocols);
75 llvm::Constant *GenerateClassStructure(
76 llvm::Constant *MetaClass,
77 llvm::Constant *SuperClass,
78 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +000079 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000080 llvm::Constant *Version,
81 llvm::Constant *InstanceSize,
82 llvm::Constant *IVars,
83 llvm::Constant *Methods,
84 llvm::Constant *Protocols);
85 llvm::Constant *GenerateProtocolMethodList(
86 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
87 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes);
88 llvm::Constant *MakeConstantString(const std::string &Str, const std::string
89 &Name="");
90 llvm::Constant *MakeGlobal(const llvm::StructType *Ty,
91 std::vector<llvm::Constant*> &V, const std::string &Name="");
92 llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty,
93 std::vector<llvm::Constant*> &V, const std::string &Name="");
Chris Lattner0f984262008-03-01 08:50:34 +000094public:
Chris Lattnerdce14062008-06-26 04:19:03 +000095 CGObjCGNU(CodeGen::CodeGenModule &cgm);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000096 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000097 virtual CodeGen::RValue
98 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000099 QualType ResultType,
100 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000101 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000102 bool IsClassMessage,
103 const CallArgList &CallArgs);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000104 virtual CodeGen::RValue
105 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000106 QualType ResultType,
107 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000108 const ObjCInterfaceDecl *Class,
109 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000110 bool IsClassMessage,
111 const CallArgList &CallArgs);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000112 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000113 const ObjCInterfaceDecl *OID);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000114 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Chris Lattner8e67b632008-06-26 04:37:12 +0000115
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000116 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD);
117 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
118 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000119 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000120 const ObjCProtocolDecl *PD);
121 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000122 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar49f66022008-09-24 03:38:44 +0000123 virtual llvm::Function *GetPropertyGetFunction();
124 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000125 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000126
127 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
128 const ObjCAtTryStmt &S);
129 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
130 const ObjCAtThrowStmt &S);
Chris Lattner10cac6f2008-11-15 21:26:17 +0000131 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
132 const ObjCAtSynchronizedStmt &S);
Chris Lattner0f984262008-03-01 08:50:34 +0000133};
134} // end anonymous namespace
135
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000136
137
138static std::string SymbolNameForClass(const std::string &ClassName) {
139 return ".objc_class_" + ClassName;
140}
141
142static std::string SymbolNameForMethod(const std::string &ClassName, const
143 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
144{
145 return "._objc_method_" + ClassName +"("+CategoryName+")"+
146 (isClassMethod ? "+" : "-") + MethodName;
147}
148
Chris Lattnerdce14062008-06-26 04:19:03 +0000149CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
150 : CGM(cgm), TheModule(CGM.getModule()) {
151 IntTy = CGM.getTypes().ConvertType(CGM.getContext().IntTy);
152 LongTy = CGM.getTypes().ConvertType(CGM.getContext().LongTy);
153
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000154 Zeros[0] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
155 Zeros[1] = Zeros[0];
156 NULLPtr = llvm::ConstantPointerNull::get(
157 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
Chris Lattner391d77a2008-03-30 23:03:07 +0000158 // C string type. Used in lots of places.
159 PtrToInt8Ty =
160 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
161 // Get the selector Type.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000162 SelStructTy = llvm::StructType::get(
Chris Lattner391d77a2008-03-30 23:03:07 +0000163 PtrToInt8Ty,
164 PtrToInt8Ty,
165 NULL);
166 SelectorTy = llvm::PointerType::getUnqual(SelStructTy);
167 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
168 PtrTy = PtrToInt8Ty;
169
170 // Object type
171 llvm::PATypeHolder OpaqueObjTy = llvm::OpaqueType::get();
172 llvm::Type *OpaqueIdTy = llvm::PointerType::getUnqual(OpaqueObjTy);
173 IdTy = llvm::StructType::get(OpaqueIdTy, NULL);
174 llvm::cast<llvm::OpaqueType>(OpaqueObjTy.get())->refineAbstractTypeTo(IdTy);
175 IdTy = llvm::cast<llvm::StructType>(OpaqueObjTy.get());
176 IdTy = llvm::PointerType::getUnqual(IdTy);
177
178 // IMP type
179 std::vector<const llvm::Type*> IMPArgs;
180 IMPArgs.push_back(IdTy);
181 IMPArgs.push_back(SelectorTy);
182 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000183}
184// This has to perform the lookup every time, since posing and related
185// techniques can modify the name -> class mapping.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000186llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000187 const ObjCInterfaceDecl *OID) {
188 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getName());
189 ClassName = Builder.CreateStructGEP(ClassName, 0);
190
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000191 llvm::Constant *ClassLookupFn =
192 TheModule.getOrInsertFunction("objc_lookup_class", IdTy, PtrToInt8Ty,
193 NULL);
194 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner391d77a2008-03-30 23:03:07 +0000195}
196
Chris Lattner8e67b632008-06-26 04:37:12 +0000197/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000198llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Chris Lattner8e67b632008-06-26 04:37:12 +0000199 // FIXME: uniquing on the string is wasteful, unique on Sel instead!
200 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getName()];
201 if (US == 0)
202 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
203 llvm::GlobalValue::InternalLinkage,
204 ".objc_untyped_selector_alias",
205 NULL, &TheModule);
206
207 return Builder.CreateLoad(US);
208
209}
210
Chris Lattner5e7dcc62008-06-26 04:44:19 +0000211llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
212 const std::string &Name) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000213 llvm::Constant * ConstStr = llvm::ConstantArray::get(Str);
214 ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true,
215 llvm::GlobalValue::InternalLinkage,
216 ConstStr, Name, &TheModule);
217 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
218}
219llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
220 std::vector<llvm::Constant*> &V, const std::string &Name) {
221 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
222 return new llvm::GlobalVariable(Ty, false,
223 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
224}
225llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
226 std::vector<llvm::Constant*> &V, const std::string &Name) {
227 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
228 return new llvm::GlobalVariable(Ty, false,
229 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
230}
231
232/// Generate an NSConstantString object.
233//TODO: In case there are any crazy people still using the GNU runtime without
234//an OpenStep implementation, this should let them select their own class for
235//constant strings.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000236llvm::Constant *CGObjCGNU::GenerateConstantString(const std::string &Str) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000237 std::vector<llvm::Constant*> Ivars;
238 Ivars.push_back(NULLPtr);
Chris Lattner13fd7e52008-06-21 21:44:18 +0000239 Ivars.push_back(MakeConstantString(Str));
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000240 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000241 llvm::Constant *ObjCStr = MakeGlobal(
242 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
243 Ivars, ".objc_str");
244 ConstantStrings.push_back(
245 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
246 return ObjCStr;
247}
248
249///Generates a message send where the super is the receiver. This is a message
250///send to self with special delivery semantics indicating which class's method
251///should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000252CodeGen::RValue
253CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000254 QualType ResultType,
255 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000256 const ObjCInterfaceDecl *Class,
257 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000258 bool IsClassMessage,
259 const CallArgList &CallArgs) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000260 const ObjCInterfaceDecl *SuperClass = Class->getSuperClass();
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000261 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000262 // TODO: This should be cached, not looked up every time.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000263 llvm::Value *ReceiverClass = GetClass(CGF.Builder, SuperClass);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000264 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Chris Lattner0f984262008-03-01 08:50:34 +0000265 std::vector<const llvm::Type*> impArgTypes;
266 impArgTypes.push_back(Receiver->getType());
Chris Lattner391d77a2008-03-30 23:03:07 +0000267 impArgTypes.push_back(SelectorTy);
Chris Lattner0f984262008-03-01 08:50:34 +0000268
269 // Avoid an explicit cast on the IMP by getting a version that has the right
270 // return type.
271 llvm::FunctionType *impType = llvm::FunctionType::get(ReturnTy, impArgTypes,
272 true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000273 // Construct the structure used to look up the IMP
274 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
275 IdTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000276 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Eli Friedman1e692ac2008-06-13 23:01:12 +0000277 // FIXME: volatility
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000278 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
279 CGF.Builder.CreateStore(ReceiverClass, CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000280
281 // Get the IMP
282 llvm::Constant *lookupFunction =
283 TheModule.getOrInsertFunction("objc_msg_lookup_super",
284 llvm::PointerType::getUnqual(impType),
285 llvm::PointerType::getUnqual(ObjCSuperTy),
286 SelectorTy, NULL);
287 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000288 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000289 lookupArgs+2);
290
291 // Call the method
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000292 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000293 ActualArgs.push_back(std::make_pair(RValue::get(Receiver),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000294 CGF.getContext().getObjCIdType()));
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000295 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000296 CGF.getContext().getObjCSelType()));
297 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000298 return CGF.EmitCall(imp, ResultType, ActualArgs);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000299}
300
301/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000302CodeGen::RValue
303CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000304 QualType ResultType,
305 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000306 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000307 bool IsClassMessage,
308 const CallArgList &CallArgs) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000309 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
310 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000311
312 // Look up the method implementation.
313 std::vector<const llvm::Type*> impArgTypes;
314 const llvm::Type *RetTy;
315 //TODO: Revisit this when LLVM supports aggregate return types.
316 if (ReturnTy->isSingleValueType() && ReturnTy != llvm::Type::VoidTy) {
317 RetTy = ReturnTy;
318 } else {
319 // For struct returns allocate the space in the caller and pass it up to
320 // the sender.
321 RetTy = llvm::Type::VoidTy;
322 impArgTypes.push_back(llvm::PointerType::getUnqual(ReturnTy));
323 }
324 impArgTypes.push_back(Receiver->getType());
325 impArgTypes.push_back(SelectorTy);
326
327 // Avoid an explicit cast on the IMP by getting a version that has the right
328 // return type.
329 llvm::FunctionType *impType = llvm::FunctionType::get(RetTy, impArgTypes,
330 true);
Chris Lattner0f984262008-03-01 08:50:34 +0000331
332 llvm::Constant *lookupFunction =
333 TheModule.getOrInsertFunction("objc_msg_lookup",
Chris Lattner391d77a2008-03-30 23:03:07 +0000334 llvm::PointerType::getUnqual(impType),
335 Receiver->getType(), SelectorTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000336 llvm::Value *imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000337
338 // Call the method.
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000339 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000340 ActualArgs.push_back(std::make_pair(RValue::get(Receiver),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000341 CGF.getContext().getObjCIdType()));
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000342 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000343 CGF.getContext().getObjCSelType()));
344 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000345 return CGF.EmitCall(imp, ResultType, ActualArgs);
Chris Lattner0f984262008-03-01 08:50:34 +0000346}
347
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000348/// Generates a MethodList. Used in construction of a objc_class and
349/// objc_category structures.
350llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattnera4210072008-06-26 05:08:00 +0000351 const std::string &CategoryName,
352 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000353 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
354 bool isClassMethodList) {
355 // Get the method structure type.
356 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
357 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
358 PtrToInt8Ty, // Method types
359 llvm::PointerType::getUnqual(IMPTy), //Method pointer
360 NULL);
361 std::vector<llvm::Constant*> Methods;
362 std::vector<llvm::Constant*> Elements;
363 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
364 Elements.clear();
Daniel Dunbar61432932008-08-13 23:20:05 +0000365 llvm::Constant *C = CGM.GetAddrOfConstantCString(MethodSels[i].getName());
Chris Lattnera4210072008-06-26 05:08:00 +0000366 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000367 Elements.push_back(
368 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
369 llvm::Constant *Method =
370 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattnera4210072008-06-26 05:08:00 +0000371 MethodSels[i].getName(),
Chris Lattner550b8db2008-06-26 04:05:20 +0000372 isClassMethodList));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000373 Method = llvm::ConstantExpr::getBitCast(Method,
374 llvm::PointerType::getUnqual(IMPTy));
375 Elements.push_back(Method);
376 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
377 }
378
379 // Array of method structures
380 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Chris Lattnera4210072008-06-26 05:08:00 +0000381 MethodSels.size());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000382 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerfba67632008-06-26 04:52:29 +0000383 Methods);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000384
385 // Structure containing list pointer, array and array count
386 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
387 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
388 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
389 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
390 IntTy,
391 ObjCMethodArrayTy,
392 NULL);
393 // Refine next pointer type to concrete type
394 llvm::cast<llvm::OpaqueType>(
395 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
396 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
397
398 Methods.clear();
399 Methods.push_back(llvm::ConstantPointerNull::get(
400 llvm::PointerType::getUnqual(ObjCMethodListTy)));
401 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
402 MethodTypes.size()));
403 Methods.push_back(MethodArray);
404
405 // Create an instance of the structure
406 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
407}
408
409/// Generates an IvarList. Used in construction of a objc_class.
410llvm::Constant *CGObjCGNU::GenerateIvarList(
411 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
412 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
413 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
414 // Get the method structure type.
415 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
416 PtrToInt8Ty,
417 PtrToInt8Ty,
418 IntTy,
419 NULL);
420 std::vector<llvm::Constant*> Ivars;
421 std::vector<llvm::Constant*> Elements;
422 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
423 Elements.clear();
424 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
425 Zeros, 2));
426 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
427 Zeros, 2));
428 Elements.push_back(IvarOffsets[i]);
429 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
430 }
431
432 // Array of method structures
433 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
434 IvarNames.size());
435
436
437 Elements.clear();
438 Elements.push_back(llvm::ConstantInt::get(
439 llvm::cast<llvm::IntegerType>(IntTy), (int)IvarNames.size()));
440 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
441 // Structure containing array and array count
442 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
443 ObjCIvarArrayTy,
444 NULL);
445
446 // Create an instance of the structure
447 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
448}
449
450/// Generate a class structure
451llvm::Constant *CGObjCGNU::GenerateClassStructure(
452 llvm::Constant *MetaClass,
453 llvm::Constant *SuperClass,
454 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000455 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000456 llvm::Constant *Version,
457 llvm::Constant *InstanceSize,
458 llvm::Constant *IVars,
459 llvm::Constant *Methods,
460 llvm::Constant *Protocols) {
461 // Set up the class structure
462 // Note: Several of these are char*s when they should be ids. This is
463 // because the runtime performs this translation on load.
464 llvm::StructType *ClassTy = llvm::StructType::get(
465 PtrToInt8Ty, // class_pointer
466 PtrToInt8Ty, // super_class
467 PtrToInt8Ty, // name
468 LongTy, // version
469 LongTy, // info
470 LongTy, // instance_size
471 IVars->getType(), // ivars
472 Methods->getType(), // methods
473 // These are all filled in by the runtime, so we pretend
474 PtrTy, // dtable
475 PtrTy, // subclass_list
476 PtrTy, // sibling_class
477 PtrTy, // protocols
478 PtrTy, // gc_object_type
479 NULL);
480 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
481 llvm::Constant *NullP =
482 llvm::ConstantPointerNull::get(llvm::cast<llvm::PointerType>(PtrTy));
483 // Fill in the structure
484 std::vector<llvm::Constant*> Elements;
485 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
486 Elements.push_back(SuperClass);
Chris Lattnerd002cc62008-06-26 04:47:04 +0000487 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000488 Elements.push_back(Zero);
489 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
490 Elements.push_back(InstanceSize);
491 Elements.push_back(IVars);
492 Elements.push_back(Methods);
493 Elements.push_back(NullP);
494 Elements.push_back(NullP);
495 Elements.push_back(NullP);
496 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
497 Elements.push_back(NullP);
498 // Create an instance of the structure
Chris Lattner1565e032008-07-21 06:31:05 +0000499 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000500}
501
502llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
503 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
504 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
505 // Get the method structure type.
506 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
507 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
508 PtrToInt8Ty,
509 NULL);
510 std::vector<llvm::Constant*> Methods;
511 std::vector<llvm::Constant*> Elements;
512 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
513 Elements.clear();
514 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
515 Zeros, 2));
516 Elements.push_back(
517 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
518 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
519 }
520 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
521 MethodNames.size());
522 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
523 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
524 IntTy, ObjCMethodArrayTy, NULL);
525 Methods.clear();
526 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
527 Methods.push_back(Array);
528 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
529}
530// Create the protocol list structure used in classes, categories and so on
531llvm::Constant *CGObjCGNU::GenerateProtocolList(
532 const llvm::SmallVectorImpl<std::string> &Protocols) {
533 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
534 Protocols.size());
535 llvm::StructType *ProtocolListTy = llvm::StructType::get(
536 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
537 LongTy,//FIXME: Should be size_t
538 ProtocolArrayTy,
539 NULL);
540 std::vector<llvm::Constant*> Elements;
541 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
542 iter != endIter ; iter++) {
543 llvm::Constant *Ptr =
544 llvm::ConstantExpr::getBitCast(ExistingProtocols[*iter], PtrToInt8Ty);
545 Elements.push_back(Ptr);
546 }
547 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
548 Elements);
549 Elements.clear();
550 Elements.push_back(NULLPtr);
551 Elements.push_back(llvm::ConstantInt::get(
552 llvm::cast<llvm::IntegerType>(LongTy), Protocols.size()));
553 Elements.push_back(ProtocolArray);
554 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
555}
556
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000557llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000558 const ObjCProtocolDecl *PD) {
559 return ExistingProtocols[PD->getName()];
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000560}
561
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000562void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
563 ASTContext &Context = CGM.getContext();
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000564 const char *ProtocolName = PD->getIdentifierName();
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000565 llvm::SmallVector<std::string, 16> Protocols;
566 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
567 E = PD->protocol_end(); PI != E; ++PI)
568 Protocols.push_back((*PI)->getName());
569 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
570 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
571 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
572 E = PD->instmeth_end(); iter != E; iter++) {
573 std::string TypeStr;
574 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
575 InstanceMethodNames.push_back(
Daniel Dunbar61432932008-08-13 23:20:05 +0000576 CGM.GetAddrOfConstantCString((*iter)->getSelector().getName()));
577 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000578 }
579 // Collect information about class methods:
580 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
581 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
582 for (ObjCProtocolDecl::classmeth_iterator iter = PD->classmeth_begin(),
583 endIter = PD->classmeth_end() ; iter != endIter ; iter++) {
584 std::string TypeStr;
585 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
586 ClassMethodNames.push_back(
Daniel Dunbar61432932008-08-13 23:20:05 +0000587 CGM.GetAddrOfConstantCString((*iter)->getSelector().getName()));
588 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000589 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000590
591 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
592 llvm::Constant *InstanceMethodList =
593 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
594 llvm::Constant *ClassMethodList =
595 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
596 // Protocols are objects containing lists of the methods implemented and
597 // protocols adopted.
598 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
599 PtrToInt8Ty,
600 ProtocolList->getType(),
601 InstanceMethodList->getType(),
602 ClassMethodList->getType(),
603 NULL);
604 std::vector<llvm::Constant*> Elements;
605 // The isa pointer must be set to a magic number so the runtime knows it's
606 // the correct layout.
607 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
608 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
609 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
610 Elements.push_back(ProtocolList);
611 Elements.push_back(InstanceMethodList);
612 Elements.push_back(ClassMethodList);
613 ExistingProtocols[ProtocolName] =
614 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
615 ".objc_protocol"), IdTy);
616}
617
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000618void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000619 const char *ClassName = OCD->getClassInterface()->getIdentifierName();
620 const char *CategoryName = OCD->getIdentifierName();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000621 // Collect information about instance methods
622 llvm::SmallVector<Selector, 16> InstanceMethodSels;
623 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
624 for (ObjCCategoryDecl::instmeth_iterator iter = OCD->instmeth_begin(),
625 endIter = OCD->instmeth_end() ; iter != endIter ; iter++) {
626 InstanceMethodSels.push_back((*iter)->getSelector());
627 std::string TypeStr;
628 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
629 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
630 }
631
632 // Collect information about class methods
633 llvm::SmallVector<Selector, 16> ClassMethodSels;
634 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
635 for (ObjCCategoryDecl::classmeth_iterator iter = OCD->classmeth_begin(),
636 endIter = OCD->classmeth_end() ; iter != endIter ; iter++) {
637 ClassMethodSels.push_back((*iter)->getSelector());
638 std::string TypeStr;
639 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
640 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
641 }
642
643 // Collect the names of referenced protocols
644 llvm::SmallVector<std::string, 16> Protocols;
645 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
646 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
647 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
648 E = Protos.end(); I != E; ++I)
649 Protocols.push_back((*I)->getName());
650
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000651 std::vector<llvm::Constant*> Elements;
652 Elements.push_back(MakeConstantString(CategoryName));
653 Elements.push_back(MakeConstantString(ClassName));
654 // Instance method list
655 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000656 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000657 false), PtrTy));
658 // Class method list
659 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000660 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000661 PtrTy));
662 // Protocol list
663 Elements.push_back(llvm::ConstantExpr::getBitCast(
664 GenerateProtocolList(Protocols), PtrTy));
665 Categories.push_back(llvm::ConstantExpr::getBitCast(
666 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
667 PtrTy, PtrTy, NULL), Elements), PtrTy));
668}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000669
670void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
671 ASTContext &Context = CGM.getContext();
672
673 // Get the superclass name.
674 const ObjCInterfaceDecl * SuperClassDecl =
675 OID->getClassInterface()->getSuperClass();
676 const char * SuperClassName = NULL;
677 if (SuperClassDecl) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000678 SuperClassName = SuperClassDecl->getIdentifierName();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000679 }
680
681 // Get the class name
682 ObjCInterfaceDecl * ClassDecl = (ObjCInterfaceDecl*)OID->getClassInterface();
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000683 const char * ClassName = ClassDecl->getIdentifierName();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000684
685 // Get the size of instances. For runtimes that support late-bound instances
686 // this should probably be something different (size just of instance
687 // varaibles in this class, not superclasses?).
688 int instanceSize = 0;
689 const llvm::Type *ObjTy = 0;
690 if (!LateBoundIVars()) {
691 ObjTy = CGM.getTypes().ConvertType(Context.getObjCInterfaceType(ClassDecl));
692 instanceSize = CGM.getTargetData().getABITypeSize(ObjTy);
693 } else {
694 // This is required by newer ObjC runtimes.
695 assert(0 && "Late-bound instance variables not yet supported");
696 }
697
698 // Collect information about instance variables.
699 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
700 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
701 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
702 const llvm::StructLayout *Layout =
703 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(ObjTy));
704 ObjTy = llvm::PointerType::getUnqual(ObjTy);
705 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
706 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
707 // Store the name
708 IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter)->getName()));
709 // Get the type encoding for this ivar
710 std::string TypeStr;
Daniel Dunbar0d504c12008-10-17 20:21:44 +0000711 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000712 IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
713 // Get the offset
714 int offset =
715 (int)Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(*iter));
716 IvarOffsets.push_back(
717 llvm::ConstantInt::get(llvm::Type::Int32Ty, offset));
718 }
719
720 // Collect information about instance methods
721 llvm::SmallVector<Selector, 16> InstanceMethodSels;
722 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
723 for (ObjCImplementationDecl::instmeth_iterator iter = OID->instmeth_begin(),
724 endIter = OID->instmeth_end() ; iter != endIter ; iter++) {
725 InstanceMethodSels.push_back((*iter)->getSelector());
726 std::string TypeStr;
727 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
728 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
729 }
730
731 // Collect information about class methods
732 llvm::SmallVector<Selector, 16> ClassMethodSels;
733 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
734 for (ObjCImplementationDecl::classmeth_iterator iter = OID->classmeth_begin(),
735 endIter = OID->classmeth_end() ; iter != endIter ; iter++) {
736 ClassMethodSels.push_back((*iter)->getSelector());
737 std::string TypeStr;
738 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
739 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
740 }
741 // Collect the names of referenced protocols
742 llvm::SmallVector<std::string, 16> Protocols;
743 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
744 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
745 E = Protos.end(); I != E; ++I)
746 Protocols.push_back((*I)->getName());
747
748
749
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000750 // Get the superclass pointer.
751 llvm::Constant *SuperClass;
752 if (SuperClassName) {
753 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
754 } else {
755 SuperClass = llvm::ConstantPointerNull::get(
756 llvm::cast<llvm::PointerType>(PtrToInt8Ty));
757 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000758 // Empty vector used to construct empty method lists
759 llvm::SmallVector<llvm::Constant*, 1> empty;
760 // Generate the method and instance variable lists
761 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000762 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000763 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000764 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000765 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
766 IvarOffsets);
767 //Generate metaclass for class methods
768 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner1565e032008-07-21 06:31:05 +0000769 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000770 empty, empty, empty), ClassMethodList, NULLPtr);
771 // Generate the class structure
772 llvm::Constant *ClassStruct = GenerateClassStructure(MetaClassStruct,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000773 SuperClass, 0x1L, ClassName, 0,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000774 llvm::ConstantInt::get(llvm::Type::Int32Ty, instanceSize), IvarList,
775 MethodList, GenerateProtocolList(Protocols));
776 // Add class structure to list to be added to the symtab later
777 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
778 Classes.push_back(ClassStruct);
779}
780
781llvm::Function *CGObjCGNU::ModuleInitFunction() {
782 // Only emit an ObjC load function if no Objective-C stuff has been called
783 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
784 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov5f58b912008-06-01 15:14:46 +0000785 UntypedSelectors.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000786 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +0000787
788 // Name the ObjC types to make the IR a bit easier to read
789 TheModule.addTypeName(".objc_selector", SelectorTy);
790 TheModule.addTypeName(".objc_id", IdTy);
791 TheModule.addTypeName(".objc_imp", IMPTy);
792
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000793 std::vector<llvm::Constant*> Elements;
794 // Generate statics list:
795 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
796 ConstantStrings.size() + 1);
797 ConstantStrings.push_back(NULLPtr);
798 Elements.push_back(MakeConstantString("NSConstantString",
799 ".objc_static_class_name"));
800 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings));
801 llvm::StructType *StaticsListTy =
802 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
Chris Lattner630404b2008-06-26 04:10:42 +0000803 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000804 llvm::Constant *Statics =
805 MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Chris Lattner4e0b2642008-06-24 17:01:28 +0000806 llvm::ArrayType *StaticsListArrayTy =
Chris Lattner630404b2008-06-26 04:10:42 +0000807 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner4e0b2642008-06-24 17:01:28 +0000808 Elements.clear();
809 Elements.push_back(Statics);
Chris Lattner630404b2008-06-26 04:10:42 +0000810 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner4e0b2642008-06-24 17:01:28 +0000811 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000812 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
813 // Array of classes, categories, and constant objects
814 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
815 Classes.size() + Categories.size() + 2);
Chris Lattner630404b2008-06-26 04:10:42 +0000816 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelectorTy,
817 llvm::Type::Int16Ty,
818 llvm::Type::Int16Ty,
819 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000820
821 Elements.clear();
822 // Pointer to an array of selectors used in this module.
823 std::vector<llvm::Constant*> Selectors;
824 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
825 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
826 iter != iterEnd ; ++iter) {
Chris Lattner630404b2008-06-26 04:10:42 +0000827 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
828 Elements.push_back(MakeConstantString(iter->first.second,
829 ".objc_sel_types"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000830 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
831 Elements.clear();
832 }
833 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
834 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner630404b2008-06-26 04:10:42 +0000835 iter != iterEnd; ++iter) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000836 Elements.push_back(
Chris Lattner630404b2008-06-26 04:10:42 +0000837 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000838 Elements.push_back(NULLPtr);
839 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
840 Elements.clear();
841 }
842 Elements.push_back(NULLPtr);
843 Elements.push_back(NULLPtr);
844 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
845 Elements.clear();
846 // Number of static selectors
847 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
848 llvm::Constant *SelectorList = MakeGlobal(
849 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
850 ".objc_selector_list");
851 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList, SelectorTy));
852
853 // Now that all of the static selectors exist, create pointers to them.
854 int index = 0;
855 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
856 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
857 iter != iterEnd; ++iter) {
858 llvm::Constant *Idxs[] = {Zeros[0],
859 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
860 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
861 llvm::GlobalValue::InternalLinkage,
862 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
863 ".objc_sel_ptr", &TheModule);
864 (*iter).second->setAliasee(SelPtr);
865 }
866 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
867 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
868 iter != iterEnd; iter++) {
869 llvm::Constant *Idxs[] = {Zeros[0],
870 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
871 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
872 llvm::GlobalValue::InternalLinkage,
873 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
874 ".objc_sel_ptr", &TheModule);
875 (*iter).second->setAliasee(SelPtr);
876 }
877 // Number of classes defined.
878 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
879 Classes.size()));
880 // Number of categories defined
881 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
882 Categories.size()));
883 // Create an array of classes, then categories, then static object instances
884 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
885 // NULL-terminated list of static object instances (mainly constant strings)
886 Classes.push_back(Statics);
887 Classes.push_back(NULLPtr);
888 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
889 Elements.push_back(ClassList);
890 // Construct the symbol table
891 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
892
893 // The symbol table is contained in a module which has some version-checking
894 // constants
895 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
896 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
897 Elements.clear();
898 // Runtime version used for compatibility checking.
899 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
900 //FIXME: Should be sizeof(ModuleTy)
901 Elements.push_back(llvm::ConstantInt::get(LongTy, 16));
902 //FIXME: Should be the path to the file where this module was declared
903 Elements.push_back(NULLPtr);
904 Elements.push_back(SymTab);
905 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
906
907 // Create the load function calling the runtime entry point with the module
908 // structure
909 std::vector<const llvm::Type*> VoidArgs;
910 llvm::Function * LoadFunction = llvm::Function::Create(
911 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
912 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
913 &TheModule);
914 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000915 CGBuilderTy Builder;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000916 Builder.SetInsertPoint(EntryBB);
917 llvm::Value *Register = TheModule.getOrInsertFunction("__objc_exec_class",
918 llvm::Type::VoidTy, llvm::PointerType::getUnqual(ModuleTy), NULL);
919 Builder.CreateCall(Register, Module);
920 Builder.CreateRetVoid();
921 return LoadFunction;
922}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000923
924llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000925 const ObjCCategoryImplDecl *OCD =
926 dyn_cast<ObjCCategoryImplDecl>(OMD->getMethodContext());
927 const std::string &CategoryName = OCD ? OCD->getName() : "";
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000928 const std::string &ClassName = OMD->getClassInterface()->getName();
929 const std::string &MethodName = OMD->getSelector().getName();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000930 bool isClassMethod = !OMD->isInstance();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000931
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000932 const llvm::FunctionType *MethodTy =
933 CGM.getTypes().GetFunctionType(CGFunctionInfo(OMD, CGM.getContext()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000934 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
935 MethodName, isClassMethod);
936
Gabor Greif984d0b42008-04-06 20:42:52 +0000937 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000938 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000939 FunctionName,
Chris Lattner391d77a2008-03-30 23:03:07 +0000940 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +0000941 return Method;
942}
943
Daniel Dunbar49f66022008-09-24 03:38:44 +0000944llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
945 return 0;
946}
947
948llvm::Function *CGObjCGNU::GetPropertySetFunction() {
949 return 0;
950}
951
952llvm::Function *CGObjCGNU::EnumerationMutationFunction() {
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000953 return 0;
954}
955
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000956void CGObjCGNU::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +0000957 const ObjCAtTryStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000958 CGF.ErrorUnsupported(&S, "@try statement");
959}
960
961void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +0000962 const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000963 CGF.ErrorUnsupported(&S, "@throw statement");
964}
965
Chris Lattner10cac6f2008-11-15 21:26:17 +0000966void CGObjCGNU::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
967 const ObjCAtSynchronizedStmt &S) {
968 CGF.ErrorUnsupported(&S, "@synchronized statement");
969}
970
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000971CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
Chris Lattnerdce14062008-06-26 04:19:03 +0000972 return new CGObjCGNU(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +0000973}