implement codegen of the relational operations

One major FIXME though.

llvm-svn: 39666
This commit is contained in:
Chris Lattner 2007-06-20 18:30:55 +00:00
parent 273c63d450
commit 1fde0b345b
3 changed files with 41 additions and 23 deletions

View File

@ -337,7 +337,7 @@ RValue CodeGenFunction::EmitExpr(const Expr *E) {
switch (E->getStmtClass()) {
default:
printf("Unimplemented expr!\n");
fprintf(stderr, "Unimplemented expr!\n");
E->dump();
return RValue::get(llvm::UndefValue::get(llvm::Type::Int32Ty));
@ -644,18 +644,35 @@ RValue CodeGenFunction::EmitBinaryOperator(const BinaryOperator *E) {
case BinaryOperator::Sub: return EmitBinarySub(E);
case BinaryOperator::Shl: return EmitBinaryShl(E);
case BinaryOperator::Shr: return EmitBinaryShr(E);
case BinaryOperator::EQ:
case BinaryOperator::NE: return EmitBinaryEquality(E);
// FIXME: relational
case BinaryOperator::And: return EmitBinaryAnd(E);
case BinaryOperator::Xor: return EmitBinaryXor(E);
case BinaryOperator::Or : return EmitBinaryOr(E);
case BinaryOperator::LAnd: return EmitBinaryLAnd(E);
case BinaryOperator::LOr: return EmitBinaryLOr(E);
case BinaryOperator::LT:
return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_ULT,
llvm::ICmpInst::ICMP_SLT,
llvm::FCmpInst::FCMP_OLT);
case BinaryOperator::GT:
return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_UGT,
llvm::ICmpInst::ICMP_SGT,
llvm::FCmpInst::FCMP_OGT);
case BinaryOperator::LE:
return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_ULE,
llvm::ICmpInst::ICMP_SLE,
llvm::FCmpInst::FCMP_OLE);
case BinaryOperator::GE:
return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_UGE,
llvm::ICmpInst::ICMP_SGE,
llvm::FCmpInst::FCMP_OGE);
case BinaryOperator::EQ:
return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_EQ,
llvm::ICmpInst::ICMP_EQ,
llvm::FCmpInst::FCMP_OEQ);
case BinaryOperator::NE:
return EmitBinaryCompare(E, llvm::ICmpInst::ICMP_NE,
llvm::ICmpInst::ICMP_NE,
llvm::FCmpInst::FCMP_UNE);
case BinaryOperator::Assign: return EmitBinaryAssign(E);
// FIXME: Assignment.
case BinaryOperator::Comma: return EmitBinaryComma(E);
@ -771,23 +788,26 @@ RValue CodeGenFunction::EmitBinaryShr(const BinaryOperator *E) {
return RValue::get(Builder.CreateAShr(LHS, RHS, "shr"));
}
RValue CodeGenFunction::EmitBinaryEquality(const BinaryOperator *E) {
RValue CodeGenFunction::EmitBinaryCompare(const BinaryOperator *E,
unsigned UICmpOpc, unsigned SICmpOpc,
unsigned FCmpOpc) {
RValue LHS, RHS;
EmitUsualArithmeticConversions(E, LHS, RHS);
llvm::Value *Result;
if (LHS.isScalar()) {
if (LHS.getVal()->getType()->isFloatingPoint()) {
if (E->getOpcode() == BinaryOperator::EQ)
Result = Builder.CreateFCmpOEQ(LHS.getVal(), RHS.getVal(), "eq");
else
Result = Builder.CreateFCmpUNE(LHS.getVal(), RHS.getVal(), "ne");
Result = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
LHS.getVal(), RHS.getVal(), "cmp");
} else if (E->getLHS()->getType()->isUnsignedIntegerType()) {
// FIXME: This check isn't right for "unsigned short < int" where ushort
// promotes to int and does a signed compare.
Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
LHS.getVal(), RHS.getVal(), "cmp");
} else {
// Otherwise, it is an integer or pointer comparison.
if (E->getOpcode() == BinaryOperator::EQ)
Result = Builder.CreateICmpEQ(LHS.getVal(), RHS.getVal(), "eq");
else
Result = Builder.CreateICmpNE(LHS.getVal(), RHS.getVal(), "ne");
// Signed integers and pointers.
Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)SICmpOpc,
LHS.getVal(), RHS.getVal(), "cmp");
}
} else {
// Struct/union/complex

View File

@ -272,10 +272,8 @@ public:
RValue EmitBinarySub(const BinaryOperator *E);
RValue EmitBinaryShl(const BinaryOperator *E);
RValue EmitBinaryShr(const BinaryOperator *E);
// FIXME: relational
RValue EmitBinaryEquality(const BinaryOperator *E);
RValue EmitBinaryCompare(const BinaryOperator *E, unsigned UICmpOpc,
unsigned SICmpOpc, unsigned FCmpOpc);
RValue EmitBinaryAnd(const BinaryOperator *E);
RValue EmitBinaryXor(const BinaryOperator *E);
RValue EmitBinaryOr (const BinaryOperator *E);

View File

@ -190,7 +190,7 @@
84916BE60C161E800080778F /* Attr.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = Attr.h; path = clang/AST/Attr.h; sourceTree = "<group>"; };
84D9A8870C1A57E100AC7ABC /* AttributeList.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = AttributeList.cpp; path = Parse/AttributeList.cpp; sourceTree = "<group>"; };
84D9A88B0C1A581300AC7ABC /* AttributeList.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = AttributeList.h; path = clang/Parse/AttributeList.h; sourceTree = "<group>"; };
8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = clang; sourceTree = BUILT_PRODUCTS_DIR; };
8DD76F6C0486A84900D96B5E /* clang */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = "compiled.mach-o.executable"; path = clang; sourceTree = BUILT_PRODUCTS_DIR; };
DE01DA480B12ADA300AC22CE /* PPCallbacks.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = PPCallbacks.h; sourceTree = "<group>"; };
DE06756B0C051CFE00EBBFD8 /* ParseExprCXX.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = ParseExprCXX.cpp; path = Parse/ParseExprCXX.cpp; sourceTree = "<group>"; };
DE06B73D0A8307640050E87E /* LangOptions.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = LangOptions.h; sourceTree = "<group>"; };