blob: f75c2e37cc909bdcd9282f3637c49dfeb17381c4 [file] [log] [blame]
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001//===------- CGObjCGNU.cpp - Emit LLVM Code from ASTs for a Module --------===//
Chris Lattnera0fd5ee2008-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 Korobeynikovcd5d08d2008-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 Lattnera0fd5ee2008-03-01 08:50:34 +000014//
15//===----------------------------------------------------------------------===//
16
17#include "CGObjCRuntime.h"
Chris Lattner547907c2008-06-26 04:19:03 +000018#include "CodeGenModule.h"
Daniel Dunbara04840b2008-08-23 03:46:30 +000019#include "CodeGenFunction.h"
Chris Lattnerfee3eb02009-05-08 00:11:50 +000020
Chris Lattner547907c2008-06-26 04:19:03 +000021#include "clang/AST/ASTContext.h"
Anders Carlsson7558ec62009-07-14 17:29:11 +000022#include "clang/AST/ASTRecordLayout.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000023#include "clang/AST/Decl.h"
Daniel Dunbar84bb85f2008-08-13 00:59:25 +000024#include "clang/AST/DeclObjC.h"
Chris Lattner4a9e9272009-04-26 01:32:48 +000025#include "clang/AST/StmtObjC.h"
Chris Lattnerfee3eb02009-05-08 00:11:50 +000026
27#include "llvm/Intrinsics.h"
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000028#include "llvm/Module.h"
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000029#include "llvm/ADT/SmallVector.h"
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000030#include "llvm/ADT/StringMap.h"
Daniel Dunbarac93e472008-08-15 22:20:32 +000031#include "llvm/Support/Compiler.h"
Daniel Dunbarac93e472008-08-15 22:20:32 +000032#include "llvm/Target/TargetData.h"
Chris Lattnerfee3eb02009-05-08 00:11:50 +000033
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000034#include <map>
Chris Lattner431dc382009-01-27 05:06:01 +000035
36
Chris Lattner547907c2008-06-26 04:19:03 +000037using namespace clang;
Daniel Dunbar0a2da0f2008-09-09 01:06:48 +000038using namespace CodeGen;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000039using llvm::dyn_cast;
40
41// The version of the runtime that this class targets. Must match the version
42// in the runtime.
43static const int RuntimeVersion = 8;
Fariborz Jahanianf25adf12009-05-20 18:41:51 +000044static const int NonFragileRuntimeVersion = 9;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000045static const int ProtocolVersion = 2;
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000046
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000047namespace {
Chris Lattner547907c2008-06-26 04:19:03 +000048class CGObjCGNU : public CodeGen::CGObjCRuntime {
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000049private:
Chris Lattner547907c2008-06-26 04:19:03 +000050 CodeGen::CodeGenModule &CGM;
Chris Lattnera0fd5ee2008-03-01 08:50:34 +000051 llvm::Module &TheModule;
Chris Lattner431dc382009-01-27 05:06:01 +000052 const llvm::PointerType *SelectorTy;
53 const llvm::PointerType *PtrToInt8Ty;
Fariborz Jahanianbb284742009-02-04 20:31:19 +000054 const llvm::FunctionType *IMPTy;
Chris Lattner431dc382009-01-27 05:06:01 +000055 const llvm::PointerType *IdTy;
56 const llvm::IntegerType *IntTy;
57 const llvm::PointerType *PtrTy;
58 const llvm::IntegerType *LongTy;
59 const llvm::PointerType *PtrToIntTy;
Daniel Dunbarc4532f52009-05-04 15:31:17 +000060 llvm::GlobalAlias *ClassPtrAlias;
61 llvm::GlobalAlias *MetaClassPtrAlias;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000062 std::vector<llvm::Constant*> Classes;
63 std::vector<llvm::Constant*> Categories;
64 std::vector<llvm::Constant*> ConstantStrings;
65 llvm::Function *LoadFunction;
66 llvm::StringMap<llvm::Constant*> ExistingProtocols;
67 typedef std::pair<std::string, std::string> TypedSelector;
68 std::map<TypedSelector, llvm::GlobalAlias*> TypedSelectors;
69 llvm::StringMap<llvm::GlobalAlias*> UntypedSelectors;
70 // Some zeros used for GEPs in lots of places.
71 llvm::Constant *Zeros[2];
72 llvm::Constant *NULLPtr;
Owen Anderson73e7f802009-07-14 23:10:40 +000073 llvm::LLVMContext &VMContext;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000074private:
75 llvm::Constant *GenerateIvarList(
76 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
77 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
78 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets);
79 llvm::Constant *GenerateMethodList(const std::string &ClassName,
80 const std::string &CategoryName,
Chris Lattner578279d2008-06-26 05:08:00 +000081 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000082 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
83 bool isClassMethodList);
Fariborz Jahanian926c3712009-03-31 18:27:22 +000084 llvm::Constant *GenerateEmptyProtocol(const std::string &ProtocolName);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000085 llvm::Constant *GenerateProtocolList(
86 const llvm::SmallVectorImpl<std::string> &Protocols);
87 llvm::Constant *GenerateClassStructure(
88 llvm::Constant *MetaClass,
89 llvm::Constant *SuperClass,
90 unsigned info,
Chris Lattnerad9c3f32008-06-26 04:47:04 +000091 const char *Name,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +000092 llvm::Constant *Version,
93 llvm::Constant *InstanceSize,
94 llvm::Constant *IVars,
95 llvm::Constant *Methods,
96 llvm::Constant *Protocols);
97 llvm::Constant *GenerateProtocolMethodList(
98 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
99 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes);
100 llvm::Constant *MakeConstantString(const std::string &Str, const std::string
101 &Name="");
102 llvm::Constant *MakeGlobal(const llvm::StructType *Ty,
103 std::vector<llvm::Constant*> &V, const std::string &Name="");
104 llvm::Constant *MakeGlobal(const llvm::ArrayType *Ty,
105 std::vector<llvm::Constant*> &V, const std::string &Name="");
Fariborz Jahanianf25adf12009-05-20 18:41:51 +0000106 llvm::GlobalVariable *ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
107 const ObjCIvarDecl *Ivar);
Chris Lattner9c445ae2009-06-15 01:09:11 +0000108 void EmitClassRef(const std::string &className);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000109public:
Chris Lattner547907c2008-06-26 04:19:03 +0000110 CGObjCGNU(CodeGen::CodeGenModule &cgm);
Steve Naroff2c8a08e2009-03-31 16:53:37 +0000111 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *);
Daniel Dunbara04840b2008-08-23 03:46:30 +0000112 virtual CodeGen::RValue
113 GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000114 QualType ResultType,
115 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000116 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000117 bool IsClassMessage,
Fariborz Jahanianc8007152009-05-05 21:36:57 +0000118 const CallArgList &CallArgs,
119 const ObjCMethodDecl *Method);
Daniel Dunbara04840b2008-08-23 03:46:30 +0000120 virtual CodeGen::RValue
121 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000122 QualType ResultType,
123 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000124 const ObjCInterfaceDecl *Class,
Fariborz Jahanian17636fa2009-02-28 20:07:56 +0000125 bool isCategoryImpl,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000126 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000127 bool IsClassMessage,
128 const CallArgList &CallArgs);
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000129 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar434627a2008-08-16 00:25:02 +0000130 const ObjCInterfaceDecl *OID);
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000131 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanianc8007152009-05-05 21:36:57 +0000132 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
133 *Method);
Chris Lattnerd71288e2008-06-26 04:37:12 +0000134
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +0000135 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
136 const ObjCContainerDecl *CD);
Daniel Dunbarac93e472008-08-15 22:20:32 +0000137 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
138 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000139 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000140 const ObjCProtocolDecl *PD);
141 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000142 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbarf7103722008-09-24 03:38:44 +0000143 virtual llvm::Function *GetPropertyGetFunction();
144 virtual llvm::Function *GetPropertySetFunction();
Anders Carlsson58d16242008-08-31 04:05:03 +0000145 virtual llvm::Function *EnumerationMutationFunction();
Anders Carlssonb01a2112008-09-09 10:04:29 +0000146
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +0000147 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
148 const Stmt &S);
Anders Carlssonb01a2112008-09-09 10:04:29 +0000149 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
150 const ObjCAtThrowStmt &S);
Fariborz Jahanian252d87f2008-11-18 22:37:34 +0000151 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian3305ad32008-11-18 21:45:40 +0000152 llvm::Value *AddrWeakObj);
Fariborz Jahanian252d87f2008-11-18 22:37:34 +0000153 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
154 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian17958902008-11-19 00:59:10 +0000155 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
156 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianf310b592008-11-20 19:23:36 +0000157 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
158 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian17958902008-11-19 00:59:10 +0000159 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
160 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian614e8f02009-07-08 01:18:33 +0000161 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
162 llvm::Value *DestPtr,
163 llvm::Value *SrcPtr,
164 unsigned long size);
Fariborz Jahanianc912eb72009-02-03 19:03:09 +0000165 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
166 QualType ObjectTy,
167 llvm::Value *BaseValue,
168 const ObjCIvarDecl *Ivar,
Fariborz Jahanianc912eb72009-02-03 19:03:09 +0000169 unsigned CVRQualifiers);
Fariborz Jahanian27cc6662009-02-10 19:02:04 +0000170 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar61e14a62009-04-22 05:08:15 +0000171 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian27cc6662009-02-10 19:02:04 +0000172 const ObjCIvarDecl *Ivar);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000173};
174} // end anonymous namespace
175
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000176
Chris Lattner9c445ae2009-06-15 01:09:11 +0000177/// Emits a reference to a dummy variable which is emitted with each class.
178/// This ensures that a linker error will be generated when trying to link
179/// together modules where a referenced class is not defined.
180void CGObjCGNU::EmitClassRef(const std::string &className){
181 std::string symbolRef = "__objc_class_ref_" + className;
182 // Don't emit two copies of the same symbol
183 if (TheModule.getGlobalVariable(symbolRef)) return;
184 std::string symbolName = "__objc_class_name_" + className;
185 llvm::GlobalVariable *ClassSymbol = TheModule.getGlobalVariable(symbolName);
186 if (!ClassSymbol) {
Owen Anderson94148482009-07-08 19:05:04 +0000187 ClassSymbol = new llvm::GlobalVariable(TheModule, LongTy, false,
188 llvm::GlobalValue::ExternalLinkage, 0, symbolName);
Chris Lattner9c445ae2009-06-15 01:09:11 +0000189 }
Owen Anderson94148482009-07-08 19:05:04 +0000190 new llvm::GlobalVariable(TheModule, ClassSymbol->getType(), true,
191 llvm::GlobalValue::CommonLinkage, ClassSymbol, symbolRef);
Chris Lattner9c445ae2009-06-15 01:09:11 +0000192}
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000193
194static std::string SymbolNameForClass(const std::string &ClassName) {
Chris Lattner9c445ae2009-06-15 01:09:11 +0000195 return "_OBJC_CLASS_" + ClassName;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000196}
197
198static std::string SymbolNameForMethod(const std::string &ClassName, const
199 std::string &CategoryName, const std::string &MethodName, bool isClassMethod)
200{
Chris Lattner9c445ae2009-06-15 01:09:11 +0000201 return "_OBJC_METHOD_" + ClassName + "("+CategoryName+")"+
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000202 (isClassMethod ? "+" : "-") + MethodName;
203}
204
Chris Lattner547907c2008-06-26 04:19:03 +0000205CGObjCGNU::CGObjCGNU(CodeGen::CodeGenModule &cgm)
Daniel Dunbarc4532f52009-05-04 15:31:17 +0000206 : CGM(cgm), TheModule(CGM.getModule()), ClassPtrAlias(0),
Owen Anderson73e7f802009-07-14 23:10:40 +0000207 MetaClassPtrAlias(0), VMContext(cgm.getLLVMContext()) {
Chris Lattner431dc382009-01-27 05:06:01 +0000208 IntTy = cast<llvm::IntegerType>(
209 CGM.getTypes().ConvertType(CGM.getContext().IntTy));
210 LongTy = cast<llvm::IntegerType>(
211 CGM.getTypes().ConvertType(CGM.getContext().LongTy));
Chris Lattner547907c2008-06-26 04:19:03 +0000212
Owen Anderson73e7f802009-07-14 23:10:40 +0000213 Zeros[0] = VMContext.getConstantInt(LongTy, 0);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000214 Zeros[1] = Zeros[0];
Owen Anderson73e7f802009-07-14 23:10:40 +0000215 NULLPtr = VMContext.getConstantPointerNull(
216 VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty));
Chris Lattnerb326b172008-03-30 23:03:07 +0000217 // C string type. Used in lots of places.
218 PtrToInt8Ty =
Owen Anderson73e7f802009-07-14 23:10:40 +0000219 VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
Chris Lattnerb326b172008-03-30 23:03:07 +0000220 // Get the selector Type.
Chris Lattner431dc382009-01-27 05:06:01 +0000221 SelectorTy = cast<llvm::PointerType>(
222 CGM.getTypes().ConvertType(CGM.getContext().getObjCSelType()));
223
Owen Anderson73e7f802009-07-14 23:10:40 +0000224 PtrToIntTy = VMContext.getPointerTypeUnqual(IntTy);
Chris Lattnerb326b172008-03-30 23:03:07 +0000225 PtrTy = PtrToInt8Ty;
226
227 // Object type
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000228 IdTy = cast<llvm::PointerType>(
229 CGM.getTypes().ConvertType(CGM.getContext().getObjCIdType()));
Chris Lattnerb326b172008-03-30 23:03:07 +0000230
231 // IMP type
232 std::vector<const llvm::Type*> IMPArgs;
233 IMPArgs.push_back(IdTy);
234 IMPArgs.push_back(SelectorTy);
Owen Anderson73e7f802009-07-14 23:10:40 +0000235 IMPTy = VMContext.getFunctionType(IdTy, IMPArgs, true);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000236}
237// This has to perform the lookup every time, since posing and related
238// techniques can modify the name -> class mapping.
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000239llvm::Value *CGObjCGNU::GetClass(CGBuilderTy &Builder,
Daniel Dunbar434627a2008-08-16 00:25:02 +0000240 const ObjCInterfaceDecl *OID) {
Chris Lattner271d4c22008-11-24 05:29:24 +0000241 llvm::Value *ClassName = CGM.GetAddrOfConstantCString(OID->getNameAsString());
Chris Lattner9c445ae2009-06-15 01:09:11 +0000242 EmitClassRef(OID->getNameAsString());
Daniel Dunbar434627a2008-08-16 00:25:02 +0000243 ClassName = Builder.CreateStructGEP(ClassName, 0);
244
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000245 std::vector<const llvm::Type*> Params(1, PtrToInt8Ty);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000246 llvm::Constant *ClassLookupFn =
Owen Anderson73e7f802009-07-14 23:10:40 +0000247 CGM.CreateRuntimeFunction(VMContext.getFunctionType(IdTy,
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000248 Params,
249 true),
250 "objc_lookup_class");
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000251 return Builder.CreateCall(ClassLookupFn, ClassName);
Chris Lattnerb326b172008-03-30 23:03:07 +0000252}
253
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000254llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Chris Lattner3a8f2942008-11-24 03:33:13 +0000255 llvm::GlobalAlias *&US = UntypedSelectors[Sel.getAsString()];
Chris Lattnerd71288e2008-06-26 04:37:12 +0000256 if (US == 0)
Owen Anderson73e7f802009-07-14 23:10:40 +0000257 US = new llvm::GlobalAlias(VMContext.getPointerTypeUnqual(SelectorTy),
Chris Lattnerd71288e2008-06-26 04:37:12 +0000258 llvm::GlobalValue::InternalLinkage,
259 ".objc_untyped_selector_alias",
260 NULL, &TheModule);
261
262 return Builder.CreateLoad(US);
Fariborz Jahanianc8007152009-05-05 21:36:57 +0000263}
264
265llvm::Value *CGObjCGNU::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
266 *Method) {
267
268 std::string SelName = Method->getSelector().getAsString();
269 std::string SelTypes;
270 CGM.getContext().getObjCEncodingForMethodDecl(Method, SelTypes);
271 // Typed selectors
272 TypedSelector Selector = TypedSelector(SelName,
273 SelTypes);
274
275 // If it's already cached, return it.
276 if (TypedSelectors[Selector])
277 {
278 return Builder.CreateLoad(TypedSelectors[Selector]);
279 }
280
281 // If it isn't, cache it.
282 llvm::GlobalAlias *Sel = new llvm::GlobalAlias(
Owen Anderson73e7f802009-07-14 23:10:40 +0000283 VMContext.getPointerTypeUnqual(SelectorTy),
Fariborz Jahanianc8007152009-05-05 21:36:57 +0000284 llvm::GlobalValue::InternalLinkage, SelName,
285 NULL, &TheModule);
286 TypedSelectors[Selector] = Sel;
287
288 return Builder.CreateLoad(Sel);
Chris Lattnerd71288e2008-06-26 04:37:12 +0000289}
290
Chris Lattnera5b18882008-06-26 04:44:19 +0000291llvm::Constant *CGObjCGNU::MakeConstantString(const std::string &Str,
292 const std::string &Name) {
Owen Anderson73e7f802009-07-14 23:10:40 +0000293 llvm::Constant * ConstStr = VMContext.getConstantArray(Str);
Owen Anderson94148482009-07-08 19:05:04 +0000294 ConstStr = new llvm::GlobalVariable(TheModule, ConstStr->getType(), true,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000295 llvm::GlobalValue::InternalLinkage,
Owen Anderson94148482009-07-08 19:05:04 +0000296 ConstStr, Name);
Owen Anderson73e7f802009-07-14 23:10:40 +0000297 return VMContext.getConstantExprGetElementPtr(ConstStr, Zeros, 2);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000298}
299llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::StructType *Ty,
300 std::vector<llvm::Constant*> &V, const std::string &Name) {
Owen Anderson73e7f802009-07-14 23:10:40 +0000301 llvm::Constant *C = VMContext.getConstantStruct(Ty, V);
Owen Anderson94148482009-07-08 19:05:04 +0000302 return new llvm::GlobalVariable(TheModule, Ty, false,
303 llvm::GlobalValue::InternalLinkage, C, Name);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000304}
305llvm::Constant *CGObjCGNU::MakeGlobal(const llvm::ArrayType *Ty,
306 std::vector<llvm::Constant*> &V, const std::string &Name) {
Owen Anderson73e7f802009-07-14 23:10:40 +0000307 llvm::Constant *C = VMContext.getConstantArray(Ty, V);
Owen Anderson94148482009-07-08 19:05:04 +0000308 return new llvm::GlobalVariable(TheModule, Ty, false,
309 llvm::GlobalValue::InternalLinkage, C, Name);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000310}
311
312/// Generate an NSConstantString object.
313//TODO: In case there are any crazy people still using the GNU runtime without
314//an OpenStep implementation, this should let them select their own class for
315//constant strings.
Steve Naroff2c8a08e2009-03-31 16:53:37 +0000316llvm::Constant *CGObjCGNU::GenerateConstantString(const ObjCStringLiteral *SL) {
317 std::string Str(SL->getString()->getStrData(),
318 SL->getString()->getByteLength());
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000319 std::vector<llvm::Constant*> Ivars;
320 Ivars.push_back(NULLPtr);
Chris Lattnerbac12452008-06-21 21:44:18 +0000321 Ivars.push_back(MakeConstantString(Str));
Owen Anderson73e7f802009-07-14 23:10:40 +0000322 Ivars.push_back(VMContext.getConstantInt(IntTy, Str.size()));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000323 llvm::Constant *ObjCStr = MakeGlobal(
Owen Anderson73e7f802009-07-14 23:10:40 +0000324 VMContext.getStructType(PtrToInt8Ty, PtrToInt8Ty, IntTy, NULL),
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000325 Ivars, ".objc_str");
326 ConstantStrings.push_back(
Owen Anderson73e7f802009-07-14 23:10:40 +0000327 VMContext.getConstantExprBitCast(ObjCStr, PtrToInt8Ty));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000328 return ObjCStr;
329}
330
331///Generates a message send where the super is the receiver. This is a message
332///send to self with special delivery semantics indicating which class's method
333///should be called.
Daniel Dunbara04840b2008-08-23 03:46:30 +0000334CodeGen::RValue
335CGObjCGNU::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000336 QualType ResultType,
337 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000338 const ObjCInterfaceDecl *Class,
Fariborz Jahanian17636fa2009-02-28 20:07:56 +0000339 bool isCategoryImpl,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000340 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000341 bool IsClassMessage,
342 const CallArgList &CallArgs) {
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000343 llvm::Value *cmd = GetSelector(CGF.Builder, Sel);
344
345 CallArgList ActualArgs;
346
347 ActualArgs.push_back(
348 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
349 CGF.getContext().getObjCIdType()));
350 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
351 CGF.getContext().getObjCSelType()));
352 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
353
354 CodeGenTypes &Types = CGM.getTypes();
355 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
356 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
357
Daniel Dunbarc4532f52009-05-04 15:31:17 +0000358 llvm::Value *ReceiverClass = 0;
Chris Lattner3a3c2962009-05-08 15:39:58 +0000359 if (isCategoryImpl) {
360 llvm::Constant *classLookupFunction = 0;
361 std::vector<const llvm::Type*> Params;
362 Params.push_back(PtrTy);
363 if (IsClassMessage) {
Owen Anderson73e7f802009-07-14 23:10:40 +0000364 classLookupFunction = CGM.CreateRuntimeFunction(VMContext.getFunctionType(
Chris Lattner3a3c2962009-05-08 15:39:58 +0000365 IdTy, Params, true), "objc_get_meta_class");
366 } else {
Owen Anderson73e7f802009-07-14 23:10:40 +0000367 classLookupFunction = CGM.CreateRuntimeFunction(VMContext.getFunctionType(
Chris Lattner3a3c2962009-05-08 15:39:58 +0000368 IdTy, Params, true), "objc_get_class");
Daniel Dunbarc4532f52009-05-04 15:31:17 +0000369 }
Chris Lattner3a3c2962009-05-08 15:39:58 +0000370 ReceiverClass = CGF.Builder.CreateCall(classLookupFunction,
371 MakeConstantString(Class->getNameAsString()));
Daniel Dunbarc4532f52009-05-04 15:31:17 +0000372 } else {
Chris Lattner3a3c2962009-05-08 15:39:58 +0000373 // Set up global aliases for the metaclass or class pointer if they do not
374 // already exist. These will are forward-references which will be set to
375 // pointers to the class and metaclass structure created for the runtime load
376 // function. To send a message to super, we look up the value of the
377 // super_class pointer from either the class or metaclass structure.
378 if (IsClassMessage) {
379 if (!MetaClassPtrAlias) {
380 MetaClassPtrAlias = new llvm::GlobalAlias(IdTy,
381 llvm::GlobalValue::InternalLinkage, ".objc_metaclass_ref" +
382 Class->getNameAsString(), NULL, &TheModule);
383 }
384 ReceiverClass = MetaClassPtrAlias;
385 } else {
386 if (!ClassPtrAlias) {
387 ClassPtrAlias = new llvm::GlobalAlias(IdTy,
388 llvm::GlobalValue::InternalLinkage, ".objc_class_ref" +
389 Class->getNameAsString(), NULL, &TheModule);
390 }
391 ReceiverClass = ClassPtrAlias;
Daniel Dunbarc4532f52009-05-04 15:31:17 +0000392 }
Chris Lattner7eaa9f02009-04-25 23:19:45 +0000393 }
Daniel Dunbarc4532f52009-05-04 15:31:17 +0000394 // Cast the pointer to a simplified version of the class structure
395 ReceiverClass = CGF.Builder.CreateBitCast(ReceiverClass,
Owen Anderson73e7f802009-07-14 23:10:40 +0000396 VMContext.getPointerTypeUnqual(
397 VMContext.getStructType(IdTy, IdTy, NULL)));
Daniel Dunbarc4532f52009-05-04 15:31:17 +0000398 // Get the superclass pointer
399 ReceiverClass = CGF.Builder.CreateStructGEP(ReceiverClass, 1);
400 // Load the superclass pointer
401 ReceiverClass = CGF.Builder.CreateLoad(ReceiverClass);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000402 // Construct the structure used to look up the IMP
Owen Anderson73e7f802009-07-14 23:10:40 +0000403 llvm::StructType *ObjCSuperTy = VMContext.getStructType(Receiver->getType(),
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000404 IdTy, NULL);
Daniel Dunbara04840b2008-08-23 03:46:30 +0000405 llvm::Value *ObjCSuper = CGF.Builder.CreateAlloca(ObjCSuperTy);
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000406
Daniel Dunbara04840b2008-08-23 03:46:30 +0000407 CGF.Builder.CreateStore(Receiver, CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarc4532f52009-05-04 15:31:17 +0000408 CGF.Builder.CreateStore(ReceiverClass,
409 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000410
411 // Get the IMP
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000412 std::vector<const llvm::Type*> Params;
Owen Anderson73e7f802009-07-14 23:10:40 +0000413 Params.push_back(VMContext.getPointerTypeUnqual(ObjCSuperTy));
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000414 Params.push_back(SelectorTy);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000415 llvm::Constant *lookupFunction =
Owen Anderson73e7f802009-07-14 23:10:40 +0000416 CGM.CreateRuntimeFunction(VMContext.getFunctionType(
417 VMContext.getPointerTypeUnqual(impType), Params, true),
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000418 "objc_msg_lookup_super");
419
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000420 llvm::Value *lookupArgs[] = {ObjCSuper, cmd};
Daniel Dunbara04840b2008-08-23 03:46:30 +0000421 llvm::Value *imp = CGF.Builder.CreateCall(lookupFunction, lookupArgs,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000422 lookupArgs+2);
423
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000424 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000425}
426
427/// Generate code for a message send expression.
Daniel Dunbara04840b2008-08-23 03:46:30 +0000428CodeGen::RValue
429CGObjCGNU::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbardd851282008-08-30 05:35:15 +0000430 QualType ResultType,
431 Selector Sel,
Daniel Dunbarb1ee5d62008-08-25 08:19:24 +0000432 llvm::Value *Receiver,
Daniel Dunbar0ed60b02008-08-30 03:02:31 +0000433 bool IsClassMessage,
Fariborz Jahanianc8007152009-05-05 21:36:57 +0000434 const CallArgList &CallArgs,
435 const ObjCMethodDecl *Method) {
436 llvm::Value *cmd;
437 if (Method)
438 cmd = GetSelector(CGF.Builder, Method);
439 else
440 cmd = GetSelector(CGF.Builder, Sel);
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000441 CallArgList ActualArgs;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000442
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000443 ActualArgs.push_back(
Fariborz Jahanianfefd5e82009-05-22 20:17:16 +0000444 std::make_pair(RValue::get(CGF.Builder.CreateBitCast(Receiver, IdTy)),
445 CGF.getContext().getObjCIdType()));
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000446 ActualArgs.push_back(std::make_pair(RValue::get(cmd),
447 CGF.getContext().getObjCSelType()));
448 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
449
450 CodeGenTypes &Types = CGM.getTypes();
451 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
452 const llvm::FunctionType *impType = Types.GetFunctionType(FnInfo, false);
453
Fariborz Jahanianfefd5e82009-05-22 20:17:16 +0000454 llvm::Value *imp;
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000455 std::vector<const llvm::Type*> Params;
456 Params.push_back(Receiver->getType());
457 Params.push_back(SelectorTy);
Fariborz Jahanianfefd5e82009-05-22 20:17:16 +0000458 // For sender-aware dispatch, we pass the sender as the third argument to a
459 // lookup function. When sending messages from C code, the sender is nil.
460 // objc_msg_lookup_sender(id receiver, SEL selector, id sender);
461 if (CGM.getContext().getLangOptions().ObjCSenderDispatch) {
462 llvm::Value *self;
Fariborz Jahanian6158e692009-03-30 18:02:14 +0000463
Fariborz Jahanianfefd5e82009-05-22 20:17:16 +0000464 if (isa<ObjCMethodDecl>(CGF.CurFuncDecl)) {
465 self = CGF.LoadObjCSelf();
466 } else {
Owen Anderson73e7f802009-07-14 23:10:40 +0000467 self = VMContext.getConstantPointerNull(IdTy);
Fariborz Jahanianfefd5e82009-05-22 20:17:16 +0000468 }
469 Params.push_back(self->getType());
470 llvm::Constant *lookupFunction =
Owen Anderson73e7f802009-07-14 23:10:40 +0000471 CGM.CreateRuntimeFunction(VMContext.getFunctionType(
472 VMContext.getPointerTypeUnqual(impType), Params, true),
Fariborz Jahanianfefd5e82009-05-22 20:17:16 +0000473 "objc_msg_lookup_sender");
474
475 imp = CGF.Builder.CreateCall3(lookupFunction, Receiver, cmd, self);
476 } else {
477 llvm::Constant *lookupFunction =
Owen Anderson73e7f802009-07-14 23:10:40 +0000478 CGM.CreateRuntimeFunction(VMContext.getFunctionType(
479 VMContext.getPointerTypeUnqual(impType), Params, true),
Fariborz Jahanianfefd5e82009-05-22 20:17:16 +0000480 "objc_msg_lookup");
481
482 imp = CGF.Builder.CreateCall2(lookupFunction, Receiver, cmd);
483 }
Chris Lattner7db5e942008-05-06 00:56:42 +0000484
Fariborz Jahanianbb284742009-02-04 20:31:19 +0000485 return CGF.EmitCall(FnInfo, imp, ActualArgs);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +0000486}
487
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000488/// Generates a MethodList. Used in construction of a objc_class and
489/// objc_category structures.
490llvm::Constant *CGObjCGNU::GenerateMethodList(const std::string &ClassName,
Chris Lattner578279d2008-06-26 05:08:00 +0000491 const std::string &CategoryName,
492 const llvm::SmallVectorImpl<Selector> &MethodSels,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000493 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes,
494 bool isClassMethodList) {
495 // Get the method structure type.
Owen Anderson73e7f802009-07-14 23:10:40 +0000496 llvm::StructType *ObjCMethodTy = VMContext.getStructType(
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000497 PtrToInt8Ty, // Really a selector, but the runtime creates it us.
498 PtrToInt8Ty, // Method types
Owen Anderson73e7f802009-07-14 23:10:40 +0000499 VMContext.getPointerTypeUnqual(IMPTy), //Method pointer
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000500 NULL);
501 std::vector<llvm::Constant*> Methods;
502 std::vector<llvm::Constant*> Elements;
503 for (unsigned int i = 0, e = MethodTypes.size(); i < e; ++i) {
504 Elements.clear();
Fariborz Jahanianf3ab5642009-05-17 16:49:27 +0000505 if (llvm::Constant *Method =
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000506 TheModule.getFunction(SymbolNameForMethod(ClassName, CategoryName,
Chris Lattner3a8f2942008-11-24 03:33:13 +0000507 MethodSels[i].getAsString(),
Fariborz Jahanianf3ab5642009-05-17 16:49:27 +0000508 isClassMethodList))) {
509 llvm::Constant *C =
510 CGM.GetAddrOfConstantCString(MethodSels[i].getAsString());
Owen Anderson73e7f802009-07-14 23:10:40 +0000511 Elements.push_back(VMContext.getConstantExprGetElementPtr(C, Zeros, 2));
Fariborz Jahanianf3ab5642009-05-17 16:49:27 +0000512 Elements.push_back(
Owen Anderson73e7f802009-07-14 23:10:40 +0000513 VMContext.getConstantExprGetElementPtr(MethodTypes[i], Zeros, 2));
514 Method = VMContext.getConstantExprBitCast(Method,
515 VMContext.getPointerTypeUnqual(IMPTy));
Fariborz Jahanianf3ab5642009-05-17 16:49:27 +0000516 Elements.push_back(Method);
Owen Anderson73e7f802009-07-14 23:10:40 +0000517 Methods.push_back(VMContext.getConstantStruct(ObjCMethodTy, Elements));
Fariborz Jahanianf3ab5642009-05-17 16:49:27 +0000518 }
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000519 }
520
521 // Array of method structures
Owen Anderson73e7f802009-07-14 23:10:40 +0000522 llvm::ArrayType *ObjCMethodArrayTy = VMContext.getArrayType(ObjCMethodTy,
Fariborz Jahanianf3ab5642009-05-17 16:49:27 +0000523 Methods.size());
Owen Anderson73e7f802009-07-14 23:10:40 +0000524 llvm::Constant *MethodArray = VMContext.getConstantArray(ObjCMethodArrayTy,
Chris Lattnerbcb3e862008-06-26 04:52:29 +0000525 Methods);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000526
527 // Structure containing list pointer, array and array count
528 llvm::SmallVector<const llvm::Type*, 16> ObjCMethodListFields;
Owen Anderson73e7f802009-07-14 23:10:40 +0000529 llvm::PATypeHolder OpaqueNextTy = VMContext.getOpaqueType();
530 llvm::Type *NextPtrTy = VMContext.getPointerTypeUnqual(OpaqueNextTy);
531 llvm::StructType *ObjCMethodListTy = VMContext.getStructType(NextPtrTy,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000532 IntTy,
533 ObjCMethodArrayTy,
534 NULL);
535 // Refine next pointer type to concrete type
536 llvm::cast<llvm::OpaqueType>(
537 OpaqueNextTy.get())->refineAbstractTypeTo(ObjCMethodListTy);
538 ObjCMethodListTy = llvm::cast<llvm::StructType>(OpaqueNextTy.get());
539
540 Methods.clear();
Owen Anderson73e7f802009-07-14 23:10:40 +0000541 Methods.push_back(VMContext.getConstantPointerNull(
542 VMContext.getPointerTypeUnqual(ObjCMethodListTy)));
543 Methods.push_back(VMContext.getConstantInt(llvm::Type::Int32Ty,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000544 MethodTypes.size()));
545 Methods.push_back(MethodArray);
546
547 // Create an instance of the structure
548 return MakeGlobal(ObjCMethodListTy, Methods, ".objc_method_list");
549}
550
551/// Generates an IvarList. Used in construction of a objc_class.
552llvm::Constant *CGObjCGNU::GenerateIvarList(
553 const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
554 const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
555 const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets) {
556 // Get the method structure type.
Owen Anderson73e7f802009-07-14 23:10:40 +0000557 llvm::StructType *ObjCIvarTy = VMContext.getStructType(
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000558 PtrToInt8Ty,
559 PtrToInt8Ty,
560 IntTy,
561 NULL);
562 std::vector<llvm::Constant*> Ivars;
563 std::vector<llvm::Constant*> Elements;
564 for (unsigned int i = 0, e = IvarNames.size() ; i < e ; i++) {
565 Elements.clear();
Owen Anderson73e7f802009-07-14 23:10:40 +0000566 Elements.push_back( VMContext.getConstantExprGetElementPtr(IvarNames[i],
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000567 Zeros, 2));
Owen Anderson73e7f802009-07-14 23:10:40 +0000568 Elements.push_back( VMContext.getConstantExprGetElementPtr(IvarTypes[i],
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000569 Zeros, 2));
570 Elements.push_back(IvarOffsets[i]);
Owen Anderson73e7f802009-07-14 23:10:40 +0000571 Ivars.push_back(VMContext.getConstantStruct(ObjCIvarTy, Elements));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000572 }
573
574 // Array of method structures
Owen Anderson73e7f802009-07-14 23:10:40 +0000575 llvm::ArrayType *ObjCIvarArrayTy = VMContext.getArrayType(ObjCIvarTy,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000576 IvarNames.size());
577
578
579 Elements.clear();
Owen Anderson73e7f802009-07-14 23:10:40 +0000580 Elements.push_back(VMContext.getConstantInt(IntTy, (int)IvarNames.size()));
581 Elements.push_back(VMContext.getConstantArray(ObjCIvarArrayTy, Ivars));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000582 // Structure containing array and array count
Owen Anderson73e7f802009-07-14 23:10:40 +0000583 llvm::StructType *ObjCIvarListTy = VMContext.getStructType(IntTy,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000584 ObjCIvarArrayTy,
585 NULL);
586
587 // Create an instance of the structure
588 return MakeGlobal(ObjCIvarListTy, Elements, ".objc_ivar_list");
589}
590
591/// Generate a class structure
592llvm::Constant *CGObjCGNU::GenerateClassStructure(
593 llvm::Constant *MetaClass,
594 llvm::Constant *SuperClass,
595 unsigned info,
Chris Lattnerad9c3f32008-06-26 04:47:04 +0000596 const char *Name,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000597 llvm::Constant *Version,
598 llvm::Constant *InstanceSize,
599 llvm::Constant *IVars,
600 llvm::Constant *Methods,
601 llvm::Constant *Protocols) {
602 // Set up the class structure
603 // Note: Several of these are char*s when they should be ids. This is
604 // because the runtime performs this translation on load.
Owen Anderson73e7f802009-07-14 23:10:40 +0000605 llvm::StructType *ClassTy = VMContext.getStructType(
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000606 PtrToInt8Ty, // class_pointer
607 PtrToInt8Ty, // super_class
608 PtrToInt8Ty, // name
609 LongTy, // version
610 LongTy, // info
611 LongTy, // instance_size
612 IVars->getType(), // ivars
613 Methods->getType(), // methods
614 // These are all filled in by the runtime, so we pretend
615 PtrTy, // dtable
616 PtrTy, // subclass_list
617 PtrTy, // sibling_class
618 PtrTy, // protocols
619 PtrTy, // gc_object_type
620 NULL);
Owen Anderson73e7f802009-07-14 23:10:40 +0000621 llvm::Constant *Zero = VMContext.getConstantInt(LongTy, 0);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000622 llvm::Constant *NullP =
Owen Anderson73e7f802009-07-14 23:10:40 +0000623 VMContext.getConstantPointerNull(PtrTy);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000624 // Fill in the structure
625 std::vector<llvm::Constant*> Elements;
Owen Anderson73e7f802009-07-14 23:10:40 +0000626 Elements.push_back(VMContext.getConstantExprBitCast(MetaClass, PtrToInt8Ty));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000627 Elements.push_back(SuperClass);
Chris Lattnerad9c3f32008-06-26 04:47:04 +0000628 Elements.push_back(MakeConstantString(Name, ".class_name"));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000629 Elements.push_back(Zero);
Owen Anderson73e7f802009-07-14 23:10:40 +0000630 Elements.push_back(VMContext.getConstantInt(LongTy, info));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000631 Elements.push_back(InstanceSize);
632 Elements.push_back(IVars);
633 Elements.push_back(Methods);
634 Elements.push_back(NullP);
635 Elements.push_back(NullP);
636 Elements.push_back(NullP);
Owen Anderson73e7f802009-07-14 23:10:40 +0000637 Elements.push_back(VMContext.getConstantExprBitCast(Protocols, PtrTy));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000638 Elements.push_back(NullP);
639 // Create an instance of the structure
Chris Lattner4d018542008-07-21 06:31:05 +0000640 return MakeGlobal(ClassTy, Elements, SymbolNameForClass(Name));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000641}
642
643llvm::Constant *CGObjCGNU::GenerateProtocolMethodList(
644 const llvm::SmallVectorImpl<llvm::Constant *> &MethodNames,
645 const llvm::SmallVectorImpl<llvm::Constant *> &MethodTypes) {
646 // Get the method structure type.
Owen Anderson73e7f802009-07-14 23:10:40 +0000647 llvm::StructType *ObjCMethodDescTy = VMContext.getStructType(
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000648 PtrToInt8Ty, // Really a selector, but the runtime does the casting for us.
649 PtrToInt8Ty,
650 NULL);
651 std::vector<llvm::Constant*> Methods;
652 std::vector<llvm::Constant*> Elements;
653 for (unsigned int i = 0, e = MethodTypes.size() ; i < e ; i++) {
654 Elements.clear();
Owen Anderson73e7f802009-07-14 23:10:40 +0000655 Elements.push_back(VMContext.getConstantExprGetElementPtr(MethodNames[i],
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000656 Zeros, 2));
657 Elements.push_back(
Owen Anderson73e7f802009-07-14 23:10:40 +0000658 VMContext.getConstantExprGetElementPtr(MethodTypes[i], Zeros, 2));
659 Methods.push_back(VMContext.getConstantStruct(ObjCMethodDescTy, Elements));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000660 }
Owen Anderson73e7f802009-07-14 23:10:40 +0000661 llvm::ArrayType *ObjCMethodArrayTy = VMContext.getArrayType(ObjCMethodDescTy,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000662 MethodNames.size());
Owen Anderson73e7f802009-07-14 23:10:40 +0000663 llvm::Constant *Array = VMContext.getConstantArray(ObjCMethodArrayTy,
664 Methods);
665 llvm::StructType *ObjCMethodDescListTy = VMContext.getStructType(
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000666 IntTy, ObjCMethodArrayTy, NULL);
667 Methods.clear();
Owen Anderson73e7f802009-07-14 23:10:40 +0000668 Methods.push_back(VMContext.getConstantInt(IntTy, MethodNames.size()));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000669 Methods.push_back(Array);
670 return MakeGlobal(ObjCMethodDescListTy, Methods, ".objc_method_list");
671}
672// Create the protocol list structure used in classes, categories and so on
673llvm::Constant *CGObjCGNU::GenerateProtocolList(
674 const llvm::SmallVectorImpl<std::string> &Protocols) {
Owen Anderson73e7f802009-07-14 23:10:40 +0000675 llvm::ArrayType *ProtocolArrayTy = VMContext.getArrayType(PtrToInt8Ty,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000676 Protocols.size());
Owen Anderson73e7f802009-07-14 23:10:40 +0000677 llvm::StructType *ProtocolListTy = VMContext.getStructType(
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000678 PtrTy, //Should be a recurisve pointer, but it's always NULL here.
679 LongTy,//FIXME: Should be size_t
680 ProtocolArrayTy,
681 NULL);
682 std::vector<llvm::Constant*> Elements;
683 for (const std::string *iter = Protocols.begin(), *endIter = Protocols.end();
684 iter != endIter ; iter++) {
Fariborz Jahanian926c3712009-03-31 18:27:22 +0000685 llvm::Constant *protocol = ExistingProtocols[*iter];
686 if (!protocol)
687 protocol = GenerateEmptyProtocol(*iter);
Owen Anderson73e7f802009-07-14 23:10:40 +0000688 llvm::Constant *Ptr = VMContext.getConstantExprBitCast(protocol,
689 PtrToInt8Ty);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000690 Elements.push_back(Ptr);
691 }
Owen Anderson73e7f802009-07-14 23:10:40 +0000692 llvm::Constant * ProtocolArray = VMContext.getConstantArray(ProtocolArrayTy,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000693 Elements);
694 Elements.clear();
695 Elements.push_back(NULLPtr);
Owen Anderson73e7f802009-07-14 23:10:40 +0000696 Elements.push_back(VMContext.getConstantInt(LongTy, Protocols.size()));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000697 Elements.push_back(ProtocolArray);
698 return MakeGlobal(ProtocolListTy, Elements, ".objc_protocol_list");
699}
700
Daniel Dunbard916e6e2008-11-01 01:53:16 +0000701llvm::Value *CGObjCGNU::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000702 const ObjCProtocolDecl *PD) {
Fariborz Jahanian926c3712009-03-31 18:27:22 +0000703 llvm::Value *protocol = ExistingProtocols[PD->getNameAsString()];
704 const llvm::Type *T =
705 CGM.getTypes().ConvertType(CGM.getContext().getObjCProtoType());
Owen Anderson73e7f802009-07-14 23:10:40 +0000706 return Builder.CreateBitCast(protocol, VMContext.getPointerTypeUnqual(T));
Fariborz Jahanian926c3712009-03-31 18:27:22 +0000707}
708
709llvm::Constant *CGObjCGNU::GenerateEmptyProtocol(
710 const std::string &ProtocolName) {
711 llvm::SmallVector<std::string, 0> EmptyStringVector;
712 llvm::SmallVector<llvm::Constant*, 0> EmptyConstantVector;
713
714 llvm::Constant *ProtocolList = GenerateProtocolList(EmptyStringVector);
715 llvm::Constant *InstanceMethodList =
716 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
717 llvm::Constant *ClassMethodList =
718 GenerateProtocolMethodList(EmptyConstantVector, EmptyConstantVector);
719 // Protocols are objects containing lists of the methods implemented and
720 // protocols adopted.
Owen Anderson73e7f802009-07-14 23:10:40 +0000721 llvm::StructType *ProtocolTy = VMContext.getStructType(IdTy,
Fariborz Jahanian926c3712009-03-31 18:27:22 +0000722 PtrToInt8Ty,
723 ProtocolList->getType(),
724 InstanceMethodList->getType(),
725 ClassMethodList->getType(),
726 NULL);
727 std::vector<llvm::Constant*> Elements;
728 // The isa pointer must be set to a magic number so the runtime knows it's
729 // the correct layout.
Owen Anderson73e7f802009-07-14 23:10:40 +0000730 Elements.push_back(VMContext.getConstantExprIntToPtr(
731 VMContext.getConstantInt(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
Fariborz Jahanian926c3712009-03-31 18:27:22 +0000732 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
733 Elements.push_back(ProtocolList);
734 Elements.push_back(InstanceMethodList);
735 Elements.push_back(ClassMethodList);
736 return MakeGlobal(ProtocolTy, Elements, ".objc_protocol");
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000737}
738
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000739void CGObjCGNU::GenerateProtocol(const ObjCProtocolDecl *PD) {
740 ASTContext &Context = CGM.getContext();
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000741 std::string ProtocolName = PD->getNameAsString();
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000742 llvm::SmallVector<std::string, 16> Protocols;
743 for (ObjCProtocolDecl::protocol_iterator PI = PD->protocol_begin(),
744 E = PD->protocol_end(); PI != E; ++PI)
Chris Lattner271d4c22008-11-24 05:29:24 +0000745 Protocols.push_back((*PI)->getNameAsString());
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000746 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodNames;
747 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000748 for (ObjCProtocolDecl::instmeth_iterator iter = PD->instmeth_begin(),
749 E = PD->instmeth_end(); iter != E; iter++) {
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000750 std::string TypeStr;
751 Context.getObjCEncodingForMethodDecl(*iter, TypeStr);
752 InstanceMethodNames.push_back(
Chris Lattner3a8f2942008-11-24 03:33:13 +0000753 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar31fe9c32008-08-13 23:20:05 +0000754 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000755 }
756 // Collect information about class methods:
757 llvm::SmallVector<llvm::Constant*, 16> ClassMethodNames;
758 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000759 for (ObjCProtocolDecl::classmeth_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000760 iter = PD->classmeth_begin(), endIter = PD->classmeth_end();
761 iter != endIter ; iter++) {
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000762 std::string TypeStr;
763 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
764 ClassMethodNames.push_back(
Chris Lattner3a8f2942008-11-24 03:33:13 +0000765 CGM.GetAddrOfConstantCString((*iter)->getSelector().getAsString()));
Daniel Dunbar31fe9c32008-08-13 23:20:05 +0000766 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
Daniel Dunbar84bb85f2008-08-13 00:59:25 +0000767 }
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000768
769 llvm::Constant *ProtocolList = GenerateProtocolList(Protocols);
770 llvm::Constant *InstanceMethodList =
771 GenerateProtocolMethodList(InstanceMethodNames, InstanceMethodTypes);
772 llvm::Constant *ClassMethodList =
773 GenerateProtocolMethodList(ClassMethodNames, ClassMethodTypes);
774 // Protocols are objects containing lists of the methods implemented and
775 // protocols adopted.
Owen Anderson73e7f802009-07-14 23:10:40 +0000776 llvm::StructType *ProtocolTy = VMContext.getStructType(IdTy,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000777 PtrToInt8Ty,
778 ProtocolList->getType(),
779 InstanceMethodList->getType(),
780 ClassMethodList->getType(),
781 NULL);
782 std::vector<llvm::Constant*> Elements;
783 // The isa pointer must be set to a magic number so the runtime knows it's
784 // the correct layout.
Owen Anderson73e7f802009-07-14 23:10:40 +0000785 Elements.push_back(VMContext.getConstantExprIntToPtr(
786 VMContext.getConstantInt(llvm::Type::Int32Ty, ProtocolVersion), IdTy));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000787 Elements.push_back(MakeConstantString(ProtocolName, ".objc_protocol_name"));
788 Elements.push_back(ProtocolList);
789 Elements.push_back(InstanceMethodList);
790 Elements.push_back(ClassMethodList);
791 ExistingProtocols[ProtocolName] =
Owen Anderson73e7f802009-07-14 23:10:40 +0000792 VMContext.getConstantExprBitCast(MakeGlobal(ProtocolTy, Elements,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000793 ".objc_protocol"), IdTy);
794}
795
Daniel Dunbarac93e472008-08-15 22:20:32 +0000796void CGObjCGNU::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000797 std::string ClassName = OCD->getClassInterface()->getNameAsString();
798 std::string CategoryName = OCD->getNameAsString();
Daniel Dunbarac93e472008-08-15 22:20:32 +0000799 // Collect information about instance methods
800 llvm::SmallVector<Selector, 16> InstanceMethodSels;
801 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Douglas Gregorcd19b572009-04-23 01:02:12 +0000802 for (ObjCCategoryImplDecl::instmeth_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000803 iter = OCD->instmeth_begin(), endIter = OCD->instmeth_end();
Douglas Gregorcd19b572009-04-23 01:02:12 +0000804 iter != endIter ; iter++) {
Daniel Dunbarac93e472008-08-15 22:20:32 +0000805 InstanceMethodSels.push_back((*iter)->getSelector());
806 std::string TypeStr;
807 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
808 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
809 }
810
811 // Collect information about class methods
812 llvm::SmallVector<Selector, 16> ClassMethodSels;
813 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregorcd19b572009-04-23 01:02:12 +0000814 for (ObjCCategoryImplDecl::classmeth_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000815 iter = OCD->classmeth_begin(), endIter = OCD->classmeth_end();
Douglas Gregorcd19b572009-04-23 01:02:12 +0000816 iter != endIter ; iter++) {
Daniel Dunbarac93e472008-08-15 22:20:32 +0000817 ClassMethodSels.push_back((*iter)->getSelector());
818 std::string TypeStr;
819 CGM.getContext().getObjCEncodingForMethodDecl(*iter,TypeStr);
820 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
821 }
822
823 // Collect the names of referenced protocols
824 llvm::SmallVector<std::string, 16> Protocols;
825 const ObjCInterfaceDecl *ClassDecl = OCD->getClassInterface();
826 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
827 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
828 E = Protos.end(); I != E; ++I)
Chris Lattner271d4c22008-11-24 05:29:24 +0000829 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbarac93e472008-08-15 22:20:32 +0000830
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000831 std::vector<llvm::Constant*> Elements;
832 Elements.push_back(MakeConstantString(CategoryName));
833 Elements.push_back(MakeConstantString(ClassName));
834 // Instance method list
Owen Anderson73e7f802009-07-14 23:10:40 +0000835 Elements.push_back(VMContext.getConstantExprBitCast(GenerateMethodList(
Chris Lattner578279d2008-06-26 05:08:00 +0000836 ClassName, CategoryName, InstanceMethodSels, InstanceMethodTypes,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000837 false), PtrTy));
838 // Class method list
Owen Anderson73e7f802009-07-14 23:10:40 +0000839 Elements.push_back(VMContext.getConstantExprBitCast(GenerateMethodList(
Chris Lattner578279d2008-06-26 05:08:00 +0000840 ClassName, CategoryName, ClassMethodSels, ClassMethodTypes, true),
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000841 PtrTy));
842 // Protocol list
Owen Anderson73e7f802009-07-14 23:10:40 +0000843 Elements.push_back(VMContext.getConstantExprBitCast(
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000844 GenerateProtocolList(Protocols), PtrTy));
Owen Anderson73e7f802009-07-14 23:10:40 +0000845 Categories.push_back(VMContext.getConstantExprBitCast(
846 MakeGlobal(VMContext.getStructType(PtrToInt8Ty, PtrToInt8Ty, PtrTy,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000847 PtrTy, PtrTy, NULL), Elements), PtrTy));
848}
Daniel Dunbarac93e472008-08-15 22:20:32 +0000849
850void CGObjCGNU::GenerateClass(const ObjCImplementationDecl *OID) {
851 ASTContext &Context = CGM.getContext();
852
853 // Get the superclass name.
854 const ObjCInterfaceDecl * SuperClassDecl =
855 OID->getClassInterface()->getSuperClass();
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000856 std::string SuperClassName;
Chris Lattner9c445ae2009-06-15 01:09:11 +0000857 if (SuperClassDecl) {
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000858 SuperClassName = SuperClassDecl->getNameAsString();
Chris Lattner9c445ae2009-06-15 01:09:11 +0000859 EmitClassRef(SuperClassName);
860 }
Daniel Dunbarac93e472008-08-15 22:20:32 +0000861
862 // Get the class name
Chris Lattnerb63b5502009-04-01 02:00:48 +0000863 ObjCInterfaceDecl *ClassDecl =
864 const_cast<ObjCInterfaceDecl *>(OID->getClassInterface());
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000865 std::string ClassName = ClassDecl->getNameAsString();
Chris Lattner9c445ae2009-06-15 01:09:11 +0000866 // Emit the symbol that is used to generate linker errors if this class is
867 // referenced in other modules but not declared.
Fariborz Jahanian11890bd2009-07-03 15:10:14 +0000868 std::string classSymbolName = "__objc_class_name_" + ClassName;
869 if (llvm::GlobalVariable *symbol =
870 TheModule.getGlobalVariable(classSymbolName)) {
Owen Anderson73e7f802009-07-14 23:10:40 +0000871 symbol->setInitializer(VMContext.getConstantInt(LongTy, 0));
Fariborz Jahanian11890bd2009-07-03 15:10:14 +0000872 } else {
Owen Anderson94148482009-07-08 19:05:04 +0000873 new llvm::GlobalVariable(TheModule, LongTy, false,
Owen Anderson73e7f802009-07-14 23:10:40 +0000874 llvm::GlobalValue::ExternalLinkage, VMContext.getConstantInt(LongTy, 0),
Owen Anderson94148482009-07-08 19:05:04 +0000875 classSymbolName);
Fariborz Jahanian11890bd2009-07-03 15:10:14 +0000876 }
Chris Lattner9c445ae2009-06-15 01:09:11 +0000877
Daniel Dunbar07ddfa92009-05-03 10:46:44 +0000878 // Get the size of instances.
879 int instanceSize = Context.getASTObjCImplementationLayout(OID).getSize() / 8;
Daniel Dunbarac93e472008-08-15 22:20:32 +0000880
881 // Collect information about instance variables.
882 llvm::SmallVector<llvm::Constant*, 16> IvarNames;
883 llvm::SmallVector<llvm::Constant*, 16> IvarTypes;
884 llvm::SmallVector<llvm::Constant*, 16> IvarOffsets;
Fariborz Jahanianf25adf12009-05-20 18:41:51 +0000885
886 int superInstanceSize = !SuperClassDecl ? 0 :
887 Context.getASTObjCInterfaceLayout(SuperClassDecl).getSize() / 8;
888 // For non-fragile ivars, set the instance size to 0 - {the size of just this
889 // class}. The runtime will then set this to the correct value on load.
890 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
891 instanceSize = 0 - (instanceSize - superInstanceSize);
892 }
Daniel Dunbarac93e472008-08-15 22:20:32 +0000893 for (ObjCInterfaceDecl::ivar_iterator iter = ClassDecl->ivar_begin(),
894 endIter = ClassDecl->ivar_end() ; iter != endIter ; iter++) {
895 // Store the name
Chris Lattner271d4c22008-11-24 05:29:24 +0000896 IvarNames.push_back(CGM.GetAddrOfConstantCString((*iter)
897 ->getNameAsString()));
Daniel Dunbarac93e472008-08-15 22:20:32 +0000898 // Get the type encoding for this ivar
899 std::string TypeStr;
Daniel Dunbarc9197cd2008-10-17 20:21:44 +0000900 Context.getObjCEncodingForType((*iter)->getType(), TypeStr);
Daniel Dunbarac93e472008-08-15 22:20:32 +0000901 IvarTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
902 // Get the offset
Fariborz Jahanianf25adf12009-05-20 18:41:51 +0000903 uint64_t Offset;
904 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
905 Offset = ComputeIvarBaseOffset(CGM, ClassDecl, *iter) -
906 superInstanceSize;
907 ObjCIvarOffsetVariable(ClassDecl, *iter);
908 } else {
909 Offset = ComputeIvarBaseOffset(CGM, ClassDecl, *iter);
910 }
Daniel Dunbarac93e472008-08-15 22:20:32 +0000911 IvarOffsets.push_back(
Owen Anderson73e7f802009-07-14 23:10:40 +0000912 VMContext.getConstantInt(llvm::Type::Int32Ty, Offset));
Daniel Dunbarac93e472008-08-15 22:20:32 +0000913 }
914
915 // Collect information about instance methods
916 llvm::SmallVector<Selector, 16> InstanceMethodSels;
917 llvm::SmallVector<llvm::Constant*, 16> InstanceMethodTypes;
Douglas Gregorcd19b572009-04-23 01:02:12 +0000918 for (ObjCImplementationDecl::instmeth_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000919 iter = OID->instmeth_begin(), endIter = OID->instmeth_end();
Douglas Gregorcd19b572009-04-23 01:02:12 +0000920 iter != endIter ; iter++) {
Daniel Dunbarac93e472008-08-15 22:20:32 +0000921 InstanceMethodSels.push_back((*iter)->getSelector());
922 std::string TypeStr;
923 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
924 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
925 }
Fariborz Jahanianf3ab5642009-05-17 16:49:27 +0000926 for (ObjCImplDecl::propimpl_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000927 iter = OID->propimpl_begin(), endIter = OID->propimpl_end();
Fariborz Jahanianf3ab5642009-05-17 16:49:27 +0000928 iter != endIter ; iter++) {
929 ObjCPropertyDecl *property = (*iter)->getPropertyDecl();
930 if (ObjCMethodDecl *getter = property->getGetterMethodDecl()) {
931 InstanceMethodSels.push_back(getter->getSelector());
932 std::string TypeStr;
933 Context.getObjCEncodingForMethodDecl(getter,TypeStr);
934 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
935 }
936 if (ObjCMethodDecl *setter = property->getSetterMethodDecl()) {
937 InstanceMethodSels.push_back(setter->getSelector());
938 std::string TypeStr;
939 Context.getObjCEncodingForMethodDecl(setter,TypeStr);
940 InstanceMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
941 }
942 }
Daniel Dunbarac93e472008-08-15 22:20:32 +0000943
944 // Collect information about class methods
945 llvm::SmallVector<Selector, 16> ClassMethodSels;
946 llvm::SmallVector<llvm::Constant*, 16> ClassMethodTypes;
Douglas Gregorcd19b572009-04-23 01:02:12 +0000947 for (ObjCImplementationDecl::classmeth_iterator
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000948 iter = OID->classmeth_begin(), endIter = OID->classmeth_end();
Douglas Gregorcd19b572009-04-23 01:02:12 +0000949 iter != endIter ; iter++) {
Daniel Dunbarac93e472008-08-15 22:20:32 +0000950 ClassMethodSels.push_back((*iter)->getSelector());
951 std::string TypeStr;
952 Context.getObjCEncodingForMethodDecl((*iter),TypeStr);
953 ClassMethodTypes.push_back(CGM.GetAddrOfConstantCString(TypeStr));
954 }
955 // Collect the names of referenced protocols
956 llvm::SmallVector<std::string, 16> Protocols;
957 const ObjCList<ObjCProtocolDecl> &Protos =ClassDecl->getReferencedProtocols();
958 for (ObjCList<ObjCProtocolDecl>::iterator I = Protos.begin(),
959 E = Protos.end(); I != E; ++I)
Chris Lattner271d4c22008-11-24 05:29:24 +0000960 Protocols.push_back((*I)->getNameAsString());
Daniel Dunbarac93e472008-08-15 22:20:32 +0000961
962
963
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000964 // Get the superclass pointer.
965 llvm::Constant *SuperClass;
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000966 if (!SuperClassName.empty()) {
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000967 SuperClass = MakeConstantString(SuperClassName, ".super_class_name");
968 } else {
Owen Anderson73e7f802009-07-14 23:10:40 +0000969 SuperClass = VMContext.getConstantPointerNull(PtrToInt8Ty);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000970 }
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000971 // Empty vector used to construct empty method lists
972 llvm::SmallVector<llvm::Constant*, 1> empty;
973 // Generate the method and instance variable lists
974 llvm::Constant *MethodList = GenerateMethodList(ClassName, "",
Chris Lattner578279d2008-06-26 05:08:00 +0000975 InstanceMethodSels, InstanceMethodTypes, false);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000976 llvm::Constant *ClassMethodList = GenerateMethodList(ClassName, "",
Chris Lattner578279d2008-06-26 05:08:00 +0000977 ClassMethodSels, ClassMethodTypes, true);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000978 llvm::Constant *IvarList = GenerateIvarList(IvarNames, IvarTypes,
979 IvarOffsets);
980 //Generate metaclass for class methods
981 llvm::Constant *MetaClassStruct = GenerateClassStructure(NULLPtr,
Chris Lattner4d018542008-07-21 06:31:05 +0000982 NULLPtr, 0x2L, /*name*/"", 0, Zeros[0], GenerateIvarList(
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000983 empty, empty, empty), ClassMethodList, NULLPtr);
Daniel Dunbarc4532f52009-05-04 15:31:17 +0000984
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000985 // Generate the class structure
Chris Lattnerd120b9e2008-11-24 03:54:41 +0000986 llvm::Constant *ClassStruct =
987 GenerateClassStructure(MetaClassStruct, SuperClass, 0x1L,
988 ClassName.c_str(), 0,
Owen Anderson73e7f802009-07-14 23:10:40 +0000989 VMContext.getConstantInt(LongTy, instanceSize), IvarList,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +0000990 MethodList, GenerateProtocolList(Protocols));
Daniel Dunbarc4532f52009-05-04 15:31:17 +0000991
992 // Resolve the class aliases, if they exist.
993 if (ClassPtrAlias) {
994 ClassPtrAlias->setAliasee(
Owen Anderson73e7f802009-07-14 23:10:40 +0000995 VMContext.getConstantExprBitCast(ClassStruct, IdTy));
Daniel Dunbarc4532f52009-05-04 15:31:17 +0000996 ClassPtrAlias = 0;
997 }
998 if (MetaClassPtrAlias) {
999 MetaClassPtrAlias->setAliasee(
Owen Anderson73e7f802009-07-14 23:10:40 +00001000 VMContext.getConstantExprBitCast(MetaClassStruct, IdTy));
Daniel Dunbarc4532f52009-05-04 15:31:17 +00001001 MetaClassPtrAlias = 0;
1002 }
1003
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001004 // Add class structure to list to be added to the symtab later
Owen Anderson73e7f802009-07-14 23:10:40 +00001005 ClassStruct = VMContext.getConstantExprBitCast(ClassStruct, PtrToInt8Ty);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001006 Classes.push_back(ClassStruct);
1007}
1008
Fariborz Jahanian3580ee12009-06-23 21:47:46 +00001009
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001010llvm::Function *CGObjCGNU::ModuleInitFunction() {
1011 // Only emit an ObjC load function if no Objective-C stuff has been called
1012 if (Classes.empty() && Categories.empty() && ConstantStrings.empty() &&
1013 ExistingProtocols.empty() && TypedSelectors.empty() &&
Anton Korobeynikovb41e4972008-06-01 15:14:46 +00001014 UntypedSelectors.empty())
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001015 return NULL;
Eli Friedman7719bc82008-06-01 16:00:02 +00001016
Chris Lattner431dc382009-01-27 05:06:01 +00001017 const llvm::StructType *SelStructTy = dyn_cast<llvm::StructType>(
1018 SelectorTy->getElementType());
1019 const llvm::Type *SelStructPtrTy = SelectorTy;
1020 bool isSelOpaque = false;
1021 if (SelStructTy == 0) {
Owen Anderson73e7f802009-07-14 23:10:40 +00001022 SelStructTy = VMContext.getStructType(PtrToInt8Ty, PtrToInt8Ty, NULL);
1023 SelStructPtrTy = VMContext.getPointerTypeUnqual(SelStructTy);
Chris Lattner431dc382009-01-27 05:06:01 +00001024 isSelOpaque = true;
1025 }
1026
Eli Friedman7719bc82008-06-01 16:00:02 +00001027 // Name the ObjC types to make the IR a bit easier to read
Chris Lattner431dc382009-01-27 05:06:01 +00001028 TheModule.addTypeName(".objc_selector", SelStructPtrTy);
Eli Friedman7719bc82008-06-01 16:00:02 +00001029 TheModule.addTypeName(".objc_id", IdTy);
1030 TheModule.addTypeName(".objc_imp", IMPTy);
1031
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001032 std::vector<llvm::Constant*> Elements;
Chris Lattner7eaa9f02009-04-25 23:19:45 +00001033 llvm::Constant *Statics = NULLPtr;
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001034 // Generate statics list:
Chris Lattner7eaa9f02009-04-25 23:19:45 +00001035 if (ConstantStrings.size()) {
Owen Anderson73e7f802009-07-14 23:10:40 +00001036 llvm::ArrayType *StaticsArrayTy = VMContext.getArrayType(PtrToInt8Ty,
Chris Lattner7eaa9f02009-04-25 23:19:45 +00001037 ConstantStrings.size() + 1);
1038 ConstantStrings.push_back(NULLPtr);
1039 Elements.push_back(MakeConstantString("NSConstantString",
1040 ".objc_static_class_name"));
Owen Anderson73e7f802009-07-14 23:10:40 +00001041 Elements.push_back(VMContext.getConstantArray(StaticsArrayTy,
Chris Lattner7eaa9f02009-04-25 23:19:45 +00001042 ConstantStrings));
1043 llvm::StructType *StaticsListTy =
Owen Anderson73e7f802009-07-14 23:10:40 +00001044 VMContext.getStructType(PtrToInt8Ty, StaticsArrayTy, NULL);
1045 llvm::Type *StaticsListPtrTy =
1046 VMContext.getPointerTypeUnqual(StaticsListTy);
Chris Lattner7eaa9f02009-04-25 23:19:45 +00001047 Statics = MakeGlobal(StaticsListTy, Elements, ".objc_statics");
Owen Anderson73e7f802009-07-14 23:10:40 +00001048 llvm::ArrayType *StaticsListArrayTy =
1049 VMContext.getArrayType(StaticsListPtrTy, 2);
Chris Lattner7eaa9f02009-04-25 23:19:45 +00001050 Elements.clear();
1051 Elements.push_back(Statics);
Owen Anderson73e7f802009-07-14 23:10:40 +00001052 Elements.push_back(VMContext.getNullValue(StaticsListPtrTy));
Chris Lattner7eaa9f02009-04-25 23:19:45 +00001053 Statics = MakeGlobal(StaticsListArrayTy, Elements, ".objc_statics_ptr");
Owen Anderson73e7f802009-07-14 23:10:40 +00001054 Statics = VMContext.getConstantExprBitCast(Statics, PtrTy);
Chris Lattner7eaa9f02009-04-25 23:19:45 +00001055 }
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001056 // Array of classes, categories, and constant objects
Owen Anderson73e7f802009-07-14 23:10:40 +00001057 llvm::ArrayType *ClassListTy = VMContext.getArrayType(PtrToInt8Ty,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001058 Classes.size() + Categories.size() + 2);
Owen Anderson73e7f802009-07-14 23:10:40 +00001059 llvm::StructType *SymTabTy = VMContext.getStructType(LongTy, SelStructPtrTy,
Chris Lattneref843042008-06-26 04:10:42 +00001060 llvm::Type::Int16Ty,
1061 llvm::Type::Int16Ty,
1062 ClassListTy, NULL);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001063
1064 Elements.clear();
1065 // Pointer to an array of selectors used in this module.
1066 std::vector<llvm::Constant*> Selectors;
1067 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
1068 iter = TypedSelectors.begin(), iterEnd = TypedSelectors.end();
1069 iter != iterEnd ; ++iter) {
Chris Lattneref843042008-06-26 04:10:42 +00001070 Elements.push_back(MakeConstantString(iter->first.first, ".objc_sel_name"));
1071 Elements.push_back(MakeConstantString(iter->first.second,
1072 ".objc_sel_types"));
Owen Anderson73e7f802009-07-14 23:10:40 +00001073 Selectors.push_back(VMContext.getConstantStruct(SelStructTy, Elements));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001074 Elements.clear();
1075 }
1076 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
1077 iter = UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
Chris Lattneref843042008-06-26 04:10:42 +00001078 iter != iterEnd; ++iter) {
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001079 Elements.push_back(
Chris Lattneref843042008-06-26 04:10:42 +00001080 MakeConstantString(iter->getKeyData(), ".objc_sel_name"));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001081 Elements.push_back(NULLPtr);
Owen Anderson73e7f802009-07-14 23:10:40 +00001082 Selectors.push_back(VMContext.getConstantStruct(SelStructTy, Elements));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001083 Elements.clear();
1084 }
1085 Elements.push_back(NULLPtr);
1086 Elements.push_back(NULLPtr);
Owen Anderson73e7f802009-07-14 23:10:40 +00001087 Selectors.push_back(VMContext.getConstantStruct(SelStructTy, Elements));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001088 Elements.clear();
1089 // Number of static selectors
Owen Anderson73e7f802009-07-14 23:10:40 +00001090 Elements.push_back(VMContext.getConstantInt(LongTy, Selectors.size() ));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001091 llvm::Constant *SelectorList = MakeGlobal(
Owen Anderson73e7f802009-07-14 23:10:40 +00001092 VMContext.getArrayType(SelStructTy, Selectors.size()), Selectors,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001093 ".objc_selector_list");
Owen Anderson73e7f802009-07-14 23:10:40 +00001094 Elements.push_back(VMContext.getConstantExprBitCast(SelectorList,
Chris Lattner431dc382009-01-27 05:06:01 +00001095 SelStructPtrTy));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001096
1097 // Now that all of the static selectors exist, create pointers to them.
1098 int index = 0;
1099 for (std::map<TypedSelector, llvm::GlobalAlias*>::iterator
1100 iter=TypedSelectors.begin(), iterEnd =TypedSelectors.end();
1101 iter != iterEnd; ++iter) {
1102 llvm::Constant *Idxs[] = {Zeros[0],
Owen Anderson73e7f802009-07-14 23:10:40 +00001103 VMContext.getConstantInt(llvm::Type::Int32Ty, index++), Zeros[0]};
Owen Anderson94148482009-07-08 19:05:04 +00001104 llvm::Constant *SelPtr = new llvm::GlobalVariable(TheModule, SelStructPtrTy,
1105 true, llvm::GlobalValue::InternalLinkage,
Owen Anderson73e7f802009-07-14 23:10:40 +00001106 VMContext.getConstantExprGetElementPtr(SelectorList, Idxs, 2),
Owen Anderson94148482009-07-08 19:05:04 +00001107 ".objc_sel_ptr");
Chris Lattner431dc382009-01-27 05:06:01 +00001108 // If selectors are defined as an opaque type, cast the pointer to this
1109 // type.
1110 if (isSelOpaque) {
Owen Anderson73e7f802009-07-14 23:10:40 +00001111 SelPtr = VMContext.getConstantExprBitCast(SelPtr,
1112 VMContext.getPointerTypeUnqual(SelectorTy));
Chris Lattner431dc382009-01-27 05:06:01 +00001113 }
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001114 (*iter).second->setAliasee(SelPtr);
1115 }
1116 for (llvm::StringMap<llvm::GlobalAlias*>::iterator
1117 iter=UntypedSelectors.begin(), iterEnd = UntypedSelectors.end();
1118 iter != iterEnd; iter++) {
1119 llvm::Constant *Idxs[] = {Zeros[0],
Owen Anderson73e7f802009-07-14 23:10:40 +00001120 VMContext.getConstantInt(llvm::Type::Int32Ty, index++), Zeros[0]};
Owen Anderson94148482009-07-08 19:05:04 +00001121 llvm::Constant *SelPtr = new llvm::GlobalVariable(TheModule, SelStructPtrTy,
1122 true, llvm::GlobalValue::InternalLinkage,
Owen Anderson73e7f802009-07-14 23:10:40 +00001123 VMContext.getConstantExprGetElementPtr(SelectorList, Idxs, 2),
Owen Anderson94148482009-07-08 19:05:04 +00001124 ".objc_sel_ptr");
Chris Lattner431dc382009-01-27 05:06:01 +00001125 // If selectors are defined as an opaque type, cast the pointer to this
1126 // type.
1127 if (isSelOpaque) {
Owen Anderson73e7f802009-07-14 23:10:40 +00001128 SelPtr = VMContext.getConstantExprBitCast(SelPtr,
1129 VMContext.getPointerTypeUnqual(SelectorTy));
Chris Lattner431dc382009-01-27 05:06:01 +00001130 }
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001131 (*iter).second->setAliasee(SelPtr);
1132 }
1133 // Number of classes defined.
Owen Anderson73e7f802009-07-14 23:10:40 +00001134 Elements.push_back(VMContext.getConstantInt(llvm::Type::Int16Ty,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001135 Classes.size()));
1136 // Number of categories defined
Owen Anderson73e7f802009-07-14 23:10:40 +00001137 Elements.push_back(VMContext.getConstantInt(llvm::Type::Int16Ty,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001138 Categories.size()));
1139 // Create an array of classes, then categories, then static object instances
1140 Classes.insert(Classes.end(), Categories.begin(), Categories.end());
1141 // NULL-terminated list of static object instances (mainly constant strings)
1142 Classes.push_back(Statics);
1143 Classes.push_back(NULLPtr);
Owen Anderson73e7f802009-07-14 23:10:40 +00001144 llvm::Constant *ClassList = VMContext.getConstantArray(ClassListTy, Classes);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001145 Elements.push_back(ClassList);
1146 // Construct the symbol table
1147 llvm::Constant *SymTab= MakeGlobal(SymTabTy, Elements);
1148
1149 // The symbol table is contained in a module which has some version-checking
1150 // constants
Owen Anderson73e7f802009-07-14 23:10:40 +00001151 llvm::StructType * ModuleTy = VMContext.getStructType(LongTy, LongTy,
1152 PtrToInt8Ty, VMContext.getPointerTypeUnqual(SymTabTy), NULL);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001153 Elements.clear();
1154 // Runtime version used for compatibility checking.
Fariborz Jahanianf25adf12009-05-20 18:41:51 +00001155 if (CGM.getContext().getLangOptions().ObjCNonFragileABI) {
Owen Anderson73e7f802009-07-14 23:10:40 +00001156 Elements.push_back(VMContext.getConstantInt(LongTy,
Fariborz Jahanianf25adf12009-05-20 18:41:51 +00001157 NonFragileRuntimeVersion));
1158 } else {
Owen Anderson73e7f802009-07-14 23:10:40 +00001159 Elements.push_back(VMContext.getConstantInt(LongTy, RuntimeVersion));
Fariborz Jahanianf25adf12009-05-20 18:41:51 +00001160 }
Fariborz Jahaniandefa06a2009-04-01 19:49:42 +00001161 // sizeof(ModuleTy)
1162 llvm::TargetData td = llvm::TargetData::TargetData(&TheModule);
Owen Anderson73e7f802009-07-14 23:10:40 +00001163 Elements.push_back(VMContext.getConstantInt(LongTy,
1164 td.getTypeSizeInBits(ModuleTy)/8));
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001165 //FIXME: Should be the path to the file where this module was declared
1166 Elements.push_back(NULLPtr);
1167 Elements.push_back(SymTab);
1168 llvm::Value *Module = MakeGlobal(ModuleTy, Elements);
1169
1170 // Create the load function calling the runtime entry point with the module
1171 // structure
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001172 llvm::Function * LoadFunction = llvm::Function::Create(
Owen Anderson73e7f802009-07-14 23:10:40 +00001173 VMContext.getFunctionType(llvm::Type::VoidTy, false),
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001174 llvm::GlobalValue::InternalLinkage, ".objc_load_function",
1175 &TheModule);
1176 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", LoadFunction);
Owen Anderson73e7f802009-07-14 23:10:40 +00001177 CGBuilderTy Builder(VMContext);
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001178 Builder.SetInsertPoint(EntryBB);
Fariborz Jahanian6158e692009-03-30 18:02:14 +00001179
1180 std::vector<const llvm::Type*> Params(1,
Owen Anderson73e7f802009-07-14 23:10:40 +00001181 VMContext.getPointerTypeUnqual(ModuleTy));
1182 llvm::Value *Register = CGM.CreateRuntimeFunction(VMContext.getFunctionType(
Fariborz Jahanian6158e692009-03-30 18:02:14 +00001183 llvm::Type::VoidTy, Params, true), "__objc_exec_class");
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001184 Builder.CreateCall(Register, Module);
1185 Builder.CreateRetVoid();
Fariborz Jahanianf25adf12009-05-20 18:41:51 +00001186
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001187 return LoadFunction;
1188}
Daniel Dunbarac93e472008-08-15 22:20:32 +00001189
Fariborz Jahanian0adaa8a2009-01-10 21:06:09 +00001190llvm::Function *CGObjCGNU::GenerateMethod(const ObjCMethodDecl *OMD,
1191 const ObjCContainerDecl *CD) {
Daniel Dunbarac93e472008-08-15 22:20:32 +00001192 const ObjCCategoryImplDecl *OCD =
Steve Naroff438be772009-01-08 19:41:02 +00001193 dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext());
Chris Lattner3a8f2942008-11-24 03:33:13 +00001194 std::string CategoryName = OCD ? OCD->getNameAsString() : "";
1195 std::string ClassName = OMD->getClassInterface()->getNameAsString();
1196 std::string MethodName = OMD->getSelector().getAsString();
Douglas Gregor5d764842009-01-09 17:18:27 +00001197 bool isClassMethod = !OMD->isInstanceMethod();
Daniel Dunbarac93e472008-08-15 22:20:32 +00001198
Daniel Dunbar34bda882009-02-02 23:23:47 +00001199 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar3ad1f072008-09-10 04:01:49 +00001200 const llvm::FunctionType *MethodTy =
Daniel Dunbar34bda882009-02-02 23:23:47 +00001201 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001202 std::string FunctionName = SymbolNameForMethod(ClassName, CategoryName,
1203 MethodName, isClassMethod);
1204
Gabor Greif815e2c12008-04-06 20:42:52 +00001205 llvm::Function *Method = llvm::Function::Create(MethodTy,
Chris Lattnerb326b172008-03-30 23:03:07 +00001206 llvm::GlobalValue::InternalLinkage,
Anton Korobeynikovcd5d08d2008-06-01 14:13:53 +00001207 FunctionName,
Chris Lattnerb326b172008-03-30 23:03:07 +00001208 &TheModule);
Chris Lattnerb326b172008-03-30 23:03:07 +00001209 return Method;
1210}
1211
Daniel Dunbarf7103722008-09-24 03:38:44 +00001212llvm::Function *CGObjCGNU::GetPropertyGetFunction() {
Fariborz Jahaniande629b62009-05-19 00:28:43 +00001213 std::vector<const llvm::Type*> Params;
1214 const llvm::Type *BoolTy =
1215 CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
1216 Params.push_back(IdTy);
1217 Params.push_back(SelectorTy);
1218 // FIXME: Using LongTy for ptrdiff_t is probably broken on Win64
1219 Params.push_back(LongTy);
1220 Params.push_back(BoolTy);
1221 // void objc_getProperty (id, SEL, ptrdiff_t, bool)
1222 const llvm::FunctionType *FTy =
Owen Anderson73e7f802009-07-14 23:10:40 +00001223 VMContext.getFunctionType(IdTy, Params, false);
Fariborz Jahaniande629b62009-05-19 00:28:43 +00001224 return cast<llvm::Function>(CGM.CreateRuntimeFunction(FTy,
1225 "objc_getProperty"));
Daniel Dunbarf7103722008-09-24 03:38:44 +00001226}
1227
1228llvm::Function *CGObjCGNU::GetPropertySetFunction() {
Fariborz Jahaniande629b62009-05-19 00:28:43 +00001229 std::vector<const llvm::Type*> Params;
1230 const llvm::Type *BoolTy =
1231 CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
1232 Params.push_back(IdTy);
1233 Params.push_back(SelectorTy);
1234 // FIXME: Using LongTy for ptrdiff_t is probably broken on Win64
1235 Params.push_back(LongTy);
1236 Params.push_back(IdTy);
1237 Params.push_back(BoolTy);
1238 Params.push_back(BoolTy);
1239 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
1240 const llvm::FunctionType *FTy =
Owen Anderson73e7f802009-07-14 23:10:40 +00001241 VMContext.getFunctionType(llvm::Type::VoidTy, Params, false);
Fariborz Jahaniande629b62009-05-19 00:28:43 +00001242 return cast<llvm::Function>(CGM.CreateRuntimeFunction(FTy,
1243 "objc_setProperty"));
Daniel Dunbarf7103722008-09-24 03:38:44 +00001244}
1245
1246llvm::Function *CGObjCGNU::EnumerationMutationFunction() {
Fariborz Jahanian6158e692009-03-30 18:02:14 +00001247 std::vector<const llvm::Type*> Params(1, IdTy);
1248 return cast<llvm::Function>(CGM.CreateRuntimeFunction(
Owen Anderson73e7f802009-07-14 23:10:40 +00001249 VMContext.getFunctionType(llvm::Type::VoidTy, Params, true),
Fariborz Jahanian6158e692009-03-30 18:02:14 +00001250 "objc_enumerationMutation"));
Anders Carlsson58d16242008-08-31 04:05:03 +00001251}
1252
Fariborz Jahanianfbeda7b2008-11-21 00:49:24 +00001253void CGObjCGNU::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1254 const Stmt &S) {
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001255 // Pointer to the personality function
1256 llvm::Constant *Personality =
Owen Anderson73e7f802009-07-14 23:10:40 +00001257 CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::Int32Ty,
Chris Lattner89a7b0a2009-07-01 04:13:52 +00001258 true),
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001259 "__gnu_objc_personality_v0");
Owen Anderson73e7f802009-07-14 23:10:40 +00001260 Personality = VMContext.getConstantExprBitCast(Personality, PtrTy);
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001261 std::vector<const llvm::Type*> Params;
1262 Params.push_back(PtrTy);
1263 llvm::Value *RethrowFn =
Owen Anderson73e7f802009-07-14 23:10:40 +00001264 CGM.CreateRuntimeFunction(VMContext.getFunctionType(llvm::Type::VoidTy,
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001265 Params, false), "_Unwind_Resume_or_Rethrow");
1266
1267 bool isTry = isa<ObjCAtTryStmt>(S);
1268 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
1269 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
1270 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Chris Lattner5b56c192009-05-11 18:16:28 +00001271 llvm::BasicBlock *CatchInCatch = CGF.createBasicBlock("catch.rethrow");
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001272 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
1273 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
1274 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
1275
1276 // GNU runtime does not currently support @synchronized()
1277 if (!isTry) {
Chris Lattner5b56c192009-05-11 18:16:28 +00001278 std::vector<const llvm::Type*> Args(1, IdTy);
1279 llvm::FunctionType *FTy =
Owen Anderson73e7f802009-07-14 23:10:40 +00001280 VMContext.getFunctionType(llvm::Type::VoidTy, Args, false);
Chris Lattner5b56c192009-05-11 18:16:28 +00001281 llvm::Value *SyncEnter = CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
1282 llvm::Value *SyncArg =
1283 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1284 SyncArg = CGF.Builder.CreateBitCast(SyncArg, IdTy);
1285 CGF.Builder.CreateCall(SyncEnter, SyncArg);
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001286 }
1287
Chris Lattner5b56c192009-05-11 18:16:28 +00001288
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001289 // Push an EH context entry, used for handling rethrows and jumps
1290 // through finally.
1291 CGF.PushCleanupBlock(FinallyBlock);
1292
1293 // Emit the statements in the @try {} block
1294 CGF.setInvokeDest(TryHandler);
1295
1296 CGF.EmitBlock(TryBlock);
1297 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
1298 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
1299
1300 // Jump to @finally if there is no exception
1301 CGF.EmitBranchThroughCleanup(FinallyEnd);
1302
1303 // Emit the handlers
1304 CGF.EmitBlock(TryHandler);
1305
Chris Lattner13395b22009-05-08 17:36:08 +00001306 // Get the correct versions of the exception handling intrinsics
1307 llvm::TargetData td = llvm::TargetData::TargetData(&TheModule);
1308 int PointerWidth = td.getTypeSizeInBits(PtrTy);
1309 assert((PointerWidth == 32 || PointerWidth == 64) &&
1310 "Can't yet handle exceptions if pointers are not 32 or 64 bits");
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001311 llvm::Value *llvm_eh_exception =
1312 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
Chris Lattner13395b22009-05-08 17:36:08 +00001313 llvm::Value *llvm_eh_selector = PointerWidth == 32 ?
1314 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i32) :
1315 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
1316 llvm::Value *llvm_eh_typeid_for = PointerWidth == 32 ?
1317 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i32) :
1318 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001319
1320 // Exception object
1321 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
1322 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
1323
1324 llvm::SmallVector<llvm::Value*, 8> ESelArgs;
1325 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
1326
1327 ESelArgs.push_back(Exc);
1328 ESelArgs.push_back(Personality);
1329
1330 bool HasCatchAll = false;
1331 // Only @try blocks are allowed @catch blocks, but both can have @finally
1332 if (isTry) {
1333 if (const ObjCAtCatchStmt* CatchStmt =
1334 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
Chris Lattner5b56c192009-05-11 18:16:28 +00001335 CGF.setInvokeDest(CatchInCatch);
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001336
1337 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
1338 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
1339 Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
1340
1341 // @catch() and @catch(id) both catch any ObjC exception
Steve Naroff329ec222009-07-10 23:34:53 +00001342 if (!CatchDecl || CatchDecl->getType()->isObjCIdType()
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001343 || CatchDecl->getType()->isObjCQualifiedIdType()) {
1344 // Use i8* null here to signal this is a catch all, not a cleanup.
1345 ESelArgs.push_back(NULLPtr);
1346 HasCatchAll = true;
1347 // No further catches after this one will ever by reached
1348 break;
1349 }
1350
1351 // All other types should be Objective-C interface pointer types.
Steve Naroff329ec222009-07-10 23:34:53 +00001352 const ObjCObjectPointerType *OPT =
1353 CatchDecl->getType()->getAsObjCObjectPointerType();
1354 assert(OPT && "Invalid @catch type.");
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001355 const ObjCInterfaceType *IT =
Steve Naroff329ec222009-07-10 23:34:53 +00001356 OPT->getPointeeType()->getAsObjCInterfaceType();
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001357 assert(IT && "Invalid @catch type.");
Chris Lattner13395b22009-05-08 17:36:08 +00001358 llvm::Value *EHType =
1359 MakeConstantString(IT->getDecl()->getNameAsString());
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001360 ESelArgs.push_back(EHType);
1361 }
1362 }
1363 }
1364
1365 // We use a cleanup unless there was already a catch all.
1366 if (!HasCatchAll) {
Owen Anderson73e7f802009-07-14 23:10:40 +00001367 ESelArgs.push_back(VMContext.getConstantInt(llvm::Type::Int32Ty, 0));
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001368 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
1369 }
1370
1371 // Find which handler was matched.
Chris Lattner13395b22009-05-08 17:36:08 +00001372 llvm::Value *ESelector = CGF.Builder.CreateCall(llvm_eh_selector,
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001373 ESelArgs.begin(), ESelArgs.end(), "selector");
1374
1375 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
1376 const ParmVarDecl *CatchParam = Handlers[i].first;
1377 const Stmt *CatchBody = Handlers[i].second;
1378
1379 llvm::BasicBlock *Next = 0;
1380
1381 // The last handler always matches.
1382 if (i + 1 != e) {
1383 assert(CatchParam && "Only last handler can be a catch all.");
1384
1385 // Test whether this block matches the type for the selector and branch
1386 // to Match if it does, or to the next BB if it doesn't.
1387 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
1388 Next = CGF.createBasicBlock("catch.next");
Chris Lattner13395b22009-05-08 17:36:08 +00001389 llvm::Value *Id = CGF.Builder.CreateCall(llvm_eh_typeid_for,
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001390 CGF.Builder.CreateBitCast(ESelArgs[i+2], PtrTy));
1391 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(ESelector, Id), Match,
1392 Next);
1393
1394 CGF.EmitBlock(Match);
1395 }
1396
1397 if (CatchBody) {
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001398 llvm::Value *ExcObject = CGF.Builder.CreateBitCast(Exc,
1399 CGF.ConvertType(CatchParam->getType()));
1400
1401 // Bind the catch parameter if it exists.
1402 if (CatchParam) {
1403 // CatchParam is a ParmVarDecl because of the grammar
1404 // construction used to handle this, but for codegen purposes
1405 // we treat this as a local decl.
1406 CGF.EmitLocalBlockVarDecl(*CatchParam);
1407 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
1408 }
1409
1410 CGF.ObjCEHValueStack.push_back(ExcObject);
1411 CGF.EmitStmt(CatchBody);
1412 CGF.ObjCEHValueStack.pop_back();
1413
1414 CGF.EmitBranchThroughCleanup(FinallyEnd);
1415
1416 if (Next)
1417 CGF.EmitBlock(Next);
1418 } else {
1419 assert(!Next && "catchup should be last handler.");
1420
1421 CGF.Builder.CreateStore(Exc, RethrowPtr);
1422 CGF.EmitBranchThroughCleanup(FinallyRethrow);
1423 }
1424 }
Chris Lattner5b56c192009-05-11 18:16:28 +00001425 // The @finally block is a secondary landing pad for any exceptions thrown in
1426 // @catch() blocks
1427 CGF.EmitBlock(CatchInCatch);
1428 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
1429 ESelArgs.clear();
1430 ESelArgs.push_back(Exc);
1431 ESelArgs.push_back(Personality);
Owen Anderson73e7f802009-07-14 23:10:40 +00001432 ESelArgs.push_back(VMContext.getConstantInt(llvm::Type::Int32Ty, 0));
Chris Lattner5b56c192009-05-11 18:16:28 +00001433 CGF.Builder.CreateCall(llvm_eh_selector, ESelArgs.begin(), ESelArgs.end(),
1434 "selector");
1435 CGF.Builder.CreateCall(llvm_eh_typeid_for,
1436 CGF.Builder.CreateIntToPtr(ESelArgs[2], PtrTy));
1437 CGF.Builder.CreateStore(Exc, RethrowPtr);
1438 CGF.EmitBranchThroughCleanup(FinallyRethrow);
1439
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001440 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
1441
1442 CGF.setInvokeDest(PrevLandingPad);
1443
1444 CGF.EmitBlock(FinallyBlock);
1445
Chris Lattner5b56c192009-05-11 18:16:28 +00001446
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001447 if (isTry) {
1448 if (const ObjCAtFinallyStmt* FinallyStmt =
1449 cast<ObjCAtTryStmt>(S).getFinallyStmt())
1450 CGF.EmitStmt(FinallyStmt->getFinallyBody());
1451 } else {
Chris Lattner5b56c192009-05-11 18:16:28 +00001452 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001453 // @synchronized.
Chris Lattner5b56c192009-05-11 18:16:28 +00001454 std::vector<const llvm::Type*> Args(1, IdTy);
1455 llvm::FunctionType *FTy =
Owen Anderson73e7f802009-07-14 23:10:40 +00001456 VMContext.getFunctionType(llvm::Type::VoidTy, Args, false);
Chris Lattner5b56c192009-05-11 18:16:28 +00001457 llvm::Value *SyncExit = CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
1458 llvm::Value *SyncArg =
1459 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
1460 SyncArg = CGF.Builder.CreateBitCast(SyncArg, IdTy);
1461 CGF.Builder.CreateCall(SyncExit, SyncArg);
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001462 }
1463
1464 if (Info.SwitchBlock)
1465 CGF.EmitBlock(Info.SwitchBlock);
1466 if (Info.EndBlock)
1467 CGF.EmitBlock(Info.EndBlock);
1468
1469 // Branch around the rethrow code.
1470 CGF.EmitBranch(FinallyEnd);
1471
1472 CGF.EmitBlock(FinallyRethrow);
1473 CGF.Builder.CreateCall(RethrowFn, CGF.Builder.CreateLoad(RethrowPtr));
1474 CGF.Builder.CreateUnreachable();
1475
1476 CGF.EmitBlock(FinallyEnd);
1477
Anders Carlssonb01a2112008-09-09 10:04:29 +00001478}
1479
1480void CGObjCGNU::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbarf7103722008-09-24 03:38:44 +00001481 const ObjCAtThrowStmt &S) {
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001482 llvm::Value *ExceptionAsObject;
1483
1484 std::vector<const llvm::Type*> Args(1, IdTy);
1485 llvm::FunctionType *FTy =
Owen Anderson73e7f802009-07-14 23:10:40 +00001486 VMContext.getFunctionType(llvm::Type::VoidTy, Args, false);
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001487 llvm::Value *ThrowFn =
1488 CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
1489
1490 if (const Expr *ThrowExpr = S.getThrowExpr()) {
1491 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Fariborz Jahanianf3ab5642009-05-17 16:49:27 +00001492 ExceptionAsObject = Exception;
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001493 } else {
1494 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
1495 "Unexpected rethrow outside @catch block.");
1496 ExceptionAsObject = CGF.ObjCEHValueStack.back();
1497 }
Fariborz Jahanianf3ab5642009-05-17 16:49:27 +00001498 ExceptionAsObject =
1499 CGF.Builder.CreateBitCast(ExceptionAsObject, IdTy, "tmp");
Chris Lattnerfee3eb02009-05-08 00:11:50 +00001500
1501 // Note: This may have to be an invoke, if we want to support constructs like:
1502 // @try {
1503 // @throw(obj);
1504 // }
1505 // @catch(id) ...
1506 //
1507 // This is effectively turning @throw into an incredibly-expensive goto, but
1508 // it may happen as a result of inlining followed by missed optimizations, or
1509 // as a result of stupidity.
1510 llvm::BasicBlock *UnwindBB = CGF.getInvokeDest();
1511 if (!UnwindBB) {
1512 CGF.Builder.CreateCall(ThrowFn, ExceptionAsObject);
1513 CGF.Builder.CreateUnreachable();
1514 } else {
1515 CGF.Builder.CreateInvoke(ThrowFn, UnwindBB, UnwindBB, &ExceptionAsObject,
1516 &ExceptionAsObject+1);
1517 }
1518 // Clear the insertion point to indicate we are in unreachable code.
1519 CGF.Builder.ClearInsertionPoint();
Anders Carlssonb01a2112008-09-09 10:04:29 +00001520}
1521
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00001522llvm::Value * CGObjCGNU::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian3305ad32008-11-18 21:45:40 +00001523 llvm::Value *AddrWeakObj)
1524{
1525 return 0;
1526}
1527
Fariborz Jahanian252d87f2008-11-18 22:37:34 +00001528void CGObjCGNU::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1529 llvm::Value *src, llvm::Value *dst)
1530{
1531 return;
1532}
1533
Fariborz Jahanian17958902008-11-19 00:59:10 +00001534void CGObjCGNU::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1535 llvm::Value *src, llvm::Value *dst)
1536{
1537 return;
1538}
1539
Fariborz Jahanianf310b592008-11-20 19:23:36 +00001540void CGObjCGNU::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1541 llvm::Value *src, llvm::Value *dst)
1542{
1543 return;
1544}
1545
Fariborz Jahanian17958902008-11-19 00:59:10 +00001546void CGObjCGNU::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1547 llvm::Value *src, llvm::Value *dst)
1548{
1549 return;
1550}
1551
Fariborz Jahanian614e8f02009-07-08 01:18:33 +00001552void CGObjCGNU::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1553 llvm::Value *DestPtr,
1554 llvm::Value *SrcPtr,
1555 unsigned long size) {
1556 return;
1557}
1558
Fariborz Jahanianf25adf12009-05-20 18:41:51 +00001559llvm::GlobalVariable *CGObjCGNU::ObjCIvarOffsetVariable(
1560 const ObjCInterfaceDecl *ID,
1561 const ObjCIvarDecl *Ivar) {
1562 const std::string Name = "__objc_ivar_offset_" + ID->getNameAsString()
1563 + '.' + Ivar->getNameAsString();
1564 // Emit the variable and initialize it with what we think the correct value
1565 // is. This allows code compiled with non-fragile ivars to work correctly
1566 // when linked against code which isn't (most of the time).
1567 llvm::GlobalVariable *IvarOffsetGV = CGM.getModule().getGlobalVariable(Name);
1568 if (!IvarOffsetGV) {
1569 uint64_t Offset = ComputeIvarBaseOffset(CGM, ID, Ivar);
1570 llvm::ConstantInt *OffsetGuess =
Owen Anderson73e7f802009-07-14 23:10:40 +00001571 VMContext.getConstantInt(LongTy, Offset, "ivar");
Owen Anderson94148482009-07-08 19:05:04 +00001572 IvarOffsetGV = new llvm::GlobalVariable(TheModule, LongTy, false,
1573 llvm::GlobalValue::CommonLinkage, OffsetGuess, Name);
Fariborz Jahanianf25adf12009-05-20 18:41:51 +00001574 }
1575 return IvarOffsetGV;
1576}
1577
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00001578LValue CGObjCGNU::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1579 QualType ObjectTy,
1580 llvm::Value *BaseValue,
1581 const ObjCIvarDecl *Ivar,
Fariborz Jahanianc912eb72009-02-03 19:03:09 +00001582 unsigned CVRQualifiers) {
Daniel Dunbarf5254bd2009-04-21 01:19:28 +00001583 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar85d37542009-04-22 07:32:20 +00001584 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
1585 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian4337afe2009-02-02 20:02:29 +00001586}
Fariborz Jahanianf25adf12009-05-20 18:41:51 +00001587static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context,
1588 const ObjCInterfaceDecl *OID,
1589 const ObjCIvarDecl *OIVD) {
Fariborz Jahanianf25adf12009-05-20 18:41:51 +00001590 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Fariborz Jahanianb290be02009-06-04 01:19:09 +00001591 Context.ShallowCollectObjCIvars(OID, Ivars);
Fariborz Jahanianf25adf12009-05-20 18:41:51 +00001592 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
1593 if (OIVD == Ivars[k])
1594 return OID;
1595 }
1596
1597 // Otherwise check in the super class.
1598 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
1599 return FindIvarInterface(Context, Super, OIVD);
1600
1601 return 0;
1602}
Fariborz Jahanian4337afe2009-02-02 20:02:29 +00001603
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001604llvm::Value *CGObjCGNU::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar61e14a62009-04-22 05:08:15 +00001605 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001606 const ObjCIvarDecl *Ivar) {
Fariborz Jahanianf25adf12009-05-20 18:41:51 +00001607 if (CGF.getContext().getLangOptions().ObjCNonFragileABI)
1608 {
1609 Interface = FindIvarInterface(CGM.getContext(), Interface, Ivar);
1610 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),
1611 false, "ivar");
1612 }
Daniel Dunbar85d37542009-04-22 07:32:20 +00001613 uint64_t Offset = ComputeIvarBaseOffset(CGF.CGM, Interface, Ivar);
Owen Anderson73e7f802009-07-14 23:10:40 +00001614 return VMContext.getConstantInt(LongTy, Offset, "ivar");
Fariborz Jahanian27cc6662009-02-10 19:02:04 +00001615}
1616
Daniel Dunbar8c85fac2008-08-11 02:45:11 +00001617CodeGen::CGObjCRuntime *CodeGen::CreateGNUObjCRuntime(CodeGen::CodeGenModule &CGM){
Chris Lattner547907c2008-06-26 04:19:03 +00001618 return new CGObjCGNU(CGM);
Chris Lattnera0fd5ee2008-03-01 08:50:34 +00001619}