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
This commit is contained in:
Chris Lattner
2009-06-21 20:16:42 +00:00
parent 10a907d70f
commit 27aa7d259b
5 changed files with 102 additions and 14 deletions

View File

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

View File

@ -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;
}

34
tools/llvm-mc/AsmParser.h Normal file
View File

@ -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

View File

@ -3,4 +3,5 @@ set(LLVM_LINK_COMPONENTS support)
add_llvm_tool(llvm-mc add_llvm_tool(llvm-mc
llvm-mc.cpp llvm-mc.cpp
AsmLexer.cpp AsmLexer.cpp
AsmParser.cpp
) )

View File

@ -19,7 +19,7 @@
#include "llvm/Support/SourceMgr.h" #include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include "llvm/System/Signals.h" #include "llvm/System/Signals.h"
#include "AsmLexer.h" #include "AsmParser.h"
using namespace llvm; using namespace llvm;
static cl::opt<std::string> static cl::opt<std::string>
@ -34,16 +34,20 @@ IncludeDirs("I", cl::desc("Directory of include files"),
cl::value_desc("directory"), cl::Prefix); cl::value_desc("directory"), cl::Prefix);
enum ActionType { enum ActionType {
AC_AsLex,
AC_Assemble AC_Assemble
}; };
static cl::opt<ActionType> static cl::opt<ActionType>
Action(cl::desc("Action to perform:"), 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)"), "Assemble a .s file (default)"),
clEnumValEnd)); clEnumValEnd));
static int AssembleInput(const char *ProgName) { static int AsLexInput(const char *ProgName) {
std::string ErrorMessage; std::string ErrorMessage;
MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename, MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
&ErrorMessage); &ErrorMessage);
@ -65,16 +69,19 @@ static int AssembleInput(const char *ProgName) {
// it later. // it later.
SrcMgr.setIncludeDirs(IncludeDirs); SrcMgr.setIncludeDirs(IncludeDirs);
AsmLexer Lexer(SrcMgr); AsmLexer Lexer(SrcMgr);
bool Error = false;
asmtok::TokKind Tok = Lexer.Lex(); asmtok::TokKind Tok = Lexer.Lex();
while (Tok != asmtok::Eof) { while (Tok != asmtok::Eof) {
switch (Tok) { 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: case asmtok::Error:
Lexer.PrintError(Lexer.getLoc(), "error, bad token"); Error = true; // error already printed.
break; break;
case asmtok::Identifier: case asmtok::Identifier:
outs() << "identifier: " << Lexer.getCurStrVal() << '\n'; outs() << "identifier: " << Lexer.getCurStrVal() << '\n';
@ -103,9 +110,35 @@ static int AssembleInput(const char *ProgName) {
Tok = Lexer.Lex(); 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) { int main(int argc, char **argv) {
// Print a stack trace if we signal out. // Print a stack trace if we signal out.
@ -116,6 +149,8 @@ int main(int argc, char **argv) {
switch (Action) { switch (Action) {
default: default:
case AC_AsLex:
return AsLexInput(argv[0]);
case AC_Assemble: case AC_Assemble:
return AssembleInput(argv[0]); return AssembleInput(argv[0]);
} }