MCAsmLexer.h revision 1f7210e808373fa92be3a2d4fa653a6f79d5088b
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"
141f7210e808373fa92be3a2d4fa653a6f79d5088bCraig Topper#include "llvm/Support/Compiler.h"
151f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/DataTypes.h"
1679036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan#include "llvm/Support/SMLoc.h"
17cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar
18dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbarnamespace llvm {
19dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar
20cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar/// AsmToken - Target independent representation for an assembler token.
21f007e853e26845cd6866b52d646455fc69f4e0afChris Lattnerclass AsmToken {
22f007e853e26845cd6866b52d646455fc69f4e0afChris Lattnerpublic:
23cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  enum TokenKind {
24cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar    // Markers
25cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar    Eof, Error,
26cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar
27cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar    // String values.
28cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar    Identifier,
29cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar    String,
3098311ecb4ae9c82baba9e3a48acf756a81c8e9a4Jim Grosbach
31cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar    // Integer values.
32cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar    Integer,
3398311ecb4ae9c82baba9e3a48acf756a81c8e9a4Jim Grosbach
3454f0a625b0eb9afeece652a8462755010d237c78Daniel Dunbar    // Real values.
3554f0a625b0eb9afeece652a8462755010d237c78Daniel Dunbar    Real,
3698311ecb4ae9c82baba9e3a48acf756a81c8e9a4Jim Grosbach
3794b9550a32d189704a8eae55505edf62662c0534Evan Cheng    // Register values (stored in IntVal).  Only used by MCTargetAsmLexer.
385d74e1f64445713cca863af03b8b6ab39321046eSean Callanan    Register,
3998311ecb4ae9c82baba9e3a48acf756a81c8e9a4Jim Grosbach
40cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar    // No-value.
41cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar    EndOfStatement,
42cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar    Colon,
43cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar    Plus, Minus, Tilde,
44cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar    Slash,    // '/'
45653664471333f316020e96dd3d664f4984f66a65Rafael Espindola    BackSlash, // '\'
46fb0f0dedd74dab13737a77671a724ee88465f5daKevin Enderby    LParen, RParen, LBrac, RBrac, LCurly, RCurly,
47d305035155ef3d138e102434bf5a733ea2e32405Chris Lattner    Star, Dot, Comma, Dollar, Equal, EqualEqual,
4898311ecb4ae9c82baba9e3a48acf756a81c8e9a4Jim Grosbach
4998311ecb4ae9c82baba9e3a48acf756a81c8e9a4Jim Grosbach    Pipe, PipePipe, Caret,
509823ca971d5cb475401e59fde244caf5087c74a1Kevin Enderby    Amp, AmpAmp, Exclaim, ExclaimEqual, Percent, Hash,
51cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar    Less, LessEqual, LessLess, LessGreater,
52924c5e58f2a7c89019000e7dee3391dcebdf8a21Matt Fleming    Greater, GreaterEqual, GreaterGreater, At
53cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  };
54cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar
55fb22ede033f792196643bad0ceafe473366ddf41Craig Topperprivate:
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;
745efabcf01d1c9cdf7ac59a17d757c6ad4cdb112cBenjamin Kramer  SMLoc getEndLoc() const;
75cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar
7676c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar  /// getStringContents - Get the contents of a string token (without quotes).
7798311ecb4ae9c82baba9e3a48acf756a81c8e9a4Jim Grosbach  StringRef getStringContents() const {
7876c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar    assert(Kind == String && "This token isn't a string!");
7976c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar    return Str.slice(1, Str.size() - 1);
8076c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar  }
8176c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar
8276c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar  /// getIdentifier - Get the identifier string for the current token, which
8376c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar  /// should be an identifier or a string. This gets the portion of the string
8476c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar  /// which should be used as the identifier, e.g., it does not include the
8576c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar  /// quotes on strings.
8676c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar  StringRef getIdentifier() const {
8776c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar    if (Kind == Identifier)
8876c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar      return getString();
8976c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar    return getStringContents();
9076c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar  }
9176c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar
92cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  /// getString - Get the string for the current token, this includes all
93cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  /// characters (for example, the quotes on strings) in the token.
94cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  ///
95cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  /// The returned StringRef points into the source manager's memory buffer, and
96cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  /// is safe to store across calls to Lex().
97cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  StringRef getString() const { return Str; }
98cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar
99cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  // FIXME: Don't compute this in advance, it makes every token larger, and is
100cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  // also not generally what we want (it is nicer for recovery etc. to lex 123br
101cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  // as a single token, then diagnose as an invalid number).
10298311ecb4ae9c82baba9e3a48acf756a81c8e9a4Jim Grosbach  int64_t getIntVal() const {
10376c4d7696c1eb566d53467a76024c5fdadd448e4Daniel Dunbar    assert(Kind == Integer && "This token isn't an integer!");
10498311ecb4ae9c82baba9e3a48acf756a81c8e9a4Jim Grosbach    return IntVal;
105cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  }
10698311ecb4ae9c82baba9e3a48acf756a81c8e9a4Jim Grosbach
1075d74e1f64445713cca863af03b8b6ab39321046eSean Callanan  /// getRegVal - Get the register number for the current token, which should
1085d74e1f64445713cca863af03b8b6ab39321046eSean Callanan  /// be a register.
1095d74e1f64445713cca863af03b8b6ab39321046eSean Callanan  unsigned getRegVal() const {
1105d74e1f64445713cca863af03b8b6ab39321046eSean Callanan    assert(Kind == Register && "This token isn't a register!");
1115d74e1f64445713cca863af03b8b6ab39321046eSean Callanan    return static_cast<unsigned>(IntVal);
1125d74e1f64445713cca863af03b8b6ab39321046eSean Callanan  }
113cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar};
114cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar
115dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar/// MCAsmLexer - Generic assembler lexer interface, for use by target specific
116dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar/// assembly lexers.
117dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbarclass MCAsmLexer {
118cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  /// The current token, stored in the base class for faster access.
119cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  AsmToken CurTok;
12098311ecb4ae9c82baba9e3a48acf756a81c8e9a4Jim Grosbach
12179036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan  /// The location and description of the current error
12279036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan  SMLoc ErrLoc;
12379036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan  std::string Err;
124cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar
1251f7210e808373fa92be3a2d4fa653a6f79d5088bCraig Topper  MCAsmLexer(const MCAsmLexer &) LLVM_DELETED_FUNCTION;
1261f7210e808373fa92be3a2d4fa653a6f79d5088bCraig Topper  void operator=(const MCAsmLexer &) LLVM_DELETED_FUNCTION;
127dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbarprotected: // Can only create subclasses.
128ca90dc6d295f7f6a5ef4240f26bcebe54276def5Daniel Dunbar  const char *TokStart;
129ca90dc6d295f7f6a5ef4240f26bcebe54276def5Daniel Dunbar
130dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar  MCAsmLexer();
131cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar
132cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  virtual AsmToken LexToken() = 0;
13398311ecb4ae9c82baba9e3a48acf756a81c8e9a4Jim Grosbach
13479036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan  void SetError(const SMLoc &errLoc, const std::string &err) {
13579036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan    ErrLoc = errLoc;
13679036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan    Err = err;
13779036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan  }
13898311ecb4ae9c82baba9e3a48acf756a81c8e9a4Jim Grosbach
139dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbarpublic:
140dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar  virtual ~MCAsmLexer();
141cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar
142cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  /// Lex - Consume the next token from the input stream and return it.
143cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  ///
144cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  /// The lexer will continuosly return the end-of-file token once the end of
145cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  /// the main input file has been reached.
146cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  const AsmToken &Lex() {
147cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar    return CurTok = LexToken();
148cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  }
149cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar
15019ad3b88f71fdc0fe0ec19e05bb37c3ef1a42b5bDaniel Dunbar  virtual StringRef LexUntilEndOfStatement() = 0;
15119ad3b88f71fdc0fe0ec19e05bb37c3ef1a42b5bDaniel Dunbar
152ca90dc6d295f7f6a5ef4240f26bcebe54276def5Daniel Dunbar  /// getLoc - Get the current source location.
153ca90dc6d295f7f6a5ef4240f26bcebe54276def5Daniel Dunbar  SMLoc getLoc() const;
154ca90dc6d295f7f6a5ef4240f26bcebe54276def5Daniel Dunbar
155cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  /// getTok - Get the current (last) lexed token.
156cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  const AsmToken &getTok() {
157cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar    return CurTok;
158cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  }
15998311ecb4ae9c82baba9e3a48acf756a81c8e9a4Jim Grosbach
16079036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan  /// getErrLoc - Get the current error location
16179036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan  const SMLoc &getErrLoc() {
16279036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan    return ErrLoc;
16379036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan  }
16498311ecb4ae9c82baba9e3a48acf756a81c8e9a4Jim Grosbach
16579036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan  /// getErr - Get the current error string
16679036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan  const std::string &getErr() {
16779036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan    return Err;
16879036e421f22cf3f661386c560fda36aa5bd04ccSean Callanan  }
169cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar
170cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  /// getKind - Get the kind of current token.
171cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  AsmToken::TokenKind getKind() const { return CurTok.getKind(); }
172cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar
173cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  /// is - Check if the current token has kind \arg K.
174cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  bool is(AsmToken::TokenKind K) const { return CurTok.is(K); }
175cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar
176cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  /// isNot - Check if the current token has kind \arg K.
177cbbe2484418536264b1a26c517c16d505a61d5c8Daniel Dunbar  bool isNot(AsmToken::TokenKind K) const { return CurTok.isNot(K); }
178dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar};
179dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar
180dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar} // End llvm namespace
181dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar
182dbd692a66e6a5f60ec3ff120ed27ae3a918c375fDaniel Dunbar#endif
183