[llvm-commits] [llvm] r73950 - in /llvm/trunk/tools/llvm-mc: AsmLexer.cpp AsmLexer.h AsmParser.cpp AsmParser.h

Chris Lattner sabre at nondot.org
Mon Jun 22 22:57:07 PDT 2009


Author: lattner
Date: Tue Jun 23 00:57:07 2009
New Revision: 73950

URL: http://llvm.org/viewvc/llvm-project?rev=73950&view=rev
Log:
implement a trivial binary expression parser, we can now parse all of 176.gcc.llc.s

Modified:
    llvm/trunk/tools/llvm-mc/AsmLexer.cpp
    llvm/trunk/tools/llvm-mc/AsmLexer.h
    llvm/trunk/tools/llvm-mc/AsmParser.cpp
    llvm/trunk/tools/llvm-mc/AsmParser.h

Modified: llvm/trunk/tools/llvm-mc/AsmLexer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mc/AsmLexer.cpp?rev=73950&r1=73949&r2=73950&view=diff

==============================================================================
--- llvm/trunk/tools/llvm-mc/AsmLexer.cpp (original)
+++ llvm/trunk/tools/llvm-mc/AsmLexer.cpp Tue Jun 23 00:57:07 2009
@@ -75,17 +75,18 @@
   while (isalnum(*CurPtr) || *CurPtr == '_' || *CurPtr == '$' ||
          *CurPtr == '.' || *CurPtr == '@')
     ++CurPtr;
-  CurStrVal.assign(TokStart, CurPtr);   // Include %
+  CurStrVal.assign(TokStart, CurPtr);
   return asmtok::Identifier;
 }
 
 /// LexPercent: Register: %[a-zA-Z0-9]+
 asmtok::TokKind AsmLexer::LexPercent() {
   if (!isalnum(*CurPtr))
-    return ReturnError(TokStart, "invalid register name");
+    return asmtok::Percent;  // Single %.
+  
   while (isalnum(*CurPtr))
     ++CurPtr;
-  CurStrVal.assign(TokStart, CurPtr);   // Skip %
+  CurStrVal.assign(TokStart, CurPtr);   // Include %
   return asmtok::Register;
 }
 
@@ -243,6 +244,10 @@
   case '*': return asmtok::Star;
   case ',': return asmtok::Comma;
   case '$': return asmtok::Dollar;
+  case '|': return asmtok::Pipe;
+  case '^': return asmtok::Caret;
+  case '&': return asmtok::Amp;
+  case '!': return asmtok::Exclaim;
   case '%': return LexPercent();
   case '/': return LexSlash();
   case '#': return LexHash();
@@ -250,6 +255,20 @@
   case '0': case '1': case '2': case '3': case '4':
   case '5': case '6': case '7': case '8': case '9':
     return LexDigit();
+  case '<':
+    if (*CurPtr == '<') {
+      ++CurPtr;
+      return asmtok::LessLess;
+    }
+    // Don't have any use for bare '<' yet.
+    return ReturnError(TokStart, "invalid character in input");
+  case '>':
+    if (*CurPtr == '>') {
+      ++CurPtr;
+      return asmtok::GreaterGreater;
+    }
+    // Don't have any use for bare '>' yet.
+    return ReturnError(TokStart, "invalid character in input");
       
   // TODO: Quoted identifiers (objc methods etc)
   // local labels: [0-9][:]

Modified: llvm/trunk/tools/llvm-mc/AsmLexer.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mc/AsmLexer.h?rev=73950&r1=73949&r2=73950&view=diff

==============================================================================
--- llvm/trunk/tools/llvm-mc/AsmLexer.h (original)
+++ llvm/trunk/tools/llvm-mc/AsmLexer.h Tue Jun 23 00:57:07 2009
@@ -42,7 +42,10 @@
     Plus, Minus, Tilde,
     Slash,    // '/'
     LParen, RParen,
-    Star, Comma, Dollar
+    Star, Comma, Dollar,
+    
+    Pipe, Caret, Amp, Exclaim,
+    Percent, LessLess, GreaterGreater
   };
 }
 

Modified: llvm/trunk/tools/llvm-mc/AsmParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mc/AsmParser.cpp?rev=73950&r1=73949&r2=73950&view=diff

==============================================================================
--- llvm/trunk/tools/llvm-mc/AsmParser.cpp (original)
+++ llvm/trunk/tools/llvm-mc/AsmParser.cpp Tue Jun 23 00:57:07 2009
@@ -168,7 +168,9 @@
       // memory operand consumed.
     } else {
       // It must be an parenthesized expression, parse it now.
-      if (ParseParenExpr(Disp)) return true;
+      if (ParseParenExpr(Disp) ||
+          ParseBinOpRHS(1, Disp))
+        return true;
       
       // After parsing the base expression we could either have a parenthesized
       // memory address or not.  If not, return now.  If so, eat the (.
@@ -274,9 +276,61 @@
 ///  expr ::= primaryexpr
 ///
 bool AsmParser::ParseExpression(int64_t &Res) {
-  return ParsePrimaryExpr(Res);
+  return ParsePrimaryExpr(Res) ||
+         ParseBinOpRHS(1, Res);
 }
-  
+
+static unsigned getBinOpPrecedence(asmtok::TokKind K) {
+  switch (K) {
+  default: return 0;    // not a binop.
+  case asmtok::Plus:
+  case asmtok::Minus:
+    return 1;
+  case asmtok::Pipe:
+  case asmtok::Caret:
+  case asmtok::Amp:
+  case asmtok::Exclaim:
+    return 2;
+  case asmtok::Star:
+  case asmtok::Slash:
+  case asmtok::Percent:
+  case asmtok::LessLess:
+  case asmtok::GreaterGreater:
+    return 3;
+  }
+}
+
+
+/// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
+/// Res contains the LHS of the expression on input.
+bool AsmParser::ParseBinOpRHS(unsigned Precedence, int64_t &Res) {
+  while (1) {
+    unsigned TokPrec = getBinOpPrecedence(Lexer.getKind());
+    
+    // If the next token is lower precedence than we are allowed to eat, return
+    // successfully with what we ate already.
+    if (TokPrec < Precedence)
+      return false;
+    
+    //asmtok::TokKind BinOp = Lexer.getKind();
+    Lexer.Lex();
+    
+    // Eat the next primary expression.
+    int64_t RHS;
+    if (ParsePrimaryExpr(RHS)) return true;
+    
+    // If BinOp binds less tightly with RHS than the operator after RHS, let
+    // the pending operator take RHS as its LHS.
+    unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind());
+    if (TokPrec < NextTokPrec) {
+      if (ParseBinOpRHS(Precedence+1, RHS)) return true;
+    }
+
+    // Merge LHS/RHS: fixme use the right operator etc.
+    Res += RHS;
+  }
+}
+
   
   
   

Modified: llvm/trunk/tools/llvm-mc/AsmParser.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mc/AsmParser.h?rev=73950&r1=73949&r2=73950&view=diff

==============================================================================
--- llvm/trunk/tools/llvm-mc/AsmParser.h (original)
+++ llvm/trunk/tools/llvm-mc/AsmParser.h Tue Jun 23 00:57:07 2009
@@ -40,6 +40,7 @@
   bool ParseX86MemOperand(X86Operand &Op);
   bool ParseExpression(int64_t &Res);
   bool ParsePrimaryExpr(int64_t &Res);
+  bool ParseBinOpRHS(unsigned Precedence, int64_t &Res);
   bool ParseParenExpr(int64_t &Res);
 };
 





More information about the llvm-commits mailing list