[cfe-commits] r39545 - in /cfe/cfe/trunk: CodeGen/CGExpr.cpp CodeGen/CGStmt.cpp CodeGen/CodeGenFunction.h include/clang/AST/Expr.h
clattner at cs.uiuc.edu
clattner at cs.uiuc.edu
Wed Jul 11 09:45:27 PDT 2007
Author: clattner
Date: Wed Jul 11 11:45:27 2007
New Revision: 39545
URL: http://llvm.org/viewvc/llvm-project?rev=39545&view=rev
Log:
stub out codegen of binary plus. We now compile:
if (11 + 42) {
to:
%tmp = add i32 11, 42 ; <i32> [#uses=1]
%tobool = icmp ne i32 %tmp, 0 ; <i1> [#uses=1]
br i1 %tobool, label %ifthen, label %ifend
but this doesn't handle any of the interesting/hard stuff yet.
Modified:
cfe/cfe/trunk/CodeGen/CGExpr.cpp
cfe/cfe/trunk/CodeGen/CGStmt.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=39545&r1=39544&r2=39545&view=diff
==============================================================================
--- cfe/cfe/trunk/CodeGen/CGExpr.cpp (original)
+++ cfe/cfe/trunk/CodeGen/CGExpr.cpp Wed Jul 11 11:45:27 2007
@@ -36,6 +36,9 @@
return EmitExpr(cast<ParenExpr>(E)->getSubExpr());
case Stmt::IntegerLiteralClass:
return EmitIntegerLiteral(cast<IntegerLiteral>(E));
+
+ case Stmt::BinaryOperatorClass:
+ return EmitBinaryOperator(cast<BinaryOperator>(E));
}
}
@@ -45,3 +48,36 @@
}
+//===--------------------------------------------------------------------===//
+// Binary Operator Emission
+//===--------------------------------------------------------------------===//
+
+// FIXME describe.
+void CodeGenFunction::EmitUsualArithmeticConversions(const BinaryOperator *E,
+ ExprResult &LHS,
+ ExprResult &RHS) {
+ // FIXME: implement right.
+ LHS = EmitExpr(E->getLHS());
+ RHS = EmitExpr(E->getRHS());
+}
+
+
+ExprResult CodeGenFunction::EmitBinaryOperator(const BinaryOperator *E) {
+ switch (E->getOpcode()) {
+ default:
+ printf("Unimplemented expr!\n");
+ E->dump();
+ return ExprResult::get(UndefValue::get(llvm::Type::Int32Ty));
+ case BinaryOperator::Add: return EmitBinaryAdd(E);
+ }
+}
+
+
+ExprResult CodeGenFunction::EmitBinaryAdd(const BinaryOperator *E) {
+ ExprResult LHS, RHS;
+
+ EmitUsualArithmeticConversions(E, LHS, RHS);
+
+
+ return ExprResult::get(Builder.CreateAdd(LHS.getVal(), RHS.getVal(), "tmp"));
+}
\ No newline at end of file
Modified: cfe/cfe/trunk/CodeGen/CGStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/CodeGen/CGStmt.cpp?rev=39545&r1=39544&r2=39545&view=diff
==============================================================================
--- cfe/cfe/trunk/CodeGen/CGStmt.cpp (original)
+++ cfe/cfe/trunk/CodeGen/CGStmt.cpp Wed Jul 11 11:45:27 2007
@@ -119,7 +119,7 @@
// Compare against zero for integers.
BoolCondVal = CondVal.getVal();
llvm::Value *Zero = Constant::getNullValue(BoolCondVal->getType());
- BoolCondVal = Builder.CreateICmpNE(BoolCondVal, Zero);
+ BoolCondVal = Builder.CreateICmpNE(BoolCondVal, Zero, "tobool");
break;
}
case BuiltinType::Float:
@@ -129,7 +129,7 @@
BoolCondVal = CondVal.getVal();
llvm::Value *Zero = Constant::getNullValue(BoolCondVal->getType());
// FIXME: llvm-gcc produces a une comparison: validate this is right.
- BoolCondVal = Builder.CreateFCmpUNE(BoolCondVal, Zero);
+ BoolCondVal = Builder.CreateFCmpUNE(BoolCondVal, Zero, "tobool");
break;
}
@@ -141,7 +141,7 @@
} else if (isa<PointerType>(CondTy)) {
BoolCondVal = CondVal.getVal();
llvm::Value *NullPtr = Constant::getNullValue(BoolCondVal->getType());
- BoolCondVal = Builder.CreateICmpNE(BoolCondVal, NullPtr);
+ BoolCondVal = Builder.CreateICmpNE(BoolCondVal, NullPtr, "tobool");
} else {
const TagType *TT = cast<TagType>(CondTy);
@@ -149,7 +149,7 @@
// Compare against zero.
BoolCondVal = CondVal.getVal();
llvm::Value *Zero = Constant::getNullValue(BoolCondVal->getType());
- BoolCondVal = Builder.CreateICmpNE(BoolCondVal, Zero);
+ BoolCondVal = Builder.CreateICmpNE(BoolCondVal, Zero, "tobool");
}
BasicBlock *ContBlock = new BasicBlock("ifend");
Modified: cfe/cfe/trunk/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/CodeGen/CodeGenFunction.h?rev=39545&r1=39544&r2=39545&view=diff
==============================================================================
--- cfe/cfe/trunk/CodeGen/CodeGenFunction.h (original)
+++ cfe/cfe/trunk/CodeGen/CodeGenFunction.h Wed Jul 11 11:45:27 2007
@@ -33,12 +33,15 @@
class Expr;
class IntegerLiteral;
+ class BinaryOperator;
namespace CodeGen {
class CodeGenModule;
class ExprResult {
Value *V;
+ // TODO: Encode this into the low bit of pointer for more efficient
+ // return-by-value.
bool IsAggregate;
public:
@@ -112,6 +115,14 @@
ExprResult EmitExpr(const Expr *E);
ExprResult EmitIntegerLiteral(const IntegerLiteral *E);
+ ExprResult EmitBinaryOperator(const BinaryOperator *E);
+
+
+ void EmitUsualArithmeticConversions(const BinaryOperator *E,
+ ExprResult &LHS, ExprResult &RHS);
+
+ // Binary Operators.
+ ExprResult EmitBinaryAdd(const BinaryOperator *E);
};
} // end namespace CodeGen
} // end namespace clang
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=39545&r1=39544&r2=39545&view=diff
==============================================================================
--- cfe/cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/cfe/trunk/include/clang/AST/Expr.h Wed Jul 11 11:45:27 2007
@@ -440,6 +440,13 @@
BinaryOperator(Expr *lhs, Expr *rhs, Opcode opc, QualType t=QualType())
: Expr(BinaryOperatorClass, t), LHS(lhs), RHS(rhs), Opc(opc) {}
+ Opcode getOpcode() const { return Opc; }
+ Expr *getLHS() const { return LHS; }
+ Expr *getRHS() const { return RHS; }
+ virtual SourceRange getSourceRange() const {
+ return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd());
+ }
+
/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
/// corresponds to, e.g. "<<=".
static const char *getOpcodeStr(Opcode Op);
@@ -454,12 +461,6 @@
bool isLogicalOp() const { return Opc == LAnd || Opc == LOr; }
bool isAssignmentOp() const { return Opc >= Assign && Opc <= OrAssign; }
- Opcode getOpcode() const { return Opc; }
- Expr *getLHS() const { return LHS; }
- Expr *getRHS() const { return RHS; }
- virtual SourceRange getSourceRange() const {
- return SourceRange(getLHS()->getLocStart(), getRHS()->getLocEnd());
- }
virtual void visit(StmtVisitor &Visitor);
static bool classof(const Stmt *T) {
More information about the cfe-commits
mailing list