[MC] Support infix operator !

Disabled for Darwin mode.

Also disabled for ARM which has compatible aliases (implied 'sp' operand
in 'srs*' instructions like 'srsda #31!').
This commit is contained in:
Fangrui Song 2020-07-30 23:24:09 -07:00
parent 3b5a9e3f2b
commit 2085074770
5 changed files with 18 additions and 8 deletions

View File

@ -500,6 +500,7 @@ public:
Mul, ///< Multiplication.
NE, ///< Inequality comparison.
Or, ///< Bitwise or.
OrNot, ///< Bitwise or not.
Shl, ///< Shift left.
AShr, ///< Arithmetic shift right.
LShr, ///< Logical shift right.

View File

@ -145,6 +145,7 @@ void MCExpr::print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens) const {
case MCBinaryExpr::Mul: OS << '*'; break;
case MCBinaryExpr::NE: OS << "!="; break;
case MCBinaryExpr::Or: OS << '|'; break;
case MCBinaryExpr::OrNot: OS << '!'; break;
case MCBinaryExpr::Shl: OS << "<<"; break;
case MCBinaryExpr::Sub: OS << '-'; break;
case MCBinaryExpr::Xor: OS << '^'; break;
@ -920,6 +921,7 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
case MCBinaryExpr::Mul: Result = LHS * RHS; break;
case MCBinaryExpr::NE: Result = LHS != RHS; break;
case MCBinaryExpr::Or: Result = LHS | RHS; break;
case MCBinaryExpr::OrNot: Result = LHS | ~RHS; break;
case MCBinaryExpr::Shl: Result = uint64_t(LHS) << uint64_t(RHS); break;
case MCBinaryExpr::Sub: Result = LHS - RHS; break;
case MCBinaryExpr::Xor: Result = LHS ^ RHS; break;

View File

@ -1497,8 +1497,6 @@ static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K,
return 1;
// Low Precedence: |, &, ^
//
// FIXME: gas seems to support '!' as an infix operator?
case AsmToken::Pipe:
Kind = MCBinaryExpr::Or;
return 2;
@ -1559,7 +1557,8 @@ static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K,
}
}
static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K,
static unsigned getGNUBinOpPrecedence(const MCAsmInfo &MAI,
AsmToken::TokenKind K,
MCBinaryExpr::Opcode &Kind,
bool ShouldUseLogicalShr) {
switch (K) {
@ -1603,12 +1602,18 @@ static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K,
Kind = MCBinaryExpr::Sub;
return 4;
// High Intermediate Precedence: |, &, ^
// High Intermediate Precedence: |, !, &, ^
//
// FIXME: gas seems to support '!' as an infix operator?
case AsmToken::Pipe:
Kind = MCBinaryExpr::Or;
return 5;
case AsmToken::Exclaim:
// Hack to support ARM compatible aliases (implied 'sp' operand in 'srs*'
// instructions like 'srsda #31!') and not parse ! as an infix operator.
if (MAI.getCommentString() == "@")
return 0;
Kind = MCBinaryExpr::OrNot;
return 5;
case AsmToken::Caret:
Kind = MCBinaryExpr::Xor;
return 5;
@ -1639,7 +1644,7 @@ unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K,
MCBinaryExpr::Opcode &Kind) {
bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr();
return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr)
: getGNUBinOpPrecedence(K, Kind, ShouldUseLogicalShr);
: getGNUBinOpPrecedence(MAI, K, Kind, ShouldUseLogicalShr);
}
/// Parse all binary operators with precedence >= 'Precedence'.

View File

@ -1720,8 +1720,6 @@ static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K,
return 4;
// High Intermediate Precedence: |, &, ^
//
// FIXME: gas seems to support '!' as an infix operator?
case AsmToken::Pipe:
Kind = MCBinaryExpr::Or;
return 5;

View File

@ -0,0 +1,4 @@
# RUN: llvm-mc -triple=x86_64 %s | FileCheck %s
# CHECK: movl %eax, 15
movl %eax, 3 ! ~12