MCAsmLexer.h revision 1f6efa3996dd1929fbc129203ce5009b620e6969
1dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar//===-- llvm/MC/MCAsmLexer.h - Abstract Asm Lexer Interface -----*- C++ -*-===// 2dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar// 3dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar// The LLVM Compiler Infrastructure 4dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar// 5dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar// This file is distributed under the University of Illinois Open Source 6dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar// License. See LICENSE.TXT for details. 7dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar// 8dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar//===----------------------------------------------------------------------===// 9dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar 10dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar#ifndef LLVM_MC_MCASMLEXER_H 11dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar#define LLVM_MC_MCASMLEXER_H 12dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar 13cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar#include "llvm/ADT/StringRef.h" 141f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/DataTypes.h" 1579036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan#include "llvm/Support/SMLoc.h" 16cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar 17dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbarnamespace llvm { 18dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbarclass MCAsmLexer; 19dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbarclass MCInst; 20dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbarclass Target; 21dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar 22cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar/// AsmToken - Target independent representation for an assembler token. 23f007e853e26845cd6866b52d646455fc69f4e0afChris Lattnerclass AsmToken { 24f007e853e26845cd6866b52d646455fc69f4e0afChris Lattnerpublic: 25cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar enum TokenKind { 26cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar // Markers 27cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar Eof, Error, 28cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar 29cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar // String values. 30cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar Identifier, 31cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar String, 32cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar 33cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar // Integer values. 34cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar Integer, 35cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar 3654f0a625b0eb9afeece652a8462755010d237c78Daniel Dunbar // Real values. 3754f0a625b0eb9afeece652a8462755010d237c78Daniel Dunbar Real, 3854f0a625b0eb9afeece652a8462755010d237c78Daniel Dunbar 395d74e1f64445713cca863af03b8b6ab39321046eSean Callanan // Register values (stored in IntVal). Only used by TargetAsmLexer. 405d74e1f64445713cca863af03b8b6ab39321046eSean Callanan Register, 415d74e1f64445713cca863af03b8b6ab39321046eSean Callanan 42cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar // No-value. 43cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar EndOfStatement, 44cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar Colon, 45cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar Plus, Minus, Tilde, 46cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar Slash, // '/' 47fb0f0dedd74dab13737a77671a724ee88465f5daKevin Enderby LParen, RParen, LBrac, RBrac, LCurly, RCurly, 48d305035155ef3d138e102434bf5a733ea2e32405Chris Lattner Star, Dot, Comma, Dollar, Equal, EqualEqual, 49cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar 50cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar Pipe, PipePipe, Caret, 519823ca971d5cb475401e59fde244caf5087c74a1Kevin Enderby Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, Hash, 52cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar Less, LessEqual, LessLess, LessGreater, 53924c5e58f2a7c89019000e7dee3391dcebdf8a21Matt Fleming Greater, GreaterEqual, GreaterGreater, At 54cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar }; 55cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar 56cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar TokenKind Kind; 57cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar 58cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar /// A reference to the entire token contents; this is always a pointer into 59cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar /// a memory buffer owned by the source manager. 60cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar StringRef Str; 61cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar 62cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar int64_t IntVal; 63cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar 64cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbarpublic: 65cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar AsmToken() {} 662928c83b010f7cfdb0f819199d806f6942a7d995Daniel Dunbar AsmToken(TokenKind _Kind, StringRef _Str, int64_t _IntVal = 0) 67cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar : Kind(_Kind), Str(_Str), IntVal(_IntVal) {} 68cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar 69cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar TokenKind getKind() const { return Kind; } 70cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar bool is(TokenKind K) const { return Kind == K; } 71cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar bool isNot(TokenKind K) const { return Kind != K; } 72cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar 73cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar SMLoc getLoc() const; 74cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar 7576c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar /// getStringContents - Get the contents of a string token (without quotes). 7676c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar StringRef getStringContents() const { 7776c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar assert(Kind == String && "This token isn't a string!"); 7876c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar return Str.slice(1, Str.size() - 1); 7976c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar } 8076c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar 8176c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar /// getIdentifier - Get the identifier string for the current token, which 8276c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar /// should be an identifier or a string. This gets the portion of the string 8376c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar /// which should be used as the identifier, e.g., it does not include the 8476c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar /// quotes on strings. 8576c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar StringRef getIdentifier() const { 8676c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar if (Kind == Identifier) 8776c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar return getString(); 8876c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar return getStringContents(); 8976c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar } 9076c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar 91cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar /// getString - Get the string for the current token, this includes all 92cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar /// characters (for example, the quotes on strings) in the token. 93cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar /// 94cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar /// The returned StringRef points into the source manager's memory buffer, and 95cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar /// is safe to store across calls to Lex(). 96cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar StringRef getString() const { return Str; } 97cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar 98cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar // FIXME: Don't compute this in advance, it makes every token larger, and is 99cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar // also not generally what we want (it is nicer for recovery etc. to lex 123br 100cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar // as a single token, then diagnose as an invalid number). 101cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar int64_t getIntVal() const { 10276c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar assert(Kind == Integer && "This token isn't an integer!"); 103cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar return IntVal; 104cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar } 1055d74e1f64445713cca863af03b8b6ab39321046eSean Callanan 1065d74e1f64445713cca863af03b8b6ab39321046eSean Callanan /// getRegVal - Get the register number for the current token, which should 1075d74e1f64445713cca863af03b8b6ab39321046eSean Callanan /// be a register. 1085d74e1f64445713cca863af03b8b6ab39321046eSean Callanan unsigned getRegVal() const { 1095d74e1f64445713cca863af03b8b6ab39321046eSean Callanan assert(Kind == Register && "This token isn't a register!"); 1105d74e1f64445713cca863af03b8b6ab39321046eSean Callanan return static_cast<unsigned>(IntVal); 1115d74e1f64445713cca863af03b8b6ab39321046eSean Callanan } 112cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar}; 113cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar 114dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar/// MCAsmLexer - Generic assembler lexer interface, for use by target specific 115dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar/// assembly lexers. 116dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbarclass MCAsmLexer { 117cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar /// The current token, stored in the base class for faster access. 118cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar AsmToken CurTok; 11979036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan 12079036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan /// The location and description of the current error 12179036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan SMLoc ErrLoc; 12279036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan std::string Err; 123cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar 124dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar MCAsmLexer(const MCAsmLexer &); // DO NOT IMPLEMENT 125dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar void operator=(const MCAsmLexer &); // DO NOT IMPLEMENT 126dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbarprotected: // Can only create subclasses. 127ca90dc6d295f7f6a5ef4240f26bcebe54276def5Daniel Dunbar const char *TokStart; 128ca90dc6d295f7f6a5ef4240f26bcebe54276def5Daniel Dunbar 129dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar MCAsmLexer(); 130cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar 131cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar virtual AsmToken LexToken() = 0; 13279036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan 13379036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan void SetError(const SMLoc &errLoc, const std::string &err) { 13479036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan ErrLoc = errLoc; 13579036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan Err = err; 13679036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan } 13779036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan 138dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbarpublic: 139dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar virtual ~MCAsmLexer(); 140cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar 141cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar /// Lex - Consume the next token from the input stream and return it. 142cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar /// 143cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar /// The lexer will continuosly return the end-of-file token once the end of 144cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar /// the main input file has been reached. 145cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar const AsmToken &Lex() { 146cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar return CurTok = LexToken(); 147cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar } 148cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar 14919ad3b88f71fdc0fe0ec19e05bb37c3ef1a42b5bDaniel Dunbar virtual StringRef LexUntilEndOfStatement() = 0; 15019ad3b88f71fdc0fe0ec19e05bb37c3ef1a42b5bDaniel Dunbar 151ca90dc6d295f7f6a5ef4240f26bcebe54276def5Daniel Dunbar /// getLoc - Get the current source location. 152ca90dc6d295f7f6a5ef4240f26bcebe54276def5Daniel Dunbar SMLoc getLoc() const; 153ca90dc6d295f7f6a5ef4240f26bcebe54276def5Daniel Dunbar 154cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar /// getTok - Get the current (last) lexed token. 155cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar const AsmToken &getTok() { 156cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar return CurTok; 157cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar } 15879036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan 15979036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan /// getErrLoc - Get the current error location 16079036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan const SMLoc &getErrLoc() { 16179036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan return ErrLoc; 16279036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan } 16379036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan 16479036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan /// getErr - Get the current error string 16579036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan const std::string &getErr() { 16679036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan return Err; 16779036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan } 168cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar 169cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar /// getKind - Get the kind of current token. 170cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar AsmToken::TokenKind getKind() const { return CurTok.getKind(); } 171cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar 172cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar /// is - Check if the current token has kind \arg K. 173cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar bool is(AsmToken::TokenKind K) const { return CurTok.is(K); } 174cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar 175cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar /// isNot - Check if the current token has kind \arg K. 176cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar bool isNot(AsmToken::TokenKind K) const { return CurTok.isNot(K); } 177dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar}; 178dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar 179dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar} // End llvm namespace 180dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar 181dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar#endif 182