blob: c862a0584063570b4c810f044b3142b42c54195e [file] [log] [blame]
Chris Lattnera59e8772009-06-21 07:19:10 +00001//===- AsmLexer.h - Lexer for Assembly Files --------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This class declares the lexer for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef ASMLEXER_H
15#define ASMLEXER_H
16
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +000017#include "llvm/ADT/StringRef.h"
Daniel Dunbardbd692a2009-07-20 20:01:54 +000018#include "llvm/MC/MCAsmLexer.h"
Chris Lattnera59e8772009-06-21 07:19:10 +000019#include "llvm/Support/DataTypes.h"
20#include <string>
21#include <cassert>
22
23namespace llvm {
24class MemoryBuffer;
25class SourceMgr;
26class SMLoc;
27
28namespace asmtok {
29 enum TokKind {
30 // Markers
31 Eof, Error,
32
Chris Lattner10a907d2009-06-21 19:56:35 +000033 // String values.
Chris Lattnera59e8772009-06-21 07:19:10 +000034 Identifier,
Chris Lattner4651bca2009-06-21 19:21:25 +000035 Register,
Chris Lattner10a907d2009-06-21 19:56:35 +000036 String,
37
38 // Integer values.
Chris Lattnera59e8772009-06-21 07:19:10 +000039 IntVal,
40
Chris Lattner10a907d2009-06-21 19:56:35 +000041 // No-value.
Chris Lattner4651bca2009-06-21 19:21:25 +000042 EndOfStatement,
Chris Lattnera59e8772009-06-21 07:19:10 +000043 Colon,
Chris Lattner74ec1a32009-06-22 06:32:03 +000044 Plus, Minus, Tilde,
Chris Lattner4651bca2009-06-21 19:21:25 +000045 Slash, // '/'
46 LParen, RParen,
Daniel Dunbar475839e2009-06-29 20:37:27 +000047 Star, Comma, Dollar, Equal, EqualEqual,
Chris Lattner8dfbe6c2009-06-23 05:57:07 +000048
Daniel Dunbar475839e2009-06-29 20:37:27 +000049 Pipe, PipePipe, Caret,
50 Amp, AmpAmp, Exclaim, ExclaimEqual, Percent,
51 Less, LessEqual, LessLess, LessGreater,
52 Greater, GreaterEqual, GreaterGreater
Chris Lattnera59e8772009-06-21 07:19:10 +000053 };
54}
55
56/// AsmLexer - Lexer class for assembly files.
Daniel Dunbardbd692a2009-07-20 20:01:54 +000057class AsmLexer : public MCAsmLexer {
Chris Lattnera59e8772009-06-21 07:19:10 +000058 SourceMgr &SrcMgr;
59
60 const char *CurPtr;
61 const MemoryBuffer *CurBuf;
Chris Lattnerfaf32c12009-06-24 00:33:19 +000062 // A llvm::StringSet<>, which provides uniqued and null-terminated strings.
63 void *TheStringSet;
Chris Lattnera59e8772009-06-21 07:19:10 +000064
65 // Information about the current token.
66 const char *TokStart;
67 asmtok::TokKind CurKind;
Chris Lattnerfaf32c12009-06-24 00:33:19 +000068 const char *CurStrVal; // This is valid for Identifier.
Chris Lattnera59e8772009-06-21 07:19:10 +000069 int64_t CurIntVal;
70
71 /// CurBuffer - This is the current buffer index we're lexing from as managed
72 /// by the SourceMgr object.
73 int CurBuffer;
74
Chris Lattnerfaf32c12009-06-24 00:33:19 +000075 void operator=(const AsmLexer&); // DO NOT IMPLEMENT
76 AsmLexer(const AsmLexer&); // DO NOT IMPLEMENT
Chris Lattnera59e8772009-06-21 07:19:10 +000077public:
78 AsmLexer(SourceMgr &SrcMgr);
Chris Lattnerfaf32c12009-06-24 00:33:19 +000079 ~AsmLexer();
Chris Lattnera59e8772009-06-21 07:19:10 +000080
81 asmtok::TokKind Lex() {
82 return CurKind = LexToken();
83 }
84
85 asmtok::TokKind getKind() const { return CurKind; }
Chris Lattnerb0789ed2009-06-21 20:54:55 +000086 bool is(asmtok::TokKind K) const { return CurKind == K; }
87 bool isNot(asmtok::TokKind K) const { return CurKind != K; }
Daniel Dunbar9a7e2cc2009-07-27 21:49:56 +000088
89 /// getCurStrVal - Get the string for the current token, this includes all
90 /// characters (for example, the quotes on strings) in the token.
91 ///
92 /// The returned StringRef points into the source manager's memory buffer, and
93 /// is safe to store across calls to Lex().
94 StringRef getCurStrVal() const {
Chris Lattner10a907d2009-06-21 19:56:35 +000095 assert((CurKind == asmtok::Identifier || CurKind == asmtok::Register ||
96 CurKind == asmtok::String) &&
Chris Lattnera59e8772009-06-21 07:19:10 +000097 "This token doesn't have a string value");
98 return CurStrVal;
99 }
100 int64_t getCurIntVal() const {
101 assert(CurKind == asmtok::IntVal && "This token isn't an integer");
102 return CurIntVal;
103 }
104
105 SMLoc getLoc() const;
106
Chris Lattner8e25e2d2009-07-16 06:14:39 +0000107 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
108 bool EnterIncludeFile(const std::string &Filename);
109
Daniel Dunbar3fb76832009-06-30 00:49:23 +0000110 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
Chris Lattnera59e8772009-06-21 07:19:10 +0000111
112private:
113 int getNextChar();
Chris Lattner4651bca2009-06-21 19:21:25 +0000114 asmtok::TokKind ReturnError(const char *Loc, const std::string &Msg);
Chris Lattnera59e8772009-06-21 07:19:10 +0000115
116 /// LexToken - Read the next token and return its code.
117 asmtok::TokKind LexToken();
Chris Lattner4651bca2009-06-21 19:21:25 +0000118 asmtok::TokKind LexIdentifier();
119 asmtok::TokKind LexPercent();
120 asmtok::TokKind LexSlash();
Daniel Dunbar1ad7edc2009-06-29 22:00:57 +0000121 asmtok::TokKind LexLineComment();
Chris Lattner4651bca2009-06-21 19:21:25 +0000122 asmtok::TokKind LexDigit();
Chris Lattner10a907d2009-06-21 19:56:35 +0000123 asmtok::TokKind LexQuote();
Chris Lattnera59e8772009-06-21 07:19:10 +0000124};
125
126} // end namespace llvm
127
128#endif