blob: 9bf53d9700d822fc3d4a91cdb0fe724ba89f40a1 [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
Chris Lattner8fdf3282008-06-24 17:04:18 +000089 if (isSuperMessage) {
Chris Lattner9384c762008-06-26 04:42:20 +000090 // super is only valid in an Objective-C method
91 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Chris Lattner630404b2008-06-26 04:10:42 +000092 const char *SuperClass =
93 OMD->getClassInterface()->getSuperClass()->getName();
Chris Lattner8fdf3282008-06-24 17:04:18 +000094 return Runtime->GenerateMessageSendSuper(Builder, ConvertType(E->getType()),
Chris Lattner630404b2008-06-26 04:10:42 +000095 Receiver, SuperClass,
Chris Lattner8e67b632008-06-26 04:37:12 +000096 Receiver, E->getSelector(),
Chris Lattner630404b2008-06-26 04:10:42 +000097 &Args[0], Args.size());
Chris Lattner8fdf3282008-06-24 17:04:18 +000098 }
99 return Runtime->GenerateMessageSend(Builder, ConvertType(E->getType()),
100 LoadObjCSelf(),
Chris Lattner9384c762008-06-26 04:42:20 +0000101 Receiver, E->getSelector(),
Chris Lattner8fdf3282008-06-24 17:04:18 +0000102 &Args[0], Args.size());
Anders Carlsson55085182007-08-21 17:43:55 +0000103}
104
Chris Lattner41110242008-06-17 18:05:57 +0000105/// Generate an Objective-C method. An Objective-C method is a C function with
106/// its pointer, name, and types registered in the class struture.
107void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
108
109 llvm::SmallVector<const llvm::Type *, 16> ParamTypes;
110 for (unsigned i=0 ; i<OMD->param_size() ; i++) {
111 const llvm::Type *Ty = ConvertType(OMD->getParamDecl(i)->getType());
112 if (Ty->isFirstClassType())
113 ParamTypes.push_back(Ty);
114 else
115 ParamTypes.push_back(llvm::PointerType::getUnqual(Ty));
116 }
117 std::string CategoryName = "";
118 if (ObjCCategoryImplDecl *OCD =
119 dyn_cast<ObjCCategoryImplDecl>(OMD->getMethodContext())) {
120 CategoryName = OCD->getName();
121 }
Chris Lattner630404b2008-06-26 04:10:42 +0000122 const llvm::Type *ReturnTy =
123 CGM.getTypes().ConvertReturnType(OMD->getResultType());
Chris Lattner41110242008-06-17 18:05:57 +0000124 CurFn = CGM.getObjCRuntime()->MethodPreamble(
Chris Lattner630404b2008-06-26 04:10:42 +0000125 OMD->getClassInterface()->getName(),
Chris Lattner41110242008-06-17 18:05:57 +0000126 CategoryName,
127 OMD->getSelector().getName(),
128 ReturnTy,
129 llvm::PointerType::getUnqual(
130 llvm::Type::Int32Ty),
131 ParamTypes.begin(),
132 OMD->param_size(),
133 !OMD->isInstance(),
134 OMD->isVariadic());
135 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn);
136
137 // Create a marker to make it easy to insert allocas into the entryblock
138 // later. Don't create this with the builder, because we don't want it
139 // folded.
140 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
141 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
142 EntryBB);
143
144 FnRetTy = OMD->getResultType();
145 CurFuncDecl = OMD;
146
147 Builder.SetInsertPoint(EntryBB);
148
149 // Emit allocs for param decls. Give the LLVM Argument nodes names.
150 llvm::Function::arg_iterator AI = CurFn->arg_begin();
151
152 if (hasAggregateLLVMType(OMD->getResultType())) {
153 ++AI;
154 }
155 // Add implicit parameters to the decl map.
156 // TODO: Add something to AST to let the runtime specify the names and types
157 // of these.
158
159 llvm::Value *&SelfEntry = LocalDeclMap[OMD->getSelfDecl()];
160 const llvm::Type *IPTy = AI->getType();
161 llvm::Value *DeclPtr = new llvm::AllocaInst(IPTy, 0, AI->getName() +
162 ".addr", AllocaInsertPt);
163 // Store the initial value into the alloca.
164 Builder.CreateStore(AI, DeclPtr);
165 SelfEntry = DeclPtr;
166 ++AI;
167 llvm::Value *&CmdEntry = LocalDeclMap[OMD->getCmdDecl()];
168 IPTy = AI->getType();
169 DeclPtr = new llvm::AllocaInst(IPTy, 0, AI->getName() +
170 ".addr", AllocaInsertPt);
171 // Store the initial value into the alloca.
172 Builder.CreateStore(AI, DeclPtr);
173 CmdEntry = DeclPtr;
174
175 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i, ++AI) {
176 assert(AI != CurFn->arg_end() && "Argument mismatch!");
177 EmitParmDecl(*OMD->getParamDecl(i), AI);
178 }
179
180 GenerateFunction(OMD->getBody());
181}
182
183llvm::Value *CodeGenFunction::LoadObjCSelf(void)
184{
185 if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurFuncDecl)) {
186 ValueDecl *Decl = OMD->getSelfDecl();
187 llvm::Value *SelfPtr = LocalDeclMap[&(*(Decl))];
188 return Builder.CreateLoad(SelfPtr, "self");
189 }
190 return NULL;
191}
192
Ted Kremenek2979ec72008-04-09 15:51:31 +0000193CGObjCRuntime::~CGObjCRuntime() {}