Skip to content

Commit a17a7b6

Browse files
author
Simon Dardis
committedOct 10, 2017
[mips] Partially fix PR34391
Previously, the parsing of the 'subu $reg, ($reg,) imm' relied on a parser which also rendered the operand to the instruction. In some cases the general parser could construct an MCExpr which was not a MCConstantExpr which MipsAsmParser was expecting. Address this by altering the special handling to cope with unexpected inputs and fine-tune the handling of cases where an register name that is not available in the current ABI is regarded as not a match for the custom parser but also not as an outright error. Also enforces the binutils restriction that only constants are accepted. This partially resolves PR34391. Thanks to Ed Maste for reporting the issue! Reviewers: nitesh.jain, arichardson Differential Revision: https://reviews.llvm.org/D37476 llvm-svn: 315310
1 parent f01516d commit a17a7b6

File tree

3 files changed

+84
-4
lines changed

3 files changed

+84
-4
lines changed
 

‎llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp

+11-4
Original file line numberDiff line numberDiff line change
@@ -5859,14 +5859,21 @@ OperandMatchResultTy
58595859
MipsAsmParser::parseInvNum(OperandVector &Operands) {
58605860
MCAsmParser &Parser = getParser();
58615861
const MCExpr *IdVal;
5862-
// If the first token is '$' we may have register operand.
5863-
if (Parser.getTok().is(AsmToken::Dollar))
5864-
return MatchOperand_NoMatch;
5862+
// If the first token is '$' we may have register operand. We have to reject
5863+
// cases where it is not a register. Complicating the matter is that
5864+
// register names are not reserved across all ABIs.
5865+
// Peek past the dollar to see if it's a register name for this ABI.
58655866
SMLoc S = Parser.getTok().getLoc();
5867+
if (Parser.getTok().is(AsmToken::Dollar)) {
5868+
return matchCPURegisterName(Parser.getLexer().peekTok().getString()) == -1
5869+
? MatchOperand_ParseFail
5870+
: MatchOperand_NoMatch;
5871+
}
58665872
if (getParser().parseExpression(IdVal))
58675873
return MatchOperand_ParseFail;
58685874
const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(IdVal);
5869-
assert(MCE && "Unexpected MCExpr type.");
5875+
if (!MCE)
5876+
return MatchOperand_NoMatch;
58705877
int64_t Val = MCE->getValue();
58715878
SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
58725879
Operands.push_back(MipsOperand::CreateImm(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# RUN: not llvm-mc -arch=mips %s 2>%t1
2+
# RUN: FileCheck --check-prefix=O32 %s < %t1
3+
4+
# RUN: not llvm-mc -arch=mips64 %s 2>%t1
5+
# RUN: FileCheck --check-prefix=N64 %s < %t1
6+
7+
# Check that subu only rejects any non-constant values.
8+
9+
.globl end
10+
subu $4, $4, %lo($start) # O32: [[@LINE]]:{{[0-9]+}}: error: unexpected token in argument list
11+
# N64: [[@LINE-1]]:{{[0-9]+}}: error: unexpected token in argument list
12+
subu $4, $4, $start # O32: [[@LINE]]:{{[0-9]+}}: error: unexpected token in argument list
13+
# N64: [[@LINE-1]]:{{[0-9]+}}: error: unexpected token in argument list
14+
subu $4, $a4, $a4 # O32: [[@LINE]]:{{[0-9]+}}: error: unexpected token in argument list
15+
subu $4, $4, %hi(end) # O32: [[@LINE]]:{{[0-9]+}}: error: unexpected token in argument list
16+
# N64: [[@LINE-1]]:{{[0-9]+}}: error: unexpected token in argument list
17+
subu $4, $4, end + 4 # O32: [[@LINE]]:{{[0-9]+}}: error: unexpected token in argument list
18+
# N64: [[@LINE-1]]:{{[0-9]+}}: error: unexpected token in argument list
19+
subu $4, $4, end # O32: [[@LINE]]:{{[0-9]+}}: error: unexpected token in argument list
20+
# N64: [[@LINE-1]]:{{[0-9]+}}: error: unexpected token in argument list
21+
subu $4, $4, sp # O32: [[@LINE]]:{{[0-9]+}}: error: unexpected token in argument list
22+
# N64: [[@LINE-1]]:{{[0-9]+}}: error: unexpected token in argument list
23+
24+
subu $4, %lo($start) # O32: [[@LINE]]:{{[0-9]+}}: error: unexpected token in argument list
25+
# N64: [[@LINE-1]]:{{[0-9]+}}: error: unexpected token in argument list
26+
subu $4, $start # O32: [[@LINE]]:{{[0-9]+}}: error: unexpected token in argument list
27+
# N64: [[@LINE-1]]:{{[0-9]+}}: error: unexpected token in argument list
28+
subu $4, $a4 # O32: [[@LINE]]:{{[0-9]+}}: error: unexpected token in argument list
29+
subu $4, %hi(end) # O32: [[@LINE]]:{{[0-9]+}}: error: unexpected token in argument list
30+
# N64: [[@LINE-1]]:{{[0-9]+}}: error: unexpected token in argument list
31+
subu $4, end + 4 # O32: [[@LINE]]:{{[0-9]+}}: error: unexpected token in argument list
32+
# N64: [[@LINE-1]]:{{[0-9]+}}: error: unexpected token in argument list
33+
subu $4, end # O32: [[@LINE]]:{{[0-9]+}}: error: unexpected token in argument list
34+
# N64: [[@LINE-1]]:{{[0-9]+}}: error: unexpected token in argument list
35+
subu $4, sp # O32: [[@LINE]]:{{[0-9]+}}: error: unexpected token in argument list
36+
# N64: [[@LINE-1]]:{{[0-9]+}}: error: unexpected token in argument list
37+
38+
$start:

‎llvm/test/MC/Mips/macro-aliases.s

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# RUN: llvm-mc -arch=mips -mcpu=mips32r2 %s -show-inst | FileCheck %s
2+
3+
# Test that subu accepts constant operands and inverts them when
4+
# rendering the operand.
5+
6+
subu $4, $4, 4 # CHECK: ADDiu
7+
# CHECK; Imm:-4
8+
subu $gp, $gp, 4 # CHECK: ADDiu
9+
# CHECK; Imm:-4
10+
subu $sp, $sp, 4 # CHECK: ADDiu
11+
# CHECK; Imm:-4
12+
subu $4, $4, -4 # CHECK: ADDiu
13+
# CHECK; Imm:4
14+
subu $gp, $gp, -4 # CHECK: ADDiu
15+
# CHECK; Imm:4
16+
subu $sp, $sp, -4 # CHECK: ADDiu
17+
# CHECK; Imm:4
18+
subu $sp, $sp, -(4 + 4) # CHECK: ADDiu
19+
# CHECK: Imm:8
20+
21+
subu $4, 8 # CHECK: ADDiu
22+
# CHECK; Imm:-8
23+
subu $gp, 8 # CHECK: ADDiu
24+
# CHECK; Imm:-8
25+
subu $sp, 8 # CHECK: ADDiu
26+
# CHECK; Imm:-8
27+
subu $4, -8 # CHECK: ADDiu
28+
# CHECK; Imm:8
29+
subu $gp, -8 # CHECK: ADDiu
30+
# CHECK; Imm:8
31+
subu $sp, -8 # CHECK: ADDiu
32+
# CHECK; Imm:8
33+
subu $sp, -(4 + 4) # CHECK: ADDiu
34+
# CHECK: Imm:8
35+

0 commit comments

Comments
 (0)
Please sign in to comment.