2009-07-20 20:01:54 +00:00
|
|
|
//===-- llvm/MC/MCAsmLexer.h - Abstract Asm Lexer Interface -----*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_MC_MCASMLEXER_H
|
|
|
|
#define LLVM_MC_MCASMLEXER_H
|
|
|
|
|
2009-07-28 17:58:44 +00:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2009-10-26 01:35:46 +00:00
|
|
|
#include "llvm/System/DataTypes.h"
|
2010-01-20 22:18:24 +00:00
|
|
|
#include "llvm/Support/SMLoc.h"
|
2009-07-28 17:58:44 +00:00
|
|
|
|
2009-07-20 20:01:54 +00:00
|
|
|
namespace llvm {
|
|
|
|
class MCAsmLexer;
|
|
|
|
class MCInst;
|
|
|
|
class Target;
|
|
|
|
|
2009-07-28 17:58:44 +00:00
|
|
|
/// AsmToken - Target independent representation for an assembler token.
|
2010-01-14 21:32:45 +00:00
|
|
|
class AsmToken {
|
|
|
|
public:
|
2009-07-28 17:58:44 +00:00
|
|
|
enum TokenKind {
|
|
|
|
// Markers
|
|
|
|
Eof, Error,
|
|
|
|
|
|
|
|
// String values.
|
|
|
|
Identifier,
|
|
|
|
String,
|
|
|
|
|
|
|
|
// Integer values.
|
|
|
|
Integer,
|
|
|
|
|
2010-01-22 22:42:22 +00:00
|
|
|
// Register values (stored in IntVal). Only used by TargetAsmLexer.
|
|
|
|
Register,
|
|
|
|
|
2009-07-28 17:58:44 +00:00
|
|
|
// No-value.
|
|
|
|
EndOfStatement,
|
|
|
|
Colon,
|
|
|
|
Plus, Minus, Tilde,
|
|
|
|
Slash, // '/'
|
2009-09-04 22:40:31 +00:00
|
|
|
LParen, RParen, LBrac, RBrac, LCurly, RCurly,
|
2010-04-14 04:40:28 +00:00
|
|
|
Star, Dot, Comma, Dollar, Equal, EqualEqual,
|
2009-07-28 17:58:44 +00:00
|
|
|
|
|
|
|
Pipe, PipePipe, Caret,
|
2009-09-04 21:45:34 +00:00
|
|
|
Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, Hash,
|
2009-07-28 17:58:44 +00:00
|
|
|
Less, LessEqual, LessLess, LessGreater,
|
2010-05-21 11:36:59 +00:00
|
|
|
Greater, GreaterEqual, GreaterGreater, At
|
2009-07-28 17:58:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TokenKind Kind;
|
|
|
|
|
|
|
|
/// 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-11-06 10:58:06 +00:00
|
|
|
AsmToken(TokenKind _Kind, StringRef _Str, int64_t _IntVal = 0)
|
2009-07-28 17:58:44 +00:00
|
|
|
: Kind(_Kind), Str(_Str), IntVal(_IntVal) {}
|
|
|
|
|
|
|
|
TokenKind getKind() const { return Kind; }
|
|
|
|
bool is(TokenKind K) const { return Kind == K; }
|
|
|
|
bool isNot(TokenKind K) const { return Kind != K; }
|
|
|
|
|
|
|
|
SMLoc getLoc() const;
|
|
|
|
|
2009-07-31 21:55:09 +00:00
|
|
|
/// getStringContents - Get the contents of a string token (without quotes).
|
|
|
|
StringRef getStringContents() const {
|
|
|
|
assert(Kind == String && "This token isn't a string!");
|
|
|
|
return Str.slice(1, Str.size() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getIdentifier - Get the identifier string for the current token, which
|
|
|
|
/// should be an identifier or a string. This gets the portion of the string
|
|
|
|
/// which should be used as the identifier, e.g., it does not include the
|
|
|
|
/// quotes on strings.
|
|
|
|
StringRef getIdentifier() const {
|
|
|
|
if (Kind == Identifier)
|
|
|
|
return getString();
|
|
|
|
return getStringContents();
|
|
|
|
}
|
|
|
|
|
2009-07-28 17:58:44 +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().
|
|
|
|
StringRef getString() const { return Str; }
|
|
|
|
|
|
|
|
// 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).
|
|
|
|
int64_t getIntVal() const {
|
2009-07-31 21:55:09 +00:00
|
|
|
assert(Kind == Integer && "This token isn't an integer!");
|
2009-07-28 17:58:44 +00:00
|
|
|
return IntVal;
|
|
|
|
}
|
2010-01-22 22:42:22 +00:00
|
|
|
|
|
|
|
/// getRegVal - Get the register number for the current token, which should
|
|
|
|
/// be a register.
|
|
|
|
unsigned getRegVal() const {
|
|
|
|
assert(Kind == Register && "This token isn't a register!");
|
|
|
|
return static_cast<unsigned>(IntVal);
|
|
|
|
}
|
2009-07-28 17:58:44 +00:00
|
|
|
};
|
|
|
|
|
2009-07-20 20:01:54 +00:00
|
|
|
/// MCAsmLexer - Generic assembler lexer interface, for use by target specific
|
|
|
|
/// assembly lexers.
|
|
|
|
class MCAsmLexer {
|
2009-07-28 17:58:44 +00:00
|
|
|
/// The current token, stored in the base class for faster access.
|
|
|
|
AsmToken CurTok;
|
2010-01-20 22:18:24 +00:00
|
|
|
|
|
|
|
/// The location and description of the current error
|
|
|
|
SMLoc ErrLoc;
|
|
|
|
std::string Err;
|
2009-07-28 17:58:44 +00:00
|
|
|
|
2009-07-20 20:01:54 +00:00
|
|
|
MCAsmLexer(const MCAsmLexer &); // DO NOT IMPLEMENT
|
|
|
|
void operator=(const MCAsmLexer &); // DO NOT IMPLEMENT
|
|
|
|
protected: // Can only create subclasses.
|
2010-07-12 17:10:00 +00:00
|
|
|
const char *TokStart;
|
|
|
|
|
2009-07-20 20:01:54 +00:00
|
|
|
MCAsmLexer();
|
2009-07-28 17:58:44 +00:00
|
|
|
|
|
|
|
virtual AsmToken LexToken() = 0;
|
2010-01-20 22:18:24 +00:00
|
|
|
|
|
|
|
void SetError(const SMLoc &errLoc, const std::string &err) {
|
|
|
|
ErrLoc = errLoc;
|
|
|
|
Err = err;
|
|
|
|
}
|
|
|
|
|
2009-07-20 20:01:54 +00:00
|
|
|
public:
|
|
|
|
virtual ~MCAsmLexer();
|
2009-07-28 17:58:44 +00:00
|
|
|
|
|
|
|
/// Lex - Consume the next token from the input stream and return it.
|
|
|
|
///
|
|
|
|
/// The lexer will continuosly return the end-of-file token once the end of
|
|
|
|
/// the main input file has been reached.
|
|
|
|
const AsmToken &Lex() {
|
|
|
|
return CurTok = LexToken();
|
|
|
|
}
|
|
|
|
|
2010-07-12 20:32:33 +00:00
|
|
|
virtual StringRef LexUntilEndOfStatement() = 0;
|
|
|
|
|
2010-07-12 17:10:00 +00:00
|
|
|
/// getLoc - Get the current source location.
|
|
|
|
SMLoc getLoc() const;
|
|
|
|
|
2009-07-28 17:58:44 +00:00
|
|
|
/// getTok - Get the current (last) lexed token.
|
|
|
|
const AsmToken &getTok() {
|
|
|
|
return CurTok;
|
|
|
|
}
|
2010-01-20 22:18:24 +00:00
|
|
|
|
|
|
|
/// getErrLoc - Get the current error location
|
|
|
|
const SMLoc &getErrLoc() {
|
|
|
|
return ErrLoc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getErr - Get the current error string
|
|
|
|
const std::string &getErr() {
|
|
|
|
return Err;
|
|
|
|
}
|
2009-07-28 17:58:44 +00:00
|
|
|
|
|
|
|
/// getKind - Get the kind of current token.
|
|
|
|
AsmToken::TokenKind getKind() const { return CurTok.getKind(); }
|
|
|
|
|
|
|
|
/// is - Check if the current token has kind \arg K.
|
|
|
|
bool is(AsmToken::TokenKind K) const { return CurTok.is(K); }
|
|
|
|
|
|
|
|
/// isNot - Check if the current token has kind \arg K.
|
|
|
|
bool isNot(AsmToken::TokenKind K) const { return CurTok.isNot(K); }
|
2009-07-20 20:01:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // End llvm namespace
|
|
|
|
|
|
|
|
#endif
|