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