Change SMRange to be half-open (exclusive end) instead of closed (inclusive)

This is necessary not only for representing empty ranges, but for handling
multibyte characters in the input. (If the end pointer in a range refers to
a multibyte character, should it point to the beginning or the end of the
character in a char array?) Some of the code in the asm parsers was already
assuming this anyway.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@171765 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Target/Mips/AsmParser/MipsAsmParser.cpp b/lib/Target/Mips/AsmParser/MipsAsmParser.cpp
index 59fefa1..085503eb 100644
--- a/lib/Target/Mips/AsmParser/MipsAsmParser.cpp
+++ b/lib/Target/Mips/AsmParser/MipsAsmParser.cpp
@@ -107,7 +107,7 @@
   bool reportParseError(StringRef ErrorMsg);
 
   bool parseMemOffset(const MCExpr *&Res);
-  bool parseRelocOperand(const MCExpr *&Res);
+  bool parseRelocOperand(const MCExpr *&Res, SMLoc &E);
 
   bool parseDirectiveSet();
 
@@ -692,6 +692,7 @@
                           StringRef Mnemonic){
 
   SMLoc S = Parser.getTok().getLoc();
+  SMLoc E = Parser.getTok().getEndLoc();
   int RegNo = -1;
 
   // FIXME: we should make a more generic method for CCR
@@ -706,8 +707,7 @@
   if (RegNo == -1)
     return true;
 
-  Operands.push_back(MipsOperand::CreateReg(RegNo, S,
-      Parser.getTok().getLoc()));
+  Operands.push_back(MipsOperand::CreateReg(RegNo, S, E));
   Parser.Lex(); // Eat register token.
   return false;
 }
@@ -760,7 +760,7 @@
     if (Parser.ParseIdentifier(Identifier))
       return true;
 
-    SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
+    SMLoc E = SMLoc::getFromPointer(Identifier.end());
 
     MCSymbol *Sym = getContext().GetOrCreateSymbol("$" + Identifier);
 
@@ -780,9 +780,9 @@
      // quoted label names
     const MCExpr *IdVal;
     SMLoc S = Parser.getTok().getLoc();
-    if (getParser().ParseExpression(IdVal))
+    SMLoc E;
+    if (getParser().ParseExpression(IdVal, E))
       return true;
-    SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
     Operands.push_back(MipsOperand::CreateImm(IdVal, S, E));
     return false;
   }
@@ -790,11 +790,10 @@
     // it is a symbol reference or constant expression
     const MCExpr *IdVal;
     SMLoc S = Parser.getTok().getLoc(); // start location of the operand
-    if (parseRelocOperand(IdVal))
+    SMLoc E;
+    if (parseRelocOperand(IdVal, E))
       return true;
 
-    SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
-
     Operands.push_back(MipsOperand::CreateImm(IdVal, S, E));
     return false;
   } // case AsmToken::Percent
@@ -802,7 +801,7 @@
   return true;
 }
 
-bool MipsAsmParser::parseRelocOperand(const MCExpr *&Res) {
+bool MipsAsmParser::parseRelocOperand(const MCExpr *&Res, SMLoc &EndLoc) {
 
   Parser.Lex(); // eat % token
   const AsmToken &Tok = Parser.getTok(); // get next token, operation
@@ -814,7 +813,6 @@
   Parser.Lex(); // eat identifier
   // now make expression from the rest of the operand
   const MCExpr *IdVal;
-  SMLoc EndLoc;
 
   if (getLexer().getKind() == AsmToken::LParen) {
     while (1) {
@@ -835,8 +833,10 @@
     if (getParser().ParseParenExpression(IdVal,EndLoc))
       return true;
 
-    while (getLexer().getKind() == AsmToken::RParen)
+    while (getLexer().getKind() == AsmToken::RParen) {
+      EndLoc = Parser.getTok().getEndLoc();
       Parser.Lex(); // eat ')' token
+    }
 
   } else
     return true; // parenthesis must follow reloc operand
@@ -868,24 +868,23 @@
                                   SMLoc &EndLoc) {
 
   StartLoc = Parser.getTok().getLoc();
+  EndLoc = Parser.getTok().getEndLoc();
   RegNo = tryParseRegister("");
-  EndLoc = Parser.getTok().getLoc();
   return (RegNo == (unsigned)-1);
 }
 
 bool MipsAsmParser::parseMemOffset(const MCExpr *&Res) {
-
-  SMLoc S;
-
   switch(getLexer().getKind()) {
   default:
     return true;
   case AsmToken::Integer:
   case AsmToken::Minus:
   case AsmToken::Plus:
-    return (getParser().ParseExpression(Res));
-  case AsmToken::Percent:
-    return parseRelocOperand(Res);
+    return getParser().ParseExpression(Res);
+  case AsmToken::Percent: {
+    SMLoc E;
+    return parseRelocOperand(Res, E);
+  }
   case AsmToken::LParen:
     return false;  // it's probably assuming 0
   }
@@ -896,9 +895,8 @@
                SmallVectorImpl<MCParsedAsmOperand*>&Operands) {
 
   const MCExpr *IdVal = 0;
-  SMLoc S;
-  // first operand is the offset
-  S = Parser.getTok().getLoc();
+  SMLoc S = Parser.getTok().getLoc();
+  SMLoc E = Parser.getTok().getEndLoc();
 
   if (parseMemOffset(IdVal))
     return MatchOperand_ParseFail;
@@ -907,7 +905,6 @@
   if (Tok.isNot(AsmToken::LParen)) {
     MipsOperand *Mnemonic = static_cast<MipsOperand*>(Operands[0]);
     if (Mnemonic->getToken() == "la") {
-      SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer()-1);
       Operands.push_back(MipsOperand::CreateImm(IdVal, S, E));
       return MatchOperand_Success;
     }
@@ -936,8 +933,7 @@
     return MatchOperand_ParseFail;
   }
 
-  SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
-
+  E = Parser.getTok().getEndLoc();
   Parser.Lex(); // Eat ')' token.
 
   if (IdVal == 0)
@@ -1087,8 +1083,8 @@
           if (Cc == -1) {
             return Error(NameLoc, "Invalid conditional code");
           }
-          SMLoc E = SMLoc::getFromPointer(
-              Parser.getTok().getLoc().getPointer() -1 );
+          // FIXME: May include trailing whitespace...
+          SMLoc E = Parser.getTok().getLoc();
           Operands.push_back(MipsOperand::CreateImm(
               MCConstantExpr::Create(Cc, getContext()), NameLoc, E));
         } else {