blob: c4d236b54d4c0f60b71a3e460057df1e21c7f150 [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"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Decl.h"
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +000021#include "clang/AST/DeclObjC.h"
Chris Lattner0f984262008-03-01 08:50:34 +000022#include "llvm/Module.h"
23#include "llvm/Support/Compiler.h"
Chris Lattner50b36742008-04-13 07:32:11 +000024#include "llvm/Support/IRBuilder.h"
Chris Lattner0f984262008-03-01 08:50:34 +000025#include "llvm/ADT/SmallVector.h"
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000026#include "llvm/ADT/StringMap.h"
27#include <map>
Chris Lattnerdce14062008-06-26 04:19:03 +000028using namespace clang;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000029using llvm::dyn_cast;
30
31// The version of the runtime that this class targets. Must match the version
32// in the runtime.
33static const int RuntimeVersion = 8;
34static const int ProtocolVersion = 2;
Chris Lattner0f984262008-03-01 08:50:34 +000035
Chris Lattner0f984262008-03-01 08:50:34 +000036namespace {
Chris Lattnerdce14062008-06-26 04:19:03 +000037class CGObjCGNU : public CodeGen::CGObjCRuntime {
Chris Lattner0f984262008-03-01 08:50:34 +000038private:
Chris Lattnerdce14062008-06-26 04:19:03 +000039 CodeGen::CodeGenModule &CGM;
Chris Lattner0f984262008-03-01 08:50:34 +000040 llvm::Module &TheModule;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000041 const llvm::StructType *SelStructTy;
Chris Lattner391d77a2008-03-30 23:03:07 +000042 const llvm::Type *SelectorTy;
43 const llvm::Type *PtrToInt8Ty;
44 const llvm::Type *IMPTy;
45 const llvm::Type *IdTy;
46 const llvm::Type *IntTy;
47 const llvm::Type *PtrTy;
48 const llvm::Type *LongTy;
49 const llvm::Type *PtrToIntTy;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000050 std::vector<llvm::Constant*> Classes;
51 std::vector<llvm::Constant*> Categories;
52 std::vector<llvm::Constant*> ConstantStrings;
53 llvm::Function *LoadFunction;
54 llvm::StringMap<llvm::Constant*> ExistingProtocols;
55 typedef std::pair<std::string, std::string> TypedSelector;
56 std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors;
57 llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors;
58 // Some zeros used for GEPs in lots of places.
59 llvm::Constant *Zeros[2];
60 llvm::Constant *NULLPtr;
61private:
62 llvm::Constant *GenerateIvarList(
63 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
64 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
65 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets);
66 llvm::Constant *GenerateMethodList(const std::string &ClassName,
67 const std::string &CategoryName,
Chris Lattnera4210072008-06-26 05:08:00 +000068 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000069 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
70 bool isClassMethodList);
71 llvm::Constant *GenerateProtocolList(
72 const llvm::SmallVectorImpl<std::string> &Protocols);
73 llvm::Constant *GenerateClassStructure(
74 llvm::Constant *MetaClass,
75 llvm::Constant *SuperClass,
76 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +000077 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000078 llvm::Constant *Version,
79 llvm::Constant *InstanceSize,
80 llvm::Constant *IVars,
81 llvm::Constant *Methods,
82 llvm::Constant *Protocols);
83 llvm::Constant *GenerateProtocolMethodList(
84 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
85 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes);
86 llvm::Constant *MakeConstantString(const std::string &Str, const std::string
87 &Name="");
88 llvm::Constant *MakeGlobal(const llvm::StructType *Ty,
89 std::vector<llvm::Constant*> &V, const std::string &Name="");
90 llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty,
91 std::vector<llvm::Constant*> &V, const std::string &Name="");
Chris Lattner0f984262008-03-01 08:50:34 +000092public:
Chris Lattnerdce14062008-06-26 04:19:03 +000093 CGObjCGNU(CodeGen::CodeGenModule &cgm);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000094 virtual llvm::Constant *GenerateConstantString(const std::string &String);
Chris Lattner85e35682008-08-08 19:57:58 +000095 virtual llvm::Value *GenerateMessageSend(llvm::IRBuilder<> &Builder,
Chris Lattner0f984262008-03-01 08:50:34 +000096 const llvm::Type *ReturnTy,
97 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);
Chris Lattner85e35682008-08-08 19:57:58 +0000101 virtual llvm::Value *GenerateMessageSendSuper(llvm::IRBuilder<> &Builder,
Chris Lattner8e67b632008-06-26 04:37:12 +0000102 const llvm::Type *ReturnTy,
Chris Lattner8e67b632008-06-26 04:37:12 +0000103 const char *SuperClassName,
104 llvm::Value *Receiver,
105 Selector Sel,
106 llvm::Value** ArgV,
107 unsigned ArgC);
Chris Lattner85e35682008-08-08 19:57:58 +0000108 virtual llvm::Value *LookupClass(llvm::IRBuilder<> &Builder,
Chris Lattner9384c762008-06-26 04:42:20 +0000109 llvm::Value *ClassName);
Chris Lattner85e35682008-08-08 19:57:58 +0000110 virtual llvm::Value *GetSelector(llvm::IRBuilder<> &Builder, Selector Sel);
Chris Lattner8e67b632008-06-26 04:37:12 +0000111
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000112 virtual llvm::Function *MethodPreamble(
113 const std::string &ClassName,
114 const std::string &CategoryName,
115 const std::string &MethodName,
116 const llvm::Type *ReturnTy,
117 const llvm::Type *SelfTy,
118 const llvm::Type **ArgTy,
119 unsigned ArgC,
120 bool isClassMethod,
121 bool isVarArg);
122 virtual void GenerateCategory(const char *ClassName, const char *CategoryName,
Chris Lattnera4210072008-06-26 05:08:00 +0000123 const llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000124 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
Chris Lattnera4210072008-06-26 05:08:00 +0000125 const llvm::SmallVectorImpl<Selector> &ClassMethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000126 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
127 const llvm::SmallVectorImpl<std::string> &Protocols);
128 virtual void GenerateClass(
129 const char *ClassName,
130 const char *SuperClassName,
131 const int instanceSize,
132 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
133 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
134 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets,
Chris Lattnera4210072008-06-26 05:08:00 +0000135 const llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000136 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
Chris Lattnera4210072008-06-26 05:08:00 +0000137 const llvm::SmallVectorImpl<Selector> &ClassMethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000138 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
139 const llvm::SmallVectorImpl<std::string> &Protocols);
Chris Lattner85e35682008-08-08 19:57:58 +0000140 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<> &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000141 const ObjCProtocolDecl *PD);
142 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000143 virtual llvm::Function *ModuleInitFunction();
Chris Lattner0f984262008-03-01 08:50:34 +0000144};
145} // end anonymous namespace
146
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000147
148
149static std::string SymbolNameForClass(const std::string &ClassName) {
150 return ".objc_class_" + ClassName;
151}
152
153static std::string SymbolNameForMethod(const std::string &ClassName, const
154 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
155{
156 return "._objc_method_" + ClassName +"("+CategoryName+")"+
157 (isClassMethod ? "+" : "-") + MethodName;
158}
159
Chris Lattnerdce14062008-06-26 04:19:03 +0000160CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
161 : CGM(cgm), TheModule(CGM.getModule()) {
162 IntTy = CGM.getTypes().ConvertType(CGM.getContext().IntTy);
163 LongTy = CGM.getTypes().ConvertType(CGM.getContext().LongTy);
164
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000165 Zeros[0] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
166 Zeros[1] = Zeros[0];
167 NULLPtr = llvm::ConstantPointerNull::get(
168 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
Chris Lattner391d77a2008-03-30 23:03:07 +0000169 // C string type. Used in lots of places.
170 PtrToInt8Ty =
171 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
172 // Get the selector Type.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000173 SelStructTy = llvm::StructType::get(
Chris Lattner391d77a2008-03-30 23:03:07 +0000174 PtrToInt8Ty,
175 PtrToInt8Ty,
176 NULL);
177 SelectorTy = llvm::PointerType::getUnqual(SelStructTy);
178 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
179 PtrTy = PtrToInt8Ty;
180
181 // Object type
182 llvm::PATypeHolder OpaqueObjTy = llvm::OpaqueType::get();
183 llvm::Type *OpaqueIdTy = llvm::PointerType::getUnqual(OpaqueObjTy);
184 IdTy = llvm::StructType::get(OpaqueIdTy, NULL);
185 llvm::cast<llvm::OpaqueType>(OpaqueObjTy.get())->refineAbstractTypeTo(IdTy);
186 IdTy = llvm::cast<llvm::StructType>(OpaqueObjTy.get());
187 IdTy = llvm::PointerType::getUnqual(IdTy);
188
189 // IMP type
190 std::vector<const llvm::Type*> IMPArgs;
191 IMPArgs.push_back(IdTy);
192 IMPArgs.push_back(SelectorTy);
193 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000194}
195// This has to perform the lookup every time, since posing and related
196// techniques can modify the name -> class mapping.
Chris Lattner85e35682008-08-08 19:57:58 +0000197llvm::Value *CGObjCGNU::LookupClass(llvm::IRBuilder<> &Builder,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000198 llvm::Value *ClassName) {
199 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.
Chris Lattner85e35682008-08-08 19:57:58 +0000206llvm::Value *CGObjCGNU::GetSelector(llvm::IRBuilder<> &Builder, Selector Sel) {
Chris Lattner8e67b632008-06-26 04:37:12 +0000207 // FIXME: uniquing on the string is wasteful, unique on Sel instead!
208 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getName()];
209 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.
Chris Lattner85e35682008-08-08 19:57:58 +0000260llvm::Value *CGObjCGNU::GenerateMessageSendSuper(llvm::IRBuilder<> &Builder,
Chris Lattner8e67b632008-06-26 04:37:12 +0000261 const llvm::Type *ReturnTy,
Chris Lattner8e67b632008-06-26 04:37:12 +0000262 const char *SuperClassName,
263 llvm::Value *Receiver,
264 Selector Sel,
265 llvm::Value** ArgV,
266 unsigned ArgC) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000267 // TODO: This should be cached, not looked up every time.
268 llvm::Value *ReceiverClass = LookupClass(Builder,
269 MakeConstantString(SuperClassName));
Chris Lattner8e67b632008-06-26 04:37:12 +0000270 llvm::Value *cmd = GetSelector(Builder, Sel);
Chris Lattner0f984262008-03-01 08:50:34 +0000271 std::vector<const llvm::Type*> impArgTypes;
272 impArgTypes.push_back(Receiver->getType());
Chris Lattner391d77a2008-03-30 23:03:07 +0000273 impArgTypes.push_back(SelectorTy);
Chris Lattner0f984262008-03-01 08:50:34 +0000274
275 // Avoid an explicit cast on the IMP by getting a version that has the right
276 // return type.
277 llvm::FunctionType *impType = llvm::FunctionType::get(ReturnTy, impArgTypes,
278 true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000279 // Construct the structure used to look up the IMP
280 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
281 IdTy, NULL);
282 llvm::Value *ObjCSuper = Builder.CreateAlloca(ObjCSuperTy);
Eli Friedman1e692ac2008-06-13 23:01:12 +0000283 // FIXME: volatility
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000284 Builder.CreateStore(Receiver, Builder.CreateStructGEP(ObjCSuper, 0));
285 Builder.CreateStore(ReceiverClass, Builder.CreateStructGEP(ObjCSuper, 1));
286
287 // Get the IMP
288 llvm::Constant *lookupFunction =
289 TheModule.getOrInsertFunction("objc_msg_lookup_super",
290 llvm::PointerType::getUnqual(impType),
291 llvm::PointerType::getUnqual(ObjCSuperTy),
292 SelectorTy, NULL);
293 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
294 llvm::Value *imp = Builder.CreateCall(lookupFunction, lookupArgs,
295 lookupArgs+2);
296
297 // Call the method
298 llvm::SmallVector<llvm::Value*, 8> callArgs;
299 callArgs.push_back(Receiver);
300 callArgs.push_back(cmd);
301 callArgs.insert(callArgs.end(), ArgV, ArgV+ArgC);
302 return Builder.CreateCall(imp, callArgs.begin(), callArgs.end());
303}
304
305/// Generate code for a message send expression.
Chris Lattner85e35682008-08-08 19:57:58 +0000306llvm::Value *CGObjCGNU::GenerateMessageSend(llvm::IRBuilder<> &Builder,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000307 const llvm::Type *ReturnTy,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000308 llvm::Value *Receiver,
Chris Lattner9384c762008-06-26 04:42:20 +0000309 Selector Sel,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000310 llvm::Value** ArgV,
311 unsigned ArgC) {
Chris Lattner9384c762008-06-26 04:42:20 +0000312 llvm::Value *cmd = GetSelector(Builder, Sel);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000313
314 // Look up the method implementation.
315 std::vector<const llvm::Type*> impArgTypes;
316 const llvm::Type *RetTy;
317 //TODO: Revisit this when LLVM supports aggregate return types.
318 if (ReturnTy->isSingleValueType() && ReturnTy != llvm::Type::VoidTy) {
319 RetTy = ReturnTy;
320 } else {
321 // For struct returns allocate the space in the caller and pass it up to
322 // the sender.
323 RetTy = llvm::Type::VoidTy;
324 impArgTypes.push_back(llvm::PointerType::getUnqual(ReturnTy));
325 }
326 impArgTypes.push_back(Receiver->getType());
327 impArgTypes.push_back(SelectorTy);
328
329 // Avoid an explicit cast on the IMP by getting a version that has the right
330 // return type.
331 llvm::FunctionType *impType = llvm::FunctionType::get(RetTy, impArgTypes,
332 true);
Chris Lattner0f984262008-03-01 08:50:34 +0000333
334 llvm::Constant *lookupFunction =
335 TheModule.getOrInsertFunction("objc_msg_lookup",
Chris Lattner391d77a2008-03-30 23:03:07 +0000336 llvm::PointerType::getUnqual(impType),
337 Receiver->getType(), SelectorTy, NULL);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000338 llvm::Value *imp = Builder.CreateCall2(lookupFunction, Receiver, cmd);
339
340 // Call the method.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000341 llvm::SmallVector<llvm::Value*, 16> Args;
342 if (!ReturnTy->isSingleValueType()) {
343 llvm::Value *Return = Builder.CreateAlloca(ReturnTy);
344 Args.push_back(Return);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000345 }
346 Args.push_back(Receiver);
347 Args.push_back(cmd);
348 Args.insert(Args.end(), ArgV, ArgV+ArgC);
Chris Lattner8fdf3282008-06-24 17:04:18 +0000349 if (!ReturnTy->isSingleValueType()) {
350 Builder.CreateCall(imp, Args.begin(), Args.end());
351 return Args[0];
352 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000353 return Builder.CreateCall(imp, Args.begin(), Args.end());
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();
Daniel Dunbar61432932008-08-13 23:20:05 +0000373 llvm::Constant *C = CGM.GetAddrOfConstantCString(MethodSels[i].getName());
Chris Lattnera4210072008-06-26 05:08:00 +0000374 Elements.push_back(llvm::ConstantExpr::getGetElementPtr(C, Zeros, 2));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000375 Elements.push_back(
376 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
377 llvm::Constant *Method =
378 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattnera4210072008-06-26 05:08:00 +0000379 MethodSels[i].getName(),
Chris Lattner550b8db2008-06-26 04:05:20 +0000380 isClassMethodList));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000381 Method = llvm::ConstantExpr::getBitCast(Method,
382 llvm::PointerType::getUnqual(IMPTy));
383 Elements.push_back(Method);
384 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
385 }
386
387 // Array of method structures
388 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
Chris Lattnera4210072008-06-26 05:08:00 +0000389 MethodSels.size());
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000390 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
Chris Lattnerfba67632008-06-26 04:52:29 +0000391 Methods);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000392
393 // Structure containing list pointer, array and array count
394 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
395 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
396 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
397 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
398 IntTy,
399 ObjCMethodArrayTy,
400 NULL);
401 // Refine next pointer type to concrete type
402 llvm::cast<llvm::OpaqueType>(
403 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
404 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
405
406 Methods.clear();
407 Methods.push_back(llvm::ConstantPointerNull::get(
408 llvm::PointerType::getUnqual(ObjCMethodListTy)));
409 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
410 MethodTypes.size()));
411 Methods.push_back(MethodArray);
412
413 // Create an instance of the structure
414 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
415}
416
417/// Generates an IvarList. Used in construction of a objc_class.
418llvm::Constant *CGObjCGNU::GenerateIvarList(
419 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
420 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
421 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
422 // Get the method structure type.
423 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
424 PtrToInt8Ty,
425 PtrToInt8Ty,
426 IntTy,
427 NULL);
428 std::vector<llvm::Constant*> Ivars;
429 std::vector<llvm::Constant*> Elements;
430 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
431 Elements.clear();
432 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
433 Zeros, 2));
434 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
435 Zeros, 2));
436 Elements.push_back(IvarOffsets[i]);
437 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
438 }
439
440 // Array of method structures
441 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
442 IvarNames.size());
443
444
445 Elements.clear();
446 Elements.push_back(llvm::ConstantInt::get(
447 llvm::cast<llvm::IntegerType>(IntTy), (int)IvarNames.size()));
448 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
449 // Structure containing array and array count
450 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
451 ObjCIvarArrayTy,
452 NULL);
453
454 // Create an instance of the structure
455 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
456}
457
458/// Generate a class structure
459llvm::Constant *CGObjCGNU::GenerateClassStructure(
460 llvm::Constant *MetaClass,
461 llvm::Constant *SuperClass,
462 unsigned info,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000463 const char *Name,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000464 llvm::Constant *Version,
465 llvm::Constant *InstanceSize,
466 llvm::Constant *IVars,
467 llvm::Constant *Methods,
468 llvm::Constant *Protocols) {
469 // Set up the class structure
470 // Note: Several of these are char*s when they should be ids. This is
471 // because the runtime performs this translation on load.
472 llvm::StructType *ClassTy = llvm::StructType::get(
473 PtrToInt8Ty, // class_pointer
474 PtrToInt8Ty, // super_class
475 PtrToInt8Ty, // name
476 LongTy, // version
477 LongTy, // info
478 LongTy, // instance_size
479 IVars->getType(), // ivars
480 Methods->getType(), // methods
481 // These are all filled in by the runtime, so we pretend
482 PtrTy, // dtable
483 PtrTy, // subclass_list
484 PtrTy, // sibling_class
485 PtrTy, // protocols
486 PtrTy, // gc_object_type
487 NULL);
488 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
489 llvm::Constant *NullP =
490 llvm::ConstantPointerNull::get(llvm::cast<llvm::PointerType>(PtrTy));
491 // Fill in the structure
492 std::vector<llvm::Constant*> Elements;
493 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
494 Elements.push_back(SuperClass);
Chris Lattnerd002cc62008-06-26 04:47:04 +0000495 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000496 Elements.push_back(Zero);
497 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
498 Elements.push_back(InstanceSize);
499 Elements.push_back(IVars);
500 Elements.push_back(Methods);
501 Elements.push_back(NullP);
502 Elements.push_back(NullP);
503 Elements.push_back(NullP);
504 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
505 Elements.push_back(NullP);
506 // Create an instance of the structure
Chris Lattner1565e032008-07-21 06:31:05 +0000507 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000508}
509
510llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
511 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
512 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
513 // Get the method structure type.
514 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
515 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
516 PtrToInt8Ty,
517 NULL);
518 std::vector<llvm::Constant*> Methods;
519 std::vector<llvm::Constant*> Elements;
520 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
521 Elements.clear();
522 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
523 Zeros, 2));
524 Elements.push_back(
525 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
526 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
527 }
528 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
529 MethodNames.size());
530 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
531 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
532 IntTy, ObjCMethodArrayTy, NULL);
533 Methods.clear();
534 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
535 Methods.push_back(Array);
536 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
537}
538// Create the protocol list structure used in classes, categories and so on
539llvm::Constant *CGObjCGNU::GenerateProtocolList(
540 const llvm::SmallVectorImpl<std::string> &Protocols) {
541 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
542 Protocols.size());
543 llvm::StructType *ProtocolListTy = llvm::StructType::get(
544 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
545 LongTy,//FIXME: Should be size_t
546 ProtocolArrayTy,
547 NULL);
548 std::vector<llvm::Constant*> Elements;
549 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
550 iter != endIter ; iter++) {
551 llvm::Constant *Ptr =
552 llvm::ConstantExpr::getBitCast(ExistingProtocols[*iter], PtrToInt8Ty);
553 Elements.push_back(Ptr);
554 }
555 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
556 Elements);
557 Elements.clear();
558 Elements.push_back(NULLPtr);
559 Elements.push_back(llvm::ConstantInt::get(
560 llvm::cast<llvm::IntegerType>(LongTy), Protocols.size()));
561 Elements.push_back(ProtocolArray);
562 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
563}
564
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000565llvm::Value *CGObjCGNU::GenerateProtocolRef(llvm::IRBuilder<> &Builder,
566 const ObjCProtocolDecl *PD) {
567 return ExistingProtocols[PD->getName()];
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000568}
569
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000570void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
571 ASTContext &Context = CGM.getContext();
572 const char *ProtocolName = PD->getName();
573 llvm::SmallVector<std::string, 16> Protocols;
574 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
575 E = PD->protocol_end(); PI != E; ++PI)
576 Protocols.push_back((*PI)->getName());
577 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
578 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
579 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
580 E = PD->instmeth_end(); iter != E; iter++) {
581 std::string TypeStr;
582 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
583 InstanceMethodNames.push_back(
Daniel Dunbar61432932008-08-13 23:20:05 +0000584 CGM.GetAddrOfConstantCString((*iter)->getSelector().getName()));
585 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000586 }
587 // Collect information about class methods:
588 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
589 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
590 for (ObjCProtocolDecl::classmeth_iterator iter = PD->classmeth_begin(),
591 endIter = PD->classmeth_end() ; iter != endIter ; iter++) {
592 std::string TypeStr;
593 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
594 ClassMethodNames.push_back(
Daniel Dunbar61432932008-08-13 23:20:05 +0000595 CGM.GetAddrOfConstantCString((*iter)->getSelector().getName()));
596 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +0000597 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000598
599 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
600 llvm::Constant *InstanceMethodList =
601 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
602 llvm::Constant *ClassMethodList =
603 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
604 // Protocols are objects containing lists of the methods implemented and
605 // protocols adopted.
606 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
607 PtrToInt8Ty,
608 ProtocolList->getType(),
609 InstanceMethodList->getType(),
610 ClassMethodList->getType(),
611 NULL);
612 std::vector<llvm::Constant*> Elements;
613 // The isa pointer must be set to a magic number so the runtime knows it's
614 // the correct layout.
615 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
616 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
617 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
618 Elements.push_back(ProtocolList);
619 Elements.push_back(InstanceMethodList);
620 Elements.push_back(ClassMethodList);
621 ExistingProtocols[ProtocolName] =
622 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
623 ".objc_protocol"), IdTy);
624}
625
626void CGObjCGNU::GenerateCategory(
627 const char *ClassName,
628 const char *CategoryName,
Chris Lattnera4210072008-06-26 05:08:00 +0000629 const llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000630 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
Chris Lattnera4210072008-06-26 05:08:00 +0000631 const llvm::SmallVectorImpl<Selector> &ClassMethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000632 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
633 const llvm::SmallVectorImpl<std::string> &Protocols) {
634 std::vector<llvm::Constant*> Elements;
635 Elements.push_back(MakeConstantString(CategoryName));
636 Elements.push_back(MakeConstantString(ClassName));
637 // Instance method list
638 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000639 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000640 false), PtrTy));
641 // Class method list
642 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
Chris Lattnera4210072008-06-26 05:08:00 +0000643 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000644 PtrTy));
645 // Protocol list
646 Elements.push_back(llvm::ConstantExpr::getBitCast(
647 GenerateProtocolList(Protocols), PtrTy));
648 Categories.push_back(llvm::ConstantExpr::getBitCast(
649 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
650 PtrTy, PtrTy, NULL), Elements), PtrTy));
651}
652void CGObjCGNU::GenerateClass(
653 const char *ClassName,
654 const char *SuperClassName,
655 const int instanceSize,
656 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
657 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
658 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets,
Chris Lattnera4210072008-06-26 05:08:00 +0000659 const llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000660 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
Chris Lattnera4210072008-06-26 05:08:00 +0000661 const llvm::SmallVectorImpl<Selector> &ClassMethodSels,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000662 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
663 const llvm::SmallVectorImpl<std::string> &Protocols) {
664 // Get the superclass pointer.
665 llvm::Constant *SuperClass;
666 if (SuperClassName) {
667 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
668 } else {
669 SuperClass = llvm::ConstantPointerNull::get(
670 llvm::cast<llvm::PointerType>(PtrToInt8Ty));
671 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000672 // Empty vector used to construct empty method lists
673 llvm::SmallVector<llvm::Constant*, 1> empty;
674 // Generate the method and instance variable lists
675 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000676 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000677 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattnera4210072008-06-26 05:08:00 +0000678 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000679 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
680 IvarOffsets);
681 //Generate metaclass for class methods
682 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner1565e032008-07-21 06:31:05 +0000683 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000684 empty, empty, empty), ClassMethodList, NULLPtr);
685 // Generate the class structure
686 llvm::Constant *ClassStruct = GenerateClassStructure(MetaClassStruct,
Chris Lattnerd002cc62008-06-26 04:47:04 +0000687 SuperClass, 0x1L, ClassName, 0,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000688 llvm::ConstantInt::get(llvm::Type::Int32Ty, instanceSize), IvarList,
689 MethodList, GenerateProtocolList(Protocols));
690 // Add class structure to list to be added to the symtab later
691 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
692 Classes.push_back(ClassStruct);
693}
694
695llvm::Function *CGObjCGNU::ModuleInitFunction() {
696 // Only emit an ObjC load function if no Objective-C stuff has been called
697 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
698 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov5f58b912008-06-01 15:14:46 +0000699 UntypedSelectors.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000700 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +0000701
702 // Name the ObjC types to make the IR a bit easier to read
703 TheModule.addTypeName(".objc_selector", SelectorTy);
704 TheModule.addTypeName(".objc_id", IdTy);
705 TheModule.addTypeName(".objc_imp", IMPTy);
706
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000707 std::vector<llvm::Constant*> Elements;
708 // Generate statics list:
709 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
710 ConstantStrings.size() + 1);
711 ConstantStrings.push_back(NULLPtr);
712 Elements.push_back(MakeConstantString("NSConstantString",
713 ".objc_static_class_name"));
714 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings));
715 llvm::StructType *StaticsListTy =
716 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
Chris Lattner630404b2008-06-26 04:10:42 +0000717 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000718 llvm::Constant *Statics =
719 MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Chris Lattner4e0b2642008-06-24 17:01:28 +0000720 llvm::ArrayType *StaticsListArrayTy =
Chris Lattner630404b2008-06-26 04:10:42 +0000721 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner4e0b2642008-06-24 17:01:28 +0000722 Elements.clear();
723 Elements.push_back(Statics);
Chris Lattner630404b2008-06-26 04:10:42 +0000724 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner4e0b2642008-06-24 17:01:28 +0000725 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000726 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
727 // Array of classes, categories, and constant objects
728 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
729 Classes.size() + Categories.size() + 2);
Chris Lattner630404b2008-06-26 04:10:42 +0000730 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelectorTy,
731 llvm::Type::Int16Ty,
732 llvm::Type::Int16Ty,
733 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000734
735 Elements.clear();
736 // Pointer to an array of selectors used in this module.
737 std::vector<llvm::Constant*> Selectors;
738 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
739 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
740 iter != iterEnd ; ++iter) {
Chris Lattner630404b2008-06-26 04:10:42 +0000741 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
742 Elements.push_back(MakeConstantString(iter->first.second,
743 ".objc_sel_types"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000744 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
745 Elements.clear();
746 }
747 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
748 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner630404b2008-06-26 04:10:42 +0000749 iter != iterEnd; ++iter) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000750 Elements.push_back(
Chris Lattner630404b2008-06-26 04:10:42 +0000751 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000752 Elements.push_back(NULLPtr);
753 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
754 Elements.clear();
755 }
756 Elements.push_back(NULLPtr);
757 Elements.push_back(NULLPtr);
758 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
759 Elements.clear();
760 // Number of static selectors
761 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
762 llvm::Constant *SelectorList = MakeGlobal(
763 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
764 ".objc_selector_list");
765 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList, SelectorTy));
766
767 // Now that all of the static selectors exist, create pointers to them.
768 int index = 0;
769 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
770 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
771 iter != iterEnd; ++iter) {
772 llvm::Constant *Idxs[] = {Zeros[0],
773 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
774 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
775 llvm::GlobalValue::InternalLinkage,
776 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
777 ".objc_sel_ptr", &TheModule);
778 (*iter).second->setAliasee(SelPtr);
779 }
780 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
781 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
782 iter != iterEnd; iter++) {
783 llvm::Constant *Idxs[] = {Zeros[0],
784 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
785 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
786 llvm::GlobalValue::InternalLinkage,
787 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
788 ".objc_sel_ptr", &TheModule);
789 (*iter).second->setAliasee(SelPtr);
790 }
791 // Number of classes defined.
792 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
793 Classes.size()));
794 // Number of categories defined
795 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
796 Categories.size()));
797 // Create an array of classes, then categories, then static object instances
798 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
799 // NULL-terminated list of static object instances (mainly constant strings)
800 Classes.push_back(Statics);
801 Classes.push_back(NULLPtr);
802 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
803 Elements.push_back(ClassList);
804 // Construct the symbol table
805 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
806
807 // The symbol table is contained in a module which has some version-checking
808 // constants
809 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
810 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
811 Elements.clear();
812 // Runtime version used for compatibility checking.
813 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
814 //FIXME: Should be sizeof(ModuleTy)
815 Elements.push_back(llvm::ConstantInt::get(LongTy, 16));
816 //FIXME: Should be the path to the file where this module was declared
817 Elements.push_back(NULLPtr);
818 Elements.push_back(SymTab);
819 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
820
821 // Create the load function calling the runtime entry point with the module
822 // structure
823 std::vector<const llvm::Type*> VoidArgs;
824 llvm::Function * LoadFunction = llvm::Function::Create(
825 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
826 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
827 &TheModule);
828 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
Chris Lattner85e35682008-08-08 19:57:58 +0000829 llvm::IRBuilder<> Builder;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000830 Builder.SetInsertPoint(EntryBB);
831 llvm::Value *Register = TheModule.getOrInsertFunction("__objc_exec_class",
832 llvm::Type::VoidTy, llvm::PointerType::getUnqual(ModuleTy), NULL);
833 Builder.CreateCall(Register, Module);
834 Builder.CreateRetVoid();
835 return LoadFunction;
836}
Chris Lattner391d77a2008-03-30 23:03:07 +0000837llvm::Function *CGObjCGNU::MethodPreamble(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000838 const std::string &ClassName,
839 const std::string &CategoryName,
840 const std::string &MethodName,
Chris Lattner391d77a2008-03-30 23:03:07 +0000841 const llvm::Type *ReturnTy,
842 const llvm::Type *SelfTy,
843 const llvm::Type **ArgTy,
844 unsigned ArgC,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000845 bool isClassMethod,
Chris Lattner391d77a2008-03-30 23:03:07 +0000846 bool isVarArg) {
847 std::vector<const llvm::Type*> Args;
Chris Lattner8fdf3282008-06-24 17:04:18 +0000848 if (!ReturnTy->isSingleValueType() && ReturnTy != llvm::Type::VoidTy) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000849 Args.push_back(llvm::PointerType::getUnqual(ReturnTy));
850 ReturnTy = llvm::Type::VoidTy;
851 }
Chris Lattner391d77a2008-03-30 23:03:07 +0000852 Args.push_back(SelfTy);
853 Args.push_back(SelectorTy);
854 Args.insert(Args.end(), ArgTy, ArgTy+ArgC);
855
856 llvm::FunctionType *MethodTy = llvm::FunctionType::get(ReturnTy,
857 Args,
858 isVarArg);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000859 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
860 MethodName, isClassMethod);
861
Gabor Greif984d0b42008-04-06 20:42:52 +0000862 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000863 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000864 FunctionName,
Chris Lattner391d77a2008-03-30 23:03:07 +0000865 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +0000866 llvm::Function::arg_iterator AI = Method->arg_begin();
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000867 // Name the struct return argument.
868 // FIXME: This is probably the wrong test.
869 if (!ReturnTy->isFirstClassType() && ReturnTy != llvm::Type::VoidTy) {
870 AI->setName("agg.result");
871 ++AI;
872 }
Chris Lattner391d77a2008-03-30 23:03:07 +0000873 AI->setName("self");
874 ++AI;
875 AI->setName("_cmd");
876 return Method;
877}
878
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000879CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
Chris Lattnerdce14062008-06-26 04:19:03 +0000880 return new CGObjCGNU(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +0000881}