Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 4f44d46

Browse files
author
Hal Finkel
committed
[PowerPC, AsmParser] Enable the mnemonic spell corrector
r307148 added an assembly mnemonic spelling correction support and enabled it on ARM. This enables that support on PowerPC as well. Patch by Dmitry Venikov, thanks! Differential Revision: https://reviews.llvm.org/D40552 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@320911 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 196a560 commit 4f44d46

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,10 @@ struct PPCOperand : public MCParsedAsmOperand {
393393
/// getEndLoc - Get the location of the last token of this operand.
394394
SMLoc getEndLoc() const override { return EndLoc; }
395395

396+
/// getLocRange - Get the range between the first and last token of this
397+
/// operand.
398+
SMRange getLocRange() const { return SMRange(StartLoc, EndLoc); }
399+
396400
/// isPPC64 - True if this operand is for an instruction in 64-bit mode.
397401
bool isPPC64() const { return IsPPC64; }
398402

@@ -1268,6 +1272,9 @@ void PPCAsmParser::ProcessInstruction(MCInst &Inst,
12681272
}
12691273
}
12701274

1275+
static std::string PPCMnemonicSpellCheck(StringRef S, uint64_t FBS,
1276+
unsigned VariantID = 0);
1277+
12711278
bool PPCAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
12721279
OperandVector &Operands,
12731280
MCStreamer &Out, uint64_t &ErrorInfo,
@@ -1283,8 +1290,13 @@ bool PPCAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
12831290
return false;
12841291
case Match_MissingFeature:
12851292
return Error(IDLoc, "instruction use requires an option to be enabled");
1286-
case Match_MnemonicFail:
1287-
return Error(IDLoc, "unrecognized instruction mnemonic");
1293+
case Match_MnemonicFail: {
1294+
uint64_t FBS = ComputeAvailableFeatures(getSTI().getFeatureBits());
1295+
std::string Suggestion = PPCMnemonicSpellCheck(
1296+
((PPCOperand &)*Operands[0]).getToken(), FBS);
1297+
return Error(IDLoc, "invalid instruction" + Suggestion,
1298+
((PPCOperand &)*Operands[0]).getLocRange());
1299+
}
12881300
case Match_InvalidOperand: {
12891301
SMLoc ErrorLoc = IDLoc;
12901302
if (ErrorInfo != ~0ULL) {
@@ -1920,6 +1932,7 @@ extern "C" void LLVMInitializePowerPCAsmParser() {
19201932

19211933
#define GET_REGISTER_MATCHER
19221934
#define GET_MATCHER_IMPLEMENTATION
1935+
#define GET_MNEMONIC_SPELL_CHECKER
19231936
#include "PPCGenAsmMatcher.inc"
19241937

19251938
// Define this matcher function after the auto-generated include so we
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# RUN: not llvm-mc -triple powerpc-unknown-unknown < %s 2>&1 | FileCheck %s
2+
3+
# This tests the mnemonic spell checker.
4+
5+
# First check what happens when an instruction is omitted:
6+
7+
%r1, %r2, %r3
8+
9+
# CHECK: error: unexpected token at start of statement
10+
# CHECK-NEXT: %r1, %r2, %r3
11+
# CHECK-NEXT: ^
12+
13+
# We don't want to see a suggestion here; the edit distance is too large to
14+
# give sensible suggestions:
15+
16+
aaaaaaaaaaaaaaa %r1, %r2, %r3
17+
18+
# CHECK: error: invalid instruction
19+
# CHECK-NEXT: aaaaaaaaaaaaaaa %r1, %r2, %r3
20+
# CHECK-NEXT: ^
21+
22+
# Check that we get one suggestion: 'vmaxfpg' is 1 edit away, i.e. an deletion.
23+
24+
vmaxfpg %r1, %r2
25+
26+
# CHECK: error: invalid instruction, did you mean: vmaxfp?
27+
# CHECK-NEXT: vmaxfpg %r1, %r2
28+
# CHECK-NEXT: ^
29+
30+
# Check edit distance 1 and 2, just insertions:
31+
32+
xsnmsubad %r1, %r2
33+
34+
# CHECK: error: invalid instruction, did you mean: xsmsubadp, xsnmsubadp?
35+
# CHECK-NEXT: xsnmsubad %r1, %r2
36+
# CHECK-NEXT: ^
37+
38+
# Check an instruction that is 2 edits away, and also has a lot of candidates:
39+
40+
adXd %r1, %r2, %r3
41+
42+
# CHECK: error: invalid instruction, did you mean: add, addc, adde, addi, fadd?
43+
# CHECK-NEXT: adXd %r1, %r2, %r3
44+
# CHECK-NEXT: ^

0 commit comments

Comments
 (0)