summaryrefslogtreecommitdiff
path: root/tools/llvm-mc/llvm-mc.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2009-06-21 20:16:42 +0000
committerChris Lattner <sabre@nondot.org>2009-06-21 20:16:42 +0000
commit27aa7d259b416a9d1bf837ed2c3c11463367b11c (patch)
tree630f8ad4b1caedcf7a1bbd296957f1ab7194bc3a /tools/llvm-mc/llvm-mc.cpp
parent10a907d70fb54c40eecabb889e81c79b44092221 (diff)
downloadllvm-27aa7d259b416a9d1bf837ed2c3c11463367b11c.tar.gz
llvm-27aa7d259b416a9d1bf837ed2c3c11463367b11c.tar.bz2
llvm-27aa7d259b416a9d1bf837ed2c3c11463367b11c.tar.xz
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). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73859 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/llvm-mc/llvm-mc.cpp')
-rw-r--r--tools/llvm-mc/llvm-mc.cpp51
1 files changed, 43 insertions, 8 deletions
diff --git a/tools/llvm-mc/llvm-mc.cpp b/tools/llvm-mc/llvm-mc.cpp
index 7a179e83ff..0acd78ea41 100644
--- a/tools/llvm-mc/llvm-mc.cpp
+++ b/tools/llvm-mc/llvm-mc.cpp
@@ -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 @@ IncludeDirs("I", cl::desc("Directory of include files"),
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 @@ static int AssembleInput(const char *ProgName) {
// 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 @@ static int AssembleInput(const char *ProgName) {
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 @@ int main(int argc, char **argv) {
switch (Action) {
default:
+ case AC_AsLex:
+ return AsLexInput(argv[0]);
case AC_Assemble:
return AssembleInput(argv[0]);
}