Index: lib/Target/ARM/AsmParser/ARMAsmParser.cpp =================================================================== --- lib/Target/ARM/AsmParser/ARMAsmParser.cpp +++ lib/Target/ARM/AsmParser/ARMAsmParser.cpp @@ -8996,6 +8996,8 @@ return PlainMatchResult; } +std::string ARMMnemonicSpellCheck(std::string S, uint64_t FBS); + static const char *getSubtargetFeatureName(uint64_t Val); bool ARMAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode, OperandVector &Operands, @@ -9089,9 +9091,13 @@ return Error(ErrorLoc, "invalid operand for instruction"); } - case Match_MnemonicFail: - return Error(IDLoc, "invalid instruction", + case Match_MnemonicFail: { + uint64_t FBS = ComputeAvailableFeatures(getSTI().getFeatureBits()); + std::string Suggestion = ARMMnemonicSpellCheck( + std::string(((ARMOperand &)*Operands[0]).getToken()), FBS); + return Error(IDLoc, "invalid instruction" + Suggestion, ((ARMOperand &)*Operands[0]).getLocRange()); + } case Match_RequiresNotITBlock: return Error(IDLoc, "flag setting instruction only valid outside IT block"); case Match_RequiresITBlock: Index: test/MC/ARM/invalid-instructions.s =================================================================== --- /dev/null +++ test/MC/ARM/invalid-instructions.s @@ -0,0 +1,51 @@ +@ RUN: not llvm-mc -triple=arm -show-encoding < %s 2>&1 | FileCheck %s + +@ First check what happens when an instruction is omitted: + + r1, r2, r3 + +@ CHECK: error: unexpected token in operand +@ CHECK-NEXT: r1, r2, r3 +@ CHECK-NEXT: ^ + +@ We don't want to see a suggestion here; the edit distance is too large to +@ give sensible suggestions: + + aaaaaaaaaaaaaaa r1, r2, r3 + +@ CHECK: error: invalid instruction +@ CHECK-NEXT: aaaaaaaaaaaaaaa r1, r2, r3 +@ CHECK-NEXT: ^ + +@ Check that we get one suggestion: 'adXd' is 1 edit away, an insertion, +@ from an alternative 'add': + + adXd r1, r2, r3 + +@ CHECK: error: invalid instruction, did you mean: add? +@ CHECK-NEXT: adXd r1, r2, r3 +@ CHECK-NEXT: ^ + +@ Check edit distance 1, a deletion: + + ad r1, r2, r3 + +@ CHECK: error: invalid instruction, did you mean: adc, add, adr, and? +@ CHECK-NEXT: ad r1, r2, r3 +@ CHECK-NEXT: ^ + +@ Let's check another one that is 1 edit away, an subsitution: + + ldrea r1, r2, r3 + +@ CHECK: error: invalid instruction, did you mean: ldrex? +@ CHECK-NEXT: ldrea r1, r2, r3 +@ CHECK-NEXT: ^ + +@ Check an instruction that is 2 edits away, resuling in a number of suggestions: + + ldre r1, r2, r3 + +@CHECK: error: invalid instruction, did you mean: ldr, ldrb, ldrd, ldrex, ldrh, ldrt? +@CHECK-NEXT: ldre r1, r2, r3 +@CHECK-NEXT: ^ Index: utils/TableGen/AsmMatcherEmitter.cpp =================================================================== --- utils/TableGen/AsmMatcherEmitter.cpp +++ utils/TableGen/AsmMatcherEmitter.cpp @@ -2711,6 +2711,72 @@ OS << "}\n\n"; } +static void emitMnemonicSpellChecker(raw_ostream &OS, CodeGenTarget &Target, + unsigned VariantCount) { + OS << "// Simple implementation of Levenshtein Distance calculation.\n"; + OS << "static unsigned EditDistance(std::string S, std::string T) {\n"; + OS << " assert(S.size() && T.size());\n"; + OS << " const unsigned N = S.size() + 1;\n"; + OS << " const unsigned M = T.size() + 1;\n"; + OS << " std::vector >Mat(M, std::vector(N));\n"; + OS << "\n"; + OS << " for (unsigned i = 0; i Candidates;\n"; + OS << " std::string Prev = \"\";\n"; + OS << " auto End = std::end(MatchTable0);\n"; + OS << "\n"; + OS << " for (auto I = std::begin(MatchTable0); I < End; I++) {\n"; + OS << " if (!(I->RequiredFeatures & FBS))\n"; + OS << " continue;\n"; + OS << " std::string T(I->getMnemonic().str());\n"; + OS << " // Avoid recomputing the edit distance for the same string.\n"; + OS << " if(T == Prev)\n"; + OS << " continue;\n"; + OS << "\n"; + OS << " Prev = T;\n"; + OS << " unsigned Dist = EditDistance(S, T);\n"; + OS << " if (Dist < MinDist) {\n"; + OS << " Candidates.clear();\n"; + OS << " Candidates.push_back(T);\n"; + OS << " MinDist = Dist;\n"; + OS << " } else if (Dist == MinDist)\n"; + OS << " Candidates.push_back(T);\n"; + OS << " }\n"; + OS << "\n"; + OS << " // Edit distance bigger than 2 are probably not useful suggestions.\n"; + OS << " if (MinDist > 2 || Candidates.empty())\n"; + OS << " return \"\";\n"; + OS << "\n"; + OS << " std::string Res = \", did you mean: \";\n"; + OS << " unsigned i = 0;\n"; + OS << " for( ; i < Candidates.size() - 1; i++)\n"; + OS << " Res += Candidates[i] + \", \";\n"; + OS << " return Res + Candidates[i] + \"?\";\n"; + } + OS << "}\n"; + OS << "\n"; +} + + void AsmMatcherEmitter::run(raw_ostream &OS) { CodeGenTarget Target(Records); Record *AsmParser = Target.getAsmParser(); @@ -2974,6 +3040,8 @@ OS << "};\n\n"; } + emitMnemonicSpellChecker(OS, Target, VariantCount); + // Finally, build the match function. OS << "unsigned " << Target.getName() << ClassName << "::\n" << "MatchInstructionImpl(const OperandVector &Operands,\n";