blob: 8eb42636f33ceeb4c8741e9bea35d88695b31c90 [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
Chris Lattner550b8db2008-06-26 04:05:20 +000025// FIXME: Remove THIS!
26#include "llvm/Analysis/ValueTracking.h"
27std::string getStringValue(llvm::Constant *C) {
28 std::string R;
29 bool V = GetConstantStringInfo(C, R);
30 assert(V && "Couldn't convert string");
31 return R;
32}
33
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000034using llvm::dyn_cast;
35
36// The version of the runtime that this class targets. Must match the version
37// in the runtime.
38static const int RuntimeVersion = 8;
39static const int ProtocolVersion = 2;
Chris Lattner0f984262008-03-01 08:50:34 +000040
Chris Lattner0f984262008-03-01 08:50:34 +000041namespace {
Chris Lattner391d77a2008-03-30 23:03:07 +000042class CGObjCGNU : public clang::CodeGen::CGObjCRuntime {
Chris Lattner0f984262008-03-01 08:50:34 +000043private:
44 llvm::Module &TheModule;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000045 const llvm::StructType *SelStructTy;
Chris Lattner391d77a2008-03-30 23:03:07 +000046 const llvm::Type *SelectorTy;
47 const llvm::Type *PtrToInt8Ty;
48 const llvm::Type *IMPTy;
49 const llvm::Type *IdTy;
50 const llvm::Type *IntTy;
51 const llvm::Type *PtrTy;
52 const llvm::Type *LongTy;
53 const llvm::Type *PtrToIntTy;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +000054 std::vector<llvm::Constant*> Classes;
55 std::vector<llvm::Constant*> Categories;
56 std::vector<llvm::Constant*> ConstantStrings;
57 llvm::Function *LoadFunction;
58 llvm::StringMap<llvm::Constant*> ExistingProtocols;
59 typedef std::pair<std::string, std::string> TypedSelector;
60 std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors;
61 llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors;
62 // Some zeros used for GEPs in lots of places.
63 llvm::Constant *Zeros[2];
64 llvm::Constant *NULLPtr;
65private:
66 llvm::Constant *GenerateIvarList(
67 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
68 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
69 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets);
70 llvm::Constant *GenerateMethodList(const std::string &ClassName,
71 const std::string &CategoryName,
72 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
73 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
74 bool isClassMethodList);
75 llvm::Constant *GenerateProtocolList(
76 const llvm::SmallVectorImpl<std::string> &Protocols);
77 llvm::Constant *GenerateClassStructure(
78 llvm::Constant *MetaClass,
79 llvm::Constant *SuperClass,
80 unsigned info,
81 llvm::Constant *Name,
82 llvm::Constant *Version,
83 llvm::Constant *InstanceSize,
84 llvm::Constant *IVars,
85 llvm::Constant *Methods,
86 llvm::Constant *Protocols);
87 llvm::Constant *GenerateProtocolMethodList(
88 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
89 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes);
90 llvm::Constant *MakeConstantString(const std::string &Str, const std::string
91 &Name="");
92 llvm::Constant *MakeGlobal(const llvm::StructType *Ty,
93 std::vector<llvm::Constant*> &V, const std::string &Name="");
94 llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty,
95 std::vector<llvm::Constant*> &V, const std::string &Name="");
Chris Lattner0f984262008-03-01 08:50:34 +000096public:
Chris Lattner391d77a2008-03-30 23:03:07 +000097 CGObjCGNU(llvm::Module &Mp,
98 const llvm::Type *LLVMIntType,
99 const llvm::Type *LLVMLongType);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000100 virtual llvm::Constant *GenerateConstantString(const char *String,
101 const size_t length);
102 virtual llvm::Value *GenerateMessageSend(llvm::IRBuilder &Builder,
Chris Lattner0f984262008-03-01 08:50:34 +0000103 const llvm::Type *ReturnTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000104 llvm::Value *Sender,
Chris Lattner0f984262008-03-01 08:50:34 +0000105 llvm::Value *Receiver,
Chris Lattner391d77a2008-03-30 23:03:07 +0000106 llvm::Value *Selector,
Chris Lattner0f984262008-03-01 08:50:34 +0000107 llvm::Value** ArgV,
108 unsigned ArgC);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000109 virtual llvm::Value *GenerateMessageSendSuper(llvm::IRBuilder &Builder,
110 const llvm::Type *ReturnTy,
111 llvm::Value *Sender,
112 const char *SuperClassName,
113 llvm::Value *Receiver,
114 llvm::Value *Selector,
115 llvm::Value** ArgV,
116 unsigned ArgC);
117 virtual llvm::Value *LookupClass(llvm::IRBuilder &Builder, llvm::Value
118 *ClassName);
119 virtual llvm::Value *GetSelector(llvm::IRBuilder &Builder,
Chris Lattner391d77a2008-03-30 23:03:07 +0000120 llvm::Value *SelName,
121 llvm::Value *SelTypes);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000122 virtual llvm::Function *MethodPreamble(
123 const std::string &ClassName,
124 const std::string &CategoryName,
125 const std::string &MethodName,
126 const llvm::Type *ReturnTy,
127 const llvm::Type *SelfTy,
128 const llvm::Type **ArgTy,
129 unsigned ArgC,
130 bool isClassMethod,
131 bool isVarArg);
132 virtual void GenerateCategory(const char *ClassName, const char *CategoryName,
133 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
134 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
135 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
136 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
137 const llvm::SmallVectorImpl<std::string> &Protocols);
138 virtual void GenerateClass(
139 const char *ClassName,
140 const char *SuperClassName,
141 const int instanceSize,
142 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
143 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
144 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets,
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 const llvm::SmallVectorImpl<std::string> &Protocols);
150 virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder &Builder, const char
151 *ProtocolName);
152 virtual void GenerateProtocol(const char *ProtocolName,
153 const llvm::SmallVectorImpl<std::string> &Protocols,
154 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
155 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
156 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
157 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes);
158 virtual llvm::Function *ModuleInitFunction();
Chris Lattner0f984262008-03-01 08:50:34 +0000159};
160} // end anonymous namespace
161
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000162
163
164static std::string SymbolNameForClass(const std::string &ClassName) {
165 return ".objc_class_" + ClassName;
166}
167
168static std::string SymbolNameForMethod(const std::string &ClassName, const
169 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
170{
171 return "._objc_method_" + ClassName +"("+CategoryName+")"+
172 (isClassMethod ? "+" : "-") + MethodName;
173}
174
Chris Lattner391d77a2008-03-30 23:03:07 +0000175CGObjCGNU::CGObjCGNU(llvm::Module &M,
176 const llvm::Type *LLVMIntType,
177 const llvm::Type *LLVMLongType) :
178 TheModule(M),
179 IntTy(LLVMIntType),
180 LongTy(LLVMLongType)
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 Lattner391d77a2008-03-30 23:03:07 +0000225 llvm::Value *SelName,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000226 llvm::Value *SelTypes) {
227 // 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 Lattner550b8db2008-06-26 04:05:20 +0000233 if (UntypedSelectors[getStringValue(CName)]) {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000234 // FIXME: Volatility
Chris Lattner550b8db2008-06-26 04:05:20 +0000235 return Builder.CreateLoad(UntypedSelectors[getStringValue(CName)]);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000236 }
237 // If it isn't, cache it.
238 llvm::GlobalAlias *Sel = new llvm::GlobalAlias(
239 llvm::PointerType::getUnqual(SelectorTy),
240 llvm::GlobalValue::InternalLinkage, ".objc_untyped_selector_alias",
241 NULL, &TheModule);
Chris Lattner550b8db2008-06-26 04:05:20 +0000242 UntypedSelectors[getStringValue(CName)] = Sel;
Eli Friedman1e692ac2008-06-13 23:01:12 +0000243 // FIXME: Volatility
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000244 return Builder.CreateLoad(Sel);
245 }
246 // Typed selectors
247 if (llvm::Constant *CTypes = dyn_cast<llvm::Constant>(SelTypes)) {
Chris Lattner550b8db2008-06-26 04:05:20 +0000248 TypedSelector Selector = TypedSelector(getStringValue(CName),
249 getStringValue(CTypes));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000250 // If it's already cached, return it.
251 if (TypedSelectors[Selector]) {
Eli Friedman1e692ac2008-06-13 23:01:12 +0000252 // FIXME: Volatility
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000253 return Builder.CreateLoad(TypedSelectors[Selector]);
254 }
255 // If it isn't, cache it.
256 llvm::GlobalAlias *Sel = new llvm::GlobalAlias(
257 llvm::PointerType::getUnqual(SelectorTy),
258 llvm::GlobalValue::InternalLinkage, ".objc_typed_selector_alias",
259 NULL, &TheModule);
260 TypedSelectors[Selector] = Sel;
Eli Friedman1e692ac2008-06-13 23:01:12 +0000261 // FIXME: Volatility
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000262 return Builder.CreateLoad(Sel);
263 }
264 }
265 // Dynamically look up selectors from non-constant sources
Chris Lattner391d77a2008-03-30 23:03:07 +0000266 llvm::Value *cmd;
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000267 if (SelTypes == 0) {
Chris Lattner391d77a2008-03-30 23:03:07 +0000268 llvm::Constant *SelFunction = TheModule.getOrInsertFunction("sel_get_uid",
269 SelectorTy,
270 PtrToInt8Ty,
271 NULL);
272 cmd = Builder.CreateCall(SelFunction, SelName);
273 }
274 else {
275 llvm::Constant *SelFunction =
276 TheModule.getOrInsertFunction("sel_get_typed_uid",
277 SelectorTy,
278 PtrToInt8Ty,
279 PtrToInt8Ty,
280 NULL);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000281 cmd = Builder.CreateCall2(SelFunction, SelName, SelTypes);
Chris Lattner391d77a2008-03-30 23:03:07 +0000282 }
283 return cmd;
284}
285
286
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000287llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str, const
288 std::string &Name) {
289 llvm::Constant * ConstStr = llvm::ConstantArray::get(Str);
290 ConstStr = new llvm::GlobalVariable(ConstStr->getType(), true,
291 llvm::GlobalValue::InternalLinkage,
292 ConstStr, Name, &TheModule);
293 return llvm::ConstantExpr::getGetElementPtr(ConstStr, Zeros, 2);
294}
295llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
296 std::vector<llvm::Constant*> &V, const std::string &Name) {
297 llvm::Constant *C = llvm::ConstantStruct::get(Ty, V);
298 return new llvm::GlobalVariable(Ty, false,
299 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
300}
301llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
302 std::vector<llvm::Constant*> &V, const std::string &Name) {
303 llvm::Constant *C = llvm::ConstantArray::get(Ty, V);
304 return new llvm::GlobalVariable(Ty, false,
305 llvm::GlobalValue::InternalLinkage, C, Name, &TheModule);
306}
307
308/// Generate an NSConstantString object.
309//TODO: In case there are any crazy people still using the GNU runtime without
310//an OpenStep implementation, this should let them select their own class for
311//constant strings.
312llvm::Constant *CGObjCGNU::GenerateConstantString(const char *String, const
313 size_t length) {
Chris Lattner13fd7e52008-06-21 21:44:18 +0000314 std::string Str(String, String +length);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000315 std::vector<llvm::Constant*> Ivars;
316 Ivars.push_back(NULLPtr);
Chris Lattner13fd7e52008-06-21 21:44:18 +0000317 Ivars.push_back(MakeConstantString(Str));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000318 Ivars.push_back(llvm::ConstantInt::get(IntTy, length));
319 llvm::Constant *ObjCStr = MakeGlobal(
320 llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
321 Ivars, ".objc_str");
322 ConstantStrings.push_back(
323 llvm::ConstantExpr::getBitCast(ObjCStr, PtrToInt8Ty));
324 return ObjCStr;
325}
326
327///Generates a message send where the super is the receiver. This is a message
328///send to self with special delivery semantics indicating which class's method
329///should be called.
330llvm::Value *CGObjCGNU::GenerateMessageSendSuper(llvm::IRBuilder &Builder,
Chris Lattner0f984262008-03-01 08:50:34 +0000331 const llvm::Type *ReturnTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000332 llvm::Value *Sender,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000333 const char *SuperClassName,
Chris Lattner0f984262008-03-01 08:50:34 +0000334 llvm::Value *Receiver,
Chris Lattner391d77a2008-03-30 23:03:07 +0000335 llvm::Value *Selector,
Chris Lattner0f984262008-03-01 08:50:34 +0000336 llvm::Value** ArgV,
337 unsigned ArgC) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000338 // TODO: This should be cached, not looked up every time.
339 llvm::Value *ReceiverClass = LookupClass(Builder,
340 MakeConstantString(SuperClassName));
341 llvm::Value *cmd = GetSelector(Builder, Selector, 0);
Chris Lattner0f984262008-03-01 08:50:34 +0000342 std::vector<const llvm::Type*> impArgTypes;
343 impArgTypes.push_back(Receiver->getType());
Chris Lattner391d77a2008-03-30 23:03:07 +0000344 impArgTypes.push_back(SelectorTy);
Chris Lattner0f984262008-03-01 08:50:34 +0000345
346 // Avoid an explicit cast on the IMP by getting a version that has the right
347 // return type.
348 llvm::FunctionType *impType = llvm::FunctionType::get(ReturnTy, impArgTypes,
349 true);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000350 // Construct the structure used to look up the IMP
351 llvm::StructType *ObjCSuperTy = llvm::StructType::get(Receiver->getType(),
352 IdTy, NULL);
353 llvm::Value *ObjCSuper = Builder.CreateAlloca(ObjCSuperTy);
Eli Friedman1e692ac2008-06-13 23:01:12 +0000354 // FIXME: volatility
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000355 Builder.CreateStore(Receiver, Builder.CreateStructGEP(ObjCSuper, 0));
356 Builder.CreateStore(ReceiverClass, Builder.CreateStructGEP(ObjCSuper, 1));
357
358 // Get the IMP
359 llvm::Constant *lookupFunction =
360 TheModule.getOrInsertFunction("objc_msg_lookup_super",
361 llvm::PointerType::getUnqual(impType),
362 llvm::PointerType::getUnqual(ObjCSuperTy),
363 SelectorTy, NULL);
364 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
365 llvm::Value *imp = Builder.CreateCall(lookupFunction, lookupArgs,
366 lookupArgs+2);
367
368 // Call the method
369 llvm::SmallVector<llvm::Value*, 8> callArgs;
370 callArgs.push_back(Receiver);
371 callArgs.push_back(cmd);
372 callArgs.insert(callArgs.end(), ArgV, ArgV+ArgC);
373 return Builder.CreateCall(imp, callArgs.begin(), callArgs.end());
374}
375
376/// Generate code for a message send expression.
377llvm::Value *CGObjCGNU::GenerateMessageSend(llvm::IRBuilder &Builder,
378 const llvm::Type *ReturnTy,
379 llvm::Value *Sender,
380 llvm::Value *Receiver,
381 llvm::Value *Selector,
382 llvm::Value** ArgV,
383 unsigned ArgC) {
384 llvm::Value *cmd = GetSelector(Builder, Selector, 0);
385
386 // Look up the method implementation.
387 std::vector<const llvm::Type*> impArgTypes;
388 const llvm::Type *RetTy;
389 //TODO: Revisit this when LLVM supports aggregate return types.
390 if (ReturnTy->isSingleValueType() && ReturnTy != llvm::Type::VoidTy) {
391 RetTy = ReturnTy;
392 } else {
393 // For struct returns allocate the space in the caller and pass it up to
394 // the sender.
395 RetTy = llvm::Type::VoidTy;
396 impArgTypes.push_back(llvm::PointerType::getUnqual(ReturnTy));
397 }
398 impArgTypes.push_back(Receiver->getType());
399 impArgTypes.push_back(SelectorTy);
400
401 // Avoid an explicit cast on the IMP by getting a version that has the right
402 // return type.
403 llvm::FunctionType *impType = llvm::FunctionType::get(RetTy, impArgTypes,
404 true);
Chris Lattner0f984262008-03-01 08:50:34 +0000405
406 llvm::Constant *lookupFunction =
407 TheModule.getOrInsertFunction("objc_msg_lookup",
Chris Lattner391d77a2008-03-30 23:03:07 +0000408 llvm::PointerType::getUnqual(impType),
409 Receiver->getType(), SelectorTy, NULL);
Chris Lattner3eae03e2008-05-06 00:56:42 +0000410 llvm::Value *imp = Builder.CreateCall2(lookupFunction, Receiver, cmd);
411
412 // Call the method.
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000413 llvm::SmallVector<llvm::Value*, 16> Args;
414 if (!ReturnTy->isSingleValueType()) {
415 llvm::Value *Return = Builder.CreateAlloca(ReturnTy);
416 Args.push_back(Return);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000417 }
418 Args.push_back(Receiver);
419 Args.push_back(cmd);
420 Args.insert(Args.end(), ArgV, ArgV+ArgC);
Chris Lattner8fdf3282008-06-24 17:04:18 +0000421 if (!ReturnTy->isSingleValueType()) {
422 Builder.CreateCall(imp, Args.begin(), Args.end());
423 return Args[0];
424 }
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000425 return Builder.CreateCall(imp, Args.begin(), Args.end());
Chris Lattner0f984262008-03-01 08:50:34 +0000426}
427
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000428/// Generates a MethodList. Used in construction of a objc_class and
429/// objc_category structures.
430llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
431 const std::string &CategoryName,
432 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
433 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
434 bool isClassMethodList) {
435 // Get the method structure type.
436 llvm::StructType *ObjCMethodTy = llvm::StructType::get(
437 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
438 PtrToInt8Ty, // Method types
439 llvm::PointerType::getUnqual(IMPTy), //Method pointer
440 NULL);
441 std::vector<llvm::Constant*> Methods;
442 std::vector<llvm::Constant*> Elements;
443 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
444 Elements.clear();
445 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
446 Zeros, 2));
447 Elements.push_back(
448 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
449 llvm::Constant *Method =
450 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattner550b8db2008-06-26 04:05:20 +0000451 getStringValue(MethodNames[i]),
452 isClassMethodList));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000453 Method = llvm::ConstantExpr::getBitCast(Method,
454 llvm::PointerType::getUnqual(IMPTy));
455 Elements.push_back(Method);
456 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodTy, Elements));
457 }
458
459 // Array of method structures
460 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodTy,
461 MethodNames.size());
462 llvm::Constant *MethodArray = llvm::ConstantArray::get(ObjCMethodArrayTy,
463 Methods);
464
465 // Structure containing list pointer, array and array count
466 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
467 llvm::PATypeHolder OpaqueNextTy = llvm::OpaqueType::get();
468 llvm::Type *NextPtrTy = llvm::PointerType::getUnqual(OpaqueNextTy);
469 llvm::StructType *ObjCMethodListTy = llvm::StructType::get(NextPtrTy,
470 IntTy,
471 ObjCMethodArrayTy,
472 NULL);
473 // Refine next pointer type to concrete type
474 llvm::cast<llvm::OpaqueType>(
475 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
476 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
477
478 Methods.clear();
479 Methods.push_back(llvm::ConstantPointerNull::get(
480 llvm::PointerType::getUnqual(ObjCMethodListTy)));
481 Methods.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
482 MethodTypes.size()));
483 Methods.push_back(MethodArray);
484
485 // Create an instance of the structure
486 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
487}
488
489/// Generates an IvarList. Used in construction of a objc_class.
490llvm::Constant *CGObjCGNU::GenerateIvarList(
491 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
492 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
493 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
494 // Get the method structure type.
495 llvm::StructType *ObjCIvarTy = llvm::StructType::get(
496 PtrToInt8Ty,
497 PtrToInt8Ty,
498 IntTy,
499 NULL);
500 std::vector<llvm::Constant*> Ivars;
501 std::vector<llvm::Constant*> Elements;
502 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
503 Elements.clear();
504 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarNames[i],
505 Zeros, 2));
506 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(IvarTypes[i],
507 Zeros, 2));
508 Elements.push_back(IvarOffsets[i]);
509 Ivars.push_back(llvm::ConstantStruct::get(ObjCIvarTy, Elements));
510 }
511
512 // Array of method structures
513 llvm::ArrayType *ObjCIvarArrayTy = llvm::ArrayType::get(ObjCIvarTy,
514 IvarNames.size());
515
516
517 Elements.clear();
518 Elements.push_back(llvm::ConstantInt::get(
519 llvm::cast<llvm::IntegerType>(IntTy), (int)IvarNames.size()));
520 Elements.push_back(llvm::ConstantArray::get(ObjCIvarArrayTy, Ivars));
521 // Structure containing array and array count
522 llvm::StructType *ObjCIvarListTy = llvm::StructType::get(IntTy,
523 ObjCIvarArrayTy,
524 NULL);
525
526 // Create an instance of the structure
527 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
528}
529
530/// Generate a class structure
531llvm::Constant *CGObjCGNU::GenerateClassStructure(
532 llvm::Constant *MetaClass,
533 llvm::Constant *SuperClass,
534 unsigned info,
535 llvm::Constant *Name,
536 llvm::Constant *Version,
537 llvm::Constant *InstanceSize,
538 llvm::Constant *IVars,
539 llvm::Constant *Methods,
540 llvm::Constant *Protocols) {
541 // Set up the class structure
542 // Note: Several of these are char*s when they should be ids. This is
543 // because the runtime performs this translation on load.
544 llvm::StructType *ClassTy = llvm::StructType::get(
545 PtrToInt8Ty, // class_pointer
546 PtrToInt8Ty, // super_class
547 PtrToInt8Ty, // name
548 LongTy, // version
549 LongTy, // info
550 LongTy, // instance_size
551 IVars->getType(), // ivars
552 Methods->getType(), // methods
553 // These are all filled in by the runtime, so we pretend
554 PtrTy, // dtable
555 PtrTy, // subclass_list
556 PtrTy, // sibling_class
557 PtrTy, // protocols
558 PtrTy, // gc_object_type
559 NULL);
560 llvm::Constant *Zero = llvm::ConstantInt::get(LongTy, 0);
561 llvm::Constant *NullP =
562 llvm::ConstantPointerNull::get(llvm::cast<llvm::PointerType>(PtrTy));
563 // Fill in the structure
564 std::vector<llvm::Constant*> Elements;
565 Elements.push_back(llvm::ConstantExpr::getBitCast(MetaClass, PtrToInt8Ty));
566 Elements.push_back(SuperClass);
567 Elements.push_back(Name);
568 Elements.push_back(Zero);
569 Elements.push_back(llvm::ConstantInt::get(LongTy, info));
570 Elements.push_back(InstanceSize);
571 Elements.push_back(IVars);
572 Elements.push_back(Methods);
573 Elements.push_back(NullP);
574 Elements.push_back(NullP);
575 Elements.push_back(NullP);
576 Elements.push_back(llvm::ConstantExpr::getBitCast(Protocols, PtrTy));
577 Elements.push_back(NullP);
578 // Create an instance of the structure
579 return MakeGlobal(ClassTy, Elements,
Chris Lattner550b8db2008-06-26 04:05:20 +0000580 SymbolNameForClass(getStringValue(Name)));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000581}
582
583llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
584 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
585 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
586 // Get the method structure type.
587 llvm::StructType *ObjCMethodDescTy = llvm::StructType::get(
588 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
589 PtrToInt8Ty,
590 NULL);
591 std::vector<llvm::Constant*> Methods;
592 std::vector<llvm::Constant*> Elements;
593 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
594 Elements.clear();
595 Elements.push_back( llvm::ConstantExpr::getGetElementPtr(MethodNames[i],
596 Zeros, 2));
597 Elements.push_back(
598 llvm::ConstantExpr::getGetElementPtr(MethodTypes[i], Zeros, 2));
599 Methods.push_back(llvm::ConstantStruct::get(ObjCMethodDescTy, Elements));
600 }
601 llvm::ArrayType *ObjCMethodArrayTy = llvm::ArrayType::get(ObjCMethodDescTy,
602 MethodNames.size());
603 llvm::Constant *Array = llvm::ConstantArray::get(ObjCMethodArrayTy, Methods);
604 llvm::StructType *ObjCMethodDescListTy = llvm::StructType::get(
605 IntTy, ObjCMethodArrayTy, NULL);
606 Methods.clear();
607 Methods.push_back(llvm::ConstantInt::get(IntTy, MethodNames.size()));
608 Methods.push_back(Array);
609 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
610}
611// Create the protocol list structure used in classes, categories and so on
612llvm::Constant *CGObjCGNU::GenerateProtocolList(
613 const llvm::SmallVectorImpl<std::string> &Protocols) {
614 llvm::ArrayType *ProtocolArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
615 Protocols.size());
616 llvm::StructType *ProtocolListTy = llvm::StructType::get(
617 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
618 LongTy,//FIXME: Should be size_t
619 ProtocolArrayTy,
620 NULL);
621 std::vector<llvm::Constant*> Elements;
622 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
623 iter != endIter ; iter++) {
624 llvm::Constant *Ptr =
625 llvm::ConstantExpr::getBitCast(ExistingProtocols[*iter], PtrToInt8Ty);
626 Elements.push_back(Ptr);
627 }
628 llvm::Constant * ProtocolArray = llvm::ConstantArray::get(ProtocolArrayTy,
629 Elements);
630 Elements.clear();
631 Elements.push_back(NULLPtr);
632 Elements.push_back(llvm::ConstantInt::get(
633 llvm::cast<llvm::IntegerType>(LongTy), Protocols.size()));
634 Elements.push_back(ProtocolArray);
635 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
636}
637
638llvm::Value *CGObjCGNU::GenerateProtocolRef(llvm::IRBuilder &Builder, const
639 char *ProtocolName) {
640 return ExistingProtocols[ProtocolName];
641}
642
643void CGObjCGNU::GenerateProtocol(const char *ProtocolName,
644 const llvm::SmallVectorImpl<std::string> &Protocols,
645 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
646 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
647 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
648 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes) {
649
650 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
651 llvm::Constant *InstanceMethodList =
652 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
653 llvm::Constant *ClassMethodList =
654 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
655 // Protocols are objects containing lists of the methods implemented and
656 // protocols adopted.
657 llvm::StructType *ProtocolTy = llvm::StructType::get(IdTy,
658 PtrToInt8Ty,
659 ProtocolList->getType(),
660 InstanceMethodList->getType(),
661 ClassMethodList->getType(),
662 NULL);
663 std::vector<llvm::Constant*> Elements;
664 // The isa pointer must be set to a magic number so the runtime knows it's
665 // the correct layout.
666 Elements.push_back(llvm::ConstantExpr::getIntToPtr(
667 llvm::ConstantInt::get(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
668 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
669 Elements.push_back(ProtocolList);
670 Elements.push_back(InstanceMethodList);
671 Elements.push_back(ClassMethodList);
672 ExistingProtocols[ProtocolName] =
673 llvm::ConstantExpr::getBitCast(MakeGlobal(ProtocolTy, Elements,
674 ".objc_protocol"), IdTy);
675}
676
677void CGObjCGNU::GenerateCategory(
678 const char *ClassName,
679 const char *CategoryName,
680 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
681 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
682 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
683 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
684 const llvm::SmallVectorImpl<std::string> &Protocols) {
685 std::vector<llvm::Constant*> Elements;
686 Elements.push_back(MakeConstantString(CategoryName));
687 Elements.push_back(MakeConstantString(ClassName));
688 // Instance method list
689 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
690 ClassName, CategoryName, InstanceMethodNames, InstanceMethodTypes,
691 false), PtrTy));
692 // Class method list
693 Elements.push_back(llvm::ConstantExpr::getBitCast(GenerateMethodList(
694 ClassName, CategoryName, ClassMethodNames, ClassMethodTypes, true),
695 PtrTy));
696 // Protocol list
697 Elements.push_back(llvm::ConstantExpr::getBitCast(
698 GenerateProtocolList(Protocols), PtrTy));
699 Categories.push_back(llvm::ConstantExpr::getBitCast(
700 MakeGlobal(llvm::StructType::get(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
701 PtrTy, PtrTy, NULL), Elements), PtrTy));
702}
703void CGObjCGNU::GenerateClass(
704 const char *ClassName,
705 const char *SuperClassName,
706 const int instanceSize,
707 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
708 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
709 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets,
710 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
711 const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
712 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
713 const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
714 const llvm::SmallVectorImpl<std::string> &Protocols) {
715 // Get the superclass pointer.
716 llvm::Constant *SuperClass;
717 if (SuperClassName) {
718 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
719 } else {
720 SuperClass = llvm::ConstantPointerNull::get(
721 llvm::cast<llvm::PointerType>(PtrToInt8Ty));
722 }
723 llvm::Constant * Name = MakeConstantString(ClassName, ".class_name");
724 // Empty vector used to construct empty method lists
725 llvm::SmallVector<llvm::Constant*, 1> empty;
726 // Generate the method and instance variable lists
727 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
728 InstanceMethodNames, InstanceMethodTypes, false);
729 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
730 ClassMethodNames, ClassMethodTypes, true);
731 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
732 IvarOffsets);
733 //Generate metaclass for class methods
734 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
735 NULLPtr, 0x2L, NULLPtr, 0, Zeros[0], GenerateIvarList(
736 empty, empty, empty), ClassMethodList, NULLPtr);
737 // Generate the class structure
738 llvm::Constant *ClassStruct = GenerateClassStructure(MetaClassStruct,
739 SuperClass, 0x1L, Name, 0,
740 llvm::ConstantInt::get(llvm::Type::Int32Ty, instanceSize), IvarList,
741 MethodList, GenerateProtocolList(Protocols));
742 // Add class structure to list to be added to the symtab later
743 ClassStruct = llvm::ConstantExpr::getBitCast(ClassStruct, PtrToInt8Ty);
744 Classes.push_back(ClassStruct);
745}
746
747llvm::Function *CGObjCGNU::ModuleInitFunction() {
748 // Only emit an ObjC load function if no Objective-C stuff has been called
749 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
750 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikov5f58b912008-06-01 15:14:46 +0000751 UntypedSelectors.empty())
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000752 return NULL;
Eli Friedman1b8956e2008-06-01 16:00:02 +0000753
754 // Name the ObjC types to make the IR a bit easier to read
755 TheModule.addTypeName(".objc_selector", SelectorTy);
756 TheModule.addTypeName(".objc_id", IdTy);
757 TheModule.addTypeName(".objc_imp", IMPTy);
758
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000759 std::vector<llvm::Constant*> Elements;
760 // Generate statics list:
761 llvm::ArrayType *StaticsArrayTy = llvm::ArrayType::get(PtrToInt8Ty,
762 ConstantStrings.size() + 1);
763 ConstantStrings.push_back(NULLPtr);
764 Elements.push_back(MakeConstantString("NSConstantString",
765 ".objc_static_class_name"));
766 Elements.push_back(llvm::ConstantArray::get(StaticsArrayTy, ConstantStrings));
767 llvm::StructType *StaticsListTy =
768 llvm::StructType::get(PtrToInt8Ty, StaticsArrayTy, NULL);
Chris Lattner630404b2008-06-26 04:10:42 +0000769 llvm::Type *StaticsListPtrTy = llvm::PointerType::getUnqual(StaticsListTy);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000770 llvm::Constant *Statics =
771 MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Chris Lattner4e0b2642008-06-24 17:01:28 +0000772 llvm::ArrayType *StaticsListArrayTy =
Chris Lattner630404b2008-06-26 04:10:42 +0000773 llvm::ArrayType::get(StaticsListPtrTy, 2);
Chris Lattner4e0b2642008-06-24 17:01:28 +0000774 Elements.clear();
775 Elements.push_back(Statics);
Chris Lattner630404b2008-06-26 04:10:42 +0000776 Elements.push_back(llvm::Constant::getNullValue(StaticsListPtrTy));
Chris Lattner4e0b2642008-06-24 17:01:28 +0000777 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000778 Statics = llvm::ConstantExpr::getBitCast(Statics, PtrTy);
779 // Array of classes, categories, and constant objects
780 llvm::ArrayType *ClassListTy = llvm::ArrayType::get(PtrToInt8Ty,
781 Classes.size() + Categories.size() + 2);
Chris Lattner630404b2008-06-26 04:10:42 +0000782 llvm::StructType *SymTabTy = llvm::StructType::get(LongTy, SelectorTy,
783 llvm::Type::Int16Ty,
784 llvm::Type::Int16Ty,
785 ClassListTy, NULL);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000786
787 Elements.clear();
788 // Pointer to an array of selectors used in this module.
789 std::vector<llvm::Constant*> Selectors;
790 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
791 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
792 iter != iterEnd ; ++iter) {
Chris Lattner630404b2008-06-26 04:10:42 +0000793 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
794 Elements.push_back(MakeConstantString(iter->first.second,
795 ".objc_sel_types"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000796 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
797 Elements.clear();
798 }
799 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
800 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattner630404b2008-06-26 04:10:42 +0000801 iter != iterEnd; ++iter) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000802 Elements.push_back(
Chris Lattner630404b2008-06-26 04:10:42 +0000803 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000804 Elements.push_back(NULLPtr);
805 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
806 Elements.clear();
807 }
808 Elements.push_back(NULLPtr);
809 Elements.push_back(NULLPtr);
810 Selectors.push_back(llvm::ConstantStruct::get(SelStructTy, Elements));
811 Elements.clear();
812 // Number of static selectors
813 Elements.push_back(llvm::ConstantInt::get(LongTy, Selectors.size() ));
814 llvm::Constant *SelectorList = MakeGlobal(
815 llvm::ArrayType::get(SelStructTy, Selectors.size()), Selectors,
816 ".objc_selector_list");
817 Elements.push_back(llvm::ConstantExpr::getBitCast(SelectorList, SelectorTy));
818
819 // Now that all of the static selectors exist, create pointers to them.
820 int index = 0;
821 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
822 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
823 iter != iterEnd; ++iter) {
824 llvm::Constant *Idxs[] = {Zeros[0],
825 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
826 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
827 llvm::GlobalValue::InternalLinkage,
828 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
829 ".objc_sel_ptr", &TheModule);
830 (*iter).second->setAliasee(SelPtr);
831 }
832 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
833 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
834 iter != iterEnd; iter++) {
835 llvm::Constant *Idxs[] = {Zeros[0],
836 llvm::ConstantInt::get(llvm::Type::Int32Ty, index++), Zeros[0]};
837 llvm::GlobalVariable *SelPtr = new llvm::GlobalVariable(SelectorTy, true,
838 llvm::GlobalValue::InternalLinkage,
839 llvm::ConstantExpr::getGetElementPtr(SelectorList, Idxs, 2),
840 ".objc_sel_ptr", &TheModule);
841 (*iter).second->setAliasee(SelPtr);
842 }
843 // Number of classes defined.
844 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
845 Classes.size()));
846 // Number of categories defined
847 Elements.push_back(llvm::ConstantInt::get(llvm::Type::Int16Ty,
848 Categories.size()));
849 // Create an array of classes, then categories, then static object instances
850 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
851 // NULL-terminated list of static object instances (mainly constant strings)
852 Classes.push_back(Statics);
853 Classes.push_back(NULLPtr);
854 llvm::Constant *ClassList = llvm::ConstantArray::get(ClassListTy, Classes);
855 Elements.push_back(ClassList);
856 // Construct the symbol table
857 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
858
859 // The symbol table is contained in a module which has some version-checking
860 // constants
861 llvm::StructType * ModuleTy = llvm::StructType::get(LongTy, LongTy,
862 PtrToInt8Ty, llvm::PointerType::getUnqual(SymTabTy), NULL);
863 Elements.clear();
864 // Runtime version used for compatibility checking.
865 Elements.push_back(llvm::ConstantInt::get(LongTy, RuntimeVersion));
866 //FIXME: Should be sizeof(ModuleTy)
867 Elements.push_back(llvm::ConstantInt::get(LongTy, 16));
868 //FIXME: Should be the path to the file where this module was declared
869 Elements.push_back(NULLPtr);
870 Elements.push_back(SymTab);
871 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
872
873 // Create the load function calling the runtime entry point with the module
874 // structure
875 std::vector<const llvm::Type*> VoidArgs;
876 llvm::Function * LoadFunction = llvm::Function::Create(
877 llvm::FunctionType::get(llvm::Type::VoidTy, VoidArgs, false),
878 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
879 &TheModule);
880 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
881 llvm::IRBuilder Builder;
882 Builder.SetInsertPoint(EntryBB);
883 llvm::Value *Register = TheModule.getOrInsertFunction("__objc_exec_class",
884 llvm::Type::VoidTy, llvm::PointerType::getUnqual(ModuleTy), NULL);
885 Builder.CreateCall(Register, Module);
886 Builder.CreateRetVoid();
887 return LoadFunction;
888}
Chris Lattner391d77a2008-03-30 23:03:07 +0000889llvm::Function *CGObjCGNU::MethodPreamble(
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000890 const std::string &ClassName,
891 const std::string &CategoryName,
892 const std::string &MethodName,
Chris Lattner391d77a2008-03-30 23:03:07 +0000893 const llvm::Type *ReturnTy,
894 const llvm::Type *SelfTy,
895 const llvm::Type **ArgTy,
896 unsigned ArgC,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000897 bool isClassMethod,
Chris Lattner391d77a2008-03-30 23:03:07 +0000898 bool isVarArg) {
899 std::vector<const llvm::Type*> Args;
Chris Lattner8fdf3282008-06-24 17:04:18 +0000900 if (!ReturnTy->isSingleValueType() && ReturnTy != llvm::Type::VoidTy) {
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000901 Args.push_back(llvm::PointerType::getUnqual(ReturnTy));
902 ReturnTy = llvm::Type::VoidTy;
903 }
Chris Lattner391d77a2008-03-30 23:03:07 +0000904 Args.push_back(SelfTy);
905 Args.push_back(SelectorTy);
906 Args.insert(Args.end(), ArgTy, ArgTy+ArgC);
907
908 llvm::FunctionType *MethodTy = llvm::FunctionType::get(ReturnTy,
909 Args,
910 isVarArg);
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000911 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
912 MethodName, isClassMethod);
913
Gabor Greif984d0b42008-04-06 20:42:52 +0000914 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattner391d77a2008-03-30 23:03:07 +0000915 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000916 FunctionName,
Chris Lattner391d77a2008-03-30 23:03:07 +0000917 &TheModule);
Chris Lattner391d77a2008-03-30 23:03:07 +0000918 llvm::Function::arg_iterator AI = Method->arg_begin();
Anton Korobeynikov20ff3102008-06-01 14:13:53 +0000919 // Name the struct return argument.
920 // FIXME: This is probably the wrong test.
921 if (!ReturnTy->isFirstClassType() && ReturnTy != llvm::Type::VoidTy) {
922 AI->setName("agg.result");
923 ++AI;
924 }
Chris Lattner391d77a2008-03-30 23:03:07 +0000925 AI->setName("self");
926 ++AI;
927 AI->setName("_cmd");
928 return Method;
929}
930
931clang::CodeGen::CGObjCRuntime *clang::CodeGen::CreateObjCRuntime(
932 llvm::Module &M,
933 const llvm::Type *LLVMIntType,
934 const llvm::Type *LLVMLongType) {
935 return new CGObjCGNU(M, LLVMIntType, LLVMLongType);
Chris Lattner0f984262008-03-01 08:50:34 +0000936}