Skip to content

Commit

Permalink
[MC] Support infix operator !
Browse files Browse the repository at this point in the history
Disabled for Darwin mode.

Also disabled for ARM which has compatible aliases (implied 'sp' operand
in 'srs*' instructions like 'srsda rust-lang#31!').
  • Loading branch information
MaskRay committed Jul 31, 2020
1 parent f561713 commit 1cc2103
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
1 change: 1 addition & 0 deletions llvm/include/llvm/MC/MCExpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ class MCBinaryExpr : public MCExpr {
Mul, ///< Multiplication.
NE, ///< Inequality comparison.
Or, ///< Bitwise or.
OrNot, ///< Bitwise or not.
Shl, ///< Shift left.
AShr, ///< Arithmetic shift right.
LShr, ///< Logical shift right.
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/MC/MCExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 11 additions & 6 deletions llvm/lib/MC/MCParser/AsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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'.
Expand Down
2 changes: 0 additions & 2 deletions llvm/lib/MC/MCParser/MasmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions llvm/test/MC/AsmParser/exprs-gnu.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# RUN: llvm-mc -triple=x86_64 %s | FileCheck %s

# CHECK: movl %eax, 15
movl %eax, 3 ! ~12

0 comments on commit 1cc2103

Please sign in to comment.