blob: 22e49acb619c6ac6c05a0c6c8e892af5788a340e [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.
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);
60 } else if (dyn_cast<PreDefinedExpr>(E->getReceiver())) {
61 isSuperMessage = true;
62 Receiver = LoadObjCSelf();
63 } else {
64 Receiver = EmitScalarExpr(E->getReceiver());
65 }
66
67 // Process the arguments
68 unsigned ArgC = E->getNumArgs();
69 llvm::SmallVector<llvm::Value*, 16> Args;
70 for (unsigned i = 0; i != ArgC; ++i) {
71 const Expr *ArgExpr = E->getArg(i);
72 QualType ArgTy = ArgExpr->getType();
73 if (!hasAggregateLLVMType(ArgTy)) {
74 // Scalar argument is passed by-value.
75 Args.push_back(EmitScalarExpr(ArgExpr));
76 } else if (ArgTy->isAnyComplexType()) {
77 // Make a temporary alloca to pass the argument.
78 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
79 EmitComplexExprIntoAddr(ArgExpr, DestMem, false);
80 Args.push_back(DestMem);
81 } else {
82 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
83 EmitAggExpr(ArgExpr, DestMem, false);
84 Args.push_back(DestMem);
85 }
86 }
87
88 // Get the selector string
89 std::string SelStr = E->getSelector().getName();
90 llvm::Constant *Selector = CGM.GetAddrOfConstantString(SelStr);
91
92 llvm::Value *SelPtr = Builder.CreateStructGEP(Selector, 0);
93 if (isSuperMessage) {
94 const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurFuncDecl);
95 assert(OMD && "super is only valid in an Objective-C method");
96 const char *SuperClass = OMD->getClassInterface()->getSuperClass()->getName();
97 return Runtime->GenerateMessageSendSuper(Builder, ConvertType(E->getType()),
98 Receiver, SuperClass,
99 Receiver, SelPtr,
100 &Args[0], Args.size());
101 }
102 return Runtime->GenerateMessageSend(Builder, ConvertType(E->getType()),
103 LoadObjCSelf(),
104 Receiver, SelPtr,
105 &Args[0], Args.size());
Anders Carlsson55085182007-08-21 17:43:55 +0000106}
107
Chris Lattner41110242008-06-17 18:05:57 +0000108/// Generate an Objective-C method. An Objective-C method is a C function with
109/// its pointer, name, and types registered in the class struture.
110void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
111
112 llvm::SmallVector<const llvm::Type *, 16> ParamTypes;
113 for (unsigned i=0 ; i<OMD->param_size() ; i++) {
114 const llvm::Type *Ty = ConvertType(OMD->getParamDecl(i)->getType());
115 if (Ty->isFirstClassType())
116 ParamTypes.push_back(Ty);
117 else
118 ParamTypes.push_back(llvm::PointerType::getUnqual(Ty));
119 }
120 std::string CategoryName = "";
121 if (ObjCCategoryImplDecl *OCD =
122 dyn_cast<ObjCCategoryImplDecl>(OMD->getMethodContext())) {
123 CategoryName = OCD->getName();
124 }
125 const llvm::Type *ReturnTy = CGM.getTypes().ConvertReturnType(OMD->getResultType());
126 CurFn = CGM.getObjCRuntime()->MethodPreamble(
127 OMD->getClassInterface()->getName(),
128 CategoryName,
129 OMD->getSelector().getName(),
130 ReturnTy,
131 llvm::PointerType::getUnqual(
132 llvm::Type::Int32Ty),
133 ParamTypes.begin(),
134 OMD->param_size(),
135 !OMD->isInstance(),
136 OMD->isVariadic());
137 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn);
138
139 // Create a marker to make it easy to insert allocas into the entryblock
140 // later. Don't create this with the builder, because we don't want it
141 // folded.
142 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
143 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
144 EntryBB);
145
146 FnRetTy = OMD->getResultType();
147 CurFuncDecl = OMD;
148
149 Builder.SetInsertPoint(EntryBB);
150
151 // Emit allocs for param decls. Give the LLVM Argument nodes names.
152 llvm::Function::arg_iterator AI = CurFn->arg_begin();
153
154 if (hasAggregateLLVMType(OMD->getResultType())) {
155 ++AI;
156 }
157 // Add implicit parameters to the decl map.
158 // TODO: Add something to AST to let the runtime specify the names and types
159 // of these.
160
161 llvm::Value *&SelfEntry = LocalDeclMap[OMD->getSelfDecl()];
162 const llvm::Type *IPTy = AI->getType();
163 llvm::Value *DeclPtr = new llvm::AllocaInst(IPTy, 0, AI->getName() +
164 ".addr", AllocaInsertPt);
165 // Store the initial value into the alloca.
166 Builder.CreateStore(AI, DeclPtr);
167 SelfEntry = DeclPtr;
168 ++AI;
169 llvm::Value *&CmdEntry = LocalDeclMap[OMD->getCmdDecl()];
170 IPTy = AI->getType();
171 DeclPtr = new llvm::AllocaInst(IPTy, 0, AI->getName() +
172 ".addr", AllocaInsertPt);
173 // Store the initial value into the alloca.
174 Builder.CreateStore(AI, DeclPtr);
175 CmdEntry = DeclPtr;
176
177 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i, ++AI) {
178 assert(AI != CurFn->arg_end() && "Argument mismatch!");
179 EmitParmDecl(*OMD->getParamDecl(i), AI);
180 }
181
182 GenerateFunction(OMD->getBody());
183}
184
185llvm::Value *CodeGenFunction::LoadObjCSelf(void)
186{
187 if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurFuncDecl)) {
188 ValueDecl *Decl = OMD->getSelfDecl();
189 llvm::Value *SelfPtr = LocalDeclMap[&(*(Decl))];
190 return Builder.CreateLoad(SelfPtr, "self");
191 }
192 return NULL;
193}
194
Ted Kremenek2979ec72008-04-09 15:51:31 +0000195CGObjCRuntime::~CGObjCRuntime() {}