Correct compatibility with the GNU Assembler's handling of comparison ops
GAS returns -1 for a comparison operator if the result is true and 0 if false.
https://www.sourceware.org/binutils/docs-2.12/as.info/Infix-Ops.html#Infix%20Ops
llvm-svn: 332215
diff --git a/llvm/lib/MC/MCExpr.cpp b/llvm/lib/MC/MCExpr.cpp
index f9b8889..831c692 100644
--- a/llvm/lib/MC/MCExpr.cpp
+++ b/llvm/lib/MC/MCExpr.cpp
@@ -74,7 +74,10 @@
case MCUnaryExpr::Not: OS << '~'; break;
case MCUnaryExpr::Plus: OS << '+'; break;
}
+ bool Binary = UE.getSubExpr()->getKind() == MCExpr::Binary;
+ if (Binary) OS << "(";
UE.getSubExpr()->print(OS, MAI);
+ if (Binary) OS << ")";
return;
}
@@ -756,7 +759,8 @@
// Apple as.
int64_t LHS = LHSValue.getConstant(), RHS = RHSValue.getConstant();
int64_t Result = 0;
- switch (ABE->getOpcode()) {
+ auto Op = ABE->getOpcode();
+ switch (Op) {
case MCBinaryExpr::AShr: Result = LHS >> RHS; break;
case MCBinaryExpr::Add: Result = LHS + RHS; break;
case MCBinaryExpr::And: Result = LHS & RHS; break;
@@ -791,7 +795,21 @@
case MCBinaryExpr::Xor: Result = LHS ^ RHS; break;
}
- Res = MCValue::get(Result);
+ switch (Op) {
+ default:
+ Res = MCValue::get(Result);
+ break;
+ case MCBinaryExpr::EQ:
+ case MCBinaryExpr::GT:
+ case MCBinaryExpr::GTE:
+ case MCBinaryExpr::LT:
+ case MCBinaryExpr::LTE:
+ case MCBinaryExpr::NE:
+ // A comparison operator returns a -1 if true and 0 if false.
+ Res = MCValue::get(Result ? -1 : 0);
+ break;
+ }
+
return true;
}
}