blob: e9d20640b7829298d6fca484e0957bcd81415f34 [file] [log] [blame]
Anders Carlsson55085182007-08-21 17:43:55 +00001//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Anders Carlsson55085182007-08-21 17:43:55 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Objective-C code as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenek2979ec72008-04-09 15:51:31 +000014#include "CGObjCRuntime.h"
Anders Carlsson55085182007-08-21 17:43:55 +000015#include "CodeGenFunction.h"
16#include "CodeGenModule.h"
Steve Narofff494b572008-05-29 21:12:08 +000017#include "clang/AST/ExprObjC.h"
Anders Carlsson55085182007-08-21 17:43:55 +000018#include "llvm/Constant.h"
Chris Lattner41110242008-06-17 18:05:57 +000019#include "llvm/Function.h"
20
Anders Carlsson55085182007-08-21 17:43:55 +000021using namespace clang;
22using namespace CodeGen;
23
Chris Lattner8fdf3282008-06-24 17:04:18 +000024/// Emits an instance of NSConstantString representing the object.
Chris Lattner7f02f722007-08-24 05:35:26 +000025llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E){
Chris Lattner8fdf3282008-06-24 17:04:18 +000026 return CGM.getObjCRuntime()->GenerateConstantString(
27 E->getString()->getStrData(), E->getString()->getByteLength());
28}
29
30/// Emit a selector.
31llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) {
32 // Untyped selector.
33 // Note that this implementation allows for non-constant strings to be passed
34 // as arguments to @selector(). Currently, the only thing preventing this
35 // behaviour is the type checking in the front end.
Chris Lattner42ba3e72008-06-26 04:38:58 +000036 return CGM.getObjCRuntime()->GetSelector(Builder, E->getSelector());
Chris Lattner8fdf3282008-06-24 17:04:18 +000037}
38
39
40
41llvm::Value *CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) {
42 // Only the lookup mechanism and first two arguments of the method
43 // implementation vary between runtimes. We can get the receiver and
44 // arguments in generic code.
45
46 CGObjCRuntime *Runtime = CGM.getObjCRuntime();
47 const Expr *ReceiverExpr = E->getReceiver();
48 bool isSuperMessage = false;
49 // Find the receiver
50 llvm::Value *Receiver;
51 if (!ReceiverExpr) {
52 const char * classname = E->getClassName()->getName();
53 if (!strcmp(classname, "super")) {
54 classname = E->getMethodDecl()->getClassInterface()->getName();
55 }
56 llvm::Value *ClassName = CGM.GetAddrOfConstantString(classname);
57 ClassName = Builder.CreateStructGEP(ClassName, 0);
58 Receiver = Runtime->LookupClass(Builder, ClassName);
Chris Lattner630404b2008-06-26 04:10:42 +000059 } else if (isa<PreDefinedExpr>(E->getReceiver())) {
60 assert(cast<PreDefinedExpr>(E->getReceiver())->getIdentType() ==
61 PreDefinedExpr::ObjCSuper);
Chris Lattner8fdf3282008-06-24 17:04:18 +000062 isSuperMessage = true;
63 Receiver = LoadObjCSelf();
64 } else {
65 Receiver = EmitScalarExpr(E->getReceiver());
66 }
67
68 // Process the arguments
69 unsigned ArgC = E->getNumArgs();
70 llvm::SmallVector<llvm::Value*, 16> Args;
71 for (unsigned i = 0; i != ArgC; ++i) {
72 const Expr *ArgExpr = E->getArg(i);
73 QualType ArgTy = ArgExpr->getType();
74 if (!hasAggregateLLVMType(ArgTy)) {
75 // Scalar argument is passed by-value.
76 Args.push_back(EmitScalarExpr(ArgExpr));
77 } else if (ArgTy->isAnyComplexType()) {
78 // Make a temporary alloca to pass the argument.
79 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
80 EmitComplexExprIntoAddr(ArgExpr, DestMem, false);
81 Args.push_back(DestMem);
82 } else {
83 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
84 EmitAggExpr(ArgExpr, DestMem, false);
85 Args.push_back(DestMem);
86 }
87 }
88
89 // Get the selector string
90 std::string SelStr = E->getSelector().getName();
91 llvm::Constant *Selector = CGM.GetAddrOfConstantString(SelStr);
92
93 llvm::Value *SelPtr = Builder.CreateStructGEP(Selector, 0);
94 if (isSuperMessage) {
95 const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurFuncDecl);
96 assert(OMD && "super is only valid in an Objective-C method");
Chris Lattner630404b2008-06-26 04:10:42 +000097 const char *SuperClass =
98 OMD->getClassInterface()->getSuperClass()->getName();
Chris Lattner8fdf3282008-06-24 17:04:18 +000099 return Runtime->GenerateMessageSendSuper(Builder, ConvertType(E->getType()),
Chris Lattner630404b2008-06-26 04:10:42 +0000100 Receiver, SuperClass,
Chris Lattner8e67b632008-06-26 04:37:12 +0000101 Receiver, E->getSelector(),
Chris Lattner630404b2008-06-26 04:10:42 +0000102 &Args[0], Args.size());
Chris Lattner8fdf3282008-06-24 17:04:18 +0000103 }
104 return Runtime->GenerateMessageSend(Builder, ConvertType(E->getType()),
105 LoadObjCSelf(),
106 Receiver, SelPtr,
107 &Args[0], Args.size());
Anders Carlsson55085182007-08-21 17:43:55 +0000108}
109
Chris Lattner41110242008-06-17 18:05:57 +0000110/// Generate an Objective-C method. An Objective-C method is a C function with
111/// its pointer, name, and types registered in the class struture.
112void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
113
114 llvm::SmallVector<const llvm::Type *, 16> ParamTypes;
115 for (unsigned i=0 ; i<OMD->param_size() ; i++) {
116 const llvm::Type *Ty = ConvertType(OMD->getParamDecl(i)->getType());
117 if (Ty->isFirstClassType())
118 ParamTypes.push_back(Ty);
119 else
120 ParamTypes.push_back(llvm::PointerType::getUnqual(Ty));
121 }
122 std::string CategoryName = "";
123 if (ObjCCategoryImplDecl *OCD =
124 dyn_cast<ObjCCategoryImplDecl>(OMD->getMethodContext())) {
125 CategoryName = OCD->getName();
126 }
Chris Lattner630404b2008-06-26 04:10:42 +0000127 const llvm::Type *ReturnTy =
128 CGM.getTypes().ConvertReturnType(OMD->getResultType());
Chris Lattner41110242008-06-17 18:05:57 +0000129 CurFn = CGM.getObjCRuntime()->MethodPreamble(
Chris Lattner630404b2008-06-26 04:10:42 +0000130 OMD->getClassInterface()->getName(),
Chris Lattner41110242008-06-17 18:05:57 +0000131 CategoryName,
132 OMD->getSelector().getName(),
133 ReturnTy,
134 llvm::PointerType::getUnqual(
135 llvm::Type::Int32Ty),
136 ParamTypes.begin(),
137 OMD->param_size(),
138 !OMD->isInstance(),
139 OMD->isVariadic());
140 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn);
141
142 // Create a marker to make it easy to insert allocas into the entryblock
143 // later. Don't create this with the builder, because we don't want it
144 // folded.
145 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
146 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
147 EntryBB);
148
149 FnRetTy = OMD->getResultType();
150 CurFuncDecl = OMD;
151
152 Builder.SetInsertPoint(EntryBB);
153
154 // Emit allocs for param decls. Give the LLVM Argument nodes names.
155 llvm::Function::arg_iterator AI = CurFn->arg_begin();
156
157 if (hasAggregateLLVMType(OMD->getResultType())) {
158 ++AI;
159 }
160 // Add implicit parameters to the decl map.
161 // TODO: Add something to AST to let the runtime specify the names and types
162 // of these.
163
164 llvm::Value *&SelfEntry = LocalDeclMap[OMD->getSelfDecl()];
165 const llvm::Type *IPTy = AI->getType();
166 llvm::Value *DeclPtr = new llvm::AllocaInst(IPTy, 0, AI->getName() +
167 ".addr", AllocaInsertPt);
168 // Store the initial value into the alloca.
169 Builder.CreateStore(AI, DeclPtr);
170 SelfEntry = DeclPtr;
171 ++AI;
172 llvm::Value *&CmdEntry = LocalDeclMap[OMD->getCmdDecl()];
173 IPTy = AI->getType();
174 DeclPtr = new llvm::AllocaInst(IPTy, 0, AI->getName() +
175 ".addr", AllocaInsertPt);
176 // Store the initial value into the alloca.
177 Builder.CreateStore(AI, DeclPtr);
178 CmdEntry = DeclPtr;
179
180 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i, ++AI) {
181 assert(AI != CurFn->arg_end() && "Argument mismatch!");
182 EmitParmDecl(*OMD->getParamDecl(i), AI);
183 }
184
185 GenerateFunction(OMD->getBody());
186}
187
188llvm::Value *CodeGenFunction::LoadObjCSelf(void)
189{
190 if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurFuncDecl)) {
191 ValueDecl *Decl = OMD->getSelfDecl();
192 llvm::Value *SelfPtr = LocalDeclMap[&(*(Decl))];
193 return Builder.CreateLoad(SelfPtr, "self");
194 }
195 return NULL;
196}
197
Ted Kremenek2979ec72008-04-09 15:51:31 +0000198CGObjCRuntime::~CGObjCRuntime() {}