blob: 5e4b958807d151a6be29a86385cd0a8f2da64a97 [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);
Daniel Dunbared7c6182008-08-20 00:28:19 +000026 // FIXME: This bitcast should just be made an invariant on the Runtime.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000027 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
Chris Lattner8fdf3282008-06-24 17:04:18 +000028}
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.
Daniel Dunbar208ff5e2008-08-11 18:12:00 +000036 return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
Chris Lattner8fdf3282008-06-24 17:04:18 +000037}
38
Daniel Dunbared7c6182008-08-20 00:28:19 +000039llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) {
40 // FIXME: This should pass the Decl not the name.
41 return CGM.getObjCRuntime().GenerateProtocolRef(Builder, E->getProtocol());
42}
Chris Lattner8fdf3282008-06-24 17:04:18 +000043
44
45llvm::Value *CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) {
46 // Only the lookup mechanism and first two arguments of the method
47 // implementation vary between runtimes. We can get the receiver and
48 // arguments in generic code.
49
Daniel Dunbar208ff5e2008-08-11 18:12:00 +000050 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
Chris Lattner8fdf3282008-06-24 17:04:18 +000051 const Expr *ReceiverExpr = E->getReceiver();
52 bool isSuperMessage = false;
53 // Find the receiver
54 llvm::Value *Receiver;
55 if (!ReceiverExpr) {
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000056 const ObjCInterfaceDecl *OID = E->getClassInfo().first;
57
58 // Very special case, super send in class method. The receiver is
59 // self (the class object) and the send uses super semantics.
60 if (!OID) {
61 assert(!strcmp(E->getClassName()->getName(), "super") &&
62 "Unexpected missing class interface in message send.");
63 OID = E->getMethodDecl()->getClassInterface();
64 isSuperMessage = true;
Chris Lattner8fdf3282008-06-24 17:04:18 +000065 }
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000066
67 Receiver = Runtime.GetClass(Builder, OID);
Chris Lattnerd9f69102008-08-10 01:53:14 +000068 } else if (const PredefinedExpr *PDE =
69 dyn_cast<PredefinedExpr>(E->getReceiver())) {
70 assert(PDE->getIdentType() == PredefinedExpr::ObjCSuper);
Chris Lattner8fdf3282008-06-24 17:04:18 +000071 isSuperMessage = true;
72 Receiver = LoadObjCSelf();
73 } else {
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000074 Receiver = EmitScalarExpr(E->getReceiver());
Chris Lattner8fdf3282008-06-24 17:04:18 +000075 }
76
77 // Process the arguments
78 unsigned ArgC = E->getNumArgs();
79 llvm::SmallVector<llvm::Value*, 16> Args;
80 for (unsigned i = 0; i != ArgC; ++i) {
81 const Expr *ArgExpr = E->getArg(i);
82 QualType ArgTy = ArgExpr->getType();
83 if (!hasAggregateLLVMType(ArgTy)) {
84 // Scalar argument is passed by-value.
85 Args.push_back(EmitScalarExpr(ArgExpr));
86 } else if (ArgTy->isAnyComplexType()) {
87 // Make a temporary alloca to pass the argument.
88 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
89 EmitComplexExprIntoAddr(ArgExpr, DestMem, false);
90 Args.push_back(DestMem);
91 } else {
92 llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy));
93 EmitAggExpr(ArgExpr, DestMem, false);
94 Args.push_back(DestMem);
95 }
96 }
97
Chris Lattner8fdf3282008-06-24 17:04:18 +000098 if (isSuperMessage) {
Chris Lattner9384c762008-06-26 04:42:20 +000099 // super is only valid in an Objective-C method
100 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Daniel Dunbar208ff5e2008-08-11 18:12:00 +0000101 return Runtime.GenerateMessageSendSuper(Builder, ConvertType(E->getType()),
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +0000102 OMD->getClassInterface()->getSuperClass(),
Chris Lattner8e67b632008-06-26 04:37:12 +0000103 Receiver, E->getSelector(),
Chris Lattner630404b2008-06-26 04:10:42 +0000104 &Args[0], Args.size());
Chris Lattner8fdf3282008-06-24 17:04:18 +0000105 }
Daniel Dunbar208ff5e2008-08-11 18:12:00 +0000106 return Runtime.GenerateMessageSend(Builder, ConvertType(E->getType()),
Chris Lattner9384c762008-06-26 04:42:20 +0000107 Receiver, E->getSelector(),
Chris Lattner8fdf3282008-06-24 17:04:18 +0000108 &Args[0], Args.size());
Anders Carlsson55085182007-08-21 17:43:55 +0000109}
110
Chris Lattner41110242008-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) {
Daniel Dunbar7ded7f42008-08-15 22:20:32 +0000114 CurFn = CGM.getObjCRuntime().GenerateMethod(OMD);
Chris Lattner41110242008-06-17 18:05:57 +0000115 llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn);
116
117 // Create a marker to make it easy to insert allocas into the entryblock
118 // later. Don't create this with the builder, because we don't want it
119 // folded.
120 llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty);
121 AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt",
122 EntryBB);
123
124 FnRetTy = OMD->getResultType();
125 CurFuncDecl = OMD;
126
127 Builder.SetInsertPoint(EntryBB);
128
129 // Emit allocs for param decls. Give the LLVM Argument nodes names.
130 llvm::Function::arg_iterator AI = CurFn->arg_begin();
131
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000132 // Name the struct return argument.
Chris Lattner41110242008-06-17 18:05:57 +0000133 if (hasAggregateLLVMType(OMD->getResultType())) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000134 AI->setName("agg.result");
Chris Lattner41110242008-06-17 18:05:57 +0000135 ++AI;
136 }
Chris Lattner41110242008-06-17 18:05:57 +0000137
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000138 // Add implicit parameters to the decl map.
139 EmitParmDecl(*OMD->getSelfDecl(), AI);
Chris Lattner41110242008-06-17 18:05:57 +0000140 ++AI;
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000141
142 EmitParmDecl(*OMD->getCmdDecl(), AI);
143 ++AI;
Chris Lattner41110242008-06-17 18:05:57 +0000144
145 for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i, ++AI) {
146 assert(AI != CurFn->arg_end() && "Argument mismatch!");
147 EmitParmDecl(*OMD->getParamDecl(i), AI);
148 }
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000149 assert(AI == CurFn->arg_end() && "Argument mismatch");
150
Chris Lattner41110242008-06-17 18:05:57 +0000151 GenerateFunction(OMD->getBody());
152}
153
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000154llvm::Value *CodeGenFunction::LoadObjCSelf(void) {
155 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
156 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner41110242008-06-17 18:05:57 +0000157}
158
Ted Kremenek2979ec72008-04-09 15:51:31 +0000159CGObjCRuntime::~CGObjCRuntime() {}