[cfe-commits] r39658 - in /cfe/cfe/trunk: CodeGen/CGExpr.cpp CodeGen/CodeGenFunction.h include/clang/AST/Expr.h
clattner at cs.uiuc.edu
clattner at cs.uiuc.edu
Wed Jul 11 09:46:45 PDT 2007
Author: clattner
Date: Wed Jul 11 11:46:44 2007
New Revision: 39658
URL: http://llvm.org/viewvc/llvm-project?rev=39658&view=rev
Log:
implement codegen of calls. We can now compile:
double foo( int (*FP)(int, int, int)) {
return FP(1, 2, 3);
}
to:
define double @foo(i32 (i32, i32, i32)* %FP) {
entry:
%call = tail call i32 %FP( i32 1, i32 2, i32 3 )
%conv = sitofp i32 %call to double
ret double %conv
}
Modified:
cfe/cfe/trunk/CodeGen/CGExpr.cpp
cfe/cfe/trunk/CodeGen/CodeGenFunction.h
cfe/cfe/trunk/include/clang/AST/Expr.h
Modified: cfe/cfe/trunk/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/CodeGen/CGExpr.cpp?rev=39658&r1=39657&r2=39658&view=diff
==============================================================================
--- cfe/cfe/trunk/CodeGen/CGExpr.cpp (original)
+++ cfe/cfe/trunk/CodeGen/CGExpr.cpp Wed Jul 11 11:46:44 2007
@@ -360,6 +360,8 @@
return EmitUnaryOperator(cast<UnaryOperator>(E));
case Expr::CastExprClass:
return EmitCastExpr(cast<CastExpr>(E));
+ case Expr::CallExprClass:
+ return EmitCallExpr(cast<CallExpr>(E));
case Expr::BinaryOperatorClass:
return EmitBinaryOperator(cast<BinaryOperator>(E));
}
@@ -381,6 +383,31 @@
return EmitConversion(Src, SrcTy, E->getType(), E->getLParenLoc());
}
+RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) {
+ QualType Ty;
+ Value *Callee =EmitExprWithUsualUnaryConversions(E->getCallee(), Ty).getVal();
+
+ SmallVector<Value*, 16> Args;
+
+ // FIXME: Handle struct return.
+ for (unsigned i = 0, e = E->getNumArgs(); i != e; ++i) {
+ RValue ArgVal = EmitExprWithUsualUnaryConversions(E->getArg(i), Ty);
+
+ if (ArgVal.isScalar())
+ Args.push_back(ArgVal.getVal());
+ else // Pass by-address. FIXME: Set attribute bit on call.
+ Args.push_back(ArgVal.getAggregateVal());
+ }
+
+ Value *V = Builder.CreateCall(Callee, &Args[0], Args.size());
+ if (V->getType() != llvm::Type::VoidTy)
+ V->setName("call");
+
+ // FIXME: Struct return;
+ return RValue::get(V);
+}
+
+
//===----------------------------------------------------------------------===//
// Unary Operator Emission
//===----------------------------------------------------------------------===//
Modified: cfe/cfe/trunk/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/CodeGen/CodeGenFunction.h?rev=39658&r1=39657&r2=39658&view=diff
==============================================================================
--- cfe/cfe/trunk/CodeGen/CodeGenFunction.h (original)
+++ cfe/cfe/trunk/CodeGen/CodeGenFunction.h Wed Jul 11 11:46:44 2007
@@ -45,6 +45,7 @@
class StringLiteral;
class IntegerLiteral;
class CastExpr;
+ class CallExpr;
class UnaryOperator;
class BinaryOperator;
class ArraySubscriptExpr;
@@ -251,6 +252,7 @@
RValue EmitIntegerLiteral(const IntegerLiteral *E);
RValue EmitCastExpr(const CastExpr *E);
+ RValue EmitCallExpr(const CallExpr *E);
// Unary Operators.
RValue EmitUnaryOperator(const UnaryOperator *E);
Modified: cfe/cfe/trunk/include/clang/AST/Expr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/include/clang/AST/Expr.h?rev=39658&r1=39657&r2=39658&view=diff
==============================================================================
--- cfe/cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/cfe/trunk/include/clang/AST/Expr.h Wed Jul 11 11:46:44 2007
@@ -328,6 +328,7 @@
const Expr *getBase() const { return Base; }
Expr *getIdx() { return Idx; }
const Expr *getIdx() const { return Idx; }
+
SourceRange getSourceRange() const {
return SourceRange(Base->getLocStart(), Loc);
}
@@ -353,17 +354,19 @@
delete [] Args;
}
- Expr *getCallee() const { return Fn; }
- SourceRange getSourceRange() const {
- return SourceRange(Fn->getLocStart(), Loc);
- }
+ const Expr *getCallee() const { return Fn; }
+ Expr *getCallee() { return Fn; }
/// getNumArgs - Return the number of actual arguments to this call.
///
unsigned getNumArgs() const { return NumArgs; }
/// getArg - Return the specified argument.
- Expr *getArg(unsigned Arg) const {
+ Expr *getArg(unsigned Arg) {
+ assert(Arg < NumArgs && "Arg access out of range!");
+ return Args[Arg];
+ }
+ const Expr *getArg(unsigned Arg) const {
assert(Arg < NumArgs && "Arg access out of range!");
return Args[Arg];
}
@@ -372,6 +375,10 @@
/// this function call.
unsigned getNumCommas() const { return NumArgs ? NumArgs - 1 : 0; }
+ SourceRange getSourceRange() const {
+ return SourceRange(Fn->getLocStart(), Loc);
+ }
+
virtual void visit(StmtVisitor &Visitor);
static bool classof(const Stmt *T) {
return T->getStmtClass() == CallExprClass;
More information about the cfe-commits
mailing list