blob: bad2cb931b954eebad997c835655d594c6dfb9ec [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
17#include "llvm/Support/DataTypes.h"
18#include <string>
19#include <cassert>
20
21namespace llvm {
22class MemoryBuffer;
23class SourceMgr;
24class SMLoc;
25
26namespace asmtok {
27 enum TokKind {
28 // Markers
29 Eof, Error,
30
Chris Lattner10a907d2009-06-21 19:56:35 +000031 // String values.
Chris Lattnera59e8772009-06-21 07:19:10 +000032 Identifier,
Chris Lattner4651bca2009-06-21 19:21:25 +000033 Register,
Chris Lattner10a907d2009-06-21 19:56:35 +000034 String,
35
36 // Integer values.
Chris Lattnera59e8772009-06-21 07:19:10 +000037 IntVal,
38
Chris Lattner10a907d2009-06-21 19:56:35 +000039 // No-value.
Chris Lattner4651bca2009-06-21 19:21:25 +000040 EndOfStatement,
Chris Lattnera59e8772009-06-21 07:19:10 +000041 Colon,
42 Plus,
Chris Lattner4651bca2009-06-21 19:21:25 +000043 Minus,
44 Slash, // '/'
45 LParen, RParen,
46 Star, Comma, Dollar
Chris Lattnera59e8772009-06-21 07:19:10 +000047 };
48}
49
50/// AsmLexer - Lexer class for assembly files.
51class AsmLexer {
52 SourceMgr &SrcMgr;
53
54 const char *CurPtr;
55 const MemoryBuffer *CurBuf;
56
57 // Information about the current token.
58 const char *TokStart;
59 asmtok::TokKind CurKind;
60 std::string CurStrVal; // This is valid for Identifier.
61 int64_t CurIntVal;
62
63 /// CurBuffer - This is the current buffer index we're lexing from as managed
64 /// by the SourceMgr object.
65 int CurBuffer;
66
67public:
68 AsmLexer(SourceMgr &SrcMgr);
69 ~AsmLexer() {}
70
71 asmtok::TokKind Lex() {
72 return CurKind = LexToken();
73 }
74
75 asmtok::TokKind getKind() const { return CurKind; }
Chris Lattnerb0789ed2009-06-21 20:54:55 +000076 bool is(asmtok::TokKind K) const { return CurKind == K; }
77 bool isNot(asmtok::TokKind K) const { return CurKind != K; }
Chris Lattnera59e8772009-06-21 07:19:10 +000078
79 const std::string &getCurStrVal() const {
Chris Lattner10a907d2009-06-21 19:56:35 +000080 assert((CurKind == asmtok::Identifier || CurKind == asmtok::Register ||
81 CurKind == asmtok::String) &&
Chris Lattnera59e8772009-06-21 07:19:10 +000082 "This token doesn't have a string value");
83 return CurStrVal;
84 }
85 int64_t getCurIntVal() const {
86 assert(CurKind == asmtok::IntVal && "This token isn't an integer");
87 return CurIntVal;
88 }
89
90 SMLoc getLoc() const;
91
Chris Lattner14ee48a2009-06-21 21:22:11 +000092 void PrintMessage(SMLoc Loc, const std::string &Msg) const;
Chris Lattnera59e8772009-06-21 07:19:10 +000093
94private:
95 int getNextChar();
Chris Lattner4651bca2009-06-21 19:21:25 +000096 asmtok::TokKind ReturnError(const char *Loc, const std::string &Msg);
Chris Lattnera59e8772009-06-21 07:19:10 +000097
98 /// LexToken - Read the next token and return its code.
99 asmtok::TokKind LexToken();
Chris Lattner4651bca2009-06-21 19:21:25 +0000100 asmtok::TokKind LexIdentifier();
101 asmtok::TokKind LexPercent();
102 asmtok::TokKind LexSlash();
103 asmtok::TokKind LexHash();
104 asmtok::TokKind LexDigit();
Chris Lattner10a907d2009-06-21 19:56:35 +0000105 asmtok::TokKind LexQuote();
Chris Lattnera59e8772009-06-21 07:19:10 +0000106};
107
108} // end namespace llvm
109
110#endif