blob: 9bcd816c6d8f083ffe73bdab1c0801c4b14e907f [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
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000127 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
128 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000129 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
130 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000131 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000132 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000133 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
134 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000135 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
136 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000137 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
138 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +0000139 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
140 llvm::Value *src, llvm::Value *dest);
Chris Lattner0f984262008-03-01 08:50:34 +0000141};
142} // end anonymous namespace
143
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000144
145
146static std::string SymbolNameForClass(const std::string &ClassName) {
147 return ".objc_class_" + ClassName;
148}
149
150static std::string SymbolNameForMethod(const std::string &ClassName, const
151 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
152{
153 return "._objc_method_" + ClassName +"("+CategoryName+")"+
154 (isClassMethod ? "+" : "-") + MethodName;
155}
156
Chris Lattnerdce14062008-06-26 04:19:03 +0000157CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
158 : CGM(cgm), TheModule(CGM.getModule()) {
159 IntTy = CGM.getTypes().ConvertType(CGM.getContext().IntTy);
160 LongTy = CGM.getTypes().ConvertType(CGM.getContext().LongTy);
161
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000162 Zeros[0] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
163 Zeros[1] = Zeros[0];
164 NULLPtr = llvm::ConstantPointerNull::get(
165 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
Chris Lattner391d77a2008-03-30 23:03:07 +0000166 // C string type. Used in lots of places.
167 PtrToInt8Ty =
168 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
169 // Get the selector Type.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000170 SelStructTy = llvm::StructType::get(
Chris Lattner391d77a2008-03-30 23:03:07 +0000171 PtrToInt8Ty,
172 PtrToInt8Ty,
173 NULL);
174 SelectorTy = llvm::PointerType::getUnqual(SelStructTy);
175 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
176 PtrTy = PtrToInt8Ty;
177
178 // Object type
179 llvm::PATypeHolder OpaqueObjTy = llvm::OpaqueType::get();
180 llvm::Type *OpaqueIdTy = llvm::PointerType::getUnqual(OpaqueObjTy);
181 IdTy = llvm::StructType::get(OpaqueIdTy, NULL);
182 llvm::cast<llvm::OpaqueType>(OpaqueObjTy.get())->refineAbstractTypeTo(IdTy);
183 IdTy = llvm::cast<llvm::StructType>(OpaqueObjTy.get());
184 IdTy = llvm::PointerType::getUnqual(IdTy);
185
186 // IMP type
187 std::vector<const llvm::Type*> IMPArgs;
188 IMPArgs.push_back(IdTy);
189 IMPArgs.push_back(SelectorTy);
190 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000191}
192// This has to perform the lookup every time, since posing and related
193// techniques can modify the name -> class mapping.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000194llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000195 const ObjCInterfaceDecl *OID) {
196 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getName());
197 ClassName = Builder.CreateStructGEP(ClassName, 0);
198
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000199 llvm::Constant *ClassLookupFn =
200 TheModule.getOrInsertFunction("objc_lookup_class", IdTy, PtrToInt8Ty,
201 NULL);
202 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner391d77a2008-03-30 23:03:07 +0000203}
204
Chris Lattner8e67b632008-06-26 04:37:12 +0000205/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000206llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Chris Lattner8e67b632008-06-26 04:37:12 +0000207 // FIXME: uniquing on the string is wasteful, unique on Sel instead!
Chris Lattner077bf5e2008-11-24 03:33:13 +0000208 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()];
Chris Lattner8e67b632008-06-26 04:37:12 +0000209 if (US == 0)
210 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
211 llvm::GlobalValue::InternalLinkage,
212 ".objc_untyped_selector_alias",
213 NULL, &TheModule);
214
215 return Builder.CreateLoad(US);
216
217}
218
Chris Lattner5e7dcc62008-06-26 04:44:19 +0000219llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
220 const std::string &Name) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000221 llvm::Constant * ConstStr = llvm::ConstantArray::get(Str);
222 ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true,
223 llvm::GlobalValue::InternalLinkage,
224 ConstStr, Name, &TheModule);
225 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
226}
227llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
228 std::vector<llvm::Constant*> &V, const std::string &Name) {
229 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
230 return new llvm::GlobalVariable(Ty, false,
231 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
232}
233llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
234 std::vector<llvm::Constant*> &V, const std::string &Name) {
235 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
236 return new llvm::GlobalVariable(Ty, false,
237 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
238}
239
240/// Generate an NSConstantString object.
241//TODO: In case there are any crazy people still using the GNU runtime without
242//an OpenStep implementation, this should let them select their own class for
243//constant strings.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000244llvm::Constant *CGObjCGNU::GenerateConstantString(const std::string &Str) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000245 std::vector<llvm::Constant*> Ivars;
246 Ivars.push_back(NULLPtr);
Chris Lattner13fd7e52008-06-21 21:44:18 +0000247 Ivars.push_back(MakeConstantString(Str));
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000248 Ivars.push_back(llvm::ConstantInt::get(IntTy, Str.size()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000249 llvm::Constant *ObjCStr = MakeGlobal(
250 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
251 Ivars, ".objc_str");
252 ConstantStrings.push_back(
253 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
254 return ObjCStr;
255}
256
257///Generates a message send where the super is the receiver. This is a message
258///send to self with special delivery semantics indicating which class's method
259///should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000260CodeGen::RValue
261CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000262 QualType ResultType,
263 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000264 const ObjCInterfaceDecl *Class,
265 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000266 bool IsClassMessage,
267 const CallArgList &CallArgs) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000268 const ObjCInterfaceDecl *SuperClass = Class->getSuperClass();
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000269 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000270 // TODO: This should be cached, not looked up every time.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000271 llvm::Value *ReceiverClass = GetClass(CGF.Builder, SuperClass);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000272 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Chris Lattner0f984262008-03-01 08:50:34 +0000273 std::vector<const llvm::Type*> impArgTypes;
274 impArgTypes.push_back(Receiver->getType());
Chris Lattner391d77a2008-03-30 23:03:07 +0000275 impArgTypes.push_back(SelectorTy);
Chris Lattner0f984262008-03-01 08:50:34 +0000276
277 // Avoid an explicit cast on the IMP by getting a version that has the right
278 // return type.
279 llvm::FunctionType *impType = llvm::FunctionType::get(ReturnTy, impArgTypes,
280 true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000281 // Construct the structure used to look up the IMP
282 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
283 IdTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000284 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Eli Friedman1e692ac2008-06-13 23:01:12 +0000285 // FIXME: volatility
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000286 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
287 CGF.Builder.CreateStore(ReceiverClass, CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000288
289 // Get the IMP
290 llvm::Constant *lookupFunction =
291 TheModule.getOrInsertFunction("objc_msg_lookup_super",
292 llvm::PointerType::getUnqual(impType),
293 llvm::PointerType::getUnqual(ObjCSuperTy),
294 SelectorTy, NULL);
295 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000296 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000297 lookupArgs+2);
298
299 // Call the method
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000300 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000301 ActualArgs.push_back(std::make_pair(RValue::get(Receiver),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000302 CGF.getContext().getObjCIdType()));
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000303 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000304 CGF.getContext().getObjCSelType()));
305 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000306 return CGF.EmitCall(imp, ResultType, ActualArgs);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000307}
308
309/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000310CodeGen::RValue
311CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000312 QualType ResultType,
313 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000314 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000315 bool IsClassMessage,
316 const CallArgList &CallArgs) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000317 const llvm::Type *ReturnTy = CGM.getTypes().ConvertType(ResultType);
318 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000319
320 // Look up the method implementation.
321 std::vector<const llvm::Type*> impArgTypes;
322 const llvm::Type *RetTy;
323 //TODO: Revisit this when LLVM supports aggregate return types.
324 if (ReturnTy->isSingleValueType() && ReturnTy != llvm::Type::VoidTy) {
325 RetTy = ReturnTy;
326 } else {
327 // For struct returns allocate the space in the caller and pass it up to
328 // the sender.
329 RetTy = llvm::Type::VoidTy;
330 impArgTypes.push_back(llvm::PointerType::getUnqual(ReturnTy));
331 }
332 impArgTypes.push_back(Receiver->getType());
333 impArgTypes.push_back(SelectorTy);
334
335 // Avoid an explicit cast on the IMP by getting a version that has the right
336 // return type.
337 llvm::FunctionType *impType = llvm::FunctionType::get(RetTy, impArgTypes,
338 true);
Chris Lattner0f984262008-03-01 08:50:34 +0000339
340 llvm::Constant *lookupFunction =
341 TheModule.getOrInsertFunction("objc_msg_lookup",
Chris Lattner391d77a2008-03-30 23:03:07 +0000342 llvm::PointerType::getUnqual(impType),
343 Receiver->getType(), SelectorTy, NULL);
Daniel Dunbar8f2926b2008-08-23 03:46:30 +0000344 llvm::Value *imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000345
346 // Call the method.
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000347 CallArgList ActualArgs;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000348 ActualArgs.push_back(std::make_pair(RValue::get(Receiver),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000349 CGF.getContext().getObjCIdType()));
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000350 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000351 CGF.getContext().getObjCSelType()));
352 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000353 return CGF.EmitCall(imp, ResultType, ActualArgs);
Chris Lattner0f984262008-03-01 08:50:34 +0000354}
355
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000356/// Generates a MethodList. Used in construction of a objc_class and
357/// objc_category structures.
358llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattnera4210072008-06-26 05:08:00 +0000359 const std::string &CategoryName,
360 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000361 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
362 bool isClassMethodList) {
363 // Get the method structure type.
364 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
365 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
366 PtrToInt8Ty, // Method types
367 llvm::PointerType::getUnqual(IMPTy), //Method pointer
368 NULL);
369 std::vector<llvm::Constant*> Methods;
370 std::vector<llvm::Constant*> Elements;
371 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
372 Elements.clear();
Chris Lattner077bf5e2008-11-24 03:33:13 +0000373 llvm::Constant *C =
374 CGM.GetAddrOfConstantCString(MethodSels[i].getAsString());
Chris Lattnera4210072008-06-26 05:08:00 +0000375 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000376 Elements.push_back(
377 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
378 llvm::Constant *Method =
379 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattner077bf5e2008-11-24 03:33:13 +0000380 MethodSels[i].getAsString(),
Chris Lattner550b8db2008-06-26 04:05:20 +0000381 isClassMethodList));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000382 Method = llvm::ConstantExpr::getBitCast(Method,
383 llvm::PointerType::getUnqual(IMPTy));
384 Elements.push_back(Method);
385 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
386 }
387
388 // Array of method structures
389 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Chris Lattnera4210072008-06-26 05:08:00 +0000390 MethodSels.size());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000391 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerfba67632008-06-26 04:52:29 +0000392 Methods);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000393
394 // Structure containing list pointer, array and array count
395 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
396 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
397 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
398 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
399 IntTy,
400 ObjCMethodArrayTy,
401 NULL);
402 // Refine next pointer type to concrete type
403 llvm::cast<llvm::OpaqueType>(
404 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
405 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
406
407 Methods.clear();
408 Methods.push_back(llvm::ConstantPointerNull::get(
409 llvm::PointerType::getUnqual(ObjCMethodListTy)));
410 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
411 MethodTypes.size()));
412 Methods.push_back(MethodArray);
413
414 // Create an instance of the structure
415 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
416}
417
418/// Generates an IvarList. Used in construction of a objc_class.
419llvm::Constant *CGObjCGNU::GenerateIvarList(
420 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
421 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
422 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
423 // Get the method structure type.
424 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
425 PtrToInt8Ty,
426 PtrToInt8Ty,
427 IntTy,
428 NULL);
429 std::vector<llvm::Constant*> Ivars;
430 std::vector<llvm::Constant*> Elements;
431 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
432 Elements.clear();
433 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
434 Zeros, 2));
435 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
436 Zeros, 2));
437 Elements.push_back(IvarOffsets[i]);
438 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
439 }
440
441 // Array of method structures
442 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
443 IvarNames.size());
444
445
446 Elements.clear();
447 Elements.push_back(llvm::ConstantInt::get(
448 llvm::cast<llvm::IntegerType>(IntTy), (int)IvarNames.size()));
449 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
450 // Structure containing array and array count
451 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
452 ObjCIvarArrayTy,
453 NULL);
454
455 // Create an instance of the structure
456 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
457}
458
459/// Generate a class structure
460llvm::Constant *CGObjCGNU::GenerateClassStructure(
461 llvm::Constant *MetaClass,
462 llvm::Constant *SuperClass,
463 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000464 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000465 llvm::Constant *Version,
466 llvm::Constant *InstanceSize,
467 llvm::Constant *IVars,
468 llvm::Constant *Methods,
469 llvm::Constant *Protocols) {
470 // Set up the class structure
471 // Note: Several of these are char*s when they should be ids. This is
472 // because the runtime performs this translation on load.
473 llvm::StructType *ClassTy = llvm::StructType::get(
474 PtrToInt8Ty, // class_pointer
475 PtrToInt8Ty, // super_class
476 PtrToInt8Ty, // name
477 LongTy, // version
478 LongTy, // info
479 LongTy, // instance_size
480 IVars->getType(), // ivars
481 Methods->getType(), // methods
482 // These are all filled in by the runtime, so we pretend
483 PtrTy, // dtable
484 PtrTy, // subclass_list
485 PtrTy, // sibling_class
486 PtrTy, // protocols
487 PtrTy, // gc_object_type
488 NULL);
489 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
490 llvm::Constant *NullP =
491 llvm::ConstantPointerNull::get(llvm::cast<llvm::PointerType>(PtrTy));
492 // Fill in the structure
493 std::vector<llvm::Constant*> Elements;
494 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
495 Elements.push_back(SuperClass);
Chris Lattnerd002cc62008-06-26 04:47:04 +0000496 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000497 Elements.push_back(Zero);
498 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
499 Elements.push_back(InstanceSize);
500 Elements.push_back(IVars);
501 Elements.push_back(Methods);
502 Elements.push_back(NullP);
503 Elements.push_back(NullP);
504 Elements.push_back(NullP);
505 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
506 Elements.push_back(NullP);
507 // Create an instance of the structure
Chris Lattner1565e032008-07-21 06:31:05 +0000508 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000509}
510
511llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
512 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
513 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
514 // Get the method structure type.
515 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
516 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
517 PtrToInt8Ty,
518 NULL);
519 std::vector<llvm::Constant*> Methods;
520 std::vector<llvm::Constant*> Elements;
521 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
522 Elements.clear();
523 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
524 Zeros, 2));
525 Elements.push_back(
526 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
527 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
528 }
529 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
530 MethodNames.size());
531 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
532 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
533 IntTy, ObjCMethodArrayTy, NULL);
534 Methods.clear();
535 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
536 Methods.push_back(Array);
537 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
538}
539// Create the protocol list structure used in classes, categories and so on
540llvm::Constant *CGObjCGNU::GenerateProtocolList(
541 const llvm::SmallVectorImpl<std::string> &Protocols) {
542 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
543 Protocols.size());
544 llvm::StructType *ProtocolListTy = llvm::StructType::get(
545 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
546 LongTy,//FIXME: Should be size_t
547 ProtocolArrayTy,
548 NULL);
549 std::vector<llvm::Constant*> Elements;
550 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
551 iter != endIter ; iter++) {
552 llvm::Constant *Ptr =
553 llvm::ConstantExpr::getBitCast(ExistingProtocols[*iter], PtrToInt8Ty);
554 Elements.push_back(Ptr);
555 }
556 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
557 Elements);
558 Elements.clear();
559 Elements.push_back(NULLPtr);
560 Elements.push_back(llvm::ConstantInt::get(
561 llvm::cast<llvm::IntegerType>(LongTy), Protocols.size()));
562 Elements.push_back(ProtocolArray);
563 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
564}
565
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000566llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000567 const ObjCProtocolDecl *PD) {
568 return ExistingProtocols[PD->getName()];
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000569}
570
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000571void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
572 ASTContext &Context = CGM.getContext();
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000573 const char *ProtocolName = PD->getIdentifierName();
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000574 llvm::SmallVector<std::string, 16> Protocols;
575 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
576 E = PD->protocol_end(); PI != E; ++PI)
577 Protocols.push_back((*PI)->getName());
578 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
579 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
580 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
581 E = PD->instmeth_end(); iter != E; iter++) {
582 std::string TypeStr;
583 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
584 InstanceMethodNames.push_back(
Chris Lattner077bf5e2008-11-24 03:33:13 +0000585 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar61432932008-08-13 23:20:05 +0000586 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000587 }
588 // Collect information about class methods:
589 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
590 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
591 for (ObjCProtocolDecl::classmeth_iterator iter = PD->classmeth_begin(),
592 endIter = PD->classmeth_end() ; iter != endIter ; iter++) {
593 std::string TypeStr;
594 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
595 ClassMethodNames.push_back(
Chris Lattner077bf5e2008-11-24 03:33:13 +0000596 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar61432932008-08-13 23:20:05 +0000597 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000598 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000599
600 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
601 llvm::Constant *InstanceMethodList =
602 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
603 llvm::Constant *ClassMethodList =
604 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
605 // Protocols are objects containing lists of the methods implemented and
606 // protocols adopted.
607 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
608 PtrToInt8Ty,
609 ProtocolList->getType(),
610 InstanceMethodList->getType(),
611 ClassMethodList->getType(),
612 NULL);
613 std::vector<llvm::Constant*> Elements;
614 // The isa pointer must be set to a magic number so the runtime knows it's
615 // the correct layout.
616 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
617 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
618 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
619 Elements.push_back(ProtocolList);
620 Elements.push_back(InstanceMethodList);
621 Elements.push_back(ClassMethodList);
622 ExistingProtocols[ProtocolName] =
623 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
624 ".objc_protocol"), IdTy);
625}
626
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000627void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000628 const char *ClassName = OCD->getClassInterface()->getIdentifierName();
629 const char *CategoryName = OCD->getIdentifierName();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000630 // Collect information about instance methods
631 llvm::SmallVector<Selector, 16> InstanceMethodSels;
632 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
633 for (ObjCCategoryDecl::instmeth_iterator iter = OCD->instmeth_begin(),
634 endIter = OCD->instmeth_end() ; iter != endIter ; iter++) {
635 InstanceMethodSels.push_back((*iter)->getSelector());
636 std::string TypeStr;
637 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
638 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
639 }
640
641 // Collect information about class methods
642 llvm::SmallVector<Selector, 16> ClassMethodSels;
643 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
644 for (ObjCCategoryDecl::classmeth_iterator iter = OCD->classmeth_begin(),
645 endIter = OCD->classmeth_end() ; iter != endIter ; iter++) {
646 ClassMethodSels.push_back((*iter)->getSelector());
647 std::string TypeStr;
648 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
649 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
650 }
651
652 // Collect the names of referenced protocols
653 llvm::SmallVector<std::string, 16> Protocols;
654 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
655 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
656 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
657 E = Protos.end(); I != E; ++I)
658 Protocols.push_back((*I)->getName());
659
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000660 std::vector<llvm::Constant*> Elements;
661 Elements.push_back(MakeConstantString(CategoryName));
662 Elements.push_back(MakeConstantString(ClassName));
663 // Instance method list
664 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000665 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000666 false), PtrTy));
667 // Class method list
668 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000669 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000670 PtrTy));
671 // Protocol list
672 Elements.push_back(llvm::ConstantExpr::getBitCast(
673 GenerateProtocolList(Protocols), PtrTy));
674 Categories.push_back(llvm::ConstantExpr::getBitCast(
675 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
676 PtrTy, PtrTy, NULL), Elements), PtrTy));
677}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000678
679void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
680 ASTContext &Context = CGM.getContext();
681
682 // Get the superclass name.
683 const ObjCInterfaceDecl * SuperClassDecl =
684 OID->getClassInterface()->getSuperClass();
685 const char * SuperClassName = NULL;
686 if (SuperClassDecl) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000687 SuperClassName = SuperClassDecl->getIdentifierName();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000688 }
689
690 // Get the class name
691 ObjCInterfaceDecl * ClassDecl = (ObjCInterfaceDecl*)OID->getClassInterface();
Douglas Gregor2e1cd422008-11-17 14:58:09 +0000692 const char * ClassName = ClassDecl->getIdentifierName();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000693
694 // Get the size of instances. For runtimes that support late-bound instances
695 // this should probably be something different (size just of instance
696 // varaibles in this class, not superclasses?).
697 int instanceSize = 0;
698 const llvm::Type *ObjTy = 0;
699 if (!LateBoundIVars()) {
700 ObjTy = CGM.getTypes().ConvertType(Context.getObjCInterfaceType(ClassDecl));
701 instanceSize = CGM.getTargetData().getABITypeSize(ObjTy);
702 } else {
703 // This is required by newer ObjC runtimes.
704 assert(0 && "Late-bound instance variables not yet supported");
705 }
706
707 // Collect information about instance variables.
708 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
709 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
710 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
711 const llvm::StructLayout *Layout =
712 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(ObjTy));
713 ObjTy = llvm::PointerType::getUnqual(ObjTy);
714 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
715 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
716 // Store the name
717 IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter)->getName()));
718 // Get the type encoding for this ivar
719 std::string TypeStr;
Daniel Dunbar0d504c12008-10-17 20:21:44 +0000720 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000721 IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
722 // Get the offset
723 int offset =
724 (int)Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(*iter));
725 IvarOffsets.push_back(
726 llvm::ConstantInt::get(llvm::Type::Int32Ty, offset));
727 }
728
729 // Collect information about instance methods
730 llvm::SmallVector<Selector, 16> InstanceMethodSels;
731 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
732 for (ObjCImplementationDecl::instmeth_iterator iter = OID->instmeth_begin(),
733 endIter = OID->instmeth_end() ; iter != endIter ; iter++) {
734 InstanceMethodSels.push_back((*iter)->getSelector());
735 std::string TypeStr;
736 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
737 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
738 }
739
740 // Collect information about class methods
741 llvm::SmallVector<Selector, 16> ClassMethodSels;
742 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
743 for (ObjCImplementationDecl::classmeth_iterator iter = OID->classmeth_begin(),
744 endIter = OID->classmeth_end() ; iter != endIter ; iter++) {
745 ClassMethodSels.push_back((*iter)->getSelector());
746 std::string TypeStr;
747 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
748 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
749 }
750 // Collect the names of referenced protocols
751 llvm::SmallVector<std::string, 16> Protocols;
752 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
753 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
754 E = Protos.end(); I != E; ++I)
755 Protocols.push_back((*I)->getName());
756
757
758
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000759 // Get the superclass pointer.
760 llvm::Constant *SuperClass;
761 if (SuperClassName) {
762 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
763 } else {
764 SuperClass = llvm::ConstantPointerNull::get(
765 llvm::cast<llvm::PointerType>(PtrToInt8Ty));
766 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000767 // Empty vector used to construct empty method lists
768 llvm::SmallVector<llvm::Constant*, 1> empty;
769 // Generate the method and instance variable lists
770 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000771 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000772 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000773 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000774 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
775 IvarOffsets);
776 //Generate metaclass for class methods
777 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner1565e032008-07-21 06:31:05 +0000778 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000779 empty, empty, empty), ClassMethodList, NULLPtr);
780 // Generate the class structure
781 llvm::Constant *ClassStruct = GenerateClassStructure(MetaClassStruct,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000782 SuperClass, 0x1L, ClassName, 0,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000783 llvm::ConstantInt::get(llvm::Type::Int32Ty, instanceSize), IvarList,
784 MethodList, GenerateProtocolList(Protocols));
785 // Add class structure to list to be added to the symtab later
786 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
787 Classes.push_back(ClassStruct);
788}
789
790llvm::Function *CGObjCGNU::ModuleInitFunction() {
791 // Only emit an ObjC load function if no Objective-C stuff has been called
792 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
793 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov5f58b912008-06-01 15:14:46 +0000794 UntypedSelectors.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000795 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +0000796
797 // Name the ObjC types to make the IR a bit easier to read
798 TheModule.addTypeName(".objc_selector", SelectorTy);
799 TheModule.addTypeName(".objc_id", IdTy);
800 TheModule.addTypeName(".objc_imp", IMPTy);
801
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000802 std::vector<llvm::Constant*> Elements;
803 // Generate statics list:
804 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
805 ConstantStrings.size() + 1);
806 ConstantStrings.push_back(NULLPtr);
807 Elements.push_back(MakeConstantString("NSConstantString",
808 ".objc_static_class_name"));
809 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings));
810 llvm::StructType *StaticsListTy =
811 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
Chris Lattner630404b2008-06-26 04:10:42 +0000812 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000813 llvm::Constant *Statics =
814 MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Chris Lattner4e0b2642008-06-24 17:01:28 +0000815 llvm::ArrayType *StaticsListArrayTy =
Chris Lattner630404b2008-06-26 04:10:42 +0000816 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner4e0b2642008-06-24 17:01:28 +0000817 Elements.clear();
818 Elements.push_back(Statics);
Chris Lattner630404b2008-06-26 04:10:42 +0000819 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner4e0b2642008-06-24 17:01:28 +0000820 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000821 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
822 // Array of classes, categories, and constant objects
823 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
824 Classes.size() + Categories.size() + 2);
Chris Lattner630404b2008-06-26 04:10:42 +0000825 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelectorTy,
826 llvm::Type::Int16Ty,
827 llvm::Type::Int16Ty,
828 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000829
830 Elements.clear();
831 // Pointer to an array of selectors used in this module.
832 std::vector<llvm::Constant*> Selectors;
833 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
834 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
835 iter != iterEnd ; ++iter) {
Chris Lattner630404b2008-06-26 04:10:42 +0000836 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
837 Elements.push_back(MakeConstantString(iter->first.second,
838 ".objc_sel_types"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000839 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
840 Elements.clear();
841 }
842 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
843 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner630404b2008-06-26 04:10:42 +0000844 iter != iterEnd; ++iter) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000845 Elements.push_back(
Chris Lattner630404b2008-06-26 04:10:42 +0000846 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000847 Elements.push_back(NULLPtr);
848 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
849 Elements.clear();
850 }
851 Elements.push_back(NULLPtr);
852 Elements.push_back(NULLPtr);
853 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
854 Elements.clear();
855 // Number of static selectors
856 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
857 llvm::Constant *SelectorList = MakeGlobal(
858 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
859 ".objc_selector_list");
860 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList, SelectorTy));
861
862 // Now that all of the static selectors exist, create pointers to them.
863 int index = 0;
864 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
865 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
866 iter != iterEnd; ++iter) {
867 llvm::Constant *Idxs[] = {Zeros[0],
868 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
869 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
870 llvm::GlobalValue::InternalLinkage,
871 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
872 ".objc_sel_ptr", &TheModule);
873 (*iter).second->setAliasee(SelPtr);
874 }
875 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
876 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
877 iter != iterEnd; iter++) {
878 llvm::Constant *Idxs[] = {Zeros[0],
879 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
880 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
881 llvm::GlobalValue::InternalLinkage,
882 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
883 ".objc_sel_ptr", &TheModule);
884 (*iter).second->setAliasee(SelPtr);
885 }
886 // Number of classes defined.
887 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
888 Classes.size()));
889 // Number of categories defined
890 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
891 Categories.size()));
892 // Create an array of classes, then categories, then static object instances
893 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
894 // NULL-terminated list of static object instances (mainly constant strings)
895 Classes.push_back(Statics);
896 Classes.push_back(NULLPtr);
897 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
898 Elements.push_back(ClassList);
899 // Construct the symbol table
900 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
901
902 // The symbol table is contained in a module which has some version-checking
903 // constants
904 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
905 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
906 Elements.clear();
907 // Runtime version used for compatibility checking.
908 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
909 //FIXME: Should be sizeof(ModuleTy)
910 Elements.push_back(llvm::ConstantInt::get(LongTy, 16));
911 //FIXME: Should be the path to the file where this module was declared
912 Elements.push_back(NULLPtr);
913 Elements.push_back(SymTab);
914 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
915
916 // Create the load function calling the runtime entry point with the module
917 // structure
918 std::vector<const llvm::Type*> VoidArgs;
919 llvm::Function * LoadFunction = llvm::Function::Create(
920 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
921 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
922 &TheModule);
923 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000924 CGBuilderTy Builder;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000925 Builder.SetInsertPoint(EntryBB);
926 llvm::Value *Register = TheModule.getOrInsertFunction("__objc_exec_class",
927 llvm::Type::VoidTy, llvm::PointerType::getUnqual(ModuleTy), NULL);
928 Builder.CreateCall(Register, Module);
929 Builder.CreateRetVoid();
930 return LoadFunction;
931}
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000932
933llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000934 const ObjCCategoryImplDecl *OCD =
935 dyn_cast<ObjCCategoryImplDecl>(OMD->getMethodContext());
Chris Lattner077bf5e2008-11-24 03:33:13 +0000936 std::string CategoryName = OCD ? OCD->getNameAsString() : "";
937 std::string ClassName = OMD->getClassInterface()->getNameAsString();
938 std::string MethodName = OMD->getSelector().getAsString();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000939 bool isClassMethod = !OMD->isInstance();
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000940
Daniel Dunbar45c25ba2008-09-10 04:01:49 +0000941 const llvm::FunctionType *MethodTy =
942 CGM.getTypes().GetFunctionType(CGFunctionInfo(OMD, CGM.getContext()));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000943 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
944 MethodName, isClassMethod);
945
Gabor Greif984d0b42008-04-06 20:42:52 +0000946 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000947 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000948 FunctionName,
Chris Lattner391d77a2008-03-30 23:03:07 +0000949 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +0000950 return Method;
951}
952
Daniel Dunbar49f66022008-09-24 03:38:44 +0000953llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
954 return 0;
955}
956
957llvm::Function *CGObjCGNU::GetPropertySetFunction() {
958 return 0;
959}
960
961llvm::Function *CGObjCGNU::EnumerationMutationFunction() {
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000962 return 0;
963}
964
Fariborz Jahanianbd71be42008-11-21 00:49:24 +0000965void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
966 const Stmt &S) {
967 CGF.ErrorUnsupported(&S, "@try/@synchronized statement");
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000968}
969
970void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar49f66022008-09-24 03:38:44 +0000971 const ObjCAtThrowStmt &S) {
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000972 CGF.ErrorUnsupported(&S, "@throw statement");
973}
974
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000975llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +0000976 llvm::Value *AddrWeakObj)
977{
978 return 0;
979}
980
Fariborz Jahanian3e283e32008-11-18 22:37:34 +0000981void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
982 llvm::Value *src, llvm::Value *dst)
983{
984 return;
985}
986
Fariborz Jahanian58626502008-11-19 00:59:10 +0000987void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
988 llvm::Value *src, llvm::Value *dst)
989{
990 return;
991}
992
Fariborz Jahanian7eda8362008-11-20 19:23:36 +0000993void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
994 llvm::Value *src, llvm::Value *dst)
995{
996 return;
997}
998
Fariborz Jahanian58626502008-11-19 00:59:10 +0000999void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1000 llvm::Value *src, llvm::Value *dst)
1001{
1002 return;
1003}
1004
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001005CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
Chris Lattnerdce14062008-06-26 04:19:03 +00001006 return new CGObjCGNU(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +00001007}