From 993edf92bed1c66ad4fb960c38b68118f26dcaea Mon Sep 17 00:00:00 2001
From: Nguyen Anh Quynh <aquynh@gmail.com>
Date: Sun, 29 May 2016 10:57:02 +0800
Subject: [PATCH] handle error of EmitIntValue() to fix some ARM assertation
 bugs

---
 llvm/include/llvm/MC/MCStreamer.h             |   2 +-
 llvm/lib/MC/MCELFStreamer.cpp                 |   5 +-
 llvm/lib/MC/MCObjectStreamer.cpp              |  11 +-
 llvm/lib/MC/MCParser/AsmParser.cpp            |  39 ++--
 llvm/lib/MC/MCParser/ELFAsmParser.cpp         |  14 +-
 llvm/lib/MC/MCStreamer.cpp                    |  18 +-
 .../Hexagon/AsmParser/HexagonAsmParser.cpp    |  29 ++-
 .../Target/PowerPC/AsmParser/PPCAsmParser.cpp |  12 +-
 .../lib/Target/X86/AsmParser/X86AsmParser.cpp | 183 +++++++++++-------
 9 files changed, 202 insertions(+), 111 deletions(-)

diff --git a/llvm/include/llvm/MC/MCStreamer.h b/llvm/include/llvm/MC/MCStreamer.h
index e9d301a..f25e523 100644
--- a/llvm/include/llvm/MC/MCStreamer.h
+++ b/llvm/include/llvm/MC/MCStreamer.h
@@ -529,7 +529,7 @@ public:
 
   /// \brief Special case of EmitValue that avoids the client having
   /// to pass in a MCExpr for constant integers.
-  virtual void EmitIntValue(uint64_t Value, unsigned Size);
+  virtual void EmitIntValue(uint64_t Value, unsigned Size, bool &Error);
 
   virtual void EmitULEB128Value(const MCExpr *Value);
 
