[llvm] r241085 - MIR Parser: make the machine instruction parsing interface more consistent. NFC.

Alex Lorenz arphaman at gmail.com
Tue Jun 30 10:47:50 PDT 2015


Author: arphaman
Date: Tue Jun 30 12:47:50 2015
New Revision: 241085

URL: http://llvm.org/viewvc/llvm-project?rev=241085&view=rev
Log:
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'.

Modified:
    llvm/trunk/lib/CodeGen/MIRParser/MIParser.cpp
    llvm/trunk/lib/CodeGen/MIRParser/MIParser.h
    llvm/trunk/lib/CodeGen/MIRParser/MIRParser.cpp

Modified: llvm/trunk/lib/CodeGen/MIRParser/MIParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MIRParser/MIParser.cpp?rev=241085&r1=241084&r2=241085&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MIRParser/MIParser.cpp (original)
+++ llvm/trunk/lib/CodeGen/MIRParser/MIParser.cpp Tue Jun 30 12:47:50 2015
@@ -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
   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(Str
   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);
 }

Modified: llvm/trunk/lib/CodeGen/MIRParser/MIParser.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MIRParser/MIParser.h?rev=241085&r1=241084&r2=241085&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MIRParser/MIParser.h (original)
+++ llvm/trunk/lib/CodeGen/MIRParser/MIParser.h Tue Jun 30 12:47:50 2015
@@ -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
 

Modified: llvm/trunk/lib/CodeGen/MIRParser/MIRParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MIRParser/MIRParser.cpp?rev=241085&r1=241084&r2=241085&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/MIRParser/MIRParser.cpp (original)
+++ llvm/trunk/lib/CodeGen/MIRParser/MIRParser.cpp Tue Jun 30 12:47:50 2015
@@ -265,13 +265,13 @@ bool MIRParserImpl::initializeMachineBas
   // 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;
 }





More information about the llvm-commits mailing list