2015-06-23 04:37:46 +08:00
|
|
|
//===- MILexer.cpp - Machine instructions lexer implementation ----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the lexing of machine instructions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MILexer.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include <cctype>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// This class provides a way to iterate and get characters from the source
|
|
|
|
/// string.
|
|
|
|
class Cursor {
|
|
|
|
const char *Ptr;
|
|
|
|
const char *End;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit Cursor(StringRef Str) {
|
|
|
|
Ptr = Str.data();
|
|
|
|
End = Ptr + Str.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isEOF() const { return Ptr == End; }
|
|
|
|
|
2015-06-24 14:40:09 +08:00
|
|
|
char peek(int I = 0) const { return End - Ptr <= I ? 0 : Ptr[I]; }
|
2015-06-23 04:37:46 +08:00
|
|
|
|
|
|
|
void advance() { ++Ptr; }
|
|
|
|
|
|
|
|
StringRef remaining() const { return StringRef(Ptr, End - Ptr); }
|
|
|
|
|
|
|
|
StringRef upto(Cursor C) const {
|
|
|
|
assert(C.Ptr >= Ptr && C.Ptr <= End);
|
|
|
|
return StringRef(Ptr, C.Ptr - Ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef::iterator location() const { return Ptr; }
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
/// Skip the leading whitespace characters and return the updated cursor.
|
|
|
|
static Cursor skipWhitespace(Cursor C) {
|
|
|
|
while (isspace(C.peek()))
|
|
|
|
C.advance();
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isIdentifierChar(char C) {
|
|
|
|
return isalpha(C) || isdigit(C) || C == '_' || C == '-' || C == '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
static Cursor lexIdentifier(Cursor C, MIToken &Token) {
|
|
|
|
auto Range = C;
|
|
|
|
while (isIdentifierChar(C.peek()))
|
|
|
|
C.advance();
|
|
|
|
Token = MIToken(MIToken::Identifier, Range.upto(C));
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2015-06-24 00:35:26 +08:00
|
|
|
static Cursor lexPercent(Cursor C, MIToken &Token) {
|
|
|
|
auto Range = C;
|
|
|
|
C.advance(); // Skip '%'
|
|
|
|
while (isIdentifierChar(C.peek()))
|
|
|
|
C.advance();
|
|
|
|
Token = MIToken(MIToken::NamedRegister, Range.upto(C));
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2015-06-24 07:42:28 +08:00
|
|
|
static Cursor lexIntegerLiteral(Cursor C, MIToken &Token) {
|
|
|
|
auto Range = C;
|
|
|
|
C.advance();
|
|
|
|
while (isdigit(C.peek()))
|
|
|
|
C.advance();
|
|
|
|
StringRef StrVal = Range.upto(C);
|
|
|
|
Token = MIToken(MIToken::IntegerLiteral, StrVal, APSInt(StrVal));
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2015-06-24 00:35:26 +08:00
|
|
|
static MIToken::TokenKind symbolToken(char C) {
|
|
|
|
switch (C) {
|
|
|
|
case ',':
|
|
|
|
return MIToken::comma;
|
|
|
|
case '=':
|
|
|
|
return MIToken::equal;
|
|
|
|
default:
|
|
|
|
return MIToken::Error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Cursor lexSymbol(Cursor C, MIToken::TokenKind Kind, MIToken &Token) {
|
|
|
|
auto Range = C;
|
|
|
|
C.advance();
|
|
|
|
Token = MIToken(Kind, Range.upto(C));
|
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2015-06-23 04:37:46 +08:00
|
|
|
StringRef llvm::lexMIToken(
|
|
|
|
StringRef Source, MIToken &Token,
|
|
|
|
function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
|
|
|
|
auto C = skipWhitespace(Cursor(Source));
|
|
|
|
if (C.isEOF()) {
|
|
|
|
Token = MIToken(MIToken::Eof, C.remaining());
|
|
|
|
return C.remaining();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Char = C.peek();
|
|
|
|
if (isalpha(Char) || Char == '_')
|
|
|
|
return lexIdentifier(C, Token).remaining();
|
2015-06-24 00:35:26 +08:00
|
|
|
if (Char == '%')
|
|
|
|
return lexPercent(C, Token).remaining();
|
2015-06-24 07:42:28 +08:00
|
|
|
if (isdigit(Char) || (Char == '-' && isdigit(C.peek(1))))
|
|
|
|
return lexIntegerLiteral(C, Token).remaining();
|
2015-06-24 00:35:26 +08:00
|
|
|
MIToken::TokenKind Kind = symbolToken(Char);
|
|
|
|
if (Kind != MIToken::Error)
|
|
|
|
return lexSymbol(C, Kind, Token).remaining();
|
2015-06-23 04:37:46 +08:00
|
|
|
Token = MIToken(MIToken::Error, C.remaining());
|
|
|
|
ErrorCallback(C.location(),
|
|
|
|
Twine("unexpected character '") + Twine(Char) + "'");
|
|
|
|
return C.remaining();
|
|
|
|
}
|