Skip to content

Commit 5df4a04

Browse files
committed
Feedback, add check for number of commas with tests
1 parent fdd6c61 commit 5df4a04

File tree

3 files changed

+52
-27
lines changed

3 files changed

+52
-27
lines changed

llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8296,6 +8296,7 @@ bool AMDGPUAsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
82968296

82978297
if (VK != AGVK::AGVK_None && peekToken().is(AsmToken::LParen)) {
82988298
SmallVector<const MCExpr *, 4> Exprs;
8299+
uint64_t CommaCount = 0;
82998300
lex(); // Eat 'max'/'or'
83008301
lex(); // Eat '('
83018302
while (true) {
@@ -8305,14 +8306,22 @@ bool AMDGPUAsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
83058306
"empty " + Twine(TokenId) + " expression");
83068307
return true;
83078308
}
8309+
if (CommaCount + 1 != Exprs.size()) {
8310+
Error(getToken().getLoc(),
8311+
"mismatch of commas in " + Twine(TokenId) + " expression");
8312+
return true;
8313+
}
83088314
Res = AMDGPUVariadicMCExpr::create(VK, Exprs, getContext());
83098315
return false;
83108316
}
83118317
const MCExpr *Expr;
83128318
if (getParser().parseExpression(Expr, EndLoc))
83138319
return true;
83148320
Exprs.push_back(Expr);
8315-
if (!trySkipToken(AsmToken::Comma) && !isToken(AsmToken::RParen)) {
8321+
bool LastTokenWasComma = trySkipToken(AsmToken::Comma);
8322+
if (LastTokenWasComma)
8323+
CommaCount++;
8324+
if (!LastTokenWasComma && !isToken(AsmToken::RParen)) {
83168325
Error(getToken().getLoc(),
83178326
"unexpected token in " + Twine(TokenId) + " expression");
83188327
return true;

llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUMCExpr.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,21 @@ void AMDGPUVariadicMCExpr::printImpl(raw_ostream &OS,
4949
OS << ')';
5050
}
5151

52+
static int64_t Op(AMDGPUVariadicMCExpr::AMDGPUVariadicKind Kind, int64_t Arg1,
53+
int64_t Arg2) {
54+
switch (Kind) {
55+
default:
56+
llvm_unreachable("Unknown AMDGPUVariadicMCExpr kind.");
57+
case AMDGPUVariadicMCExpr::AMDGPUVariadicKind::AGVK_Max:
58+
return std::max(Arg1, Arg2);
59+
case AMDGPUVariadicMCExpr::AMDGPUVariadicKind::AGVK_Or:
60+
return Arg1 || Arg2;
61+
}
62+
}
63+
5264
bool AMDGPUVariadicMCExpr::evaluateAsRelocatableImpl(
5365
MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const {
54-
std::optional<int64_t> Total = {};
55-
56-
auto Op = [this](int64_t Arg1, int64_t Arg2) -> int64_t {
57-
switch (Kind) {
58-
default:
59-
llvm_unreachable("Unknown AMDGPUVariadicMCExpr kind.");
60-
case AGVK_Max:
61-
return std::max(Arg1, Arg2);
62-
case AGVK_Or:
63-
return Arg1 || Arg2;
64-
}
65-
};
66+
std::optional<int64_t> Total;
6667

6768
for (const MCExpr *Arg : Args) {
6869
MCValue ArgRes;
@@ -72,7 +73,7 @@ bool AMDGPUVariadicMCExpr::evaluateAsRelocatableImpl(
7273

7374
if (!Total.has_value())
7475
Total = ArgRes.getConstant();
75-
Total = Op(*Total, ArgRes.getConstant());
76+
Total = Op(Kind, *Total, ArgRes.getConstant());
7677
}
7778

7879
Res = MCValue::get(*Total);

llvm/test/MC/AMDGPU/mcexpr_amd_err.s

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,44 @@
11
// RUN: not llvm-mc -triple amdgcn-amd-amdhsa %s 2>&1 | FileCheck --check-prefix=ASM %s
22

3-
// ASM: 20:22: error: empty max expression
4-
// ASM: 20:22: error: missing expression
5-
// ASM: 21:20: error: empty or expression
6-
// ASM: 21:20: error: missing expression
7-
// ASM: 23:29: error: unknown token in expression
8-
// ASM: 23:29: error: missing expression
9-
// ASM: 24:32: error: unexpected token in max expression
10-
// ASM: 24:32: error: missing expression
11-
// ASM: 25:42: error: unknown token in expression
12-
// ASM: 25:42: error: missing expression
13-
// ASM: 26:38: error: unexpected token in or expression
14-
// ASM: 26:38: error: missing expression
15-
163
.set one, 1
174
.set two, 2
185
.set three, 3
196

207
.set max_empty, max()
8+
// ASM: :[[@LINE-1]]:{{[0-9]+}}: error: empty max expression
9+
// ASM: :[[@LINE-2]]:{{[0-9]+}}: error: missing expression
10+
2111
.set or_empty, or()
12+
// ASM: :[[@LINE-1]]:{{[0-9]+}}: error: empty or expression
13+
// ASM: :[[@LINE-2]]:{{[0-9]+}}: error: missing expression
14+
2215
.set max_post_aux_comma, max(one,)
16+
// ASM: :[[@LINE-1]]:{{[0-9]+}}: error: mismatch of commas in max expression
17+
// ASM: :[[@LINE-2]]:{{[0-9]+}}: error: missing expression
18+
2319
.set max_pre_aux_comma, max(,one)
20+
// asm: :[[@line-1]]:{{[0-9]+}}: error: unknown token in expression
21+
// ASM: :[[@LINE-2]]:{{[0-9]+}}: error: missing expression
22+
23+
.set max_double_comma, max(one,, two)
24+
// ASM: :[[@LINE-1]]:{{[0-9]+}}: error: unknown token in expression
25+
// ASM: :[[@LINE-2]]:{{[0-9]+}}: error: missing expression
26+
27+
.set max_no_comma, max(one two)
28+
// ASM: :[[@LINE-1]]:{{[0-9]+}}: error: unexpected token in max expression
29+
// ASM: :[[@LINE-2]]:{{[0-9]+}}: error: missing expression
30+
2431
.set max_missing_paren, max(two
32+
// ASM: :[[@LINE-1]]:{{[0-9]+}}: error: unexpected token in max expression
33+
// ASM: :[[@LINE-2]]:{{[0-9]+}}: error: missing expression
34+
2535
.set max_expression_one, max(three, four,
36+
// ASM: :[[@LINE-1]]:{{[0-9]+}}: error: unknown token in expression
37+
// ASM: :[[@LINE-2]]:{{[0-9]+}}: error: missing expression
38+
2639
.set or_expression_one, or(four, five
40+
// ASM: :[[@LINE-1]]:{{[0-9]+}}: error: unexpected token in or expression
41+
// ASM: :[[@LINE-2]]:{{[0-9]+}}: error: missing expression
2742

2843
.set four, 4
2944
.set five, 5

0 commit comments

Comments
 (0)