2009-06-21 07:19:10 +00:00
|
|
|
//===- AsmLexer.h - Lexer 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 lexer for assembly files.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef ASMLEXER_H
|
|
|
|
#define ASMLEXER_H
|
|
|
|
|
2009-07-27 21:49:56 +00:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2009-07-20 20:01:54 +00:00
|
|
|
#include "llvm/MC/MCAsmLexer.h"
|
2009-06-21 07:19:10 +00:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
|
|
|
#include <string>
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
class MemoryBuffer;
|
|
|
|
class SourceMgr;
|
|
|
|
class SMLoc;
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
/// AsmToken - Target independent representation for an assembler token.
|
|
|
|
struct AsmToken {
|
|
|
|
enum TokenKind {
|
2009-06-21 07:19:10 +00:00
|
|
|
// Markers
|
|
|
|
Eof, Error,
|
|
|
|
|
2009-06-21 19:56:35 +00:00
|
|
|
// String values.
|
2009-06-21 07:19:10 +00:00
|
|
|
Identifier,
|
2009-06-21 19:21:25 +00:00
|
|
|
Register,
|
2009-06-21 19:56:35 +00:00
|
|
|
String,
|
|
|
|
|
|
|
|
// Integer values.
|
2009-07-28 16:08:33 +00:00
|
|
|
Integer,
|
2009-06-21 07:19:10 +00:00
|
|
|
|
2009-06-21 19:56:35 +00:00
|
|
|
// No-value.
|
2009-06-21 19:21:25 +00:00
|
|
|
EndOfStatement,
|
2009-06-21 07:19:10 +00:00
|
|
|
Colon,
|
2009-06-22 06:32:03 +00:00
|
|
|
Plus, Minus, Tilde,
|
2009-06-21 19:21:25 +00:00
|
|
|
Slash, // '/'
|
|
|
|
LParen, RParen,
|
2009-06-29 20:37:27 +00:00
|
|
|
Star, Comma, Dollar, Equal, EqualEqual,
|
2009-06-23 05:57:07 +00:00
|
|
|
|
2009-06-29 20:37:27 +00:00
|
|
|
Pipe, PipePipe, Caret,
|
|
|
|
Amp, AmpAmp, Exclaim, ExclaimEqual, Percent,
|
|
|
|
Less, LessEqual, LessLess, LessGreater,
|
|
|
|
Greater, GreaterEqual, GreaterGreater
|
2009-06-21 07:19:10 +00:00
|
|
|
};
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
TokenKind Kind;
|
2009-07-28 03:00:54 +00:00
|
|
|
|
|
|
|
/// A reference to the entire token contents; this is always a pointer into
|
|
|
|
/// a memory buffer owned by the source manager.
|
|
|
|
StringRef Str;
|
|
|
|
|
|
|
|
int64_t IntVal;
|
|
|
|
|
|
|
|
public:
|
|
|
|
AsmToken() {}
|
2009-07-28 16:08:33 +00:00
|
|
|
AsmToken(TokenKind _Kind, const StringRef &_Str, int64_t _IntVal = 0)
|
2009-07-28 03:00:54 +00:00
|
|
|
: Kind(_Kind), Str(_Str), IntVal(_IntVal) {}
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
TokenKind getKind() const { return Kind; }
|
|
|
|
bool is(TokenKind K) const { return Kind == K; }
|
|
|
|
bool isNot(TokenKind K) const { return Kind != K; }
|
2009-07-28 03:00:54 +00:00
|
|
|
|
|
|
|
SMLoc getLoc() const;
|
|
|
|
|
2009-07-28 16:38:40 +00:00
|
|
|
/// getString - Get the string for the current token, this includes all
|
|
|
|
/// characters (for example, the quotes on strings) in the token.
|
|
|
|
///
|
|
|
|
/// The returned StringRef points into the source manager's memory buffer, and
|
|
|
|
/// is safe to store across calls to Lex().
|
2009-07-28 03:00:54 +00:00
|
|
|
StringRef getString() const { return Str; }
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
// FIXME: Don't compute this in advance, it makes every token larger, and is
|
|
|
|
// also not generally what we want (it is nicer for recovery etc. to lex 123br
|
|
|
|
// as a single token, then diagnose as an invalid number).
|
2009-07-28 03:00:54 +00:00
|
|
|
int64_t getIntVal() const {
|
2009-07-28 16:08:33 +00:00
|
|
|
assert(Kind == Integer && "This token isn't an integer");
|
2009-07-28 03:00:54 +00:00
|
|
|
return IntVal;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-06-21 07:19:10 +00:00
|
|
|
/// AsmLexer - Lexer class for assembly files.
|
2009-07-20 20:01:54 +00:00
|
|
|
class AsmLexer : public MCAsmLexer {
|
2009-06-21 07:19:10 +00:00
|
|
|
SourceMgr &SrcMgr;
|
|
|
|
|
|
|
|
const char *CurPtr;
|
|
|
|
const MemoryBuffer *CurBuf;
|
|
|
|
|
|
|
|
const char *TokStart;
|
2009-07-28 03:00:54 +00:00
|
|
|
|
|
|
|
/// The current token.
|
|
|
|
AsmToken CurTok;
|
2009-06-21 07:19:10 +00:00
|
|
|
|
2009-07-28 03:00:54 +00:00
|
|
|
/// This is the current buffer index we're lexing from as managed by the
|
|
|
|
/// SourceMgr object.
|
2009-06-21 07:19:10 +00:00
|
|
|
int CurBuffer;
|
|
|
|
|
2009-06-24 00:33:19 +00:00
|
|
|
void operator=(const AsmLexer&); // DO NOT IMPLEMENT
|
|
|
|
AsmLexer(const AsmLexer&); // DO NOT IMPLEMENT
|
2009-06-21 07:19:10 +00:00
|
|
|
public:
|
|
|
|
AsmLexer(SourceMgr &SrcMgr);
|
2009-06-24 00:33:19 +00:00
|
|
|
~AsmLexer();
|
2009-06-21 07:19:10 +00:00
|
|
|
|
2009-07-28 16:56:42 +00:00
|
|
|
const AsmToken &Lex() {
|
|
|
|
return CurTok = LexToken();
|
2009-06-21 07:19:10 +00:00
|
|
|
}
|
|
|
|
|
2009-07-28 16:08:33 +00:00
|
|
|
AsmToken::TokenKind getKind() const { return CurTok.getKind(); }
|
|
|
|
bool is(AsmToken::TokenKind K) const { return CurTok.is(K); }
|
|
|
|
bool isNot(AsmToken::TokenKind K) const { return CurTok.isNot(K); }
|
2009-07-27 21:49:56 +00:00
|
|
|
|
2009-06-21 07:19:10 +00:00
|
|
|
SMLoc getLoc() const;
|
2009-07-28 16:38:40 +00:00
|
|
|
|
|
|
|
/// getTok - Return a reference to the current (last) lexed token.
|
|
|
|
const AsmToken &getTok() const { return CurTok; }
|
2009-06-21 07:19:10 +00:00
|
|
|
|
2009-07-16 06:14:39 +00:00
|
|
|
/// EnterIncludeFile - Enter the specified file. This returns true on failure.
|
|
|
|
bool EnterIncludeFile(const std::string &Filename);
|
|
|
|
|
2009-06-30 00:49:23 +00:00
|
|
|
void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
|
2009-06-21 07:19:10 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
int getNextChar();
|
2009-07-28 03:00:54 +00:00
|
|
|
AsmToken ReturnError(const char *Loc, const std::string &Msg);
|
2009-06-21 07:19:10 +00:00
|
|
|
|
|
|
|
/// LexToken - Read the next token and return its code.
|
2009-07-28 03:00:54 +00:00
|
|
|
AsmToken LexToken();
|
|
|
|
AsmToken LexIdentifier();
|
|
|
|
AsmToken LexPercent();
|
|
|
|
AsmToken LexSlash();
|
|
|
|
AsmToken LexLineComment();
|
|
|
|
AsmToken LexDigit();
|
|
|
|
AsmToken LexQuote();
|
2009-06-21 07:19:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif
|