blob: 2b8f7edc051a240678e28bf5912298e9c0d5fa67 [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 Lattner391d77a2008-03-30 23:03:07 +0000108 llvm::Value *Selector,
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,
112 const llvm::Type *ReturnTy,
113 llvm::Value *Sender,
114 const char *SuperClassName,
115 llvm::Value *Receiver,
116 llvm::Value *Selector,
117 llvm::Value** ArgV,
118 unsigned ArgC);
119 virtual llvm::Value *LookupClass(llvm::IRBuilder &Builder, llvm::Value
120 *ClassName);
121 virtual llvm::Value *GetSelector(llvm::IRBuilder &Builder,
Chris Lattner391d77a2008-03-30 23:03:07 +0000122 llvm::Value *SelName,
123 llvm::Value *SelTypes);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000124 virtual llvm::Function *MethodPreamble(
125 const std::string &ClassName,
126 const std::string &CategoryName,
127 const std::string &MethodName,
128 const llvm::Type *ReturnTy,
129 const llvm::Type *SelfTy,
130 const llvm::Type **ArgTy,
131 unsigned ArgC,
132 bool isClassMethod,
133 bool isVarArg);
134 virtual void GenerateCategory(const char *ClassName, const char *CategoryName,
135 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
136 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
137 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
138 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
139 const llvm::SmallVectorImpl<std::string> &Protocols);
140 virtual void GenerateClass(
141 const char *ClassName,
142 const char *SuperClassName,
143 const int instanceSize,
144 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
145 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
146 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets,
147 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
148 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
149 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
150 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
151 const llvm::SmallVectorImpl<std::string> &Protocols);
152 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder &Builder, const char
153 *ProtocolName);
154 virtual void GenerateProtocol(const char *ProtocolName,
155 const llvm::SmallVectorImpl<std::string> &Protocols,
156 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
157 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
158 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
159 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes);
160 virtual llvm::Function *ModuleInitFunction();
Chris Lattner0f984262008-03-01 08:50:34 +0000161};
162} // end anonymous namespace
163
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000164
165
166static std::string SymbolNameForClass(const std::string &ClassName) {
167 return ".objc_class_" + ClassName;
168}
169
170static std::string SymbolNameForMethod(const std::string &ClassName, const
171 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
172{
173 return "._objc_method_" + ClassName +"("+CategoryName+")"+
174 (isClassMethod ? "+" : "-") + MethodName;
175}
176
Chris Lattnerdce14062008-06-26 04:19:03 +0000177CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
178 : CGM(cgm), TheModule(CGM.getModule()) {
179 IntTy = CGM.getTypes().ConvertType(CGM.getContext().IntTy);
180 LongTy = CGM.getTypes().ConvertType(CGM.getContext().LongTy);
181
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000182 Zeros[0] = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
183 Zeros[1] = Zeros[0];
184 NULLPtr = llvm::ConstantPointerNull::get(
185 llvm::PointerType::getUnqual(llvm::Type::Int8Ty));
Chris Lattner391d77a2008-03-30 23:03:07 +0000186 // C string type. Used in lots of places.
187 PtrToInt8Ty =
188 llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
189 // Get the selector Type.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000190 SelStructTy = llvm::StructType::get(
Chris Lattner391d77a2008-03-30 23:03:07 +0000191 PtrToInt8Ty,
192 PtrToInt8Ty,
193 NULL);
194 SelectorTy = llvm::PointerType::getUnqual(SelStructTy);
195 PtrToIntTy = llvm::PointerType::getUnqual(IntTy);
196 PtrTy = PtrToInt8Ty;
197
198 // Object type
199 llvm::PATypeHolder OpaqueObjTy = llvm::OpaqueType::get();
200 llvm::Type *OpaqueIdTy = llvm::PointerType::getUnqual(OpaqueObjTy);
201 IdTy = llvm::StructType::get(OpaqueIdTy, NULL);
202 llvm::cast<llvm::OpaqueType>(OpaqueObjTy.get())->refineAbstractTypeTo(IdTy);
203 IdTy = llvm::cast<llvm::StructType>(OpaqueObjTy.get());
204 IdTy = llvm::PointerType::getUnqual(IdTy);
205
206 // IMP type
207 std::vector<const llvm::Type*> IMPArgs;
208 IMPArgs.push_back(IdTy);
209 IMPArgs.push_back(SelectorTy);
210 IMPTy = llvm::FunctionType::get(IdTy, IMPArgs, true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000211}
212// This has to perform the lookup every time, since posing and related
213// techniques can modify the name -> class mapping.
214llvm::Value *CGObjCGNU::LookupClass(llvm::IRBuilder &Builder,
215 llvm::Value *ClassName) {
216 llvm::Constant *ClassLookupFn =
217 TheModule.getOrInsertFunction("objc_lookup_class", IdTy, PtrToInt8Ty,
218 NULL);
219 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattner391d77a2008-03-30 23:03:07 +0000220}
221
222/// Looks up the selector for the specified name / type pair.
223// FIXME: Selectors should be statically cached, not looked up on every call.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000224llvm::Value *CGObjCGNU::GetSelector(llvm::IRBuilder &Builder,
Chris Lattner3aba07c2008-06-26 04:24:57 +0000225 llvm::Value *SelName,
226 llvm::Value *SelTypes) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000227 // For static selectors, we return an alias for now then store them all in a
228 // list that the runtime will initialise later.
229 if (llvm::Constant *CName = dyn_cast<llvm::Constant>(SelName)) {
230 // Untyped selector
231 if (SelTypes == 0) {
232 // If it's already cached, return it.
Chris Lattner3aba07c2008-06-26 04:24:57 +0000233 llvm::GlobalAlias *&US = UntypedSelectors[getStringValue(CName)];
234 if (US == 0) {
235 // If it isn't, cache it.
236 US = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
237 llvm::GlobalValue::InternalLinkage,
238 ".objc_untyped_selector_alias",
239 NULL, &TheModule);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000240 }
Chris Lattner3aba07c2008-06-26 04:24:57 +0000241 return Builder.CreateLoad(US);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000242 }
243 // Typed selectors
244 if (llvm::Constant *CTypes = dyn_cast<llvm::Constant>(SelTypes)) {
Chris Lattner550b8db2008-06-26 04:05:20 +0000245 TypedSelector Selector = TypedSelector(getStringValue(CName),
246 getStringValue(CTypes));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000247 // If it's already cached, return it.
Chris Lattner3aba07c2008-06-26 04:24:57 +0000248 llvm::GlobalAlias *&TS = TypedSelectors[Selector];
249 if (TS == 0) {
250 // If it isn't, cache it.
251 TS = new llvm::GlobalAlias(llvm::PointerType::getUnqual(SelectorTy),
252 llvm::GlobalValue::InternalLinkage,
253 ".objc_typed_selector_alias",
254 NULL, &TheModule);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000255 }
Chris Lattner3aba07c2008-06-26 04:24:57 +0000256 return Builder.CreateLoad(TS);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000257 }
258 }
259 // Dynamically look up selectors from non-constant sources
Chris Lattner391d77a2008-03-30 23:03:07 +0000260 llvm::Value *cmd;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000261 if (SelTypes == 0) {
Chris Lattner391d77a2008-03-30 23:03:07 +0000262 llvm::Constant *SelFunction = TheModule.getOrInsertFunction("sel_get_uid",
Chris Lattner3aba07c2008-06-26 04:24:57 +0000263 SelectorTy,
264 PtrToInt8Ty,
265 NULL);
Chris Lattner391d77a2008-03-30 23:03:07 +0000266 cmd = Builder.CreateCall(SelFunction, SelName);
267 }
268 else {
269 llvm::Constant *SelFunction =
270 TheModule.getOrInsertFunction("sel_get_typed_uid",
Chris Lattner3aba07c2008-06-26 04:24:57 +0000271 SelectorTy, PtrToInt8Ty, PtrToInt8Ty, 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) {
Chris Lattner13fd7e52008-06-21 21:44:18 +0000305 std::string Str(String, String +length);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000306 std::vector<llvm::Constant*> Ivars;
307 Ivars.push_back(NULLPtr);
Chris Lattner13fd7e52008-06-21 21:44:18 +0000308 Ivars.push_back(MakeConstantString(Str));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000309 Ivars.push_back(llvm::ConstantInt::get(IntTy, length));
310 llvm::Constant *ObjCStr = MakeGlobal(
311 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
312 Ivars, ".objc_str");
313 ConstantStrings.push_back(
314 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
315 return ObjCStr;
316}
317
318///Generates a message send where the super is the receiver. This is a message
319///send to self with special delivery semantics indicating which class's method
320///should be called.
321llvm::Value *CGObjCGNU::GenerateMessageSendSuper(llvm::IRBuilder &Builder,
Chris Lattner0f984262008-03-01 08:50:34 +0000322 const llvm::Type *ReturnTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000323 llvm::Value *Sender,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000324 const char *SuperClassName,
Chris Lattner0f984262008-03-01 08:50:34 +0000325 llvm::Value *Receiver,
Chris Lattner391d77a2008-03-30 23:03:07 +0000326 llvm::Value *Selector,
Chris Lattner0f984262008-03-01 08:50:34 +0000327 llvm::Value** ArgV,
328 unsigned ArgC) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000329 // TODO: This should be cached, not looked up every time.
330 llvm::Value *ReceiverClass = LookupClass(Builder,
331 MakeConstantString(SuperClassName));
332 llvm::Value *cmd = GetSelector(Builder, Selector, 0);
Chris Lattner0f984262008-03-01 08:50:34 +0000333 std::vector<const llvm::Type*> impArgTypes;
334 impArgTypes.push_back(Receiver->getType());
Chris Lattner391d77a2008-03-30 23:03:07 +0000335 impArgTypes.push_back(SelectorTy);
Chris Lattner0f984262008-03-01 08:50:34 +0000336
337 // Avoid an explicit cast on the IMP by getting a version that has the right
338 // return type.
339 llvm::FunctionType *impType = llvm::FunctionType::get(ReturnTy, impArgTypes,
340 true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000341 // Construct the structure used to look up the IMP
342 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
343 IdTy, NULL);
344 llvm::Value *ObjCSuper = Builder.CreateAlloca(ObjCSuperTy);
Eli Friedman1e692ac2008-06-13 23:01:12 +0000345 // FIXME: volatility
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000346 Builder.CreateStore(Receiver, Builder.CreateStructGEP(ObjCSuper, 0));
347 Builder.CreateStore(ReceiverClass, Builder.CreateStructGEP(ObjCSuper, 1));
348
349 // Get the IMP
350 llvm::Constant *lookupFunction =
351 TheModule.getOrInsertFunction("objc_msg_lookup_super",
352 llvm::PointerType::getUnqual(impType),
353 llvm::PointerType::getUnqual(ObjCSuperTy),
354 SelectorTy, NULL);
355 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
356 llvm::Value *imp = Builder.CreateCall(lookupFunction, lookupArgs,
357 lookupArgs+2);
358
359 // Call the method
360 llvm::SmallVector<llvm::Value*, 8> callArgs;
361 callArgs.push_back(Receiver);
362 callArgs.push_back(cmd);
363 callArgs.insert(callArgs.end(), ArgV, ArgV+ArgC);
364 return Builder.CreateCall(imp, callArgs.begin(), callArgs.end());
365}
366
367/// Generate code for a message send expression.
368llvm::Value *CGObjCGNU::GenerateMessageSend(llvm::IRBuilder &Builder,
369 const llvm::Type *ReturnTy,
370 llvm::Value *Sender,
371 llvm::Value *Receiver,
372 llvm::Value *Selector,
373 llvm::Value** ArgV,
374 unsigned ArgC) {
375 llvm::Value *cmd = GetSelector(Builder, Selector, 0);
376
377 // Look up the method implementation.
378 std::vector<const llvm::Type*> impArgTypes;
379 const llvm::Type *RetTy;
380 //TODO: Revisit this when LLVM supports aggregate return types.
381 if (ReturnTy->isSingleValueType() && ReturnTy != llvm::Type::VoidTy) {
382 RetTy = ReturnTy;
383 } else {
384 // For struct returns allocate the space in the caller and pass it up to
385 // the sender.
386 RetTy = llvm::Type::VoidTy;
387 impArgTypes.push_back(llvm::PointerType::getUnqual(ReturnTy));
388 }
389 impArgTypes.push_back(Receiver->getType());
390 impArgTypes.push_back(SelectorTy);
391
392 // Avoid an explicit cast on the IMP by getting a version that has the right
393 // return type.
394 llvm::FunctionType *impType = llvm::FunctionType::get(RetTy, impArgTypes,
395 true);
Chris Lattner0f984262008-03-01 08:50:34 +0000396
397 llvm::Constant *lookupFunction =
398 TheModule.getOrInsertFunction("objc_msg_lookup",
Chris Lattner391d77a2008-03-30 23:03:07 +0000399 llvm::PointerType::getUnqual(impType),
400 Receiver->getType(), SelectorTy, NULL);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000401 llvm::Value *imp = Builder.CreateCall2(lookupFunction, Receiver, cmd);
402
403 // Call the method.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000404 llvm::SmallVector<llvm::Value*, 16> Args;
405 if (!ReturnTy->isSingleValueType()) {
406 llvm::Value *Return = Builder.CreateAlloca(ReturnTy);
407 Args.push_back(Return);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000408 }
409 Args.push_back(Receiver);
410 Args.push_back(cmd);
411 Args.insert(Args.end(), ArgV, ArgV+ArgC);
Chris Lattner8fdf3282008-06-24 17:04:18 +0000412 if (!ReturnTy->isSingleValueType()) {
413 Builder.CreateCall(imp, Args.begin(), Args.end());
414 return Args[0];
415 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000416 return Builder.CreateCall(imp, Args.begin(), Args.end());
Chris Lattner0f984262008-03-01 08:50:34 +0000417}
418
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000419/// Generates a MethodList. Used in construction of a objc_class and
420/// objc_category structures.
421llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
422 const std::string &CategoryName,
423 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
424 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
425 bool isClassMethodList) {
426 // Get the method structure type.
427 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
428 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
429 PtrToInt8Ty, // Method types
430 llvm::PointerType::getUnqual(IMPTy), //Method pointer
431 NULL);
432 std::vector<llvm::Constant*> Methods;
433 std::vector<llvm::Constant*> Elements;
434 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
435 Elements.clear();
436 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
437 Zeros, 2));
438 Elements.push_back(
439 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
440 llvm::Constant *Method =
441 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattner550b8db2008-06-26 04:05:20 +0000442 getStringValue(MethodNames[i]),
443 isClassMethodList));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000444 Method = llvm::ConstantExpr::getBitCast(Method,
445 llvm::PointerType::getUnqual(IMPTy));
446 Elements.push_back(Method);
447 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
448 }
449
450 // Array of method structures
451 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
452 MethodNames.size());
453 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
454 Methods);
455
456 // Structure containing list pointer, array and array count
457 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
458 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
459 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
460 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
461 IntTy,
462 ObjCMethodArrayTy,
463 NULL);
464 // Refine next pointer type to concrete type
465 llvm::cast<llvm::OpaqueType>(
466 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
467 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
468
469 Methods.clear();
470 Methods.push_back(llvm::ConstantPointerNull::get(
471 llvm::PointerType::getUnqual(ObjCMethodListTy)));
472 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
473 MethodTypes.size()));
474 Methods.push_back(MethodArray);
475
476 // Create an instance of the structure
477 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
478}
479
480/// Generates an IvarList. Used in construction of a objc_class.
481llvm::Constant *CGObjCGNU::GenerateIvarList(
482 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
483 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
484 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
485 // Get the method structure type.
486 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
487 PtrToInt8Ty,
488 PtrToInt8Ty,
489 IntTy,
490 NULL);
491 std::vector<llvm::Constant*> Ivars;
492 std::vector<llvm::Constant*> Elements;
493 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
494 Elements.clear();
495 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
496 Zeros, 2));
497 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
498 Zeros, 2));
499 Elements.push_back(IvarOffsets[i]);
500 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
501 }
502
503 // Array of method structures
504 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
505 IvarNames.size());
506
507
508 Elements.clear();
509 Elements.push_back(llvm::ConstantInt::get(
510 llvm::cast<llvm::IntegerType>(IntTy), (int)IvarNames.size()));
511 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
512 // Structure containing array and array count
513 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
514 ObjCIvarArrayTy,
515 NULL);
516
517 // Create an instance of the structure
518 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
519}
520
521/// Generate a class structure
522llvm::Constant *CGObjCGNU::GenerateClassStructure(
523 llvm::Constant *MetaClass,
524 llvm::Constant *SuperClass,
525 unsigned info,
526 llvm::Constant *Name,
527 llvm::Constant *Version,
528 llvm::Constant *InstanceSize,
529 llvm::Constant *IVars,
530 llvm::Constant *Methods,
531 llvm::Constant *Protocols) {
532 // Set up the class structure
533 // Note: Several of these are char*s when they should be ids. This is
534 // because the runtime performs this translation on load.
535 llvm::StructType *ClassTy = llvm::StructType::get(
536 PtrToInt8Ty, // class_pointer
537 PtrToInt8Ty, // super_class
538 PtrToInt8Ty, // name
539 LongTy, // version
540 LongTy, // info
541 LongTy, // instance_size
542 IVars->getType(), // ivars
543 Methods->getType(), // methods
544 // These are all filled in by the runtime, so we pretend
545 PtrTy, // dtable
546 PtrTy, // subclass_list
547 PtrTy, // sibling_class
548 PtrTy, // protocols
549 PtrTy, // gc_object_type
550 NULL);
551 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
552 llvm::Constant *NullP =
553 llvm::ConstantPointerNull::get(llvm::cast<llvm::PointerType>(PtrTy));
554 // Fill in the structure
555 std::vector<llvm::Constant*> Elements;
556 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
557 Elements.push_back(SuperClass);
558 Elements.push_back(Name);
559 Elements.push_back(Zero);
560 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
561 Elements.push_back(InstanceSize);
562 Elements.push_back(IVars);
563 Elements.push_back(Methods);
564 Elements.push_back(NullP);
565 Elements.push_back(NullP);
566 Elements.push_back(NullP);
567 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
568 Elements.push_back(NullP);
569 // Create an instance of the structure
570 return MakeGlobal(ClassTy, Elements,
Chris Lattner550b8db2008-06-26 04:05:20 +0000571 SymbolNameForClass(getStringValue(Name)));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000572}
573
574llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
575 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
576 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
577 // Get the method structure type.
578 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
579 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
580 PtrToInt8Ty,
581 NULL);
582 std::vector<llvm::Constant*> Methods;
583 std::vector<llvm::Constant*> Elements;
584 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
585 Elements.clear();
586 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
587 Zeros, 2));
588 Elements.push_back(
589 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
590 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
591 }
592 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
593 MethodNames.size());
594 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
595 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
596 IntTy, ObjCMethodArrayTy, NULL);
597 Methods.clear();
598 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
599 Methods.push_back(Array);
600 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
601}
602// Create the protocol list structure used in classes, categories and so on
603llvm::Constant *CGObjCGNU::GenerateProtocolList(
604 const llvm::SmallVectorImpl<std::string> &Protocols) {
605 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
606 Protocols.size());
607 llvm::StructType *ProtocolListTy = llvm::StructType::get(
608 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
609 LongTy,//FIXME: Should be size_t
610 ProtocolArrayTy,
611 NULL);
612 std::vector<llvm::Constant*> Elements;
613 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
614 iter != endIter ; iter++) {
615 llvm::Constant *Ptr =
616 llvm::ConstantExpr::getBitCast(ExistingProtocols[*iter], PtrToInt8Ty);
617 Elements.push_back(Ptr);
618 }
619 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
620 Elements);
621 Elements.clear();
622 Elements.push_back(NULLPtr);
623 Elements.push_back(llvm::ConstantInt::get(
624 llvm::cast<llvm::IntegerType>(LongTy), Protocols.size()));
625 Elements.push_back(ProtocolArray);
626 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
627}
628
629llvm::Value *CGObjCGNU::GenerateProtocolRef(llvm::IRBuilder &Builder, const
630 char *ProtocolName) {
631 return ExistingProtocols[ProtocolName];
632}
633
634void CGObjCGNU::GenerateProtocol(const char *ProtocolName,
635 const llvm::SmallVectorImpl<std::string> &Protocols,
636 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
637 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
638 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
639 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes) {
640
641 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
642 llvm::Constant *InstanceMethodList =
643 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
644 llvm::Constant *ClassMethodList =
645 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
646 // Protocols are objects containing lists of the methods implemented and
647 // protocols adopted.
648 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
649 PtrToInt8Ty,
650 ProtocolList->getType(),
651 InstanceMethodList->getType(),
652 ClassMethodList->getType(),
653 NULL);
654 std::vector<llvm::Constant*> Elements;
655 // The isa pointer must be set to a magic number so the runtime knows it's
656 // the correct layout.
657 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
658 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
659 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
660 Elements.push_back(ProtocolList);
661 Elements.push_back(InstanceMethodList);
662 Elements.push_back(ClassMethodList);
663 ExistingProtocols[ProtocolName] =
664 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
665 ".objc_protocol"), IdTy);
666}
667
668void CGObjCGNU::GenerateCategory(
669 const char *ClassName,
670 const char *CategoryName,
671 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
672 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
673 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
674 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
675 const llvm::SmallVectorImpl<std::string> &Protocols) {
676 std::vector<llvm::Constant*> Elements;
677 Elements.push_back(MakeConstantString(CategoryName));
678 Elements.push_back(MakeConstantString(ClassName));
679 // Instance method list
680 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
681 ClassName, CategoryName, InstanceMethodNames, InstanceMethodTypes,
682 false), PtrTy));
683 // Class method list
684 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
685 ClassName, CategoryName, ClassMethodNames, ClassMethodTypes, true),
686 PtrTy));
687 // Protocol list
688 Elements.push_back(llvm::ConstantExpr::getBitCast(
689 GenerateProtocolList(Protocols), PtrTy));
690 Categories.push_back(llvm::ConstantExpr::getBitCast(
691 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
692 PtrTy, PtrTy, NULL), Elements), PtrTy));
693}
694void CGObjCGNU::GenerateClass(
695 const char *ClassName,
696 const char *SuperClassName,
697 const int instanceSize,
698 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
699 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
700 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets,
701 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
702 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
703 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
704 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
705 const llvm::SmallVectorImpl<std::string> &Protocols) {
706 // Get the superclass pointer.
707 llvm::Constant *SuperClass;
708 if (SuperClassName) {
709 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
710 } else {
711 SuperClass = llvm::ConstantPointerNull::get(
712 llvm::cast<llvm::PointerType>(PtrToInt8Ty));
713 }
714 llvm::Constant * Name = MakeConstantString(ClassName, ".class_name");
715 // Empty vector used to construct empty method lists
716 llvm::SmallVector<llvm::Constant*, 1> empty;
717 // Generate the method and instance variable lists
718 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
719 InstanceMethodNames, InstanceMethodTypes, false);
720 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
721 ClassMethodNames, ClassMethodTypes, true);
722 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
723 IvarOffsets);
724 //Generate metaclass for class methods
725 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
726 NULLPtr, 0x2L, NULLPtr, 0, Zeros[0], GenerateIvarList(
727 empty, empty, empty), ClassMethodList, NULLPtr);
728 // Generate the class structure
729 llvm::Constant *ClassStruct = GenerateClassStructure(MetaClassStruct,
730 SuperClass, 0x1L, Name, 0,
731 llvm::ConstantInt::get(llvm::Type::Int32Ty, instanceSize), IvarList,
732 MethodList, GenerateProtocolList(Protocols));
733 // Add class structure to list to be added to the symtab later
734 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
735 Classes.push_back(ClassStruct);
736}
737
738llvm::Function *CGObjCGNU::ModuleInitFunction() {
739 // Only emit an ObjC load function if no Objective-C stuff has been called
740 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
741 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov5f58b912008-06-01 15:14:46 +0000742 UntypedSelectors.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000743 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +0000744
745 // Name the ObjC types to make the IR a bit easier to read
746 TheModule.addTypeName(".objc_selector", SelectorTy);
747 TheModule.addTypeName(".objc_id", IdTy);
748 TheModule.addTypeName(".objc_imp", IMPTy);
749
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000750 std::vector<llvm::Constant*> Elements;
751 // Generate statics list:
752 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
753 ConstantStrings.size() + 1);
754 ConstantStrings.push_back(NULLPtr);
755 Elements.push_back(MakeConstantString("NSConstantString",
756 ".objc_static_class_name"));
757 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings));
758 llvm::StructType *StaticsListTy =
759 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
Chris Lattner630404b2008-06-26 04:10:42 +0000760 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000761 llvm::Constant *Statics =
762 MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Chris Lattner4e0b2642008-06-24 17:01:28 +0000763 llvm::ArrayType *StaticsListArrayTy =
Chris Lattner630404b2008-06-26 04:10:42 +0000764 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner4e0b2642008-06-24 17:01:28 +0000765 Elements.clear();
766 Elements.push_back(Statics);
Chris Lattner630404b2008-06-26 04:10:42 +0000767 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner4e0b2642008-06-24 17:01:28 +0000768 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000769 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
770 // Array of classes, categories, and constant objects
771 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
772 Classes.size() + Categories.size() + 2);
Chris Lattner630404b2008-06-26 04:10:42 +0000773 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelectorTy,
774 llvm::Type::Int16Ty,
775 llvm::Type::Int16Ty,
776 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000777
778 Elements.clear();
779 // Pointer to an array of selectors used in this module.
780 std::vector<llvm::Constant*> Selectors;
781 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
782 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
783 iter != iterEnd ; ++iter) {
Chris Lattner630404b2008-06-26 04:10:42 +0000784 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
785 Elements.push_back(MakeConstantString(iter->first.second,
786 ".objc_sel_types"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000787 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
788 Elements.clear();
789 }
790 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
791 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner630404b2008-06-26 04:10:42 +0000792 iter != iterEnd; ++iter) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000793 Elements.push_back(
Chris Lattner630404b2008-06-26 04:10:42 +0000794 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000795 Elements.push_back(NULLPtr);
796 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
797 Elements.clear();
798 }
799 Elements.push_back(NULLPtr);
800 Elements.push_back(NULLPtr);
801 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
802 Elements.clear();
803 // Number of static selectors
804 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
805 llvm::Constant *SelectorList = MakeGlobal(
806 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
807 ".objc_selector_list");
808 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList, SelectorTy));
809
810 // Now that all of the static selectors exist, create pointers to them.
811 int index = 0;
812 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
813 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
814 iter != iterEnd; ++iter) {
815 llvm::Constant *Idxs[] = {Zeros[0],
816 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
817 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
818 llvm::GlobalValue::InternalLinkage,
819 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
820 ".objc_sel_ptr", &TheModule);
821 (*iter).second->setAliasee(SelPtr);
822 }
823 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
824 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
825 iter != iterEnd; iter++) {
826 llvm::Constant *Idxs[] = {Zeros[0],
827 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
828 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
829 llvm::GlobalValue::InternalLinkage,
830 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
831 ".objc_sel_ptr", &TheModule);
832 (*iter).second->setAliasee(SelPtr);
833 }
834 // Number of classes defined.
835 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
836 Classes.size()));
837 // Number of categories defined
838 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
839 Categories.size()));
840 // Create an array of classes, then categories, then static object instances
841 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
842 // NULL-terminated list of static object instances (mainly constant strings)
843 Classes.push_back(Statics);
844 Classes.push_back(NULLPtr);
845 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
846 Elements.push_back(ClassList);
847 // Construct the symbol table
848 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
849
850 // The symbol table is contained in a module which has some version-checking
851 // constants
852 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
853 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
854 Elements.clear();
855 // Runtime version used for compatibility checking.
856 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
857 //FIXME: Should be sizeof(ModuleTy)
858 Elements.push_back(llvm::ConstantInt::get(LongTy, 16));
859 //FIXME: Should be the path to the file where this module was declared
860 Elements.push_back(NULLPtr);
861 Elements.push_back(SymTab);
862 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
863
864 // Create the load function calling the runtime entry point with the module
865 // structure
866 std::vector<const llvm::Type*> VoidArgs;
867 llvm::Function * LoadFunction = llvm::Function::Create(
868 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
869 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
870 &TheModule);
871 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
872 llvm::IRBuilder Builder;
873 Builder.SetInsertPoint(EntryBB);
874 llvm::Value *Register = TheModule.getOrInsertFunction("__objc_exec_class",
875 llvm::Type::VoidTy, llvm::PointerType::getUnqual(ModuleTy), NULL);
876 Builder.CreateCall(Register, Module);
877 Builder.CreateRetVoid();
878 return LoadFunction;
879}
Chris Lattner391d77a2008-03-30 23:03:07 +0000880llvm::Function *CGObjCGNU::MethodPreamble(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000881 const std::string &ClassName,
882 const std::string &CategoryName,
883 const std::string &MethodName,
Chris Lattner391d77a2008-03-30 23:03:07 +0000884 const llvm::Type *ReturnTy,
885 const llvm::Type *SelfTy,
886 const llvm::Type **ArgTy,
887 unsigned ArgC,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000888 bool isClassMethod,
Chris Lattner391d77a2008-03-30 23:03:07 +0000889 bool isVarArg) {
890 std::vector<const llvm::Type*> Args;
Chris Lattner8fdf3282008-06-24 17:04:18 +0000891 if (!ReturnTy->isSingleValueType() && ReturnTy != llvm::Type::VoidTy) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000892 Args.push_back(llvm::PointerType::getUnqual(ReturnTy));
893 ReturnTy = llvm::Type::VoidTy;
894 }
Chris Lattner391d77a2008-03-30 23:03:07 +0000895 Args.push_back(SelfTy);
896 Args.push_back(SelectorTy);
897 Args.insert(Args.end(), ArgTy, ArgTy+ArgC);
898
899 llvm::FunctionType *MethodTy = llvm::FunctionType::get(ReturnTy,
900 Args,
901 isVarArg);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000902 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
903 MethodName, isClassMethod);
904
Gabor Greif984d0b42008-04-06 20:42:52 +0000905 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000906 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000907 FunctionName,
Chris Lattner391d77a2008-03-30 23:03:07 +0000908 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +0000909 llvm::Function::arg_iterator AI = Method->arg_begin();
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000910 // Name the struct return argument.
911 // FIXME: This is probably the wrong test.
912 if (!ReturnTy->isFirstClassType() && ReturnTy != llvm::Type::VoidTy) {
913 AI->setName("agg.result");
914 ++AI;
915 }
Chris Lattner391d77a2008-03-30 23:03:07 +0000916 AI->setName("self");
917 ++AI;
918 AI->setName("_cmd");
919 return Method;
920}
921
Chris Lattnerdce14062008-06-26 04:19:03 +0000922CodeGen::CGObjCRuntime *CodeGen::CreateObjCRuntime(CodeGen::CodeGenModule &CGM){
923 return new CGObjCGNU(CGM);
Chris Lattner0f984262008-03-01 08:50:34 +0000924}