Skip to content

Commit 1cc2103

Browse files
committed
[MC] Support infix operator !
Disabled for Darwin mode. Also disabled for ARM which has compatible aliases (implied 'sp' operand in 'srs*' instructions like 'srsda rust-lang#31!').
1 parent f561713 commit 1cc2103

File tree

5 files changed

+18
-8
lines changed

5 files changed

+18
-8
lines changed

llvm/include/llvm/MC/MCExpr.h

+1
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ class MCBinaryExpr : public MCExpr {
500500
Mul, ///< Multiplication.
501501
NE, ///< Inequality comparison.
502502
Or, ///< Bitwise or.
503+
OrNot, ///< Bitwise or not.
503504
Shl, ///< Shift left.
504505
AShr, ///< Arithmetic shift right.
505506
LShr, ///< Logical shift right.

llvm/lib/MC/MCExpr.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ void MCExpr::print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens) const {
145145
case MCBinaryExpr::Mul: OS << '*'; break;
146146
case MCBinaryExpr::NE: OS << "!="; break;
147147
case MCBinaryExpr::Or: OS << '|'; break;
148+
case MCBinaryExpr::OrNot: OS << '!'; break;
148149
case MCBinaryExpr::Shl: OS << "<<"; break;
149150
case MCBinaryExpr::Sub: OS << '-'; break;
150151
case MCBinaryExpr::Xor: OS << '^'; break;
@@ -920,6 +921,7 @@ bool MCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
920921
case MCBinaryExpr::Mul: Result = LHS * RHS; break;
921922
case MCBinaryExpr::NE: Result = LHS != RHS; break;
922923
case MCBinaryExpr::Or: Result = LHS | RHS; break;
924+
case MCBinaryExpr::OrNot: Result = LHS | ~RHS; break;
923925
case MCBinaryExpr::Shl: Result = uint64_t(LHS) << uint64_t(RHS); break;
924926
case MCBinaryExpr::Sub: Result = LHS - RHS; break;
925927
case MCBinaryExpr::Xor: Result = LHS ^ RHS; break;

llvm/lib/MC/MCParser/AsmParser.cpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -1497,8 +1497,6 @@ static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K,
14971497
return 1;
14981498

14991499
// Low Precedence: |, &, ^
1500-
//
1501-
// FIXME: gas seems to support '!' as an infix operator?
15021500
case AsmToken::Pipe:
15031501
Kind = MCBinaryExpr::Or;
15041502
return 2;
@@ -1559,7 +1557,8 @@ static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K,
15591557
}
15601558
}
15611559

1562-
static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K,
1560+
static unsigned getGNUBinOpPrecedence(const MCAsmInfo &MAI,
1561+
AsmToken::TokenKind K,
15631562
MCBinaryExpr::Opcode &Kind,
15641563
bool ShouldUseLogicalShr) {
15651564
switch (K) {
@@ -1603,12 +1602,18 @@ static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K,
16031602
Kind = MCBinaryExpr::Sub;
16041603
return 4;
16051604

1606-
// High Intermediate Precedence: |, &, ^
1605+
// High Intermediate Precedence: |, !, &, ^
16071606
//
1608-
// FIXME: gas seems to support '!' as an infix operator?
16091607
case AsmToken::Pipe:
16101608
Kind = MCBinaryExpr::Or;
16111609
return 5;
1610+
case AsmToken::Exclaim:
1611+
// Hack to support ARM compatible aliases (implied 'sp' operand in 'srs*'
1612+
// instructions like 'srsda #31!') and not parse ! as an infix operator.
1613+
if (MAI.getCommentString() == "@")
1614+
return 0;
1615+
Kind = MCBinaryExpr::OrNot;
1616+
return 5;
16121617
case AsmToken::Caret:
16131618
Kind = MCBinaryExpr::Xor;
16141619
return 5;
@@ -1639,7 +1644,7 @@ unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K,
16391644
MCBinaryExpr::Opcode &Kind) {
16401645
bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr();
16411646
return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr)
1642-
: getGNUBinOpPrecedence(K, Kind, ShouldUseLogicalShr);
1647+
: getGNUBinOpPrecedence(MAI, K, Kind, ShouldUseLogicalShr);
16431648
}
16441649

16451650
/// Parse all binary operators with precedence >= 'Precedence'.

llvm/lib/MC/MCParser/MasmParser.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -1720,8 +1720,6 @@ static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K,
17201720
return 4;
17211721

17221722
// High Intermediate Precedence: |, &, ^
1723-
//
1724-
// FIXME: gas seems to support '!' as an infix operator?
17251723
case AsmToken::Pipe:
17261724
Kind = MCBinaryExpr::Or;
17271725
return 5;

llvm/test/MC/AsmParser/exprs-gnu.s

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

0 commit comments

Comments
 (0)