MIR Parser: make the machine instruction parsing interface more consistent. NFC.

This commit refactors the interface for machine instruction parser. It adopts
the pattern of returning a bool and passing in the result in the first argument
that is used by the other parsing methods for the the method 'parse' and the
function 'parseMachineInstr'.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241085 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Alex Lorenz 2015-06-30 17:47:50 +00:00
parent 8f1e30d67c
commit 939f023bf9
3 changed files with 26 additions and 30 deletions

View File

@ -63,7 +63,7 @@ public:
/// This function always return true.
bool error(StringRef::iterator Loc, const Twine &Msg);
MachineInstr *parse();
bool parse(MachineInstr *&MI);
bool parseRegister(unsigned &Reg);
bool parseRegisterOperand(MachineOperand &Dest, bool IsDef = false);
@ -129,7 +129,7 @@ bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
return true;
}
MachineInstr *MIParser::parse() {
bool MIParser::parse(MachineInstr *&MI) {
lex();
// Parse any register operands before '='
@ -138,32 +138,28 @@ MachineInstr *MIParser::parse() {
SmallVector<MachineOperand, 8> Operands;
if (Token.isRegister()) {
if (parseRegisterOperand(MO, /*IsDef=*/true))
return nullptr;
return true;
Operands.push_back(MO);
if (Token.isNot(MIToken::equal)) {
error("expected '='");
return nullptr;
}
if (Token.isNot(MIToken::equal))
return error("expected '='");
lex();
}
unsigned OpCode;
if (Token.isError() || parseInstruction(OpCode))
return nullptr;
return true;
// TODO: Parse the instruction flags and memory operands.
// Parse the remaining machine operands.
while (Token.isNot(MIToken::Eof)) {
if (parseMachineOperand(MO))
return nullptr;
return true;
Operands.push_back(MO);
if (Token.is(MIToken::Eof))
break;
if (Token.isNot(MIToken::comma)) {
error("expected ',' before the next machine operand");
return nullptr;
}
if (Token.isNot(MIToken::comma))
return error("expected ',' before the next machine operand");
lex();
}
@ -184,10 +180,10 @@ MachineInstr *MIParser::parse() {
// TODO: Determine the implicit behaviour when implicit register flags are
// parsed.
auto *MI = MF.CreateMachineInstr(MCID, DebugLoc(), /*NoImplicit=*/true);
MI = MF.CreateMachineInstr(MCID, DebugLoc(), /*NoImplicit=*/true);
for (const auto &Operand : Operands)
MI->addOperand(MF, Operand);
return MI;
return false;
}
bool MIParser::parseInstruction(unsigned &OpCode) {
@ -390,9 +386,9 @@ const uint32_t *MIParser::getRegMask(StringRef Identifier) {
return RegMaskInfo->getValue();
}
MachineInstr *
llvm::parseMachineInstr(SourceMgr &SM, MachineFunction &MF, StringRef Src,
const DenseMap<unsigned, MachineBasicBlock *> &MBBSlots,
const SlotMapping &IRSlots, SMDiagnostic &Error) {
return MIParser(SM, MF, Error, Src, MBBSlots, IRSlots).parse();
bool llvm::parseMachineInstr(
MachineInstr *&MI, SourceMgr &SM, MachineFunction &MF, StringRef Src,
const DenseMap<unsigned, MachineBasicBlock *> &MBBSlots,
const SlotMapping &IRSlots, SMDiagnostic &Error) {
return MIParser(SM, MF, Error, Src, MBBSlots, IRSlots).parse(MI);
}

View File

@ -26,10 +26,10 @@ struct SlotMapping;
class SMDiagnostic;
class SourceMgr;
MachineInstr *
parseMachineInstr(SourceMgr &SM, MachineFunction &MF, StringRef Src,
const DenseMap<unsigned, MachineBasicBlock *> &MBBSlots,
const SlotMapping &IRSlots, SMDiagnostic &Error);
bool parseMachineInstr(MachineInstr *&MI, SourceMgr &SM, MachineFunction &MF,
StringRef Src,
const DenseMap<unsigned, MachineBasicBlock *> &MBBSlots,
const SlotMapping &IRSlots, SMDiagnostic &Error);
} // end namespace llvm

View File

@ -265,13 +265,13 @@ bool MIRParserImpl::initializeMachineBasicBlock(
// Parse the instructions.
for (const auto &MISource : YamlMBB.Instructions) {
SMDiagnostic Error;
if (auto *MI = parseMachineInstr(SM, MF, MISource.Value, MBBSlots, IRSlots,
Error)) {
MBB.insert(MBB.end(), MI);
continue;
MachineInstr *MI = nullptr;
if (parseMachineInstr(MI, SM, MF, MISource.Value, MBBSlots, IRSlots,
Error)) {
reportDiagnostic(diagFromMIStringDiag(Error, MISource.SourceRange));
return true;
}
reportDiagnostic(diagFromMIStringDiag(Error, MISource.SourceRange));
return true;
MBB.insert(MBB.end(), MI);
}
return false;
}