blob: 54b4e19c572acbfdefbe895f1f4f9e14aa071477 [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"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000017#include "clang/AST/DeclObjC.h"
Chris Lattner41110242008-06-17 18:05:57 +000018
Anders Carlsson55085182007-08-21 17:43:55 +000019using namespace clang;
20using namespace CodeGen;
21
Chris Lattner8fdf3282008-06-24 17:04:18 +000022/// Emits an instance of NSConstantString representing the object.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000023llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E) {
24 std::string String(E->getString()->getStrData(), E->getString()->getByteLength());
25 llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(String);
26 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
Chris Lattner8fdf3282008-06-24 17:04:18 +000027}
28
29/// Emit a selector.
30llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) {
31 // Untyped selector.
32 // Note that this implementation allows for non-constant strings to be passed
33 // as arguments to @selector(). Currently, the only thing preventing this
34 // behaviour is the type checking in the front end.
Daniel Dunbar208ff5e2008-08-11 18:12:00 +000035 return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
Chris Lattner8fdf3282008-06-24 17:04:18 +000036}
37
38
39
40llvm::Value *CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) {
41 // Only the lookup mechanism and first two arguments of the method
42 // implementation vary between runtimes. We can get the receiver and
43 // arguments in generic code.
44
Daniel Dunbar208ff5e2008-08-11 18:12:00 +000045 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
Chris Lattner8fdf3282008-06-24 17:04:18 +000046 const Expr *ReceiverExpr = E->getReceiver();
47 bool isSuperMessage = false;
48 // Find the receiver
49 llvm::Value *Receiver;
50 if (!ReceiverExpr) {
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000051 const ObjCInterfaceDecl *OID = E->getClassInfo().first;
52
53 // Very special case, super send in class method. The receiver is
54 // self (the class object) and the send uses super semantics.
55 if (!OID) {
56 assert(!strcmp(E->getClassName()->getName(), "super") &&
57 "Unexpected missing class interface in message send.");
58 OID = E->getMethodDecl()->getClassInterface();
59 isSuperMessage = true;
Chris Lattner8fdf3282008-06-24 17:04:18 +000060 }
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000061
62 Receiver = Runtime.GetClass(Builder, OID);
Chris Lattnerd9f69102008-08-10 01:53:14 +000063 } else if (const PredefinedExpr *PDE =
64 dyn_cast<PredefinedExpr>(E->getReceiver())) {
65 assert(PDE->getIdentType() == PredefinedExpr::ObjCSuper);
Chris Lattner8fdf3282008-06-24 17:04:18 +000066 isSuperMessage = true;
67 Receiver = LoadObjCSelf();
68 } else {
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000069 Receiver = EmitScalarExpr(E->getReceiver());
Chris Lattner8fdf3282008-06-24 17:04:18 +000070 }
71
72 // Process the arguments
73 unsigned ArgC = E->getNumArgs();
74 llvm::SmallVector<llvm::Value*, 16> Args;
75 for (unsigned i = 0; i != ArgC; ++i) {
76 const Expr *ArgExpr = E->getArg(i);
77 QualType ArgTy = ArgExpr->getType();
78 if (!hasAggregateLLVMType(ArgTy)) {
79 // Scalar argument is passed by-value.
80 Args.push_back(EmitScalarExpr(ArgExpr));
81 } else if (ArgTy->isAnyComplexType()) {
82 // Make a temporary alloca to pass the argument.
83 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
84 EmitComplexExprIntoAddr(ArgExpr, DestMem, false);
85 Args.push_back(DestMem);
86 } else {
87 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
88 EmitAggExpr(ArgExpr, DestMem, false);
89 Args.push_back(DestMem);
90 }
91 }
92
Chris Lattner8fdf3282008-06-24 17:04:18 +000093 if (isSuperMessage) {
Chris Lattner9384c762008-06-26 04:42:20 +000094 // super is only valid in an Objective-C method
95 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Daniel Dunbar208ff5e2008-08-11 18:12:00 +000096 return Runtime.GenerateMessageSendSuper(Builder, ConvertType(E->getType()),
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000097 OMD->getClassInterface()->getSuperClass(),
Chris Lattner8e67b632008-06-26 04:37:12 +000098 Receiver, E->getSelector(),
Chris Lattner630404b2008-06-26 04:10:42 +000099 &Args[0], Args.size());
Chris Lattner8fdf3282008-06-24 17:04:18 +0000100 }
Daniel Dunbar208ff5e2008-08-11 18:12:00 +0000101 return Runtime.GenerateMessageSend(Builder, ConvertType(E->getType()),
Chris Lattner9384c762008-06-26 04:42:20 +0000102 Receiver, E->getSelector(),
Chris Lattner8fdf3282008-06-24 17:04:18 +0000103 &Args[0], Args.size());
Anders Carlsson55085182007-08-21 17:43:55 +0000104}
105
Chris Lattner41110242008-06-17 18:05:57 +0000106/// Generate an Objective-C method. An Objective-C method is a C function with
107/// its pointer, name, and types registered in the class struture.
108void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000109 CurFn = CGM.getObjCRuntime().GenerateMethod(OMD);
Chris Lattner41110242008-06-17 18:05:57 +0000110 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn);
111
112 // Create a marker to make it easy to insert allocas into the entryblock
113 // later. Don't create this with the builder, because we don't want it
114 // folded.
115 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
116 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
117 EntryBB);
118
119 FnRetTy = OMD->getResultType();
120 CurFuncDecl = OMD;
121
122 Builder.SetInsertPoint(EntryBB);
123
124 // Emit allocs for param decls. Give the LLVM Argument nodes names.
125 llvm::Function::arg_iterator AI = CurFn->arg_begin();
126
127 if (hasAggregateLLVMType(OMD->getResultType())) {
128 ++AI;
129 }
130 // Add implicit parameters to the decl map.
131 // TODO: Add something to AST to let the runtime specify the names and types
132 // of these.
133
134 llvm::Value *&SelfEntry = LocalDeclMap[OMD->getSelfDecl()];
135 const llvm::Type *IPTy = AI->getType();
136 llvm::Value *DeclPtr = new llvm::AllocaInst(IPTy, 0, AI->getName() +
137 ".addr", AllocaInsertPt);
138 // Store the initial value into the alloca.
139 Builder.CreateStore(AI, DeclPtr);
140 SelfEntry = DeclPtr;
141 ++AI;
142 llvm::Value *&CmdEntry = LocalDeclMap[OMD->getCmdDecl()];
143 IPTy = AI->getType();
144 DeclPtr = new llvm::AllocaInst(IPTy, 0, AI->getName() +
145 ".addr", AllocaInsertPt);
146 // Store the initial value into the alloca.
147 Builder.CreateStore(AI, DeclPtr);
148 CmdEntry = DeclPtr;
149
150 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i, ++AI) {
151 assert(AI != CurFn->arg_end() && "Argument mismatch!");
152 EmitParmDecl(*OMD->getParamDecl(i), AI);
153 }
154
155 GenerateFunction(OMD->getBody());
156}
157
158llvm::Value *CodeGenFunction::LoadObjCSelf(void)
159{
160 if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(CurFuncDecl)) {
161 ValueDecl *Decl = OMD->getSelfDecl();
162 llvm::Value *SelfPtr = LocalDeclMap[&(*(Decl))];
163 return Builder.CreateLoad(SelfPtr, "self");
164 }
165 return NULL;
166}
167
Ted Kremenek2979ec72008-04-09 15:51:31 +0000168CGObjCRuntime::~CGObjCRuntime() {}