in the case where an instruction only has one implementation

of a mneumonic, report operand errors with better location
info.  For example, we now report:

t.s:6:14: error: invalid operand for instruction
        cwtl $1
             ^

but we fail for common cases like:

t.s:11:4: error: invalid operand for instruction
   addl $1, $1
   ^

because we don't know if this is supposed to be the reg/imm or imm/reg
form.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@113178 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2010-09-06 22:11:18 +00:00
parent a008e8ac73
commit ce4a3355d9
3 changed files with 48 additions and 19 deletions

View File

@ -83,7 +83,8 @@ private:
bool MatchInstruction(SMLoc IDLoc, bool MatchInstruction(SMLoc IDLoc,
const SmallVectorImpl<MCParsedAsmOperand*> &Operands, const SmallVectorImpl<MCParsedAsmOperand*> &Operands,
MCInst &Inst) { MCInst &Inst) {
if (MatchInstructionImpl(Operands, Inst) == Match_Success) unsigned ErrorInfo;
if (MatchInstructionImpl(Operands, Inst, ErrorInfo) == Match_Success)
return false; return false;
// FIXME: We should give nicer diagnostics about the exact failure. // FIXME: We should give nicer diagnostics about the exact failure.

View File

@ -863,9 +863,10 @@ X86ATTAsmParser::MatchInstruction(SMLoc IDLoc,
assert(!Operands.empty() && "Unexpect empty operand list!"); assert(!Operands.empty() && "Unexpect empty operand list!");
bool WasOriginallyInvalidOperand = false; bool WasOriginallyInvalidOperand = false;
unsigned OrigErrorInfo;
// First, try a direct match. // First, try a direct match.
switch (MatchInstructionImpl(Operands, Inst)) { switch (MatchInstructionImpl(Operands, Inst, OrigErrorInfo)) {
case Match_Success: case Match_Success:
return false; return false;
case Match_MissingFeature: case Match_MissingFeature:
@ -895,13 +896,14 @@ X86ATTAsmParser::MatchInstruction(SMLoc IDLoc,
// Check for the various suffix matches. // Check for the various suffix matches.
Tmp[Base.size()] = 'b'; Tmp[Base.size()] = 'b';
MatchResultTy MatchB = MatchInstructionImpl(Operands, Inst); unsigned BErrorInfo, WErrorInfo, LErrorInfo, QErrorInfo;
MatchResultTy MatchB = MatchInstructionImpl(Operands, Inst, BErrorInfo);
Tmp[Base.size()] = 'w'; Tmp[Base.size()] = 'w';
MatchResultTy MatchW = MatchInstructionImpl(Operands, Inst); MatchResultTy MatchW = MatchInstructionImpl(Operands, Inst, WErrorInfo);
Tmp[Base.size()] = 'l'; Tmp[Base.size()] = 'l';
MatchResultTy MatchL = MatchInstructionImpl(Operands, Inst); MatchResultTy MatchL = MatchInstructionImpl(Operands, Inst, LErrorInfo);
Tmp[Base.size()] = 'q'; Tmp[Base.size()] = 'q';
MatchResultTy MatchQ = MatchInstructionImpl(Operands, Inst); MatchResultTy MatchQ = MatchInstructionImpl(Operands, Inst, QErrorInfo);
// Restore the old token. // Restore the old token.
Op->setTokenValue(Base); Op->setTokenValue(Base);
@ -952,13 +954,22 @@ X86ATTAsmParser::MatchInstruction(SMLoc IDLoc,
// mnemonic was invalid. // mnemonic was invalid.
if ((MatchB == Match_MnemonicFail) && (MatchW == Match_MnemonicFail) && if ((MatchB == Match_MnemonicFail) && (MatchW == Match_MnemonicFail) &&
(MatchL == Match_MnemonicFail) && (MatchQ == Match_MnemonicFail)) { (MatchL == Match_MnemonicFail) && (MatchQ == Match_MnemonicFail)) {
if (WasOriginallyInvalidOperand) if (!WasOriginallyInvalidOperand) {
Error(IDLoc, "invalid operand for instruction");
else
Error(IDLoc, "invalid instruction mnemonic '" + Base + "'"); Error(IDLoc, "invalid instruction mnemonic '" + Base + "'");
return true; return true;
} }
// Recover location info for the operand if we know which was the problem.
SMLoc ErrorLoc = IDLoc;
if (OrigErrorInfo != ~0U) {
ErrorLoc = ((X86Operand*)Operands[OrigErrorInfo])->getStartLoc();
if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
}
Error(ErrorLoc, "invalid operand for instruction");
return true;
}
// If one instruction matched with a missing feature, report this as a // If one instruction matched with a missing feature, report this as a
// missing feature. // missing feature.
if ((MatchB == Match_MissingFeature) + (MatchW == Match_MissingFeature) + if ((MatchB == Match_MissingFeature) + (MatchW == Match_MissingFeature) +

View File

@ -1563,7 +1563,7 @@ void AsmMatcherEmitter::run(raw_ostream &OS) {
OS << " Match_MissingFeature\n"; OS << " Match_MissingFeature\n";
OS << " };\n"; OS << " };\n";
OS << " MatchResultTy MatchInstructionImpl(const SmallVectorImpl<MCParsedAsmOperand*>" OS << " MatchResultTy MatchInstructionImpl(const SmallVectorImpl<MCParsedAsmOperand*>"
<< " &Operands, MCInst &Inst);\n\n"; << " &Operands, MCInst &Inst, unsigned &ErrorInfo);\n\n";
OS << "#endif // GET_ASSEMBLER_HEADER_INFO\n\n"; OS << "#endif // GET_ASSEMBLER_HEADER_INFO\n\n";
@ -1679,16 +1679,19 @@ void AsmMatcherEmitter::run(raw_ostream &OS) {
<< Target.getName() << ClassName << "::\n" << Target.getName() << ClassName << "::\n"
<< "MatchInstructionImpl(const SmallVectorImpl<MCParsedAsmOperand*>" << "MatchInstructionImpl(const SmallVectorImpl<MCParsedAsmOperand*>"
<< " &Operands,\n"; << " &Operands,\n";
OS << " MCInst &Inst) {\n"; OS << " MCInst &Inst, unsigned &ErrorInfo) {\n";
// Emit code to get the available features. // Emit code to get the available features.
OS << " // Get the current feature set.\n"; OS << " // Get the current feature set.\n";
OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n"; OS << " unsigned AvailableFeatures = getAvailableFeatures();\n\n";
OS << " ErrorInfo = 0;\n";
// Emit code to compute the class list for this operand vector. // Emit code to compute the class list for this operand vector.
OS << " // Eliminate obvious mismatches.\n"; OS << " // Eliminate obvious mismatches.\n";
OS << " if (Operands.size() > " << MaxNumOperands << "+1)\n"; OS << " if (Operands.size() > " << (MaxNumOperands+1) << ") {\n";
OS << " return Match_InvalidOperand;\n\n"; OS << " ErrorInfo = " << (MaxNumOperands+1) << ";\n";
OS << " return Match_InvalidOperand;\n";
OS << " }\n\n";
OS << " // Compute the class list for this operand vector.\n"; OS << " // Compute the class list for this operand vector.\n";
OS << " MatchClassKind Classes[" << MaxNumOperands << "];\n"; OS << " MatchClassKind Classes[" << MaxNumOperands << "];\n";
@ -1696,8 +1699,10 @@ void AsmMatcherEmitter::run(raw_ostream &OS) {
OS << " Classes[i-1] = ClassifyOperand(Operands[i]);\n\n"; OS << " Classes[i-1] = ClassifyOperand(Operands[i]);\n\n";
OS << " // Check for invalid operands before matching.\n"; OS << " // Check for invalid operands before matching.\n";
OS << " if (Classes[i-1] == InvalidMatchClass)\n"; OS << " if (Classes[i-1] == InvalidMatchClass) {\n";
OS << " ErrorInfo = i;\n";
OS << " return Match_InvalidOperand;\n"; OS << " return Match_InvalidOperand;\n";
OS << " }\n";
OS << " }\n\n"; OS << " }\n\n";
OS << " // Mark unused classes.\n"; OS << " // Mark unused classes.\n";
@ -1729,11 +1734,22 @@ void AsmMatcherEmitter::run(raw_ostream &OS) {
OS << " assert(Mnemonic == it->Mnemonic);\n"; OS << " assert(Mnemonic == it->Mnemonic);\n";
// Emit check that the subclasses match. // Emit check that the subclasses match.
for (unsigned i = 0; i != MaxNumOperands; ++i) { OS << " bool OperandsValid = true;\n";
OS << " if (!IsSubclass(Classes[" OS << " for (unsigned i = 0; i != " << MaxNumOperands << "; ++i) {\n";
<< i << "], it->Classes[" << i << "]))\n"; OS << " if (IsSubclass(Classes[i], it->Classes[i]))\n";
OS << " continue;\n"; OS << " continue;\n";
} OS << " // If there is only one instruction with this opcode, report\n";
OS << " // this as an operand error with location info.\n";
OS << " if (MnemonicRange.first+1 == ie) {\n";
OS << " ErrorInfo = i+1;\n";
OS << " return Match_InvalidOperand;\n";
OS << " }\n";
OS << " // Otherwise, just reject this instance of the mnemonic.\n";
OS << " OperandsValid = false;\n";
OS << " break;\n";
OS << " }\n\n";
OS << " if (!OperandsValid) continue;\n";
// Emit check that the required features are available. // Emit check that the required features are available.
OS << " if ((AvailableFeatures & it->RequiredFeatures) " OS << " if ((AvailableFeatures & it->RequiredFeatures) "
@ -1756,6 +1772,7 @@ void AsmMatcherEmitter::run(raw_ostream &OS) {
OS << " // Okay, we had no match. Try to return a useful error code.\n"; OS << " // Okay, we had no match. Try to return a useful error code.\n";
OS << " if (HadMatchOtherThanFeatures) return Match_MissingFeature;\n"; OS << " if (HadMatchOtherThanFeatures) return Match_MissingFeature;\n";
OS << " ErrorInfo = ~0U;\n";
OS << " return Match_InvalidOperand;\n"; OS << " return Match_InvalidOperand;\n";
OS << "}\n\n"; OS << "}\n\n";