diff --git a/llvm/lib/MC/MCELFStreamer.cpp b/llvm/lib/MC/MCELFStreamer.cpp
index 3445f02..06d17a9 100644
--- a/llvm/lib/MC/MCELFStreamer.cpp
+++ b/llvm/lib/MC/MCELFStreamer.cpp
@@ -367,16 +367,17 @@ void MCELFStreamer::EmitFileDirective(StringRef Filename) {
 }
 
 void MCELFStreamer::EmitIdent(StringRef IdentString) {
+  bool Error;
   MCSection *Comment = getAssembler().getContext().getELFSection(
       ".comment", ELF::SHT_PROGBITS, ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
   PushSection();
   SwitchSection(Comment);
   if (!SeenIdent) {
-    EmitIntValue(0, 1);
+    EmitIntValue(0, 1, Error);
     SeenIdent = true;
   }
   EmitBytes(IdentString);
-  EmitIntValue(0, 1);
+  EmitIntValue(0, 1, Error);
   PopSection();
 }
 
diff --git a/llvm/lib/MC/MCObjectStreamer.cpp b/llvm/lib/MC/MCObjectStreamer.cpp
index b2d6e1b..a40f65c 100644
--- a/llvm/lib/MC/MCObjectStreamer.cpp
+++ b/llvm/lib/MC/MCObjectStreamer.cpp
@@ -58,6 +58,7 @@ void MCObjectStreamer::flushPendingLabels(MCFragment *F, uint64_t FOffset) {
 void MCObjectStreamer::emitAbsoluteSymbolDiff(const MCSymbol *Hi,
                                               const MCSymbol *Lo,
                                               unsigned Size) {
+  bool Error;
   // If not assigned to the same (valid) fragment, fallback.
   if (!Hi->getFragment() || Hi->getFragment() != Lo->getFragment() ||
       Hi->isVariable() || Lo->isVariable()) {
@@ -65,7 +66,8 @@ void MCObjectStreamer::emitAbsoluteSymbolDiff(const MCSymbol *Hi,
     return;
   }
 
-  EmitIntValue(Hi->getOffset() - Lo->getOffset(), Size);
+  // TODO: hande Error?
+  EmitIntValue(Hi->getOffset() - Lo->getOffset(), Size, Error);
 }
 
 void MCObjectStreamer::reset() {
@@ -123,7 +125,8 @@ void MCObjectStreamer::EmitCFISections(bool EH, bool Debug) {
 }
 
 void MCObjectStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
-                                     SMLoc Loc) {
+                                     SMLoc Loc)
+{
   MCStreamer::EmitValueImpl(Value, Size, Loc);
   MCDataFragment *DF = getOrCreateDataFragment();
   flushPendingLabels(DF, DF->getContents().size());
@@ -143,8 +146,10 @@ void MCObjectStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
 
   // Avoid fixups when possible.
   int64_t AbsValue;
+  bool Error;
   if (Value->evaluateAsAbsolute(AbsValue, getAssembler())) {
-    EmitIntValue(AbsValue, Size);
+    // TODO: hande Error?
+    EmitIntValue(AbsValue, Size, Error);
     return;
   }
   DF->getFixups().push_back(
diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index 6d82328..0bb61ec 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -2773,7 +2773,12 @@ bool AsmParser::parseDirectiveValue(unsigned Size, unsigned int &KsError)
             KsError = KS_ERR_ASM_DIRECTIVE_VALUE_RANGE;
             return true;
         }
-        getStreamer().EmitIntValue(IntValue, Size);
+        bool Error;
+        getStreamer().EmitIntValue(IntValue, Size, Error);
+        if (Error) {
+            KsError = KS_ERR_ASM_DIRECTIVE_TOKEN;
+            return true;
+        }
       } else
         getStreamer().EmitValue(Value, Size, ExprLoc);
 
@@ -2829,12 +2834,13 @@ bool AsmParser::parseDirectiveOctaValue(unsigned int &KsError)
         return true;
       }
 
+      bool Error;
       if (MAI.isLittleEndian()) {
-        getStreamer().EmitIntValue(lo, 8);
-        getStreamer().EmitIntValue(hi, 8);
+        getStreamer().EmitIntValue(lo, 8, Error);
+        getStreamer().EmitIntValue(hi, 8, Error);
       } else {
-        getStreamer().EmitIntValue(hi, 8);
-        getStreamer().EmitIntValue(lo, 8);
+        getStreamer().EmitIntValue(hi, 8, Error);
+        getStreamer().EmitIntValue(lo, 8, Error);
       }
 
       if (getLexer().is(AsmToken::EndOfStatement))
@@ -2856,7 +2862,8 @@ bool AsmParser::parseDirectiveOctaValue(unsigned int &KsError)
 
 /// parseDirectiveRealValue
 ///  ::= (.single | .double) [ expression (, expression)* ]
-bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
+bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics)
+{
   if (getLexer().isNot(AsmToken::EndOfStatement)) {
     checkForValidSection();
 
@@ -2905,8 +2912,11 @@ bool AsmParser::parseDirectiveRealValue(const fltSemantics &Semantics) {
 
       // Emit the value as an integer.
       APInt AsInt = Value.bitcastToAPInt();
+      bool Error;
       getStreamer().EmitIntValue(AsInt.getLimitedValue(),
-                                 AsInt.getBitWidth() / 8);
+                                 AsInt.getBitWidth() / 8, Error);
+      if (Error)
+          return true;
 
       if (getLexer().is(AsmToken::EndOfStatement))
         break;
@@ -2955,7 +2965,8 @@ bool AsmParser::parseDirectiveZero() {
 
 /// parseDirectiveFill
 ///  ::= .fill expression [ , expression [ , expression ] ]
-bool AsmParser::parseDirectiveFill() {
+bool AsmParser::parseDirectiveFill()
+{
   checkForValidSection();
 
   SMLoc RepeatLoc = getLexer().getLoc();
@@ -3022,10 +3033,16 @@ bool AsmParser::parseDirectiveFill() {
   if (NumValues > 0) {
     int64_t NonZeroFillSize = FillSize > 4 ? 4 : FillSize;
     FillExpr &= ~0ULL >> (64 - NonZeroFillSize * 8);
+    bool Error;
     for (uint64_t i = 0, e = NumValues; i != e; ++i) {
-      getStreamer().EmitIntValue(FillExpr, NonZeroFillSize);
-      if (NonZeroFillSize < FillSize)
-        getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize);
+      getStreamer().EmitIntValue(FillExpr, NonZeroFillSize, Error);
+      if (Error)
+          return true;
+      if (NonZeroFillSize < FillSize) {
+        getStreamer().EmitIntValue(0, FillSize - NonZeroFillSize, Error);
+        if (Error)
+            return true;
+      }
     }
   }
 
diff --git a/llvm/lib/MC/MCParser/ELFAsmParser.cpp b/llvm/lib/MC/MCParser/ELFAsmParser.cpp
index 97461ee..805ddd4 100644
--- a/llvm/lib/MC/MCParser/ELFAsmParser.cpp
+++ b/llvm/lib/MC/MCParser/ELFAsmParser.cpp
@@ -671,7 +671,9 @@ bool ELFAsmParser::ParseDirectiveSymver(StringRef, SMLoc) {
 
 /// ParseDirectiveVersion
 ///  ::= .version string
-bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
+bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc)
+{
+  bool Error;
   if (getLexer().isNot(AsmToken::String))
     return TokError("unexpected token in '.version' directive");
 
@@ -683,11 +685,13 @@ bool ELFAsmParser::ParseDirectiveVersion(StringRef, SMLoc) {
 
   getStreamer().PushSection();
   getStreamer().SwitchSection(Note);
-  getStreamer().EmitIntValue(Data.size()+1, 4); // namesz.
-  getStreamer().EmitIntValue(0, 4);             // descsz = 0 (no description).
-  getStreamer().EmitIntValue(1, 4);             // type = NT_VERSION.
+  getStreamer().EmitIntValue(Data.size()+1, 4, Error); // namesz.
+  if (Error)
+      return true;
+  getStreamer().EmitIntValue(0, 4, Error);             // descsz = 0 (no description).
+  getStreamer().EmitIntValue(1, 4, Error);             // type = NT_VERSION.
   getStreamer().EmitBytes(Data);                // name.
-  getStreamer().EmitIntValue(0, 1);             // terminate the string.
+  getStreamer().EmitIntValue(0, 1, Error);             // terminate the string.
   getStreamer().EmitValueToAlignment(4);        // ensure 4 byte alignment.
   getStreamer().PopSection();
   return false;
diff --git a/llvm/lib/MC/MCStreamer.cpp b/llvm/lib/MC/MCStreamer.cpp
index ac51eaf..1371bbf 100644
--- a/llvm/lib/MC/MCStreamer.cpp
+++ b/llvm/lib/MC/MCStreamer.cpp
@@ -78,10 +78,20 @@ void MCStreamer::generateCompactUnwindEncodings(MCAsmBackend *MAB) {
 
 /// EmitIntValue - Special case of EmitValue that avoids the client having to
 /// pass in a MCExpr for constant integers.
-void MCStreamer::EmitIntValue(uint64_t Value, unsigned Size) {
-  assert(1 <= Size && Size <= 8 && "Invalid size");
-  assert((isUIntN(8 * Size, Value) || isIntN(8 * Size, Value)) &&
-         "Invalid size");
+void MCStreamer::EmitIntValue(uint64_t Value, unsigned Size, bool &Error)
+{
+  Error = false;
+  //assert(1 <= Size && Size <= 8 && "Invalid size");
+  if (1 > Size || Size > 8) {
+      Error = true;
+      return;
+  }
+  //assert((isUIntN(8 * Size, Value) || isIntN(8 * Size, Value)) &&
+  //       "Invalid size");
+  if (!isUIntN(8 * Size, Value) && !isIntN(8 * Size, Value)) {
+      Error = true;
+      return;
+  }
   char buf[8];
   const bool isLittleEndian = Context.getAsmInfo()->isLittleEndian();
   for (unsigned i = 0; i != Size; ++i) {
diff --git a/llvm/lib/Target/Hexagon/AsmParser/HexagonAsmParser.cpp b/llvm/lib/Target/Hexagon/AsmParser/HexagonAsmParser.cpp
index 103c90c..10c1a61 100644
--- a/llvm/lib/Target/Hexagon/AsmParser/HexagonAsmParser.cpp
+++ b/llvm/lib/Target/Hexagon/AsmParser/HexagonAsmParser.cpp
@@ -950,22 +950,30 @@ bool HexagonAsmParser::ParseDirectiveFalign(unsigned Size, SMLoc L) {
 }
 
 ///  ::= .word [ expression (, expression)* ]
-bool HexagonAsmParser::ParseDirectiveValue(unsigned Size, SMLoc L) {
+bool HexagonAsmParser::ParseDirectiveValue(unsigned Size, SMLoc L)
+{
   if (getLexer().isNot(AsmToken::EndOfStatement)) {
 
     for (;;) {
       const MCExpr *Value;
-      SMLoc ExprLoc = L;
+      //SMLoc ExprLoc = L;
       if (getParser().parseExpression(Value))
         return true;
 
       // Special case constant expressions to match code generator.
       if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
-        assert(Size <= 8 && "Invalid size");
+        bool Error;
+        //assert(Size <= 8 && "Invalid size");
+        if (Size > 8)
+            return true;
         uint64_t IntValue = MCE->getValue();
         if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
-          return Error(ExprLoc, "literal value out of range for directive");
-        getStreamer().EmitIntValue(IntValue, Size);
+          //return Error(ExprLoc, "literal value out of range for directive");
+          return true;
+        getStreamer().EmitIntValue(IntValue, Size, Error);
+        if (Error) {
+            return true;
+        }
       } else
         getStreamer().EmitValue(Value, Size);
 
@@ -974,7 +982,8 @@ bool HexagonAsmParser::ParseDirectiveValue(unsigned Size, SMLoc L) {
 
       // FIXME: Improve diagnostic.
       if (getLexer().isNot(AsmToken::Comma))
-        return TokError("unexpected token in directive");
+        //return TokError("unexpected token in directive");
+        return true;
       Lex();
     }
   }
@@ -1524,7 +1533,8 @@ void HexagonAsmParser::OutOfRange(SMLoc IDLoc, long long Val, long long Max) {
 
 int HexagonAsmParser::processInstruction(MCInst &Inst,
                                          OperandVector const &Operands,
-                                         SMLoc IDLoc, bool &MustExtend) {
+                                         SMLoc IDLoc, bool &MustExtend)
+{
   MCContext &Context = getParser().getContext();
   const MCRegisterInfo *RI = getContext().getRegisterInfo();
   std::string r = "r";
@@ -1721,9 +1731,12 @@ int HexagonAsmParser::processInstruction(MCInst &Inst,
       if (Absolute) {
         Sym = getContext().getOrCreateSymbol(StringRef(myCharStr.c_str() + 16));
         if (Sym->isUndefined()) {
+          bool Error;
           getStreamer().EmitLabel(Sym);
           getStreamer().EmitSymbolAttribute(Sym, MCSA_Global);
-          getStreamer().EmitIntValue(Value, byteSize);
+          getStreamer().EmitIntValue(Value, byteSize, Error);
+          if (Error)
+              return Match_InvalidOperand;
         }
       } else if (MO_1.isExpr()) {
         const char *StringStart = 0;
diff --git a/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp b/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp
index 04f0103..5be8185 100644
--- a/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp
+++ b/llvm/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp
@@ -1750,11 +1750,17 @@ bool PPCAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
         return false;
 
       if (const auto *MCE = dyn_cast<MCConstantExpr>(Value)) {
-        assert(Size <= 8 && "Invalid size");
+        bool Error;
+        //assert(Size <= 8 && "Invalid size");
+        if (Size > 8)
+            return true;
         uint64_t IntValue = MCE->getValue();
         if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
-          return Error(ExprLoc, "literal value out of range for directive");
-        getStreamer().EmitIntValue(IntValue, Size);
+          //return Error(ExprLoc, "literal value out of range for directive");
+          return true;
+        getStreamer().EmitIntValue(IntValue, Size, Error);
+        if (Error)
+            return true;
       } else {
         getStreamer().EmitValue(Value, Size, ExprLoc);
       }
diff --git a/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp b/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
index 580a24a..243f9e1 100644
--- a/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
+++ b/llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp
@@ -739,7 +739,7 @@ private:
   }
 
   std::nullptr_t ErrorOperand(SMLoc Loc, StringRef Msg) {
-    Error(Loc, Msg);
+    //Error(Loc, Msg);
     return nullptr;
   }
 
@@ -931,7 +931,8 @@ static bool CheckBaseRegAndIndexReg(unsigned BaseReg, unsigned IndexReg,
 }
 
 bool X86AsmParser::ParseRegister(unsigned &RegNo,
-                                 SMLoc &StartLoc, SMLoc &EndLoc, unsigned int &ErrorCode) {
+                                 SMLoc &StartLoc, SMLoc &EndLoc, unsigned int &ErrorCode)
+{
   MCAsmParser &Parser = getParser();
   RegNo = 0;
   const AsmToken &PercentTok = Parser.getTok();
@@ -947,8 +948,9 @@ bool X86AsmParser::ParseRegister(unsigned &RegNo,
 
   if (Tok.isNot(AsmToken::Identifier)) {
     if (isParsingIntelSyntax()) return true;
-    return Error(StartLoc, "invalid register name",
-                 SMRange(StartLoc, EndLoc));
+    //return Error(StartLoc, "invalid register name",
+    //             SMRange(StartLoc, EndLoc));
+    return true;
   }
 
   RegNo = MatchRegisterName(Tok.getString());
@@ -972,9 +974,10 @@ bool X86AsmParser::ParseRegister(unsigned &RegNo,
         X86MCRegisterClasses[X86::GR64RegClassID].contains(RegNo) ||
         X86II::isX86_64NonExtLowByteReg(RegNo) ||
         X86II::isX86_64ExtendedReg(RegNo))
-      return Error(StartLoc, "register %"
-                   + Tok.getString() + " is only available in 64-bit mode",
-                   SMRange(StartLoc, EndLoc));
+      //return Error(StartLoc, "register %"
+      //             + Tok.getString() + " is only available in 64-bit mode",
+      //             SMRange(StartLoc, EndLoc));
+      return true;
   }
 
   // Parse "%st" as "%st(0)" and "%st(1)", which is multiple tokens.
@@ -990,7 +993,8 @@ bool X86AsmParser::ParseRegister(unsigned &RegNo,
 
     const AsmToken &IntTok = Parser.getTok();
     if (IntTok.isNot(AsmToken::Integer))
-      return Error(IntTok.getLoc(), "expected stack index");
+      //return Error(IntTok.getLoc(), "expected stack index");
+      return true;
     switch (IntTok.getIntVal()) {
     case 0: RegNo = X86::ST0; break;
     case 1: RegNo = X86::ST1; break;
@@ -1000,11 +1004,12 @@ bool X86AsmParser::ParseRegister(unsigned &RegNo,
     case 5: RegNo = X86::ST5; break;
     case 6: RegNo = X86::ST6; break;
     case 7: RegNo = X86::ST7; break;
-    default: return Error(IntTok.getLoc(), "invalid stack index");
+    default: return true; //return Error(IntTok.getLoc(), "invalid stack index");
     }
 
     if (getParser().Lex().isNot(AsmToken::RParen))
-      return Error(Parser.getTok().getLoc(), "expected ')'");
+      //return Error(Parser.getTok().getLoc(), "expected ')'");
+      return true;
 
     EndLoc = Parser.getTok().getEndLoc();
     Parser.Lex(); // Eat ')'
@@ -1037,8 +1042,9 @@ bool X86AsmParser::ParseRegister(unsigned &RegNo,
 
   if (RegNo == 0) {
     if (isParsingIntelSyntax()) return true;
-    return Error(StartLoc, "invalid register name",
-                 SMRange(StartLoc, EndLoc));
+    //return Error(StartLoc, "invalid register name",
+    //             SMRange(StartLoc, EndLoc));
+    return true;
   }
 
   Parser.Lex(); // Eat identifier token.
@@ -1144,8 +1150,9 @@ bool X86AsmParser::VerifyAndAdjustOperands(OperandVector &OrigOperands,
         // bases are of the same register class
         if (RegClassID != -1 &&
             !X86MCRegisterClasses[RegClassID].contains(OrigReg)) {
-          return Error(OrigOp.getStartLoc(),
-                       "mismatching source and destination index registers");
+          //return Error(OrigOp.getStartLoc(),
+          //             "mismatching source and destination index registers");
+          return true;
         }
 
         if (X86MCRegisterClasses[X86::GR64RegClassID].contains(OrigReg))
@@ -1367,7 +1374,8 @@ bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End)
         Done = true;
         break;
       }
-      return Error(Tok.getLoc(), "unknown token in expression");
+      //return Error(Tok.getLoc(), "unknown token in expression");
+      return true;
     }
     case AsmToken::EndOfStatement: {
       Done = true;
@@ -1387,7 +1395,8 @@ bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End)
       } else {
         if (!isParsingInlineAsm()) {
           if (getParser().parsePrimaryExpr(Val, End))
-            return Error(Tok.getLoc(), "Unexpected identifier!");
+            //return Error(Tok.getLoc(), "Unexpected identifier!");
+            return true;
         } else {
           // This is a dot operator, not an adjacent identifier.
           if (Identifier.find('.') != StringRef::npos &&
@@ -1404,14 +1413,15 @@ bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End)
         UpdateLocLex = false;
         break;
       }
-      return Error(Tok.getLoc(), "Unexpected identifier!");
+      //return Error(Tok.getLoc(), "Unexpected identifier!");
+      return true;
     }
     case AsmToken::Integer: {
       StringRef ErrMsg;
       if (isParsingInlineAsm() && SM.getAddImmPrefix())
         InstInfo->AsmRewrites->emplace_back(AOK_ImmPrefix, Tok.getLoc());
       // Look for 'b' or 'f' following an Integer as a directional label
-      SMLoc Loc = getTok().getLoc();
+      //SMLoc Loc = getTok().getLoc();
       int64_t IntVal = getTok().getIntVal();
       End = consumeToken();
       UpdateLocLex = false;
@@ -1424,17 +1434,20 @@ bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End)
           const MCExpr *Val =
               MCSymbolRefExpr::create(Sym, Variant, getContext());
           if (IDVal == "b" && Sym->isUndefined())
-            return Error(Loc, "invalid reference to undefined symbol");
+            //return Error(Loc, "invalid reference to undefined symbol");
+            return true;
           StringRef Identifier = Sym->getName();
           SM.onIdentifierExpr(Val, Identifier);
           End = consumeToken();
         } else {
           if (SM.onInteger(IntVal, ErrMsg))
-            return Error(Loc, ErrMsg);
+            //return Error(Loc, ErrMsg);
+            return true;
         }
       } else {
         if (SM.onInteger(IntVal, ErrMsg))
-          return Error(Loc, ErrMsg);
+          //return Error(Loc, ErrMsg);
+          return true;
       }
       break;
     }
@@ -1456,7 +1469,8 @@ bool X86AsmParser::ParseIntelExpression(IntelExprStateMachine &SM, SMLoc &End)
     case AsmToken::RParen:  SM.onRParen(); break;
     }
     if (SM.hadError())
-      return Error(Tok.getLoc(), "unknown token in expression");
+      //return Error(Tok.getLoc(), "unknown token in expression");
+      return true;
 
     if (!Done && UpdateLocLex)
       End = consumeToken();
@@ -1541,7 +1555,7 @@ X86AsmParser::ParseIntelBracExpression(unsigned SegReg, SMLoc Start,
     }
     StringRef ErrMsg;
     if (CheckBaseRegAndIndexReg(BaseReg, IndexReg, ErrMsg)) {
-      Error(StartInBrac, ErrMsg);
+      //Error(StartInBrac, ErrMsg);
       return nullptr;
     }
     return X86Operand::CreateMem(getPointerWidth(), SegReg, Disp, BaseReg,
@@ -1722,7 +1736,8 @@ std::unique_ptr<X86Operand> X86AsmParser::ParseIntelMemOperand(std::string Mnem,
 
 /// Parse the '.' operator.
 bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
-                                                const MCExpr *&NewDisp) {
+                                                const MCExpr *&NewDisp)
+{
   MCAsmParser &Parser = getParser();
   const AsmToken &Tok = Parser.getTok();
   int64_t OrigDispVal, DotDispVal;
@@ -1731,7 +1746,8 @@ bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
   if (const MCConstantExpr *OrigDisp = dyn_cast<MCConstantExpr>(Disp))
     OrigDispVal = OrigDisp->getValue();
   else
-    return Error(Tok.getLoc(), "Non-constant offsets are not supported!");
+    //return Error(Tok.getLoc(), "Non-constant offsets are not supported!");
+    return true;
 
   // Drop the optional '.'.
   StringRef DotDispStr = Tok.getString();
@@ -1748,10 +1764,12 @@ bool X86AsmParser::ParseIntelDotOperator(const MCExpr *Disp,
     std::pair<StringRef, StringRef> BaseMember = DotDispStr.split('.');
     if (SemaCallback->LookupInlineAsmField(BaseMember.first, BaseMember.second,
                                            DotDisp))
-      return Error(Tok.getLoc(), "Unable to lookup field reference!");
+      //return Error(Tok.getLoc(), "Unable to lookup field reference!");
+      return true;
     DotDispVal = DotDisp;
   } else
-    return Error(Tok.getLoc(), "Unexpected token type!");
+    //return Error(Tok.getLoc(), "Unexpected token type!");
+    return true;
 
   if (isParsingInlineAsm() && Tok.is(AsmToken::Identifier)) {
     SMLoc Loc = SMLoc::getFromPointer(DotDispStr.data());
@@ -1982,8 +2000,8 @@ std::unique_ptr<X86Operand> X86AsmParser::ParseATTOperand()
     unsigned int ErrorCode;
     if (ParseRegister(RegNo, Start, End, ErrorCode)) return nullptr;
     if (RegNo == X86::EIZ || RegNo == X86::RIZ) {
-      Error(Start, "%eiz and %riz can only be used as index registers",
-            SMRange(Start, End));
+      //Error(Start, "%eiz and %riz can only be used as index registers",
+      //      SMRange(Start, End));
       return nullptr;
     }
 
@@ -2027,13 +2045,15 @@ bool X86AsmParser::HandleAVX512Operand(OperandVector &Operands,
       if(getLexer().is(AsmToken::Integer)) {
         // Parse memory broadcasting ({1to<NUM>}).
         if (getLexer().getTok().getIntVal() != 1)
-          return !ErrorAndEatStatement(getLexer().getLoc(),
-                                       "Expected 1to<NUM> at this point");
+          //return !ErrorAndEatStatement(getLexer().getLoc(),
+          //                             "Expected 1to<NUM> at this point");
+          return false;
         Parser.Lex();  // Eat "1" of 1to8
         if (!getLexer().is(AsmToken::Identifier) ||
             !getLexer().getTok().getIdentifier().startswith("to"))
-          return !ErrorAndEatStatement(getLexer().getLoc(),
-                                       "Expected 1to<NUM> at this point");
+          //return !ErrorAndEatStatement(getLexer().getLoc(),
+          //                             "Expected 1to<NUM> at this point");
+          return false;
         // Recognize only reasonable suffixes.
         const char *BroadcastPrimitive =
           StringSwitch<const char*>(getLexer().getTok().getIdentifier())
@@ -2043,12 +2063,14 @@ bool X86AsmParser::HandleAVX512Operand(OperandVector &Operands,
             .Case("to16", "{1to16}")
             .Default(nullptr);
         if (!BroadcastPrimitive)
-          return !ErrorAndEatStatement(getLexer().getLoc(),
-                                       "Invalid memory broadcast primitive.");
+          //return !ErrorAndEatStatement(getLexer().getLoc(),
+          //                             "Invalid memory broadcast primitive.");
+          return false;
         Parser.Lex();  // Eat "toN" of 1toN
         if (!getLexer().is(AsmToken::RCurly))
-          return !ErrorAndEatStatement(getLexer().getLoc(),
-                                       "Expected } at this point");
+          //return !ErrorAndEatStatement(getLexer().getLoc(),
+          //                             "Expected } at this point");
+          return false;
         Parser.Lex();  // Eat "}"
         Operands.push_back(X86Operand::CreateToken(BroadcastPrimitive,
                                                    consumedToken));
@@ -2062,8 +2084,9 @@ bool X86AsmParser::HandleAVX512Operand(OperandVector &Operands,
         if (std::unique_ptr<X86Operand> Op = ParseOperand("", KsError)) {
           Operands.push_back(std::move(Op));
           if (!getLexer().is(AsmToken::RCurly))
-            return !ErrorAndEatStatement(getLexer().getLoc(),
-                                         "Expected } at this point");
+            //return !ErrorAndEatStatement(getLexer().getLoc(),
+            //                             "Expected } at this point");
+            return false;
           Operands.push_back(X86Operand::CreateToken("}", consumeToken()));
 
           // Parse "zeroing non-masked" semantic {z}
@@ -2071,12 +2094,14 @@ bool X86AsmParser::HandleAVX512Operand(OperandVector &Operands,
             Operands.push_back(X86Operand::CreateToken("{z}", consumeToken()));
             if (!getLexer().is(AsmToken::Identifier) ||
                 getLexer().getTok().getIdentifier() != "z")
-              return !ErrorAndEatStatement(getLexer().getLoc(),
-                                           "Expected z at this point");
+              //return !ErrorAndEatStatement(getLexer().getLoc(),
+              //                             "Expected z at this point");
+              return false;
             Parser.Lex();  // Eat the z
             if (!getLexer().is(AsmToken::RCurly))
-              return !ErrorAndEatStatement(getLexer().getLoc(),
-                                           "Expected } at this point");
+              //return !ErrorAndEatStatement(getLexer().getLoc(),
+              //                             "Expected } at this point");
+              return false;
             Parser.Lex();  // Eat the }
           }
         } else
@@ -2158,8 +2183,8 @@ std::unique_ptr<X86Operand> X86AsmParser::ParseMemOperand(unsigned SegReg,
     BaseLoc = Parser.getTok().getLoc();
     if (ParseRegister(BaseReg, StartLoc, EndLoc, ErrorCode)) return nullptr;
     if (BaseReg == X86::EIZ || BaseReg == X86::RIZ) {
-      Error(StartLoc, "eiz and riz can only be used as index registers",
-            SMRange(StartLoc, EndLoc));
+      //Error(StartLoc, "eiz and riz can only be used as index registers",
+      //      SMRange(StartLoc, EndLoc));
       return nullptr;
     }
   }
@@ -2182,30 +2207,30 @@ std::unique_ptr<X86Operand> X86AsmParser::ParseMemOperand(unsigned SegReg,
         // Parse the scale amount:
         //  ::= ',' [scale-expression]
         if (getLexer().isNot(AsmToken::Comma)) {
-          Error(Parser.getTok().getLoc(),
-                "expected comma in scale expression");
+          //Error(Parser.getTok().getLoc(),
+          //      "expected comma in scale expression");
           return nullptr;
         }
         Parser.Lex(); // Eat the comma.
 
         if (getLexer().isNot(AsmToken::RParen)) {
-          SMLoc Loc = Parser.getTok().getLoc();
+          //SMLoc Loc = Parser.getTok().getLoc();
 
           int64_t ScaleVal;
           if (getParser().parseAbsoluteExpression(ScaleVal)){
-            Error(Loc, "expected scale expression");
+            //Error(Loc, "expected scale expression");
             return nullptr;
           }
 
           // Validate the scale amount.
           if (X86MCRegisterClasses[X86::GR16RegClassID].contains(BaseReg) &&
               ScaleVal != 1) {
-            Error(Loc, "scale factor in 16-bit address must be 1");
+            //Error(Loc, "scale factor in 16-bit address must be 1");
             return nullptr;
           }
           if (ScaleVal != 1 && ScaleVal != 2 && ScaleVal != 4 &&
               ScaleVal != 8) {
-            Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
+            //Error(Loc, "scale factor in address must be 1, 2, 4 or 8");
             return nullptr;
           }
           Scale = (unsigned)ScaleVal;
@@ -2214,21 +2239,21 @@ std::unique_ptr<X86Operand> X86AsmParser::ParseMemOperand(unsigned SegReg,
     } else if (getLexer().isNot(AsmToken::RParen)) {
       // A scale amount without an index is ignored.
       // index.
-      SMLoc Loc = Parser.getTok().getLoc();
+      //SMLoc Loc = Parser.getTok().getLoc();
 
       int64_t Value;
       if (getParser().parseAbsoluteExpression(Value))
         return nullptr;
 
-      if (Value != 1)
-        Warning(Loc, "scale factor without index register is ignored");
+      //if (Value != 1)
+      //  Warning(Loc, "scale factor without index register is ignored");
       Scale = 1;
     }
   }
 
   // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
   if (getLexer().isNot(AsmToken::RParen)) {
-    Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
+    //Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
     return nullptr;
   }
   SMLoc MemEnd = Parser.getTok().getEndLoc();
@@ -2241,18 +2266,18 @@ std::unique_ptr<X86Operand> X86AsmParser::ParseMemOperand(unsigned SegReg,
       (is64BitMode() || (BaseReg != X86::BX && BaseReg != X86::BP &&
                          BaseReg != X86::SI && BaseReg != X86::DI)) &&
       BaseReg != X86::DX) {
-    Error(BaseLoc, "invalid 16-bit base register");
+    //Error(BaseLoc, "invalid 16-bit base register");
     return nullptr;
   }
   if (BaseReg == 0 &&
       X86MCRegisterClasses[X86::GR16RegClassID].contains(IndexReg)) {
-    Error(IndexLoc, "16-bit memory operand may not include only index register");
+    //Error(IndexLoc, "16-bit memory operand may not include only index register");
     return nullptr;
   }
 
   StringRef ErrMsg;
   if (CheckBaseRegAndIndexReg(BaseReg, IndexReg, ErrMsg)) {
-    Error(BaseLoc, ErrMsg);
+    //Error(BaseLoc, ErrMsg);
     return nullptr;
   }
 
@@ -2720,7 +2745,7 @@ void X86AsmParser::MatchFPUWaitAlias(SMLoc IDLoc, X86Operand &Op,
 bool X86AsmParser::ErrorMissingFeature(SMLoc IDLoc, uint64_t ErrorInfo,
                                        bool MatchingInlineAsm) {
   assert(ErrorInfo && "Unknown missing feature!");
-  ArrayRef<SMRange> EmptyRanges = None;
+  //ArrayRef<SMRange> EmptyRanges = None;
   SmallString<126> Msg;
   raw_svector_ostream OS(Msg);
   OS << "instruction requires:";
@@ -2730,7 +2755,8 @@ bool X86AsmParser::ErrorMissingFeature(SMLoc IDLoc, uint64_t ErrorInfo,
       OS << ' ' << getSubtargetFeatureName(ErrorInfo & Mask);
     Mask <<= 1;
   }
-  return Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
+  //return Error(IDLoc, OS.str(), EmptyRanges, MatchingInlineAsm);
+  return true;
 }
 
 bool X86AsmParser::MatchAndEmitATTInstruction(SMLoc IDLoc, unsigned &Opcode,
@@ -2889,9 +2915,10 @@ bool X86AsmParser::MatchAndEmitATTInstruction(SMLoc IDLoc, unsigned &Opcode,
 
       X86Operand &Operand = (X86Operand &)*Operands[ErrorInfo];
       if (Operand.getStartLoc().isValid()) {
-        SMRange OperandRange = Operand.getLocRange();
-        return Error(Operand.getStartLoc(), "invalid operand for instruction",
-                     OperandRange, MatchingInlineAsm);
+        //SMRange OperandRange = Operand.getLocRange();
+        //return Error(Operand.getStartLoc(), "invalid operand for instruction",
+        //             OperandRange, MatchingInlineAsm);
+        return true;
       }
     }
 
@@ -3105,9 +3132,10 @@ bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
       if (Parser.getTok().getString() == "prefix")
         Parser.Lex();
       else if (Parser.getTok().getString() == "noprefix")
-        return Error(DirectiveID.getLoc(), "'.att_syntax noprefix' is not "
-                                           "supported: registers must have a "
-                                           "'%' prefix in .att_syntax");
+        //return Error(DirectiveID.getLoc(), "'.att_syntax noprefix' is not "
+        //                                   "supported: registers must have a "
+        //                                   "'%' prefix in .att_syntax");
+        return true;
     }
     getParser().setAssemblerDialect(0);
     return false;
@@ -3117,9 +3145,10 @@ bool X86AsmParser::ParseDirective(AsmToken DirectiveID) {
       if (Parser.getTok().getString() == "noprefix")
         Parser.Lex();
       else if (Parser.getTok().getString() == "prefix")
-        return Error(DirectiveID.getLoc(), "'.intel_syntax prefix' is not "
-                                           "supported: registers must not have "
-                                           "a '%' prefix in .intel_syntax");
+        //return Error(DirectiveID.getLoc(), "'.intel_syntax prefix' is not "
+        //                                   "supported: registers must not have "
+        //                                   "a '%' prefix in .intel_syntax");
+        return true;
     }
     return false;
   } else if (IDVal == ".even")
@@ -3157,11 +3186,17 @@ bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
         return false;
 
       if (const auto *MCE = dyn_cast<MCConstantExpr>(Value)) {
-        assert(Size <= 8 && "Invalid size");
+        bool Error;
+        //assert(Size <= 8 && "Invalid size");
+        if (Size > 8)
+            return true;
         uint64_t IntValue = MCE->getValue();
         if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
-          return Error(ExprLoc, "literal value out of range for directive");
-        getStreamer().EmitIntValue(IntValue, Size);
+          //return Error(ExprLoc, "literal value out of range for directive");
+          return true;
+        getStreamer().EmitIntValue(IntValue, Size, Error);
+        if (Error)
+            return true;
       } else {
         getStreamer().EmitValue(Value, Size, ExprLoc);
       }
@@ -3171,7 +3206,7 @@ bool X86AsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
 
       // FIXME: Improve diagnostic.
       if (getLexer().isNot(AsmToken::Comma)) {
-        Error(L, "unexpected token in directive");
+        //Error(L, "unexpected token in directive");
         return false;
       }
       Parser.Lex();
@@ -3205,7 +3240,7 @@ bool X86AsmParser::ParseDirectiveCode(StringRef IDVal, SMLoc L) {
       getParser().getStreamer().EmitAssemblerFlag(MCAF_Code64);
     }
   } else {
-    Error(L, "unknown directive " + IDVal);
+    //Error(L, "unknown directive " + IDVal);
     return false;
   }
 
-- 
GitLab