blob: 5b11b3f667a413d8d8217b9cb9f00c96f1af21ac [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 +000027
Chris Lattner550b8db2008-06-26 04:05:20 +000028// FIXME: Remove THIS!
29#include "llvm/Analysis/ValueTracking.h"
30std::string getStringValue(llvm::Constant *C) {
31 std::string R;
32 bool V = GetConstantStringInfo(C, R);
33 assert(V && "Couldn't convert string");
34 return R;
35}
36
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000037using llvm::dyn_cast;
38
39// The version of the runtime that this class targets. Must match the version
40// in the runtime.
41static const int RuntimeVersion = 8;
42static const int ProtocolVersion = 2;
Chris Lattner0f984262008-03-01 08:50:34 +000043
Chris Lattner0f984262008-03-01 08:50:34 +000044namespace {
Chris Lattnerdce14062008-06-26 04:19:03 +000045class CGObjCGNU : public CodeGen::CGObjCRuntime {
Chris Lattner0f984262008-03-01 08:50:34 +000046private:
Chris Lattnerdce14062008-06-26 04:19:03 +000047 CodeGen::CodeGenModule &CGM;
Chris Lattner0f984262008-03-01 08:50:34 +000048 llvm::Module &TheModule;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000049 const llvm::StructType *SelStructTy;
Chris Lattner391d77a2008-03-30 23:03:07 +000050 const llvm::Type *SelectorTy;
51 const llvm::Type *PtrToInt8Ty;
52 const llvm::Type *IMPTy;
53 const llvm::Type *IdTy;
54 const llvm::Type *IntTy;
55 const llvm::Type *PtrTy;
56 const llvm::Type *LongTy;
57 const llvm::Type *PtrToIntTy;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000058 std::vector<llvm::Constant*> Classes;
59 std::vector<llvm::Constant*> Categories;
60 std::vector<llvm::Constant*> ConstantStrings;
61 llvm::Function *LoadFunction;
62 llvm::StringMap<llvm::Constant*> ExistingProtocols;
63 typedef std::pair<std::string, std::string> TypedSelector;
64 std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors;
65 llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors;
66 // Some zeros used for GEPs in lots of places.
67 llvm::Constant *Zeros[2];
68 llvm::Constant *NULLPtr;
69private:
70 llvm::Constant *GenerateIvarList(
71 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
72 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
73 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets);
74 llvm::Constant *GenerateMethodList(const std::string &ClassName,
75 const std::string &CategoryName,
76 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
77 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
78 bool isClassMethodList);
79 llvm::Constant *GenerateProtocolList(
80 const llvm::SmallVectorImpl<std::string> &Protocols);
81 llvm::Constant *GenerateClassStructure(
82 llvm::Constant *MetaClass,
83 llvm::Constant *SuperClass,
84 unsigned info,
85 llvm::Constant *Name,
86 llvm::Constant *Version,
87 llvm::Constant *InstanceSize,
88 llvm::Constant *IVars,
89 llvm::Constant *Methods,
90 llvm::Constant *Protocols);
91 llvm::Constant *GenerateProtocolMethodList(
92 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
93 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes);
94 llvm::Constant *MakeConstantString(const std::string &Str, const std::string
95 &Name="");
96 llvm::Constant *MakeGlobal(const llvm::StructType *Ty,
97 std::vector<llvm::Constant*> &V, const std::string &Name="");
98 llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty,
99 std::vector<llvm::Constant*> &V, const std::string &Name="");
Chris Lattner0f984262008-03-01 08:50:34 +0000100public:
Chris Lattnerdce14062008-06-26 04:19:03 +0000101 CGObjCGNU(CodeGen::CodeGenModule &cgm);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000102 virtual llvm::Constant *GenerateConstantString(const char *String,
103 const size_t length);
104 virtual llvm::Value *GenerateMessageSend(llvm::IRBuilder &Builder,
Chris Lattner0f984262008-03-01 08:50:34 +0000105 const llvm::Type *ReturnTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000106 llvm::Value *Sender,
Chris Lattner0f984262008-03-01 08:50:34 +0000107 llvm::Value *Receiver,
Chris Lattner9384c762008-06-26 04:42:20 +0000108 Selector Sel,
Chris Lattner0f984262008-03-01 08:50:34 +0000109 llvm::Value** ArgV,
110 unsigned ArgC);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000111 virtual llvm::Value *GenerateMessageSendSuper(llvm::IRBuilder &Builder,
Chris Lattner8e67b632008-06-26 04:37:12 +0000112 const llvm::Type *ReturnTy,
113 llvm::Value *Sender,
114 const char *SuperClassName,
115 llvm::Value *Receiver,
116 Selector Sel,
117 llvm::Value** ArgV,
118 unsigned ArgC);
Chris Lattner9384c762008-06-26 04:42:20 +0000119 virtual llvm::Value *LookupClass(llvm::IRBuilder &Builder,
120 llvm::Value *ClassName);
Chris Lattner42ba3e72008-06-26 04:38:58 +0000121 virtual llvm::Value *GetSelector(llvm::IRBuilder &Builder, Selector Sel);
Chris Lattner8e67b632008-06-26 04:37:12 +0000122
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000123 virtual llvm::Function *MethodPreamble(
124 const std::string &ClassName,
125 const std::string &CategoryName,
126 const std::string &MethodName,
127 const llvm::Type *ReturnTy,
128 const llvm::Type *SelfTy,
129 const llvm::Type **ArgTy,
130 unsigned ArgC,
131 bool isClassMethod,
132 bool isVarArg);
133 virtual void GenerateCategory(const char *ClassName, const char *CategoryName,
134 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
135 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
136 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
137 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
138 const llvm::SmallVectorImpl<std::string> &Protocols);
139 virtual void GenerateClass(
140 const char *ClassName,
141 const char *SuperClassName,
142 const int instanceSize,
143 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
144 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
145 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets,
146 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
147 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
148 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
149 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
150 const llvm::SmallVectorImpl<std::string> &Protocols);
151 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder &Builder, const char
152 *ProtocolName);
153 virtual void GenerateProtocol(const char *ProtocolName,
154 const llvm::SmallVectorImpl<std::string> &Protocols,
155 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
156 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
157 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
158 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes);
159 virtual llvm::Function *ModuleInitFunction();
Chris Lattner0f984262008-03-01 08:50:34 +0000160};
161} // end anonymous namespace
162
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000163
164
165static std::string SymbolNameForClass(const std::string &ClassName) {
166 return ".objc_class_" + ClassName;
167}
168
169static std::string SymbolNameForMethod(const std::string &ClassName, const
170 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
171{
172 return "._objc_method_" + ClassName +"("+CategoryName+")"+
173 (isClassMethod ? "+" : "-") + MethodName;
174}
175
Chris Lattnerdce14062008-06-26 04:19:03 +0000176CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
177 : CGM(cgm), TheModule(CGM.getModule()) {
178 IntTy = CGM.getTypes().ConvertType(CGM.getContext().IntTy);
179 LongTy = CGM.getTypes().ConvertType(CGM.getContext().LongTy);
180
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000181 Zeros[0] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
182 Zeros[1] = Zeros[0];
183 NULLPtr = llvm::ConstantPointerNull::get(
184 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
Chris Lattner391d77a2008-03-30 23:03:07 +0000185 // C string type. Used in lots of places.
186 PtrToInt8Ty =
187 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
188 // Get the selector Type.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000189 SelStructTy = llvm::StructType::get(
Chris Lattner391d77a2008-03-30 23:03:07 +0000190 PtrToInt8Ty,
191 PtrToInt8Ty,
192 NULL);
193 SelectorTy = llvm::PointerType::getUnqual(SelStructTy);
194 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
195 PtrTy = PtrToInt8Ty;
196
197 // Object type
198 llvm::PATypeHolder OpaqueObjTy = llvm::OpaqueType::get();
199 llvm::Type *OpaqueIdTy = llvm::PointerType::getUnqual(OpaqueObjTy);
200 IdTy = llvm::StructType::get(OpaqueIdTy, NULL);
201 llvm::cast<llvm::OpaqueType>(OpaqueObjTy.get())->refineAbstractTypeTo(IdTy);
202 IdTy = llvm::cast<llvm::StructType>(OpaqueObjTy.get());
203 IdTy = llvm::PointerType::getUnqual(IdTy);
204
205 // IMP type
206 std::vector<const llvm::Type*> IMPArgs;
207 IMPArgs.push_back(IdTy);
208 IMPArgs.push_back(SelectorTy);
209 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000210}
211// This has to perform the lookup every time, since posing and related
212// techniques can modify the name -> class mapping.
213llvm::Value *CGObjCGNU::LookupClass(llvm::IRBuilder &Builder,
214 llvm::Value *ClassName) {
215 llvm::Constant *ClassLookupFn =
216 TheModule.getOrInsertFunction("objc_lookup_class", IdTy, PtrToInt8Ty,
217 NULL);
218 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner391d77a2008-03-30 23:03:07 +0000219}
220
Chris Lattner8e67b632008-06-26 04:37:12 +0000221/// GetSelector - Return the pointer to the unique'd string for this selector.
222llvm::Value *CGObjCGNU::GetSelector(llvm::IRBuilder &Builder, Selector Sel) {
223 // FIXME: uniquing on the string is wasteful, unique on Sel instead!
224 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getName()];
225 if (US == 0)
226 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
227 llvm::GlobalValue::InternalLinkage,
228 ".objc_untyped_selector_alias",
229 NULL, &TheModule);
230
231 return Builder.CreateLoad(US);
232
233}
234
Chris Lattner5e7dcc62008-06-26 04:44:19 +0000235llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
236 const std::string &Name) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000237 llvm::Constant * ConstStr = llvm::ConstantArray::get(Str);
238 ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true,
239 llvm::GlobalValue::InternalLinkage,
240 ConstStr, Name, &TheModule);
241 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
242}
243llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
244 std::vector<llvm::Constant*> &V, const std::string &Name) {
245 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
246 return new llvm::GlobalVariable(Ty, false,
247 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
248}
249llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
250 std::vector<llvm::Constant*> &V, const std::string &Name) {
251 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
252 return new llvm::GlobalVariable(Ty, false,
253 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
254}
255
256/// Generate an NSConstantString object.
257//TODO: In case there are any crazy people still using the GNU runtime without
258//an OpenStep implementation, this should let them select their own class for
259//constant strings.
260llvm::Constant *CGObjCGNU::GenerateConstantString(const char *String, const
261 size_t length) {
Chris Lattner13fd7e52008-06-21 21:44:18 +0000262 std::string Str(String, String +length);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000263 std::vector<llvm::Constant*> Ivars;
264 Ivars.push_back(NULLPtr);
Chris Lattner13fd7e52008-06-21 21:44:18 +0000265 Ivars.push_back(MakeConstantString(Str));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000266 Ivars.push_back(llvm::ConstantInt::get(IntTy, length));
267 llvm::Constant *ObjCStr = MakeGlobal(
268 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
269 Ivars, ".objc_str");
270 ConstantStrings.push_back(
271 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
272 return ObjCStr;
273}
274
275///Generates a message send where the super is the receiver. This is a message
276///send to self with special delivery semantics indicating which class's method
277///should be called.
278llvm::Value *CGObjCGNU::GenerateMessageSendSuper(llvm::IRBuilder &Builder,
Chris Lattner8e67b632008-06-26 04:37:12 +0000279 const llvm::Type *ReturnTy,
280 llvm::Value *Sender,
281 const char *SuperClassName,
282 llvm::Value *Receiver,
283 Selector Sel,
284 llvm::Value** ArgV,
285 unsigned ArgC) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000286 // TODO: This should be cached, not looked up every time.
287 llvm::Value *ReceiverClass = LookupClass(Builder,
288 MakeConstantString(SuperClassName));
Chris Lattner8e67b632008-06-26 04:37:12 +0000289 llvm::Value *cmd = GetSelector(Builder, Sel);
Chris Lattner0f984262008-03-01 08:50:34 +0000290 std::vector<const llvm::Type*> impArgTypes;
291 impArgTypes.push_back(Receiver->getType());
Chris Lattner391d77a2008-03-30 23:03:07 +0000292 impArgTypes.push_back(SelectorTy);
Chris Lattner0f984262008-03-01 08:50:34 +0000293
294 // Avoid an explicit cast on the IMP by getting a version that has the right
295 // return type.
296 llvm::FunctionType *impType = llvm::FunctionType::get(ReturnTy, impArgTypes,
297 true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000298 // Construct the structure used to look up the IMP
299 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
300 IdTy, NULL);
301 llvm::Value *ObjCSuper = Builder.CreateAlloca(ObjCSuperTy);
Eli Friedman1e692ac2008-06-13 23:01:12 +0000302 // FIXME: volatility
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000303 Builder.CreateStore(Receiver, Builder.CreateStructGEP(ObjCSuper, 0));
304 Builder.CreateStore(ReceiverClass, Builder.CreateStructGEP(ObjCSuper, 1));
305
306 // Get the IMP
307 llvm::Constant *lookupFunction =
308 TheModule.getOrInsertFunction("objc_msg_lookup_super",
309 llvm::PointerType::getUnqual(impType),
310 llvm::PointerType::getUnqual(ObjCSuperTy),
311 SelectorTy, NULL);
312 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
313 llvm::Value *imp = Builder.CreateCall(lookupFunction, lookupArgs,
314 lookupArgs+2);
315
316 // Call the method
317 llvm::SmallVector<llvm::Value*, 8> callArgs;
318 callArgs.push_back(Receiver);
319 callArgs.push_back(cmd);
320 callArgs.insert(callArgs.end(), ArgV, ArgV+ArgC);
321 return Builder.CreateCall(imp, callArgs.begin(), callArgs.end());
322}
323
324/// Generate code for a message send expression.
325llvm::Value *CGObjCGNU::GenerateMessageSend(llvm::IRBuilder &Builder,
326 const llvm::Type *ReturnTy,
327 llvm::Value *Sender,
328 llvm::Value *Receiver,
Chris Lattner9384c762008-06-26 04:42:20 +0000329 Selector Sel,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000330 llvm::Value** ArgV,
331 unsigned ArgC) {
Chris Lattner9384c762008-06-26 04:42:20 +0000332 llvm::Value *cmd = GetSelector(Builder, Sel);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000333
334 // Look up the method implementation.
335 std::vector<const llvm::Type*> impArgTypes;
336 const llvm::Type *RetTy;
337 //TODO: Revisit this when LLVM supports aggregate return types.
338 if (ReturnTy->isSingleValueType() && ReturnTy != llvm::Type::VoidTy) {
339 RetTy = ReturnTy;
340 } else {
341 // For struct returns allocate the space in the caller and pass it up to
342 // the sender.
343 RetTy = llvm::Type::VoidTy;
344 impArgTypes.push_back(llvm::PointerType::getUnqual(ReturnTy));
345 }
346 impArgTypes.push_back(Receiver->getType());
347 impArgTypes.push_back(SelectorTy);
348
349 // Avoid an explicit cast on the IMP by getting a version that has the right
350 // return type.
351 llvm::FunctionType *impType = llvm::FunctionType::get(RetTy, impArgTypes,
352 true);
Chris Lattner0f984262008-03-01 08:50:34 +0000353
354 llvm::Constant *lookupFunction =
355 TheModule.getOrInsertFunction("objc_msg_lookup",
Chris Lattner391d77a2008-03-30 23:03:07 +0000356 llvm::PointerType::getUnqual(impType),
357 Receiver->getType(), SelectorTy, NULL);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000358 llvm::Value *imp = Builder.CreateCall2(lookupFunction, Receiver, cmd);
359
360 // Call the method.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000361 llvm::SmallVector<llvm::Value*, 16> Args;
362 if (!ReturnTy->isSingleValueType()) {
363 llvm::Value *Return = Builder.CreateAlloca(ReturnTy);
364 Args.push_back(Return);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000365 }
366 Args.push_back(Receiver);
367 Args.push_back(cmd);
368 Args.insert(Args.end(), ArgV, ArgV+ArgC);
Chris Lattner8fdf3282008-06-24 17:04:18 +0000369 if (!ReturnTy->isSingleValueType()) {
370 Builder.CreateCall(imp, Args.begin(), Args.end());
371 return Args[0];
372 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000373 return Builder.CreateCall(imp, Args.begin(), Args.end());
Chris Lattner0f984262008-03-01 08:50:34 +0000374}
375
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000376/// Generates a MethodList. Used in construction of a objc_class and
377/// objc_category structures.
378llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
379 const std::string &CategoryName,
380 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
381 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
382 bool isClassMethodList) {
383 // Get the method structure type.
384 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
385 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
386 PtrToInt8Ty, // Method types
387 llvm::PointerType::getUnqual(IMPTy), //Method pointer
388 NULL);
389 std::vector<llvm::Constant*> Methods;
390 std::vector<llvm::Constant*> Elements;
391 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
392 Elements.clear();
393 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
394 Zeros, 2));
395 Elements.push_back(
396 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
397 llvm::Constant *Method =
398 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattner550b8db2008-06-26 04:05:20 +0000399 getStringValue(MethodNames[i]),
400 isClassMethodList));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000401 Method = llvm::ConstantExpr::getBitCast(Method,
402 llvm::PointerType::getUnqual(IMPTy));
403 Elements.push_back(Method);
404 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
405 }
406
407 // Array of method structures
408 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
409 MethodNames.size());
410 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
411 Methods);
412
413 // Structure containing list pointer, array and array count
414 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
415 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
416 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
417 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
418 IntTy,
419 ObjCMethodArrayTy,
420 NULL);
421 // Refine next pointer type to concrete type
422 llvm::cast<llvm::OpaqueType>(
423 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
424 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
425
426 Methods.clear();
427 Methods.push_back(llvm::ConstantPointerNull::get(
428 llvm::PointerType::getUnqual(ObjCMethodListTy)));
429 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
430 MethodTypes.size()));
431 Methods.push_back(MethodArray);
432
433 // Create an instance of the structure
434 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
435}
436
437/// Generates an IvarList. Used in construction of a objc_class.
438llvm::Constant *CGObjCGNU::GenerateIvarList(
439 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
440 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
441 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
442 // Get the method structure type.
443 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
444 PtrToInt8Ty,
445 PtrToInt8Ty,
446 IntTy,
447 NULL);
448 std::vector<llvm::Constant*> Ivars;
449 std::vector<llvm::Constant*> Elements;
450 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
451 Elements.clear();
452 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
453 Zeros, 2));
454 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
455 Zeros, 2));
456 Elements.push_back(IvarOffsets[i]);
457 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
458 }
459
460 // Array of method structures
461 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
462 IvarNames.size());
463
464
465 Elements.clear();
466 Elements.push_back(llvm::ConstantInt::get(
467 llvm::cast<llvm::IntegerType>(IntTy), (int)IvarNames.size()));
468 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
469 // Structure containing array and array count
470 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
471 ObjCIvarArrayTy,
472 NULL);
473
474 // Create an instance of the structure
475 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
476}
477
478/// Generate a class structure
479llvm::Constant *CGObjCGNU::GenerateClassStructure(
480 llvm::Constant *MetaClass,
481 llvm::Constant *SuperClass,
482 unsigned info,
483 llvm::Constant *Name,
484 llvm::Constant *Version,
485 llvm::Constant *InstanceSize,
486 llvm::Constant *IVars,
487 llvm::Constant *Methods,
488 llvm::Constant *Protocols) {
489 // Set up the class structure
490 // Note: Several of these are char*s when they should be ids. This is
491 // because the runtime performs this translation on load.
492 llvm::StructType *ClassTy = llvm::StructType::get(
493 PtrToInt8Ty, // class_pointer
494 PtrToInt8Ty, // super_class
495 PtrToInt8Ty, // name
496 LongTy, // version
497 LongTy, // info
498 LongTy, // instance_size
499 IVars->getType(), // ivars
500 Methods->getType(), // methods
501 // These are all filled in by the runtime, so we pretend
502 PtrTy, // dtable
503 PtrTy, // subclass_list
504 PtrTy, // sibling_class
505 PtrTy, // protocols
506 PtrTy, // gc_object_type
507 NULL);
508 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
509 llvm::Constant *NullP =
510 llvm::ConstantPointerNull::get(llvm::cast<llvm::PointerType>(PtrTy));
511 // Fill in the structure
512 std::vector<llvm::Constant*> Elements;
513 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
514 Elements.push_back(SuperClass);
515 Elements.push_back(Name);
516 Elements.push_back(Zero);
517 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
518 Elements.push_back(InstanceSize);
519 Elements.push_back(IVars);
520 Elements.push_back(Methods);
521 Elements.push_back(NullP);
522 Elements.push_back(NullP);
523 Elements.push_back(NullP);
524 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
525 Elements.push_back(NullP);
526 // Create an instance of the structure
527 return MakeGlobal(ClassTy, Elements,
Chris Lattner550b8db2008-06-26 04:05:20 +0000528 SymbolNameForClass(getStringValue(Name)));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000529}
530
531llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
532 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
533 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
534 // Get the method structure type.
535 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
536 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
537 PtrToInt8Ty,
538 NULL);
539 std::vector<llvm::Constant*> Methods;
540 std::vector<llvm::Constant*> Elements;
541 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
542 Elements.clear();
543 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
544 Zeros, 2));
545 Elements.push_back(
546 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
547 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
548 }
549 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
550 MethodNames.size());
551 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
552 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
553 IntTy, ObjCMethodArrayTy, NULL);
554 Methods.clear();
555 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
556 Methods.push_back(Array);
557 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
558}
559// Create the protocol list structure used in classes, categories and so on
560llvm::Constant *CGObjCGNU::GenerateProtocolList(
561 const llvm::SmallVectorImpl<std::string> &Protocols) {
562 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
563 Protocols.size());
564 llvm::StructType *ProtocolListTy = llvm::StructType::get(
565 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
566 LongTy,//FIXME: Should be size_t
567 ProtocolArrayTy,
568 NULL);
569 std::vector<llvm::Constant*> Elements;
570 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
571 iter != endIter ; iter++) {
572 llvm::Constant *Ptr =
573 llvm::ConstantExpr::getBitCast(ExistingProtocols[*iter], PtrToInt8Ty);
574 Elements.push_back(Ptr);
575 }
576 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
577 Elements);
578 Elements.clear();
579 Elements.push_back(NULLPtr);
580 Elements.push_back(llvm::ConstantInt::get(
581 llvm::cast<llvm::IntegerType>(LongTy), Protocols.size()));
582 Elements.push_back(ProtocolArray);
583 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
584}
585
586llvm::Value *CGObjCGNU::GenerateProtocolRef(llvm::IRBuilder &Builder, const
587 char *ProtocolName) {
588 return ExistingProtocols[ProtocolName];
589}
590
591void CGObjCGNU::GenerateProtocol(const char *ProtocolName,
592 const llvm::SmallVectorImpl<std::string> &Protocols,
593 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
594 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
595 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
596 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes) {
597
598 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
599 llvm::Constant *InstanceMethodList =
600 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
601 llvm::Constant *ClassMethodList =
602 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
603 // Protocols are objects containing lists of the methods implemented and
604 // protocols adopted.
605 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
606 PtrToInt8Ty,
607 ProtocolList->getType(),
608 InstanceMethodList->getType(),
609 ClassMethodList->getType(),
610 NULL);
611 std::vector<llvm::Constant*> Elements;
612 // The isa pointer must be set to a magic number so the runtime knows it's
613 // the correct layout.
614 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
615 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
616 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
617 Elements.push_back(ProtocolList);
618 Elements.push_back(InstanceMethodList);
619 Elements.push_back(ClassMethodList);
620 ExistingProtocols[ProtocolName] =
621 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
622 ".objc_protocol"), IdTy);
623}
624
625void CGObjCGNU::GenerateCategory(
626 const char *ClassName,
627 const char *CategoryName,
628 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
629 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
630 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
631 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
632 const llvm::SmallVectorImpl<std::string> &Protocols) {
633 std::vector<llvm::Constant*> Elements;
634 Elements.push_back(MakeConstantString(CategoryName));
635 Elements.push_back(MakeConstantString(ClassName));
636 // Instance method list
637 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
638 ClassName, CategoryName, InstanceMethodNames, InstanceMethodTypes,
639 false), PtrTy));
640 // Class method list
641 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
642 ClassName, CategoryName, ClassMethodNames, ClassMethodTypes, true),
643 PtrTy));
644 // Protocol list
645 Elements.push_back(llvm::ConstantExpr::getBitCast(
646 GenerateProtocolList(Protocols), PtrTy));
647 Categories.push_back(llvm::ConstantExpr::getBitCast(
648 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
649 PtrTy, PtrTy, NULL), Elements), PtrTy));
650}
651void CGObjCGNU::GenerateClass(
652 const char *ClassName,
653 const char *SuperClassName,
654 const int instanceSize,
655 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
656 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
657 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets,
658 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
659 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
660 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
661 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
662 const llvm::SmallVectorImpl<std::string> &Protocols) {
663 // Get the superclass pointer.
664 llvm::Constant *SuperClass;
665 if (SuperClassName) {
666 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
667 } else {
668 SuperClass = llvm::ConstantPointerNull::get(
669 llvm::cast<llvm::PointerType>(PtrToInt8Ty));
670 }
671 llvm::Constant * Name = MakeConstantString(ClassName, ".class_name");
672 // 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, "",
676 InstanceMethodNames, InstanceMethodTypes, false);
677 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
678 ClassMethodNames, ClassMethodTypes, true);
679 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
680 IvarOffsets);
681 //Generate metaclass for class methods
682 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
683 NULLPtr, 0x2L, NULLPtr, 0, Zeros[0], GenerateIvarList(
684 empty, empty, empty), ClassMethodList, NULLPtr);
685 // Generate the class structure
686 llvm::Constant *ClassStruct = GenerateClassStructure(MetaClassStruct,
687 SuperClass, 0x1L, Name, 0,
688 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);
829 llvm::IRBuilder Builder;
830 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
Chris Lattnerdce14062008-06-26 04:19:03 +0000879CodeGen::CGObjCRuntime *CodeGen::CreateObjCRuntime(CodeGen::CodeGenModule &CGM){
880 return new CGObjCGNU(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +0000881}