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

Chris Lattner sabre at nondot.org
Sun Jun 21 13:16:42 PDT 2009


Author: lattner
Date: Sun Jun 21 15:16:42 2009
New Revision: 73859

URL: http://llvm.org/viewvc/llvm-project?rev=73859&view=rev
Log:
stub out parser for asm files.  Change invariant on lexer to always
print its error message when it returns an asmtok::Error token.
Compute a proper error code for llvm-mc in 'lex' mode.  Add new
-as-lex option to enable lexing mode (vs parsing mode).

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

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

==============================================================================
--- llvm/trunk/tools/llvm-mc/AsmLexer.cpp (original)
+++ llvm/trunk/tools/llvm-mc/AsmLexer.cpp Sun Jun 21 15:16:42 2009
@@ -84,7 +84,7 @@
 /// LexPercent: Register: %[a-zA-Z0-9]+
 asmtok::TokKind AsmLexer::LexPercent() {
   if (!isalnum(*CurPtr))
-    return asmtok::Error;  // Must have at least one character.
+    return ReturnError(TokStart, "invalid register name");
   while (isalnum(*CurPtr))
     ++CurPtr;
   CurStrVal.assign(TokStart, CurPtr);   // Skip %
@@ -103,8 +103,7 @@
     int CurChar = getNextChar();
     switch (CurChar) {
     case EOF:
-      PrintError(TokStart, "Unterminated comment!");
-      return asmtok::Error;
+      return ReturnError(TokStart, "unterminated comment");
     case '*':
       // End of the comment?
       if (CurPtr[0] != '/') break;
@@ -137,9 +136,9 @@
 /// TODO: FP literal.
 asmtok::TokKind AsmLexer::LexDigit() {
   if (*CurPtr == ':')
-    return asmtok::Error;  // FIXME LOCAL LABEL.
+    return ReturnError(TokStart, "FIXME: local label not implemented");
   if (*CurPtr == 'f' || *CurPtr == 'b')
-    return asmtok::Error;  // FIXME FORWARD/BACKWARD LABEL.
+    return ReturnError(TokStart, "FIXME: directional label not implemented");
   
   // Decimal integer: [1-9][0-9]*
   if (CurPtr[-1] != '0') {
@@ -229,7 +228,7 @@
       return LexIdentifier();
     
     // Unknown character, emit an error.
-    return asmtok::Error;
+    return ReturnError(TokStart, "invalid character in input");
   case EOF: return asmtok::Eof;
   case 0:
   case ' ':

Added: llvm/trunk/tools/llvm-mc/AsmParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mc/AsmParser.cpp?rev=73859&view=auto

==============================================================================
--- llvm/trunk/tools/llvm-mc/AsmParser.cpp (added)
+++ llvm/trunk/tools/llvm-mc/AsmParser.cpp Sun Jun 21 15:16:42 2009
@@ -0,0 +1,19 @@
+//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This class implements the parser for assembly files.
+//
+//===----------------------------------------------------------------------===//
+
+#include "AsmParser.h"
+using namespace llvm;
+
+bool AsmParser::Run() {
+  return false;
+}

Added: llvm/trunk/tools/llvm-mc/AsmParser.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mc/AsmParser.h?rev=73859&view=auto

==============================================================================
--- llvm/trunk/tools/llvm-mc/AsmParser.h (added)
+++ llvm/trunk/tools/llvm-mc/AsmParser.h Sun Jun 21 15:16:42 2009
@@ -0,0 +1,34 @@
+//===- AsmParser.h - Parser for Assembly Files ------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This class declares the parser for assembly files.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef ASMPARSER_H
+#define ASMPARSER_H
+
+#include "AsmLexer.h"
+
+namespace llvm {
+
+class AsmParser {
+  AsmLexer Lexer;
+  
+public:
+  AsmParser(SourceMgr &SM) : Lexer(SM) {}
+  ~AsmParser() {}
+  
+  bool Run();
+  
+};
+
+} // end namespace llvm
+
+#endif

Modified: llvm/trunk/tools/llvm-mc/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mc/CMakeLists.txt?rev=73859&r1=73858&r2=73859&view=diff

==============================================================================
--- llvm/trunk/tools/llvm-mc/CMakeLists.txt (original)
+++ llvm/trunk/tools/llvm-mc/CMakeLists.txt Sun Jun 21 15:16:42 2009
@@ -3,4 +3,5 @@
 add_llvm_tool(llvm-mc
   llvm-mc.cpp
   AsmLexer.cpp
+  AsmParser.cpp
   )

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

==============================================================================
--- llvm/trunk/tools/llvm-mc/llvm-mc.cpp (original)
+++ llvm/trunk/tools/llvm-mc/llvm-mc.cpp Sun Jun 21 15:16:42 2009
@@ -19,7 +19,7 @@
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/System/Signals.h"
-#include "AsmLexer.h"
+#include "AsmParser.h"
 using namespace llvm;
 
 static cl::opt<std::string>
@@ -34,16 +34,20 @@
             cl::value_desc("directory"), cl::Prefix);
 
 enum ActionType {
+  AC_AsLex,
   AC_Assemble
 };
 
 static cl::opt<ActionType>
 Action(cl::desc("Action to perform:"),
-       cl::values(clEnumValN(AC_Assemble, "assemble",
+       cl::init(AC_Assemble),
+       cl::values(clEnumValN(AC_AsLex, "as-lex",
+                             "Lex tokens from a .s file"),
+                  clEnumValN(AC_Assemble, "assemble",
                              "Assemble a .s file (default)"),
                   clEnumValEnd));
 
-static int AssembleInput(const char *ProgName) {
+static int AsLexInput(const char *ProgName) {
   std::string ErrorMessage;
   MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
                                                       &ErrorMessage);
@@ -65,16 +69,19 @@
   // it later.
   SrcMgr.setIncludeDirs(IncludeDirs);
 
-  
-  
   AsmLexer Lexer(SrcMgr);
   
+  bool Error = false;
+  
   asmtok::TokKind Tok = Lexer.Lex();
   while (Tok != asmtok::Eof) {
     switch (Tok) {
-    default: Lexer.PrintError(Lexer.getLoc(), "driver: unknown token"); break;
+    default:
+      Lexer.PrintError(Lexer.getLoc(), "driver: unknown token");
+      Error = true;
+      break;
     case asmtok::Error:
-      Lexer.PrintError(Lexer.getLoc(), "error, bad token");
+      Error = true; // error already printed.
       break;
     case asmtok::Identifier:
       outs() << "identifier: " << Lexer.getCurStrVal() << '\n';
@@ -103,9 +110,35 @@
     Tok = Lexer.Lex();
   }
   
-  return 1;
+  return Error;
 }
 
+static int AssembleInput(const char *ProgName) {
+  std::string ErrorMessage;
+  MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
+                                                      &ErrorMessage);
+  if (Buffer == 0) {
+    errs() << ProgName << ": ";
+    if (ErrorMessage.size())
+      errs() << ErrorMessage << "\n";
+    else
+      errs() << "input file didn't read correctly.\n";
+    return 1;
+  }
+  
+  SourceMgr SrcMgr;
+  
+  // Tell SrcMgr about this buffer, which is what TGParser will pick up.
+  SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
+  
+  // Record the location of the include directories so that the lexer can find
+  // it later.
+  SrcMgr.setIncludeDirs(IncludeDirs);
+  
+  AsmParser Parser(SrcMgr);
+  return Parser.Run();
+}  
+
 
 int main(int argc, char **argv) {
   // Print a stack trace if we signal out.
@@ -116,6 +149,8 @@
 
   switch (Action) {
   default:
+  case AC_AsLex:
+    return AsLexInput(argv[0]);
   case AC_Assemble:
     return AssembleInput(argv[0]);
   }





More information about the llvm-commits mailing list