blob: 57c021f064227b46f926799b6028a5e13618599b [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"
19#include "clang/AST/ASTContext.h"
Chris Lattner0f984262008-03-01 08:50:34 +000020#include "llvm/Module.h"
21#include "llvm/Support/Compiler.h"
Chris Lattner50b36742008-04-13 07:32:11 +000022#include "llvm/Support/IRBuilder.h"
Chris Lattner0f984262008-03-01 08:50:34 +000023#include "llvm/ADT/SmallVector.h"
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000024#include "llvm/ADT/StringMap.h"
25#include <map>
Chris Lattnerdce14062008-06-26 04:19:03 +000026using namespace clang;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000027using llvm::dyn_cast;
28
29// The version of the runtime that this class targets. Must match the version
30// in the runtime.
31static const int RuntimeVersion = 8;
32static const int ProtocolVersion = 2;
Chris Lattner0f984262008-03-01 08:50:34 +000033
Chris Lattner0f984262008-03-01 08:50:34 +000034namespace {
Chris Lattnerdce14062008-06-26 04:19:03 +000035class CGObjCGNU : public CodeGen::CGObjCRuntime {
Chris Lattner0f984262008-03-01 08:50:34 +000036private:
Chris Lattnerdce14062008-06-26 04:19:03 +000037 CodeGen::CodeGenModule &CGM;
Chris Lattner0f984262008-03-01 08:50:34 +000038 llvm::Module &TheModule;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000039 const llvm::StructType *SelStructTy;
Chris Lattner391d77a2008-03-30 23:03:07 +000040 const llvm::Type *SelectorTy;
41 const llvm::Type *PtrToInt8Ty;
42 const llvm::Type *IMPTy;
43 const llvm::Type *IdTy;
44 const llvm::Type *IntTy;
45 const llvm::Type *PtrTy;
46 const llvm::Type *LongTy;
47 const llvm::Type *PtrToIntTy;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000048 std::vector<llvm::Constant*> Classes;
49 std::vector<llvm::Constant*> Categories;
50 std::vector<llvm::Constant*> ConstantStrings;
51 llvm::Function *LoadFunction;
52 llvm::StringMap<llvm::Constant*> ExistingProtocols;
53 typedef std::pair<std::string, std::string> TypedSelector;
54 std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors;
55 llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors;
56 // Some zeros used for GEPs in lots of places.
57 llvm::Constant *Zeros[2];
58 llvm::Constant *NULLPtr;
59private:
60 llvm::Constant *GenerateIvarList(
61 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
62 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
63 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets);
64 llvm::Constant *GenerateMethodList(const std::string &ClassName,
65 const std::string &CategoryName,
Chris Lattnera4210072008-06-26 05:08:00 +000066 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000067 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
68 bool isClassMethodList);
69 llvm::Constant *GenerateProtocolList(
70 const llvm::SmallVectorImpl<std::string> &Protocols);
71 llvm::Constant *GenerateClassStructure(
72 llvm::Constant *MetaClass,
73 llvm::Constant *SuperClass,
74 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +000075 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000076 llvm::Constant *Version,
77 llvm::Constant *InstanceSize,
78 llvm::Constant *IVars,
79 llvm::Constant *Methods,
80 llvm::Constant *Protocols);
81 llvm::Constant *GenerateProtocolMethodList(
82 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
83 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes);
84 llvm::Constant *MakeConstantString(const std::string &Str, const std::string
85 &Name="");
86 llvm::Constant *MakeGlobal(const llvm::StructType *Ty,
87 std::vector<llvm::Constant*> &V, const std::string &Name="");
88 llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty,
89 std::vector<llvm::Constant*> &V, const std::string &Name="");
Chris Lattner0f984262008-03-01 08:50:34 +000090public:
Chris Lattnerdce14062008-06-26 04:19:03 +000091 CGObjCGNU(CodeGen::CodeGenModule &cgm);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000092 virtual llvm::Constant *GenerateConstantString(const char *String,
93 const size_t length);
94 virtual llvm::Value *GenerateMessageSend(llvm::IRBuilder &Builder,
Chris Lattner0f984262008-03-01 08:50:34 +000095 const llvm::Type *ReturnTy,
Chris Lattner391d77a2008-03-30 23:03:07 +000096 llvm::Value *Sender,
Chris Lattner0f984262008-03-01 08:50:34 +000097 llvm::Value *Receiver,
Chris Lattner9384c762008-06-26 04:42:20 +000098 Selector Sel,
Chris Lattner0f984262008-03-01 08:50:34 +000099 llvm::Value** ArgV,
100 unsigned ArgC);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000101 virtual llvm::Value *GenerateMessageSendSuper(llvm::IRBuilder &Builder,
Chris Lattner8e67b632008-06-26 04:37:12 +0000102 const llvm::Type *ReturnTy,
103 llvm::Value *Sender,
104 const char *SuperClassName,
105 llvm::Value *Receiver,
106 Selector Sel,
107 llvm::Value** ArgV,
108 unsigned ArgC);
Chris Lattner9384c762008-06-26 04:42:20 +0000109 virtual llvm::Value *LookupClass(llvm::IRBuilder &Builder,
110 llvm::Value *ClassName);
Chris Lattner42ba3e72008-06-26 04:38:58 +0000111 virtual llvm::Value *GetSelector(llvm::IRBuilder &Builder, Selector Sel);
Chris Lattner8e67b632008-06-26 04:37:12 +0000112
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000113 virtual llvm::Function *MethodPreamble(
114 const std::string &ClassName,
115 const std::string &CategoryName,
116 const std::string &MethodName,
117 const llvm::Type *ReturnTy,
118 const llvm::Type *SelfTy,
119 const llvm::Type **ArgTy,
120 unsigned ArgC,
121 bool isClassMethod,
122 bool isVarArg);
123 virtual void GenerateCategory(const char *ClassName, const char *CategoryName,
Chris Lattnera4210072008-06-26 05:08:00 +0000124 const llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000125 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
Chris Lattnera4210072008-06-26 05:08:00 +0000126 const llvm::SmallVectorImpl<Selector> &ClassMethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000127 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
128 const llvm::SmallVectorImpl<std::string> &Protocols);
129 virtual void GenerateClass(
130 const char *ClassName,
131 const char *SuperClassName,
132 const int instanceSize,
133 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
134 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
135 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets,
Chris Lattnera4210072008-06-26 05:08:00 +0000136 const llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000137 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
Chris Lattnera4210072008-06-26 05:08:00 +0000138 const llvm::SmallVectorImpl<Selector> &ClassMethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000139 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
140 const llvm::SmallVectorImpl<std::string> &Protocols);
141 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder &Builder, const char
142 *ProtocolName);
143 virtual void GenerateProtocol(const char *ProtocolName,
144 const llvm::SmallVectorImpl<std::string> &Protocols,
145 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
146 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
147 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
148 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes);
149 virtual llvm::Function *ModuleInitFunction();
Chris Lattner0f984262008-03-01 08:50:34 +0000150};
151} // end anonymous namespace
152
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000153
154
155static std::string SymbolNameForClass(const std::string &ClassName) {
156 return ".objc_class_" + ClassName;
157}
158
159static std::string SymbolNameForMethod(const std::string &ClassName, const
160 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
161{
162 return "._objc_method_" + ClassName +"("+CategoryName+")"+
163 (isClassMethod ? "+" : "-") + MethodName;
164}
165
Chris Lattnerdce14062008-06-26 04:19:03 +0000166CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
167 : CGM(cgm), TheModule(CGM.getModule()) {
168 IntTy = CGM.getTypes().ConvertType(CGM.getContext().IntTy);
169 LongTy = CGM.getTypes().ConvertType(CGM.getContext().LongTy);
170
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000171 Zeros[0] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
172 Zeros[1] = Zeros[0];
173 NULLPtr = llvm::ConstantPointerNull::get(
174 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
Chris Lattner391d77a2008-03-30 23:03:07 +0000175 // C string type. Used in lots of places.
176 PtrToInt8Ty =
177 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
178 // Get the selector Type.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000179 SelStructTy = llvm::StructType::get(
Chris Lattner391d77a2008-03-30 23:03:07 +0000180 PtrToInt8Ty,
181 PtrToInt8Ty,
182 NULL);
183 SelectorTy = llvm::PointerType::getUnqual(SelStructTy);
184 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
185 PtrTy = PtrToInt8Ty;
186
187 // Object type
188 llvm::PATypeHolder OpaqueObjTy = llvm::OpaqueType::get();
189 llvm::Type *OpaqueIdTy = llvm::PointerType::getUnqual(OpaqueObjTy);
190 IdTy = llvm::StructType::get(OpaqueIdTy, NULL);
191 llvm::cast<llvm::OpaqueType>(OpaqueObjTy.get())->refineAbstractTypeTo(IdTy);
192 IdTy = llvm::cast<llvm::StructType>(OpaqueObjTy.get());
193 IdTy = llvm::PointerType::getUnqual(IdTy);
194
195 // IMP type
196 std::vector<const llvm::Type*> IMPArgs;
197 IMPArgs.push_back(IdTy);
198 IMPArgs.push_back(SelectorTy);
199 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000200}
201// This has to perform the lookup every time, since posing and related
202// techniques can modify the name -> class mapping.
203llvm::Value *CGObjCGNU::LookupClass(llvm::IRBuilder &Builder,
204 llvm::Value *ClassName) {
205 llvm::Constant *ClassLookupFn =
206 TheModule.getOrInsertFunction("objc_lookup_class", IdTy, PtrToInt8Ty,
207 NULL);
208 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner391d77a2008-03-30 23:03:07 +0000209}
210
Chris Lattner8e67b632008-06-26 04:37:12 +0000211/// GetSelector - Return the pointer to the unique'd string for this selector.
212llvm::Value *CGObjCGNU::GetSelector(llvm::IRBuilder &Builder, Selector Sel) {
213 // FIXME: uniquing on the string is wasteful, unique on Sel instead!
214 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getName()];
215 if (US == 0)
216 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
217 llvm::GlobalValue::InternalLinkage,
218 ".objc_untyped_selector_alias",
219 NULL, &TheModule);
220
221 return Builder.CreateLoad(US);
222
223}
224
Chris Lattner5e7dcc62008-06-26 04:44:19 +0000225llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
226 const std::string &Name) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000227 llvm::Constant * ConstStr = llvm::ConstantArray::get(Str);
228 ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true,
229 llvm::GlobalValue::InternalLinkage,
230 ConstStr, Name, &TheModule);
231 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
232}
233llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
234 std::vector<llvm::Constant*> &V, const std::string &Name) {
235 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
236 return new llvm::GlobalVariable(Ty, false,
237 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
238}
239llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
240 std::vector<llvm::Constant*> &V, const std::string &Name) {
241 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
242 return new llvm::GlobalVariable(Ty, false,
243 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
244}
245
246/// Generate an NSConstantString object.
247//TODO: In case there are any crazy people still using the GNU runtime without
248//an OpenStep implementation, this should let them select their own class for
249//constant strings.
250llvm::Constant *CGObjCGNU::GenerateConstantString(const char *String, const
251 size_t length) {
Chris Lattner13fd7e52008-06-21 21:44:18 +0000252 std::string Str(String, String +length);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000253 std::vector<llvm::Constant*> Ivars;
254 Ivars.push_back(NULLPtr);
Chris Lattner13fd7e52008-06-21 21:44:18 +0000255 Ivars.push_back(MakeConstantString(Str));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000256 Ivars.push_back(llvm::ConstantInt::get(IntTy, length));
257 llvm::Constant *ObjCStr = MakeGlobal(
258 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
259 Ivars, ".objc_str");
260 ConstantStrings.push_back(
261 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
262 return ObjCStr;
263}
264
265///Generates a message send where the super is the receiver. This is a message
266///send to self with special delivery semantics indicating which class's method
267///should be called.
268llvm::Value *CGObjCGNU::GenerateMessageSendSuper(llvm::IRBuilder &Builder,
Chris Lattner8e67b632008-06-26 04:37:12 +0000269 const llvm::Type *ReturnTy,
270 llvm::Value *Sender,
271 const char *SuperClassName,
272 llvm::Value *Receiver,
273 Selector Sel,
274 llvm::Value** ArgV,
275 unsigned ArgC) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000276 // TODO: This should be cached, not looked up every time.
277 llvm::Value *ReceiverClass = LookupClass(Builder,
278 MakeConstantString(SuperClassName));
Chris Lattner8e67b632008-06-26 04:37:12 +0000279 llvm::Value *cmd = GetSelector(Builder, Sel);
Chris Lattner0f984262008-03-01 08:50:34 +0000280 std::vector<const llvm::Type*> impArgTypes;
281 impArgTypes.push_back(Receiver->getType());
Chris Lattner391d77a2008-03-30 23:03:07 +0000282 impArgTypes.push_back(SelectorTy);
Chris Lattner0f984262008-03-01 08:50:34 +0000283
284 // Avoid an explicit cast on the IMP by getting a version that has the right
285 // return type.
286 llvm::FunctionType *impType = llvm::FunctionType::get(ReturnTy, impArgTypes,
287 true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000288 // Construct the structure used to look up the IMP
289 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
290 IdTy, NULL);
291 llvm::Value *ObjCSuper = Builder.CreateAlloca(ObjCSuperTy);
Eli Friedman1e692ac2008-06-13 23:01:12 +0000292 // FIXME: volatility
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000293 Builder.CreateStore(Receiver, Builder.CreateStructGEP(ObjCSuper, 0));
294 Builder.CreateStore(ReceiverClass, Builder.CreateStructGEP(ObjCSuper, 1));
295
296 // Get the IMP
297 llvm::Constant *lookupFunction =
298 TheModule.getOrInsertFunction("objc_msg_lookup_super",
299 llvm::PointerType::getUnqual(impType),
300 llvm::PointerType::getUnqual(ObjCSuperTy),
301 SelectorTy, NULL);
302 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
303 llvm::Value *imp = Builder.CreateCall(lookupFunction, lookupArgs,
304 lookupArgs+2);
305
306 // Call the method
307 llvm::SmallVector<llvm::Value*, 8> callArgs;
308 callArgs.push_back(Receiver);
309 callArgs.push_back(cmd);
310 callArgs.insert(callArgs.end(), ArgV, ArgV+ArgC);
311 return Builder.CreateCall(imp, callArgs.begin(), callArgs.end());
312}
313
314/// Generate code for a message send expression.
315llvm::Value *CGObjCGNU::GenerateMessageSend(llvm::IRBuilder &Builder,
316 const llvm::Type *ReturnTy,
317 llvm::Value *Sender,
318 llvm::Value *Receiver,
Chris Lattner9384c762008-06-26 04:42:20 +0000319 Selector Sel,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000320 llvm::Value** ArgV,
321 unsigned ArgC) {
Chris Lattner9384c762008-06-26 04:42:20 +0000322 llvm::Value *cmd = GetSelector(Builder, Sel);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000323
324 // Look up the method implementation.
325 std::vector<const llvm::Type*> impArgTypes;
326 const llvm::Type *RetTy;
327 //TODO: Revisit this when LLVM supports aggregate return types.
328 if (ReturnTy->isSingleValueType() && ReturnTy != llvm::Type::VoidTy) {
329 RetTy = ReturnTy;
330 } else {
331 // For struct returns allocate the space in the caller and pass it up to
332 // the sender.
333 RetTy = llvm::Type::VoidTy;
334 impArgTypes.push_back(llvm::PointerType::getUnqual(ReturnTy));
335 }
336 impArgTypes.push_back(Receiver->getType());
337 impArgTypes.push_back(SelectorTy);
338
339 // Avoid an explicit cast on the IMP by getting a version that has the right
340 // return type.
341 llvm::FunctionType *impType = llvm::FunctionType::get(RetTy, impArgTypes,
342 true);
Chris Lattner0f984262008-03-01 08:50:34 +0000343
344 llvm::Constant *lookupFunction =
345 TheModule.getOrInsertFunction("objc_msg_lookup",
Chris Lattner391d77a2008-03-30 23:03:07 +0000346 llvm::PointerType::getUnqual(impType),
347 Receiver->getType(), SelectorTy, NULL);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000348 llvm::Value *imp = Builder.CreateCall2(lookupFunction, Receiver, cmd);
349
350 // Call the method.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000351 llvm::SmallVector<llvm::Value*, 16> Args;
352 if (!ReturnTy->isSingleValueType()) {
353 llvm::Value *Return = Builder.CreateAlloca(ReturnTy);
354 Args.push_back(Return);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000355 }
356 Args.push_back(Receiver);
357 Args.push_back(cmd);
358 Args.insert(Args.end(), ArgV, ArgV+ArgC);
Chris Lattner8fdf3282008-06-24 17:04:18 +0000359 if (!ReturnTy->isSingleValueType()) {
360 Builder.CreateCall(imp, Args.begin(), Args.end());
361 return Args[0];
362 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000363 return Builder.CreateCall(imp, Args.begin(), Args.end());
Chris Lattner0f984262008-03-01 08:50:34 +0000364}
365
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000366/// Generates a MethodList. Used in construction of a objc_class and
367/// objc_category structures.
368llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattnera4210072008-06-26 05:08:00 +0000369 const std::string &CategoryName,
370 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000371 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
372 bool isClassMethodList) {
373 // Get the method structure type.
374 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
375 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
376 PtrToInt8Ty, // Method types
377 llvm::PointerType::getUnqual(IMPTy), //Method pointer
378 NULL);
379 std::vector<llvm::Constant*> Methods;
380 std::vector<llvm::Constant*> Elements;
381 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
382 Elements.clear();
Chris Lattnera4210072008-06-26 05:08:00 +0000383 llvm::Constant *C = CGM.GetAddrOfConstantString(MethodSels[i].getName());
384 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000385 Elements.push_back(
386 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
387 llvm::Constant *Method =
388 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattnera4210072008-06-26 05:08:00 +0000389 MethodSels[i].getName(),
Chris Lattner550b8db2008-06-26 04:05:20 +0000390 isClassMethodList));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000391 Method = llvm::ConstantExpr::getBitCast(Method,
392 llvm::PointerType::getUnqual(IMPTy));
393 Elements.push_back(Method);
394 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
395 }
396
397 // Array of method structures
398 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Chris Lattnera4210072008-06-26 05:08:00 +0000399 MethodSels.size());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000400 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerfba67632008-06-26 04:52:29 +0000401 Methods);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000402
403 // Structure containing list pointer, array and array count
404 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
405 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
406 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
407 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
408 IntTy,
409 ObjCMethodArrayTy,
410 NULL);
411 // Refine next pointer type to concrete type
412 llvm::cast<llvm::OpaqueType>(
413 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
414 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
415
416 Methods.clear();
417 Methods.push_back(llvm::ConstantPointerNull::get(
418 llvm::PointerType::getUnqual(ObjCMethodListTy)));
419 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
420 MethodTypes.size()));
421 Methods.push_back(MethodArray);
422
423 // Create an instance of the structure
424 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
425}
426
427/// Generates an IvarList. Used in construction of a objc_class.
428llvm::Constant *CGObjCGNU::GenerateIvarList(
429 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
430 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
431 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
432 // Get the method structure type.
433 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
434 PtrToInt8Ty,
435 PtrToInt8Ty,
436 IntTy,
437 NULL);
438 std::vector<llvm::Constant*> Ivars;
439 std::vector<llvm::Constant*> Elements;
440 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
441 Elements.clear();
442 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
443 Zeros, 2));
444 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
445 Zeros, 2));
446 Elements.push_back(IvarOffsets[i]);
447 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
448 }
449
450 // Array of method structures
451 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
452 IvarNames.size());
453
454
455 Elements.clear();
456 Elements.push_back(llvm::ConstantInt::get(
457 llvm::cast<llvm::IntegerType>(IntTy), (int)IvarNames.size()));
458 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
459 // Structure containing array and array count
460 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
461 ObjCIvarArrayTy,
462 NULL);
463
464 // Create an instance of the structure
465 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
466}
467
468/// Generate a class structure
469llvm::Constant *CGObjCGNU::GenerateClassStructure(
470 llvm::Constant *MetaClass,
471 llvm::Constant *SuperClass,
472 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000473 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000474 llvm::Constant *Version,
475 llvm::Constant *InstanceSize,
476 llvm::Constant *IVars,
477 llvm::Constant *Methods,
478 llvm::Constant *Protocols) {
479 // Set up the class structure
480 // Note: Several of these are char*s when they should be ids. This is
481 // because the runtime performs this translation on load.
482 llvm::StructType *ClassTy = llvm::StructType::get(
483 PtrToInt8Ty, // class_pointer
484 PtrToInt8Ty, // super_class
485 PtrToInt8Ty, // name
486 LongTy, // version
487 LongTy, // info
488 LongTy, // instance_size
489 IVars->getType(), // ivars
490 Methods->getType(), // methods
491 // These are all filled in by the runtime, so we pretend
492 PtrTy, // dtable
493 PtrTy, // subclass_list
494 PtrTy, // sibling_class
495 PtrTy, // protocols
496 PtrTy, // gc_object_type
497 NULL);
498 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
499 llvm::Constant *NullP =
500 llvm::ConstantPointerNull::get(llvm::cast<llvm::PointerType>(PtrTy));
501 // Fill in the structure
502 std::vector<llvm::Constant*> Elements;
503 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
504 Elements.push_back(SuperClass);
Chris Lattnerd002cc62008-06-26 04:47:04 +0000505 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000506 Elements.push_back(Zero);
507 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
508 Elements.push_back(InstanceSize);
509 Elements.push_back(IVars);
510 Elements.push_back(Methods);
511 Elements.push_back(NullP);
512 Elements.push_back(NullP);
513 Elements.push_back(NullP);
514 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
515 Elements.push_back(NullP);
516 // Create an instance of the structure
Chris Lattner1565e032008-07-21 06:31:05 +0000517 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000518}
519
520llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
521 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
522 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
523 // Get the method structure type.
524 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
525 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
526 PtrToInt8Ty,
527 NULL);
528 std::vector<llvm::Constant*> Methods;
529 std::vector<llvm::Constant*> Elements;
530 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
531 Elements.clear();
532 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
533 Zeros, 2));
534 Elements.push_back(
535 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
536 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
537 }
538 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
539 MethodNames.size());
540 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
541 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
542 IntTy, ObjCMethodArrayTy, NULL);
543 Methods.clear();
544 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
545 Methods.push_back(Array);
546 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
547}
548// Create the protocol list structure used in classes, categories and so on
549llvm::Constant *CGObjCGNU::GenerateProtocolList(
550 const llvm::SmallVectorImpl<std::string> &Protocols) {
551 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
552 Protocols.size());
553 llvm::StructType *ProtocolListTy = llvm::StructType::get(
554 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
555 LongTy,//FIXME: Should be size_t
556 ProtocolArrayTy,
557 NULL);
558 std::vector<llvm::Constant*> Elements;
559 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
560 iter != endIter ; iter++) {
561 llvm::Constant *Ptr =
562 llvm::ConstantExpr::getBitCast(ExistingProtocols[*iter], PtrToInt8Ty);
563 Elements.push_back(Ptr);
564 }
565 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
566 Elements);
567 Elements.clear();
568 Elements.push_back(NULLPtr);
569 Elements.push_back(llvm::ConstantInt::get(
570 llvm::cast<llvm::IntegerType>(LongTy), Protocols.size()));
571 Elements.push_back(ProtocolArray);
572 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
573}
574
575llvm::Value *CGObjCGNU::GenerateProtocolRef(llvm::IRBuilder &Builder, const
576 char *ProtocolName) {
577 return ExistingProtocols[ProtocolName];
578}
579
580void CGObjCGNU::GenerateProtocol(const char *ProtocolName,
581 const llvm::SmallVectorImpl<std::string> &Protocols,
582 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
583 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
584 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
585 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes) {
586
587 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
588 llvm::Constant *InstanceMethodList =
589 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
590 llvm::Constant *ClassMethodList =
591 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
592 // Protocols are objects containing lists of the methods implemented and
593 // protocols adopted.
594 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
595 PtrToInt8Ty,
596 ProtocolList->getType(),
597 InstanceMethodList->getType(),
598 ClassMethodList->getType(),
599 NULL);
600 std::vector<llvm::Constant*> Elements;
601 // The isa pointer must be set to a magic number so the runtime knows it's
602 // the correct layout.
603 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
604 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
605 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
606 Elements.push_back(ProtocolList);
607 Elements.push_back(InstanceMethodList);
608 Elements.push_back(ClassMethodList);
609 ExistingProtocols[ProtocolName] =
610 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
611 ".objc_protocol"), IdTy);
612}
613
614void CGObjCGNU::GenerateCategory(
615 const char *ClassName,
616 const char *CategoryName,
Chris Lattnera4210072008-06-26 05:08:00 +0000617 const llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000618 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
Chris Lattnera4210072008-06-26 05:08:00 +0000619 const llvm::SmallVectorImpl<Selector> &ClassMethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000620 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
621 const llvm::SmallVectorImpl<std::string> &Protocols) {
622 std::vector<llvm::Constant*> Elements;
623 Elements.push_back(MakeConstantString(CategoryName));
624 Elements.push_back(MakeConstantString(ClassName));
625 // Instance method list
626 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000627 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000628 false), PtrTy));
629 // Class method list
630 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000631 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000632 PtrTy));
633 // Protocol list
634 Elements.push_back(llvm::ConstantExpr::getBitCast(
635 GenerateProtocolList(Protocols), PtrTy));
636 Categories.push_back(llvm::ConstantExpr::getBitCast(
637 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
638 PtrTy, PtrTy, NULL), Elements), PtrTy));
639}
640void CGObjCGNU::GenerateClass(
641 const char *ClassName,
642 const char *SuperClassName,
643 const int instanceSize,
644 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
645 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
646 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets,
Chris Lattnera4210072008-06-26 05:08:00 +0000647 const llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000648 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
Chris Lattnera4210072008-06-26 05:08:00 +0000649 const llvm::SmallVectorImpl<Selector> &ClassMethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000650 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
651 const llvm::SmallVectorImpl<std::string> &Protocols) {
652 // Get the superclass pointer.
653 llvm::Constant *SuperClass;
654 if (SuperClassName) {
655 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
656 } else {
657 SuperClass = llvm::ConstantPointerNull::get(
658 llvm::cast<llvm::PointerType>(PtrToInt8Ty));
659 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000660 // Empty vector used to construct empty method lists
661 llvm::SmallVector<llvm::Constant*, 1> empty;
662 // Generate the method and instance variable lists
663 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000664 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000665 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000666 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000667 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
668 IvarOffsets);
669 //Generate metaclass for class methods
670 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner1565e032008-07-21 06:31:05 +0000671 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000672 empty, empty, empty), ClassMethodList, NULLPtr);
673 // Generate the class structure
674 llvm::Constant *ClassStruct = GenerateClassStructure(MetaClassStruct,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000675 SuperClass, 0x1L, ClassName, 0,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000676 llvm::ConstantInt::get(llvm::Type::Int32Ty, instanceSize), IvarList,
677 MethodList, GenerateProtocolList(Protocols));
678 // Add class structure to list to be added to the symtab later
679 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
680 Classes.push_back(ClassStruct);
681}
682
683llvm::Function *CGObjCGNU::ModuleInitFunction() {
684 // Only emit an ObjC load function if no Objective-C stuff has been called
685 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
686 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov5f58b912008-06-01 15:14:46 +0000687 UntypedSelectors.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000688 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +0000689
690 // Name the ObjC types to make the IR a bit easier to read
691 TheModule.addTypeName(".objc_selector", SelectorTy);
692 TheModule.addTypeName(".objc_id", IdTy);
693 TheModule.addTypeName(".objc_imp", IMPTy);
694
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000695 std::vector<llvm::Constant*> Elements;
696 // Generate statics list:
697 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
698 ConstantStrings.size() + 1);
699 ConstantStrings.push_back(NULLPtr);
700 Elements.push_back(MakeConstantString("NSConstantString",
701 ".objc_static_class_name"));
702 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings));
703 llvm::StructType *StaticsListTy =
704 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
Chris Lattner630404b2008-06-26 04:10:42 +0000705 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000706 llvm::Constant *Statics =
707 MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Chris Lattner4e0b2642008-06-24 17:01:28 +0000708 llvm::ArrayType *StaticsListArrayTy =
Chris Lattner630404b2008-06-26 04:10:42 +0000709 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner4e0b2642008-06-24 17:01:28 +0000710 Elements.clear();
711 Elements.push_back(Statics);
Chris Lattner630404b2008-06-26 04:10:42 +0000712 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner4e0b2642008-06-24 17:01:28 +0000713 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000714 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
715 // Array of classes, categories, and constant objects
716 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
717 Classes.size() + Categories.size() + 2);
Chris Lattner630404b2008-06-26 04:10:42 +0000718 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelectorTy,
719 llvm::Type::Int16Ty,
720 llvm::Type::Int16Ty,
721 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000722
723 Elements.clear();
724 // Pointer to an array of selectors used in this module.
725 std::vector<llvm::Constant*> Selectors;
726 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
727 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
728 iter != iterEnd ; ++iter) {
Chris Lattner630404b2008-06-26 04:10:42 +0000729 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
730 Elements.push_back(MakeConstantString(iter->first.second,
731 ".objc_sel_types"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000732 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
733 Elements.clear();
734 }
735 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
736 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner630404b2008-06-26 04:10:42 +0000737 iter != iterEnd; ++iter) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000738 Elements.push_back(
Chris Lattner630404b2008-06-26 04:10:42 +0000739 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000740 Elements.push_back(NULLPtr);
741 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
742 Elements.clear();
743 }
744 Elements.push_back(NULLPtr);
745 Elements.push_back(NULLPtr);
746 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
747 Elements.clear();
748 // Number of static selectors
749 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
750 llvm::Constant *SelectorList = MakeGlobal(
751 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
752 ".objc_selector_list");
753 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList, SelectorTy));
754
755 // Now that all of the static selectors exist, create pointers to them.
756 int index = 0;
757 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
758 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
759 iter != iterEnd; ++iter) {
760 llvm::Constant *Idxs[] = {Zeros[0],
761 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
762 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
763 llvm::GlobalValue::InternalLinkage,
764 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
765 ".objc_sel_ptr", &TheModule);
766 (*iter).second->setAliasee(SelPtr);
767 }
768 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
769 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
770 iter != iterEnd; iter++) {
771 llvm::Constant *Idxs[] = {Zeros[0],
772 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
773 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
774 llvm::GlobalValue::InternalLinkage,
775 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
776 ".objc_sel_ptr", &TheModule);
777 (*iter).second->setAliasee(SelPtr);
778 }
779 // Number of classes defined.
780 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
781 Classes.size()));
782 // Number of categories defined
783 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
784 Categories.size()));
785 // Create an array of classes, then categories, then static object instances
786 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
787 // NULL-terminated list of static object instances (mainly constant strings)
788 Classes.push_back(Statics);
789 Classes.push_back(NULLPtr);
790 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
791 Elements.push_back(ClassList);
792 // Construct the symbol table
793 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
794
795 // The symbol table is contained in a module which has some version-checking
796 // constants
797 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
798 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
799 Elements.clear();
800 // Runtime version used for compatibility checking.
801 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
802 //FIXME: Should be sizeof(ModuleTy)
803 Elements.push_back(llvm::ConstantInt::get(LongTy, 16));
804 //FIXME: Should be the path to the file where this module was declared
805 Elements.push_back(NULLPtr);
806 Elements.push_back(SymTab);
807 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
808
809 // Create the load function calling the runtime entry point with the module
810 // structure
811 std::vector<const llvm::Type*> VoidArgs;
812 llvm::Function * LoadFunction = llvm::Function::Create(
813 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
814 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
815 &TheModule);
816 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
817 llvm::IRBuilder Builder;
818 Builder.SetInsertPoint(EntryBB);
819 llvm::Value *Register = TheModule.getOrInsertFunction("__objc_exec_class",
820 llvm::Type::VoidTy, llvm::PointerType::getUnqual(ModuleTy), NULL);
821 Builder.CreateCall(Register, Module);
822 Builder.CreateRetVoid();
823 return LoadFunction;
824}
Chris Lattner391d77a2008-03-30 23:03:07 +0000825llvm::Function *CGObjCGNU::MethodPreamble(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000826 const std::string &ClassName,
827 const std::string &CategoryName,
828 const std::string &MethodName,
Chris Lattner391d77a2008-03-30 23:03:07 +0000829 const llvm::Type *ReturnTy,
830 const llvm::Type *SelfTy,
831 const llvm::Type **ArgTy,
832 unsigned ArgC,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000833 bool isClassMethod,
Chris Lattner391d77a2008-03-30 23:03:07 +0000834 bool isVarArg) {
835 std::vector<const llvm::Type*> Args;
Chris Lattner8fdf3282008-06-24 17:04:18 +0000836 if (!ReturnTy->isSingleValueType() && ReturnTy != llvm::Type::VoidTy) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000837 Args.push_back(llvm::PointerType::getUnqual(ReturnTy));
838 ReturnTy = llvm::Type::VoidTy;
839 }
Chris Lattner391d77a2008-03-30 23:03:07 +0000840 Args.push_back(SelfTy);
841 Args.push_back(SelectorTy);
842 Args.insert(Args.end(), ArgTy, ArgTy+ArgC);
843
844 llvm::FunctionType *MethodTy = llvm::FunctionType::get(ReturnTy,
845 Args,
846 isVarArg);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000847 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
848 MethodName, isClassMethod);
849
Gabor Greif984d0b42008-04-06 20:42:52 +0000850 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000851 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000852 FunctionName,
Chris Lattner391d77a2008-03-30 23:03:07 +0000853 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +0000854 llvm::Function::arg_iterator AI = Method->arg_begin();
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000855 // Name the struct return argument.
856 // FIXME: This is probably the wrong test.
857 if (!ReturnTy->isFirstClassType() && ReturnTy != llvm::Type::VoidTy) {
858 AI->setName("agg.result");
859 ++AI;
860 }
Chris Lattner391d77a2008-03-30 23:03:07 +0000861 AI->setName("self");
862 ++AI;
863 AI->setName("_cmd");
864 return Method;
865}
866
Chris Lattnerdce14062008-06-26 04:19:03 +0000867CodeGen::CGObjCRuntime *CodeGen::CreateObjCRuntime(CodeGen::CodeGenModule &CGM){
868 return new CGObjCGNU(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +0000869}