blob: e545fc12eefbdc30ab0f8ce307d8419d4d8a3425 [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"
18#include "llvm/Module.h"
19#include "llvm/Support/Compiler.h"
Chris Lattner50b36742008-04-13 07:32:11 +000020#include "llvm/Support/IRBuilder.h"
Chris Lattner0f984262008-03-01 08:50:34 +000021#include "llvm/ADT/SmallVector.h"
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000022#include "llvm/ADT/StringMap.h"
23#include <map>
24
25using llvm::dyn_cast;
26
27// The version of the runtime that this class targets. Must match the version
28// in the runtime.
29static const int RuntimeVersion = 8;
30static const int ProtocolVersion = 2;
Chris Lattner0f984262008-03-01 08:50:34 +000031
Chris Lattner0f984262008-03-01 08:50:34 +000032namespace {
Chris Lattner391d77a2008-03-30 23:03:07 +000033class CGObjCGNU : public clang::CodeGen::CGObjCRuntime {
Chris Lattner0f984262008-03-01 08:50:34 +000034private:
35 llvm::Module &TheModule;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000036 const llvm::StructType *SelStructTy;
Chris Lattner391d77a2008-03-30 23:03:07 +000037 const llvm::Type *SelectorTy;
38 const llvm::Type *PtrToInt8Ty;
39 const llvm::Type *IMPTy;
40 const llvm::Type *IdTy;
41 const llvm::Type *IntTy;
42 const llvm::Type *PtrTy;
43 const llvm::Type *LongTy;
44 const llvm::Type *PtrToIntTy;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000045 std::vector<llvm::Constant*> Classes;
46 std::vector<llvm::Constant*> Categories;
47 std::vector<llvm::Constant*> ConstantStrings;
48 llvm::Function *LoadFunction;
49 llvm::StringMap<llvm::Constant*> ExistingProtocols;
50 typedef std::pair<std::string, std::string> TypedSelector;
51 std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors;
52 llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors;
53 // Some zeros used for GEPs in lots of places.
54 llvm::Constant *Zeros[2];
55 llvm::Constant *NULLPtr;
56private:
57 llvm::Constant *GenerateIvarList(
58 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
59 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
60 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets);
61 llvm::Constant *GenerateMethodList(const std::string &ClassName,
62 const std::string &CategoryName,
63 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
64 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
65 bool isClassMethodList);
66 llvm::Constant *GenerateProtocolList(
67 const llvm::SmallVectorImpl<std::string> &Protocols);
68 llvm::Constant *GenerateClassStructure(
69 llvm::Constant *MetaClass,
70 llvm::Constant *SuperClass,
71 unsigned info,
72 llvm::Constant *Name,
73 llvm::Constant *Version,
74 llvm::Constant *InstanceSize,
75 llvm::Constant *IVars,
76 llvm::Constant *Methods,
77 llvm::Constant *Protocols);
78 llvm::Constant *GenerateProtocolMethodList(
79 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
80 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes);
81 llvm::Constant *MakeConstantString(const std::string &Str, const std::string
82 &Name="");
83 llvm::Constant *MakeGlobal(const llvm::StructType *Ty,
84 std::vector<llvm::Constant*> &V, const std::string &Name="");
85 llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty,
86 std::vector<llvm::Constant*> &V, const std::string &Name="");
Chris Lattner0f984262008-03-01 08:50:34 +000087public:
Chris Lattner391d77a2008-03-30 23:03:07 +000088 CGObjCGNU(llvm::Module &Mp,
89 const llvm::Type *LLVMIntType,
90 const llvm::Type *LLVMLongType);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000091 virtual llvm::Constant *GenerateConstantString(const char *String,
92 const size_t length);
93 virtual llvm::Value *GenerateMessageSend(llvm::IRBuilder &Builder,
Chris Lattner0f984262008-03-01 08:50:34 +000094 const llvm::Type *ReturnTy,
Chris Lattner391d77a2008-03-30 23:03:07 +000095 llvm::Value *Sender,
Chris Lattner0f984262008-03-01 08:50:34 +000096 llvm::Value *Receiver,
Chris Lattner391d77a2008-03-30 23:03:07 +000097 llvm::Value *Selector,
Chris Lattner0f984262008-03-01 08:50:34 +000098 llvm::Value** ArgV,
99 unsigned ArgC);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000100 virtual llvm::Value *GenerateMessageSendSuper(llvm::IRBuilder &Builder,
101 const llvm::Type *ReturnTy,
102 llvm::Value *Sender,
103 const char *SuperClassName,
104 llvm::Value *Receiver,
105 llvm::Value *Selector,
106 llvm::Value** ArgV,
107 unsigned ArgC);
108 virtual llvm::Value *LookupClass(llvm::IRBuilder &Builder, llvm::Value
109 *ClassName);
110 virtual llvm::Value *GetSelector(llvm::IRBuilder &Builder,
Chris Lattner391d77a2008-03-30 23:03:07 +0000111 llvm::Value *SelName,
112 llvm::Value *SelTypes);
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,
124 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
125 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
126 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
127 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,
136 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
137 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
138 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
139 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 Lattner391d77a2008-03-30 23:03:07 +0000166CGObjCGNU::CGObjCGNU(llvm::Module &M,
167 const llvm::Type *LLVMIntType,
168 const llvm::Type *LLVMLongType) :
169 TheModule(M),
170 IntTy(LLVMIntType),
171 LongTy(LLVMLongType)
172{
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000173 Zeros[0] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
174 Zeros[1] = Zeros[0];
175 NULLPtr = llvm::ConstantPointerNull::get(
176 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
Chris Lattner391d77a2008-03-30 23:03:07 +0000177 // C string type. Used in lots of places.
178 PtrToInt8Ty =
179 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
180 // Get the selector Type.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000181 SelStructTy = llvm::StructType::get(
Chris Lattner391d77a2008-03-30 23:03:07 +0000182 PtrToInt8Ty,
183 PtrToInt8Ty,
184 NULL);
185 SelectorTy = llvm::PointerType::getUnqual(SelStructTy);
186 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
187 PtrTy = PtrToInt8Ty;
188
189 // Object type
190 llvm::PATypeHolder OpaqueObjTy = llvm::OpaqueType::get();
191 llvm::Type *OpaqueIdTy = llvm::PointerType::getUnqual(OpaqueObjTy);
192 IdTy = llvm::StructType::get(OpaqueIdTy, NULL);
193 llvm::cast<llvm::OpaqueType>(OpaqueObjTy.get())->refineAbstractTypeTo(IdTy);
194 IdTy = llvm::cast<llvm::StructType>(OpaqueObjTy.get());
195 IdTy = llvm::PointerType::getUnqual(IdTy);
196
197 // IMP type
198 std::vector<const llvm::Type*> IMPArgs;
199 IMPArgs.push_back(IdTy);
200 IMPArgs.push_back(SelectorTy);
201 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000202}
203// This has to perform the lookup every time, since posing and related
204// techniques can modify the name -> class mapping.
205llvm::Value *CGObjCGNU::LookupClass(llvm::IRBuilder &Builder,
206 llvm::Value *ClassName) {
207 llvm::Constant *ClassLookupFn =
208 TheModule.getOrInsertFunction("objc_lookup_class", IdTy, PtrToInt8Ty,
209 NULL);
210 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner391d77a2008-03-30 23:03:07 +0000211}
212
213/// Looks up the selector for the specified name / type pair.
214// FIXME: Selectors should be statically cached, not looked up on every call.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000215llvm::Value *CGObjCGNU::GetSelector(llvm::IRBuilder &Builder,
Chris Lattner391d77a2008-03-30 23:03:07 +0000216 llvm::Value *SelName,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000217 llvm::Value *SelTypes) {
218 // For static selectors, we return an alias for now then store them all in a
219 // list that the runtime will initialise later.
220 if (llvm::Constant *CName = dyn_cast<llvm::Constant>(SelName)) {
221 // Untyped selector
222 if (SelTypes == 0) {
223 // If it's already cached, return it.
224 if (UntypedSelectors[CName->getStringValue()]) {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000225 // FIXME: Volatility
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000226 return Builder.CreateLoad(UntypedSelectors[CName->getStringValue()]);
227 }
228 // If it isn't, cache it.
229 llvm::GlobalAlias *Sel = new llvm::GlobalAlias(
230 llvm::PointerType::getUnqual(SelectorTy),
231 llvm::GlobalValue::InternalLinkage, ".objc_untyped_selector_alias",
232 NULL, &TheModule);
233 UntypedSelectors[CName->getStringValue()] = Sel;
Eli Friedman1e692ac2008-06-13 23:01:12 +0000234 // FIXME: Volatility
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000235 return Builder.CreateLoad(Sel);
236 }
237 // Typed selectors
238 if (llvm::Constant *CTypes = dyn_cast<llvm::Constant>(SelTypes)) {
239 TypedSelector Selector = TypedSelector(CName->getStringValue(),
240 CTypes->getStringValue());
241 // If it's already cached, return it.
242 if (TypedSelectors[Selector]) {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000243 // FIXME: Volatility
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000244 return Builder.CreateLoad(TypedSelectors[Selector]);
245 }
246 // If it isn't, cache it.
247 llvm::GlobalAlias *Sel = new llvm::GlobalAlias(
248 llvm::PointerType::getUnqual(SelectorTy),
249 llvm::GlobalValue::InternalLinkage, ".objc_typed_selector_alias",
250 NULL, &TheModule);
251 TypedSelectors[Selector] = Sel;
Eli Friedman1e692ac2008-06-13 23:01:12 +0000252 // FIXME: Volatility
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000253 return Builder.CreateLoad(Sel);
254 }
255 }
256 // Dynamically look up selectors from non-constant sources
Chris Lattner391d77a2008-03-30 23:03:07 +0000257 llvm::Value *cmd;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000258 if (SelTypes == 0) {
Chris Lattner391d77a2008-03-30 23:03:07 +0000259 llvm::Constant *SelFunction = TheModule.getOrInsertFunction("sel_get_uid",
260 SelectorTy,
261 PtrToInt8Ty,
262 NULL);
263 cmd = Builder.CreateCall(SelFunction, SelName);
264 }
265 else {
266 llvm::Constant *SelFunction =
267 TheModule.getOrInsertFunction("sel_get_typed_uid",
268 SelectorTy,
269 PtrToInt8Ty,
270 PtrToInt8Ty,
271 NULL);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000272 cmd = Builder.CreateCall2(SelFunction, SelName, SelTypes);
Chris Lattner391d77a2008-03-30 23:03:07 +0000273 }
274 return cmd;
275}
276
277
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000278llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str, const
279 std::string &Name) {
280 llvm::Constant * ConstStr = llvm::ConstantArray::get(Str);
281 ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true,
282 llvm::GlobalValue::InternalLinkage,
283 ConstStr, Name, &TheModule);
284 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
285}
286llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
287 std::vector<llvm::Constant*> &V, const std::string &Name) {
288 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
289 return new llvm::GlobalVariable(Ty, false,
290 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
291}
292llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
293 std::vector<llvm::Constant*> &V, const std::string &Name) {
294 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
295 return new llvm::GlobalVariable(Ty, false,
296 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
297}
298
299/// Generate an NSConstantString object.
300//TODO: In case there are any crazy people still using the GNU runtime without
301//an OpenStep implementation, this should let them select their own class for
302//constant strings.
303llvm::Constant *CGObjCGNU::GenerateConstantString(const char *String, const
304 size_t length) {
305 std::vector<llvm::Constant*> Ivars;
306 Ivars.push_back(NULLPtr);
307 Ivars.push_back(MakeConstantString(String));
308 Ivars.push_back(llvm::ConstantInt::get(IntTy, length));
309 llvm::Constant *ObjCStr = MakeGlobal(
310 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
311 Ivars, ".objc_str");
312 ConstantStrings.push_back(
313 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
314 return ObjCStr;
315}
316
317///Generates a message send where the super is the receiver. This is a message
318///send to self with special delivery semantics indicating which class's method
319///should be called.
320llvm::Value *CGObjCGNU::GenerateMessageSendSuper(llvm::IRBuilder &Builder,
Chris Lattner0f984262008-03-01 08:50:34 +0000321 const llvm::Type *ReturnTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000322 llvm::Value *Sender,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000323 const char *SuperClassName,
Chris Lattner0f984262008-03-01 08:50:34 +0000324 llvm::Value *Receiver,
Chris Lattner391d77a2008-03-30 23:03:07 +0000325 llvm::Value *Selector,
Chris Lattner0f984262008-03-01 08:50:34 +0000326 llvm::Value** ArgV,
327 unsigned ArgC) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000328 // TODO: This should be cached, not looked up every time.
329 llvm::Value *ReceiverClass = LookupClass(Builder,
330 MakeConstantString(SuperClassName));
331 llvm::Value *cmd = GetSelector(Builder, Selector, 0);
Chris Lattner0f984262008-03-01 08:50:34 +0000332 std::vector<const llvm::Type*> impArgTypes;
333 impArgTypes.push_back(Receiver->getType());
Chris Lattner391d77a2008-03-30 23:03:07 +0000334 impArgTypes.push_back(SelectorTy);
Chris Lattner0f984262008-03-01 08:50:34 +0000335
336 // Avoid an explicit cast on the IMP by getting a version that has the right
337 // return type.
338 llvm::FunctionType *impType = llvm::FunctionType::get(ReturnTy, impArgTypes,
339 true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000340 // Construct the structure used to look up the IMP
341 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
342 IdTy, NULL);
343 llvm::Value *ObjCSuper = Builder.CreateAlloca(ObjCSuperTy);
Eli Friedman1e692ac2008-06-13 23:01:12 +0000344 // FIXME: volatility
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000345 Builder.CreateStore(Receiver, Builder.CreateStructGEP(ObjCSuper, 0));
346 Builder.CreateStore(ReceiverClass, Builder.CreateStructGEP(ObjCSuper, 1));
347
348 // Get the IMP
349 llvm::Constant *lookupFunction =
350 TheModule.getOrInsertFunction("objc_msg_lookup_super",
351 llvm::PointerType::getUnqual(impType),
352 llvm::PointerType::getUnqual(ObjCSuperTy),
353 SelectorTy, NULL);
354 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
355 llvm::Value *imp = Builder.CreateCall(lookupFunction, lookupArgs,
356 lookupArgs+2);
357
358 // Call the method
359 llvm::SmallVector<llvm::Value*, 8> callArgs;
360 callArgs.push_back(Receiver);
361 callArgs.push_back(cmd);
362 callArgs.insert(callArgs.end(), ArgV, ArgV+ArgC);
363 return Builder.CreateCall(imp, callArgs.begin(), callArgs.end());
364}
365
366/// Generate code for a message send expression.
367llvm::Value *CGObjCGNU::GenerateMessageSend(llvm::IRBuilder &Builder,
368 const llvm::Type *ReturnTy,
369 llvm::Value *Sender,
370 llvm::Value *Receiver,
371 llvm::Value *Selector,
372 llvm::Value** ArgV,
373 unsigned ArgC) {
374 llvm::Value *cmd = GetSelector(Builder, Selector, 0);
375
376 // Look up the method implementation.
377 std::vector<const llvm::Type*> impArgTypes;
378 const llvm::Type *RetTy;
379 //TODO: Revisit this when LLVM supports aggregate return types.
380 if (ReturnTy->isSingleValueType() && ReturnTy != llvm::Type::VoidTy) {
381 RetTy = ReturnTy;
382 } else {
383 // For struct returns allocate the space in the caller and pass it up to
384 // the sender.
385 RetTy = llvm::Type::VoidTy;
386 impArgTypes.push_back(llvm::PointerType::getUnqual(ReturnTy));
387 }
388 impArgTypes.push_back(Receiver->getType());
389 impArgTypes.push_back(SelectorTy);
390
391 // Avoid an explicit cast on the IMP by getting a version that has the right
392 // return type.
393 llvm::FunctionType *impType = llvm::FunctionType::get(RetTy, impArgTypes,
394 true);
Chris Lattner0f984262008-03-01 08:50:34 +0000395
396 llvm::Constant *lookupFunction =
397 TheModule.getOrInsertFunction("objc_msg_lookup",
Chris Lattner391d77a2008-03-30 23:03:07 +0000398 llvm::PointerType::getUnqual(impType),
399 Receiver->getType(), SelectorTy, NULL);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000400 llvm::Value *imp = Builder.CreateCall2(lookupFunction, Receiver, cmd);
401
402 // Call the method.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000403 llvm::SmallVector<llvm::Value*, 16> Args;
404 if (!ReturnTy->isSingleValueType()) {
405 llvm::Value *Return = Builder.CreateAlloca(ReturnTy);
406 Args.push_back(Return);
407 return Return;
408 }
409 Args.push_back(Receiver);
410 Args.push_back(cmd);
411 Args.insert(Args.end(), ArgV, ArgV+ArgC);
412 return Builder.CreateCall(imp, Args.begin(), Args.end());
Chris Lattner0f984262008-03-01 08:50:34 +0000413}
414
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000415/// Generates a MethodList. Used in construction of a objc_class and
416/// objc_category structures.
417llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
418 const std::string &CategoryName,
419 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
420 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
421 bool isClassMethodList) {
422 // Get the method structure type.
423 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
424 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
425 PtrToInt8Ty, // Method types
426 llvm::PointerType::getUnqual(IMPTy), //Method pointer
427 NULL);
428 std::vector<llvm::Constant*> Methods;
429 std::vector<llvm::Constant*> Elements;
430 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
431 Elements.clear();
432 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
433 Zeros, 2));
434 Elements.push_back(
435 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
436 llvm::Constant *Method =
437 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
438 MethodNames[i]->getStringValue(), isClassMethodList));
439 Method = llvm::ConstantExpr::getBitCast(Method,
440 llvm::PointerType::getUnqual(IMPTy));
441 Elements.push_back(Method);
442 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
443 }
444
445 // Array of method structures
446 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
447 MethodNames.size());
448 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
449 Methods);
450
451 // Structure containing list pointer, array and array count
452 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
453 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
454 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
455 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
456 IntTy,
457 ObjCMethodArrayTy,
458 NULL);
459 // Refine next pointer type to concrete type
460 llvm::cast<llvm::OpaqueType>(
461 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
462 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
463
464 Methods.clear();
465 Methods.push_back(llvm::ConstantPointerNull::get(
466 llvm::PointerType::getUnqual(ObjCMethodListTy)));
467 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
468 MethodTypes.size()));
469 Methods.push_back(MethodArray);
470
471 // Create an instance of the structure
472 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
473}
474
475/// Generates an IvarList. Used in construction of a objc_class.
476llvm::Constant *CGObjCGNU::GenerateIvarList(
477 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
478 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
479 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
480 // Get the method structure type.
481 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
482 PtrToInt8Ty,
483 PtrToInt8Ty,
484 IntTy,
485 NULL);
486 std::vector<llvm::Constant*> Ivars;
487 std::vector<llvm::Constant*> Elements;
488 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
489 Elements.clear();
490 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
491 Zeros, 2));
492 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
493 Zeros, 2));
494 Elements.push_back(IvarOffsets[i]);
495 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
496 }
497
498 // Array of method structures
499 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
500 IvarNames.size());
501
502
503 Elements.clear();
504 Elements.push_back(llvm::ConstantInt::get(
505 llvm::cast<llvm::IntegerType>(IntTy), (int)IvarNames.size()));
506 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
507 // Structure containing array and array count
508 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
509 ObjCIvarArrayTy,
510 NULL);
511
512 // Create an instance of the structure
513 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
514}
515
516/// Generate a class structure
517llvm::Constant *CGObjCGNU::GenerateClassStructure(
518 llvm::Constant *MetaClass,
519 llvm::Constant *SuperClass,
520 unsigned info,
521 llvm::Constant *Name,
522 llvm::Constant *Version,
523 llvm::Constant *InstanceSize,
524 llvm::Constant *IVars,
525 llvm::Constant *Methods,
526 llvm::Constant *Protocols) {
527 // Set up the class structure
528 // Note: Several of these are char*s when they should be ids. This is
529 // because the runtime performs this translation on load.
530 llvm::StructType *ClassTy = llvm::StructType::get(
531 PtrToInt8Ty, // class_pointer
532 PtrToInt8Ty, // super_class
533 PtrToInt8Ty, // name
534 LongTy, // version
535 LongTy, // info
536 LongTy, // instance_size
537 IVars->getType(), // ivars
538 Methods->getType(), // methods
539 // These are all filled in by the runtime, so we pretend
540 PtrTy, // dtable
541 PtrTy, // subclass_list
542 PtrTy, // sibling_class
543 PtrTy, // protocols
544 PtrTy, // gc_object_type
545 NULL);
546 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
547 llvm::Constant *NullP =
548 llvm::ConstantPointerNull::get(llvm::cast<llvm::PointerType>(PtrTy));
549 // Fill in the structure
550 std::vector<llvm::Constant*> Elements;
551 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
552 Elements.push_back(SuperClass);
553 Elements.push_back(Name);
554 Elements.push_back(Zero);
555 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
556 Elements.push_back(InstanceSize);
557 Elements.push_back(IVars);
558 Elements.push_back(Methods);
559 Elements.push_back(NullP);
560 Elements.push_back(NullP);
561 Elements.push_back(NullP);
562 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
563 Elements.push_back(NullP);
564 // Create an instance of the structure
565 return MakeGlobal(ClassTy, Elements,
566 SymbolNameForClass(Name->getStringValue()));
567}
568
569llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
570 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
571 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
572 // Get the method structure type.
573 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
574 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
575 PtrToInt8Ty,
576 NULL);
577 std::vector<llvm::Constant*> Methods;
578 std::vector<llvm::Constant*> Elements;
579 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
580 Elements.clear();
581 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
582 Zeros, 2));
583 Elements.push_back(
584 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
585 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
586 }
587 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
588 MethodNames.size());
589 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
590 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
591 IntTy, ObjCMethodArrayTy, NULL);
592 Methods.clear();
593 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
594 Methods.push_back(Array);
595 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
596}
597// Create the protocol list structure used in classes, categories and so on
598llvm::Constant *CGObjCGNU::GenerateProtocolList(
599 const llvm::SmallVectorImpl<std::string> &Protocols) {
600 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
601 Protocols.size());
602 llvm::StructType *ProtocolListTy = llvm::StructType::get(
603 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
604 LongTy,//FIXME: Should be size_t
605 ProtocolArrayTy,
606 NULL);
607 std::vector<llvm::Constant*> Elements;
608 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
609 iter != endIter ; iter++) {
610 llvm::Constant *Ptr =
611 llvm::ConstantExpr::getBitCast(ExistingProtocols[*iter], PtrToInt8Ty);
612 Elements.push_back(Ptr);
613 }
614 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
615 Elements);
616 Elements.clear();
617 Elements.push_back(NULLPtr);
618 Elements.push_back(llvm::ConstantInt::get(
619 llvm::cast<llvm::IntegerType>(LongTy), Protocols.size()));
620 Elements.push_back(ProtocolArray);
621 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
622}
623
624llvm::Value *CGObjCGNU::GenerateProtocolRef(llvm::IRBuilder &Builder, const
625 char *ProtocolName) {
626 return ExistingProtocols[ProtocolName];
627}
628
629void CGObjCGNU::GenerateProtocol(const char *ProtocolName,
630 const llvm::SmallVectorImpl<std::string> &Protocols,
631 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
632 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
633 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
634 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes) {
635
636 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
637 llvm::Constant *InstanceMethodList =
638 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
639 llvm::Constant *ClassMethodList =
640 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
641 // Protocols are objects containing lists of the methods implemented and
642 // protocols adopted.
643 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
644 PtrToInt8Ty,
645 ProtocolList->getType(),
646 InstanceMethodList->getType(),
647 ClassMethodList->getType(),
648 NULL);
649 std::vector<llvm::Constant*> Elements;
650 // The isa pointer must be set to a magic number so the runtime knows it's
651 // the correct layout.
652 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
653 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
654 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
655 Elements.push_back(ProtocolList);
656 Elements.push_back(InstanceMethodList);
657 Elements.push_back(ClassMethodList);
658 ExistingProtocols[ProtocolName] =
659 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
660 ".objc_protocol"), IdTy);
661}
662
663void CGObjCGNU::GenerateCategory(
664 const char *ClassName,
665 const char *CategoryName,
666 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
667 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
668 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
669 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
670 const llvm::SmallVectorImpl<std::string> &Protocols) {
671 std::vector<llvm::Constant*> Elements;
672 Elements.push_back(MakeConstantString(CategoryName));
673 Elements.push_back(MakeConstantString(ClassName));
674 // Instance method list
675 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
676 ClassName, CategoryName, InstanceMethodNames, InstanceMethodTypes,
677 false), PtrTy));
678 // Class method list
679 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
680 ClassName, CategoryName, ClassMethodNames, ClassMethodTypes, true),
681 PtrTy));
682 // Protocol list
683 Elements.push_back(llvm::ConstantExpr::getBitCast(
684 GenerateProtocolList(Protocols), PtrTy));
685 Categories.push_back(llvm::ConstantExpr::getBitCast(
686 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
687 PtrTy, PtrTy, NULL), Elements), PtrTy));
688}
689void CGObjCGNU::GenerateClass(
690 const char *ClassName,
691 const char *SuperClassName,
692 const int instanceSize,
693 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
694 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
695 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets,
696 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
697 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
698 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
699 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
700 const llvm::SmallVectorImpl<std::string> &Protocols) {
701 // Get the superclass pointer.
702 llvm::Constant *SuperClass;
703 if (SuperClassName) {
704 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
705 } else {
706 SuperClass = llvm::ConstantPointerNull::get(
707 llvm::cast<llvm::PointerType>(PtrToInt8Ty));
708 }
709 llvm::Constant * Name = MakeConstantString(ClassName, ".class_name");
710 // Empty vector used to construct empty method lists
711 llvm::SmallVector<llvm::Constant*, 1> empty;
712 // Generate the method and instance variable lists
713 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
714 InstanceMethodNames, InstanceMethodTypes, false);
715 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
716 ClassMethodNames, ClassMethodTypes, true);
717 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
718 IvarOffsets);
719 //Generate metaclass for class methods
720 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
721 NULLPtr, 0x2L, NULLPtr, 0, Zeros[0], GenerateIvarList(
722 empty, empty, empty), ClassMethodList, NULLPtr);
723 // Generate the class structure
724 llvm::Constant *ClassStruct = GenerateClassStructure(MetaClassStruct,
725 SuperClass, 0x1L, Name, 0,
726 llvm::ConstantInt::get(llvm::Type::Int32Ty, instanceSize), IvarList,
727 MethodList, GenerateProtocolList(Protocols));
728 // Add class structure to list to be added to the symtab later
729 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
730 Classes.push_back(ClassStruct);
731}
732
733llvm::Function *CGObjCGNU::ModuleInitFunction() {
734 // Only emit an ObjC load function if no Objective-C stuff has been called
735 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
736 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov5f58b912008-06-01 15:14:46 +0000737 UntypedSelectors.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000738 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +0000739
740 // Name the ObjC types to make the IR a bit easier to read
741 TheModule.addTypeName(".objc_selector", SelectorTy);
742 TheModule.addTypeName(".objc_id", IdTy);
743 TheModule.addTypeName(".objc_imp", IMPTy);
744
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000745 std::vector<llvm::Constant*> Elements;
746 // Generate statics list:
747 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
748 ConstantStrings.size() + 1);
749 ConstantStrings.push_back(NULLPtr);
750 Elements.push_back(MakeConstantString("NSConstantString",
751 ".objc_static_class_name"));
752 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings));
753 llvm::StructType *StaticsListTy =
754 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
755 llvm::Constant *Statics =
756 MakeGlobal(StaticsListTy, Elements, ".objc_statics");
757 Statics = new
758 llvm::GlobalVariable(llvm::PointerType::getUnqual(StaticsListTy), false,
759 llvm::GlobalValue::InternalLinkage, Statics, ".objc_statics_ptr",
760 &TheModule);
761 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
762 // Array of classes, categories, and constant objects
763 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
764 Classes.size() + Categories.size() + 2);
765 llvm::StructType *SymTabTy = llvm::StructType::get(
766 LongTy,
767 SelectorTy,
768 llvm::Type::Int16Ty,
769 llvm::Type::Int16Ty,
770 ClassListTy,
771 NULL);
772
773 Elements.clear();
774 // Pointer to an array of selectors used in this module.
775 std::vector<llvm::Constant*> Selectors;
776 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
777 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
778 iter != iterEnd ; ++iter) {
779 Elements.push_back(MakeConstantString((*iter).first.first,
780 ".objc_sel_name"));
781 Elements.push_back(MakeConstantString((*iter).first.second,
782 ".objc_sel_types"));
783 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
784 Elements.clear();
785 }
786 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
787 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
788 iter != iterEnd; iter++) {
789 Elements.push_back(
790 MakeConstantString((*iter).getKeyData(), ".objc_sel_name"));
791 Elements.push_back(NULLPtr);
792 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
793 Elements.clear();
794 }
795 Elements.push_back(NULLPtr);
796 Elements.push_back(NULLPtr);
797 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
798 Elements.clear();
799 // Number of static selectors
800 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
801 llvm::Constant *SelectorList = MakeGlobal(
802 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
803 ".objc_selector_list");
804 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList, SelectorTy));
805
806 // Now that all of the static selectors exist, create pointers to them.
807 int index = 0;
808 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
809 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
810 iter != iterEnd; ++iter) {
811 llvm::Constant *Idxs[] = {Zeros[0],
812 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
813 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
814 llvm::GlobalValue::InternalLinkage,
815 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
816 ".objc_sel_ptr", &TheModule);
817 (*iter).second->setAliasee(SelPtr);
818 }
819 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
820 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
821 iter != iterEnd; iter++) {
822 llvm::Constant *Idxs[] = {Zeros[0],
823 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
824 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
825 llvm::GlobalValue::InternalLinkage,
826 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
827 ".objc_sel_ptr", &TheModule);
828 (*iter).second->setAliasee(SelPtr);
829 }
830 // Number of classes defined.
831 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
832 Classes.size()));
833 // Number of categories defined
834 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
835 Categories.size()));
836 // Create an array of classes, then categories, then static object instances
837 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
838 // NULL-terminated list of static object instances (mainly constant strings)
839 Classes.push_back(Statics);
840 Classes.push_back(NULLPtr);
841 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
842 Elements.push_back(ClassList);
843 // Construct the symbol table
844 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
845
846 // The symbol table is contained in a module which has some version-checking
847 // constants
848 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
849 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
850 Elements.clear();
851 // Runtime version used for compatibility checking.
852 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
853 //FIXME: Should be sizeof(ModuleTy)
854 Elements.push_back(llvm::ConstantInt::get(LongTy, 16));
855 //FIXME: Should be the path to the file where this module was declared
856 Elements.push_back(NULLPtr);
857 Elements.push_back(SymTab);
858 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
859
860 // Create the load function calling the runtime entry point with the module
861 // structure
862 std::vector<const llvm::Type*> VoidArgs;
863 llvm::Function * LoadFunction = llvm::Function::Create(
864 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
865 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
866 &TheModule);
867 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
868 llvm::IRBuilder Builder;
869 Builder.SetInsertPoint(EntryBB);
870 llvm::Value *Register = TheModule.getOrInsertFunction("__objc_exec_class",
871 llvm::Type::VoidTy, llvm::PointerType::getUnqual(ModuleTy), NULL);
872 Builder.CreateCall(Register, Module);
873 Builder.CreateRetVoid();
874 return LoadFunction;
875}
Chris Lattner391d77a2008-03-30 23:03:07 +0000876llvm::Function *CGObjCGNU::MethodPreamble(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000877 const std::string &ClassName,
878 const std::string &CategoryName,
879 const std::string &MethodName,
Chris Lattner391d77a2008-03-30 23:03:07 +0000880 const llvm::Type *ReturnTy,
881 const llvm::Type *SelfTy,
882 const llvm::Type **ArgTy,
883 unsigned ArgC,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000884 bool isClassMethod,
Chris Lattner391d77a2008-03-30 23:03:07 +0000885 bool isVarArg) {
886 std::vector<const llvm::Type*> Args;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000887 if (!ReturnTy->isFirstClassType() && ReturnTy != llvm::Type::VoidTy) {
888 Args.push_back(llvm::PointerType::getUnqual(ReturnTy));
889 ReturnTy = llvm::Type::VoidTy;
890 }
Chris Lattner391d77a2008-03-30 23:03:07 +0000891 Args.push_back(SelfTy);
892 Args.push_back(SelectorTy);
893 Args.insert(Args.end(), ArgTy, ArgTy+ArgC);
894
895 llvm::FunctionType *MethodTy = llvm::FunctionType::get(ReturnTy,
896 Args,
897 isVarArg);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000898 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
899 MethodName, isClassMethod);
900
Gabor Greif984d0b42008-04-06 20:42:52 +0000901 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000902 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000903 FunctionName,
Chris Lattner391d77a2008-03-30 23:03:07 +0000904 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +0000905 llvm::Function::arg_iterator AI = Method->arg_begin();
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000906 // Name the struct return argument.
907 // FIXME: This is probably the wrong test.
908 if (!ReturnTy->isFirstClassType() && ReturnTy != llvm::Type::VoidTy) {
909 AI->setName("agg.result");
910 ++AI;
911 }
Chris Lattner391d77a2008-03-30 23:03:07 +0000912 AI->setName("self");
913 ++AI;
914 AI->setName("_cmd");
915 return Method;
916}
917
918clang::CodeGen::CGObjCRuntime *clang::CodeGen::CreateObjCRuntime(
919 llvm::Module &M,
920 const llvm::Type *LLVMIntType,
921 const llvm::Type *LLVMLongType) {
922 return new CGObjCGNU(M, LLVMIntType, LLVMLongType);
Chris Lattner0f984262008-03-01 08:50:34 +0000923}