blob: 25311c8f9f3f51819fbe692791204d7c74ffbdec [file] [log] [blame]
Chris Lattner22b67fb2009-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 Dunbar9a7b61d2009-07-27 21:49:56 +000017#include "llvm/ADT/StringRef.h"
Daniel Dunbar78929e52009-07-20 20:01:54 +000018#include "llvm/MC/MCAsmLexer.h"
Chris Lattner22b67fb2009-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 Lattnerba605b72009-06-21 19:56:35 +000033 // String values.
Chris Lattner22b67fb2009-06-21 07:19:10 +000034 Identifier,
Chris Lattnerc688c232009-06-21 19:21:25 +000035 Register,
Chris Lattnerba605b72009-06-21 19:56:35 +000036 String,
37
38 // Integer values.
Chris Lattner22b67fb2009-06-21 07:19:10 +000039 IntVal,
40
Chris Lattnerba605b72009-06-21 19:56:35 +000041 // No-value.
Chris Lattnerc688c232009-06-21 19:21:25 +000042 EndOfStatement,
Chris Lattner22b67fb2009-06-21 07:19:10 +000043 Colon,
Chris Lattnere8164362009-06-22 06:32:03 +000044 Plus, Minus, Tilde,
Chris Lattnerc688c232009-06-21 19:21:25 +000045 Slash, // '/'
46 LParen, RParen,
Daniel Dunbarc3b8a4b2009-06-29 20:37:27 +000047 Star, Comma, Dollar, Equal, EqualEqual,
Chris Lattnerd8817ef2009-06-23 05:57:07 +000048
Daniel Dunbarc3b8a4b2009-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 Lattner22b67fb2009-06-21 07:19:10 +000053 };
54}
55
56/// AsmLexer - Lexer class for assembly files.
Daniel Dunbar78929e52009-07-20 20:01:54 +000057class AsmLexer : public MCAsmLexer {
Chris Lattner22b67fb2009-06-21 07:19:10 +000058 SourceMgr &SrcMgr;
59
60 const char *CurPtr;
61 const MemoryBuffer *CurBuf;
62
63 // Information about the current token.
64 const char *TokStart;
65 asmtok::TokKind CurKind;
Daniel Dunbarfcb9b642009-07-28 00:58:50 +000066 StringRef CurStrVal; // This is valid for Identifier.
Chris Lattner22b67fb2009-06-21 07:19:10 +000067 int64_t CurIntVal;
68
69 /// CurBuffer - This is the current buffer index we're lexing from as managed
70 /// by the SourceMgr object.
71 int CurBuffer;
72
Chris Lattner592e3bb2009-06-24 00:33:19 +000073 void operator=(const AsmLexer&); // DO NOT IMPLEMENT
74 AsmLexer(const AsmLexer&); // DO NOT IMPLEMENT
Chris Lattner22b67fb2009-06-21 07:19:10 +000075public:
76 AsmLexer(SourceMgr &SrcMgr);
Chris Lattner592e3bb2009-06-24 00:33:19 +000077 ~AsmLexer();
Chris Lattner22b67fb2009-06-21 07:19:10 +000078
79 asmtok::TokKind Lex() {
80 return CurKind = LexToken();
81 }
82
83 asmtok::TokKind getKind() const { return CurKind; }
Chris Lattnercfd2e3b2009-06-21 20:54:55 +000084 bool is(asmtok::TokKind K) const { return CurKind == K; }
85 bool isNot(asmtok::TokKind K) const { return CurKind != K; }
Daniel Dunbar9a7b61d2009-07-27 21:49:56 +000086
87 /// getCurStrVal - Get the string for the current token, this includes all
88 /// characters (for example, the quotes on strings) in the token.
89 ///
90 /// The returned StringRef points into the source manager's memory buffer, and
91 /// is safe to store across calls to Lex().
92 StringRef getCurStrVal() const {
Chris Lattnerba605b72009-06-21 19:56:35 +000093 assert((CurKind == asmtok::Identifier || CurKind == asmtok::Register ||
94 CurKind == asmtok::String) &&
Chris Lattner22b67fb2009-06-21 07:19:10 +000095 "This token doesn't have a string value");
96 return CurStrVal;
97 }
98 int64_t getCurIntVal() const {
99 assert(CurKind == asmtok::IntVal && "This token isn't an integer");
100 return CurIntVal;
101 }
102
103 SMLoc getLoc() const;
104
Chris Lattner7aca1522009-07-16 06:14:39 +0000105 /// EnterIncludeFile - Enter the specified file. This returns true on failure.
106 bool EnterIncludeFile(const std::string &Filename);
107
Daniel Dunbarb5aec992009-06-30 00:49:23 +0000108 void PrintMessage(SMLoc Loc, const std::string &Msg, const char *Type) const;
Chris Lattner22b67fb2009-06-21 07:19:10 +0000109
110private:
111 int getNextChar();
Chris Lattnerc688c232009-06-21 19:21:25 +0000112 asmtok::TokKind ReturnError(const char *Loc, const std::string &Msg);
Chris Lattner22b67fb2009-06-21 07:19:10 +0000113
114 /// LexToken - Read the next token and return its code.
115 asmtok::TokKind LexToken();
Chris Lattnerc688c232009-06-21 19:21:25 +0000116 asmtok::TokKind LexIdentifier();
117 asmtok::TokKind LexPercent();
118 asmtok::TokKind LexSlash();
Daniel Dunbara2f3cd52009-06-29 22:00:57 +0000119 asmtok::TokKind LexLineComment();
Chris Lattnerc688c232009-06-21 19:21:25 +0000120 asmtok::TokKind LexDigit();
Chris Lattnerba605b72009-06-21 19:56:35 +0000121 asmtok::TokKind LexQuote();
Chris Lattner22b67fb2009-06-21 07:19:10 +0000122};
123
124} // end namespace llvm
125
126#endif